From 8da2beaee1c52c452f2677a970a76787027b1ef2 Mon Sep 17 00:00:00 2001 From: Sunny King Date: Tue, 18 Sep 2012 03:46:51 +0100 Subject: [PATCH] PPCoin: Combine FixSpentCoins and CheckSpentCoins --- src/bitcoinrpc.cpp | 3 +- src/wallet.cpp | 59 +++++++++++++-------------------------------------- src/wallet.h | 3 +- 3 files changed, 18 insertions(+), 47 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 63375fe..3c39a30 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -2107,8 +2107,9 @@ Value checkwallet(const Array& params, bool fHelp) int nMismatchSpent; int64 nBalanceInQuestion; + pwalletMain->FixSpentCoins(nMismatchSpent, nBalanceInQuestion, true); Object result; - if (pwalletMain->CheckSpentCoins(nMismatchSpent, nBalanceInQuestion)) + if (nMismatchSpent == 0) result.push_back(Pair("wallet check passed", true)); else { diff --git a/src/wallet.cpp b/src/wallet.cpp index be8fccf..c585590 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -1751,45 +1751,8 @@ int64 CWallet::GetOldestKeyPoolTime() } // ppcoin: check 'spent' consistency between wallet and txindex -bool CWallet::CheckSpentCoins(int& nMismatchFound, int64& nBalanceInQuestion) -{ - nMismatchFound = 0; - nBalanceInQuestion = 0; - - LOCK(cs_wallet); - vector vCoins; - vCoins.reserve(mapWallet.size()); - for (map::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) - vCoins.push_back(&(*it).second); - - CTxDB txdb("r"); - BOOST_FOREACH(const CWalletTx* pcoin, vCoins) - { - // Find the corresponding transaction index - CTxIndex txindex; - if (!txdb.ReadTxIndex(pcoin->GetHash(), txindex)) - continue; - for (int n=0; n < pcoin->vout.size(); n++) - { - if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull())) - { - printf("CheckSpentCoins found lost coin %sppc %s[%d]\n", FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n); - nMismatchFound++; - nBalanceInQuestion += pcoin->vout[n].nValue; - } - else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull())) - { - printf("CheckSpentCoins found spent coin %sppc %s[%d]\n", FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n); - nMismatchFound++; - nBalanceInQuestion += pcoin->vout[n].nValue; - } - } - } - return (nMismatchFound == 0); -} - // ppcoin: fix wallet spent state according to txindex -void CWallet::FixSpentCoins(int& nMismatchFound, int64& nBalanceInQuestion) +void CWallet::FixSpentCoins(int& nMismatchFound, int64& nBalanceInQuestion, bool fCheckOnly) { nMismatchFound = 0; nBalanceInQuestion = 0; @@ -1811,19 +1774,27 @@ void CWallet::FixSpentCoins(int& nMismatchFound, int64& nBalanceInQuestion) { if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull())) { - printf("FixSpentCoins found lost coin %sppc %s[%d]\n", FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n); + printf("FixSpentCoins found lost coin %sppc %s[%d], %s\n", + FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing"); nMismatchFound++; nBalanceInQuestion += pcoin->vout[n].nValue; - pcoin->MarkUnspent(n); - pcoin->WriteToDisk(); + if (!fCheckOnly) + { + pcoin->MarkUnspent(n); + pcoin->WriteToDisk(); + } } else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull())) { - printf("FixSpentCoins found spent coin %sppc %s[%d]\n", FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n); + printf("FixSpentCoins found spent coin %sppc %s[%d], %s\n", + FormatMoney(pcoin->vout[n].nValue).c_str(), pcoin->GetHash().ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing"); nMismatchFound++; nBalanceInQuestion += pcoin->vout[n].nValue; - pcoin->MarkSpent(n); - pcoin->WriteToDisk(); + if (!fCheckOnly) + { + pcoin->MarkSpent(n); + pcoin->WriteToDisk(); + } } } } diff --git a/src/wallet.h b/src/wallet.h index 4750a6b..9412013 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -276,8 +276,7 @@ public: // get the current wallet format (the oldest client version guaranteed to understand this wallet) int GetVersion() { return nWalletVersion; } - bool CheckSpentCoins(int& nMismatchSpent, int64& nBalanceInQuestion); - void FixSpentCoins(int& nMismatchSpent, int64& nBalanceInQuestion); + void FixSpentCoins(int& nMismatchSpent, int64& nBalanceInQuestion, bool fCheckOnly = false); void DisableTransaction(const CTransaction &tx); }; -- 1.7.1