aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2011-01-19 16:50:20 -0500
committerGavin Andresen <gavinandresen@gmail.com>2011-01-19 16:50:20 -0500
commit1d23c7436164faa0b1822acf5ce9507bf6ff257a (patch)
tree1ef71d923e9183521e1338ffa40526b3a73201ec /main.cpp
parentc9f70b381dc8ee4197fd5b3d4d20f302632da0ed (diff)
ReacceptWalletTransactions bugfix
Fix two bugs that can happen if you copy your wallet to another machine and perform transactions on both. First, ReacceptWalletTransactions would notice if the other wallet spent coins, and would correctly mark the receiving transaction spent. However, it did not add the spending transaction to the wallet. Now it does. Second, account balances could get out of sync with 'getbalance' because coins received by the other copy of the wallet were not necessarily detected. Now ReacceptWalletTransactions will scan the entire blockchain for transactions that should be in the wallet if it runs across a 'spent in the other wallet' transaction. Finally, there was a small bug in the accounts getbalance code-- generated coins with between 100 and 119 confirmations were not being counted in the balance of account "".
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp73
1 files changed, 66 insertions, 7 deletions
diff --git a/main.cpp b/main.cpp
index b7dfd9fabe..ac0c563ece 100644
--- a/main.cpp
+++ b/main.cpp
@@ -187,6 +187,19 @@ bool AddToWalletIfMine(const CTransaction& tx, const CBlock* pblock)
return true;
}
+bool AddToWalletIfFromMe(const CTransaction& tx, const CBlock* pblock)
+{
+ if (tx.IsFromMe() || mapWallet.count(tx.GetHash()))
+ {
+ CWalletTx wtx(tx);
+ // Get merkle branch if transaction was found in a block
+ if (pblock)
+ wtx.SetMerkleBranch(pblock);
+ return AddToWallet(wtx);
+ }
+ return true;
+}
+
bool EraseFromWallet(uint256 hash)
{
CRITICAL_BLOCK(cs_mapWallet)
@@ -404,7 +417,7 @@ void CWalletTx::GetAmounts(int64& nGenerated, list<pair<string, int64> >& listRe
if (IsCoinBase())
{
- if (GetBlocksToMaturity() == 0)
+ if (GetDepthInMainChain() >= COINBASE_MATURITY)
nGenerated = GetCredit();
return;
}
@@ -849,11 +862,50 @@ bool CWalletTx::AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs)
return false;
}
+int ScanForWalletTransactions(CBlockIndex* pindexStart)
+{
+ int ret = 0;
+
+ CBlockIndex* pindex = pindexStart;
+ CRITICAL_BLOCK(cs_mapWallet)
+ {
+ while (pindex)
+ {
+ CBlock block;
+ block.ReadFromDisk(pindex, true);
+ foreach(CTransaction& tx, block.vtx)
+ {
+ uint256 hash = tx.GetHash();
+ if (mapWallet.count(hash)) continue;
+ AddToWalletIfMine(tx, &block);
+ if (mapWallet.count(hash))
+ {
+ ++ret;
+ printf("Added missing RECEIVE %s\n", hash.ToString().c_str());
+ continue;
+ }
+ AddToWalletIfFromMe(tx, &block);
+ if (mapWallet.count(hash))
+ {
+ ++ret;
+ printf("Added missing SEND %s\n", hash.ToString().c_str());
+ continue;
+ }
+ }
+ pindex = pindex->pnext;
+ }
+ }
+ return ret;
+}
+
void ReacceptWalletTransactions()
{
CTxDB txdb("r");
- CRITICAL_BLOCK(cs_mapWallet)
+ bool fRepeat = true;
+ while (fRepeat) CRITICAL_BLOCK(cs_mapWallet)
{
+ fRepeat = false;
+ vector<CDiskTxPos> vMissingTx;
foreach(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
{
CWalletTx& wtx = item.second;
@@ -875,12 +927,15 @@ void ReacceptWalletTransactions()
{
if (!txindex.vSpent[i].IsNull() && wtx.vout[i].IsMine())
{
- printf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
wtx.fSpent = true;
- wtx.WriteToDisk();
- break;
+ vMissingTx.push_back(txindex.vSpent[i]);
}
}
+ if (wtx.fSpent)
+ {
+ printf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
+ wtx.WriteToDisk();
+ }
}
}
else
@@ -890,6 +945,12 @@ void ReacceptWalletTransactions()
wtx.AcceptWalletTransaction(txdb, false);
}
}
+ if (!vMissingTx.empty())
+ {
+ // TODO: optimize this to scan just part of the block chain?
+ if (ScanForWalletTransactions(pindexGenesisBlock))
+ fRepeat = true; // Found missing transactions: re-do Reaccept.
+ }
}
}
@@ -1361,8 +1422,6 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex)
return true;
}
-
-
bool Reorganize(CTxDB& txdb, CBlockIndex* pindexNew)
{
printf("REORGANIZE\n");