From 9cdcb37c10f5a46eec62ac4cc74fa4530821d495 Mon Sep 17 00:00:00 2001 From: ddude1 Date: Fri, 12 Jan 2018 10:51:40 -0500 Subject: [PATCH] remove orphans from wallet. --- src/rpcwallet.cpp | 16 ++++++++++++---- src/wallet.cpp | 26 ++++++++++++++++++++++---- src/wallet.h | 2 +- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 3b69cfc..a20d4cb 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -1565,14 +1565,18 @@ Value checkwallet(const Array& params, bool fHelp) int nMismatchSpent; int64_t nBalanceInQuestion; - pwalletMain->FixSpentCoins(nMismatchSpent, nBalanceInQuestion, true); + int nOrphansFound; // Fix Orphans + //pwalletMain->FixSpentCoins(nMismatchSpent, nBalanceInQuestion, true); + pwalletMain->FixSpentCoins(nMismatchSpent, nBalanceInQuestion, nOrphansFound, true); Object result; - if (nMismatchSpent == 0) + //if (nMismatchSpent == 0) + if (nMismatchSpent == 0 && nOrphansFound == 0) result.push_back(Pair("wallet check passed", true)); else { result.push_back(Pair("mismatched spent coins", nMismatchSpent)); result.push_back(Pair("amount in question", ValueFromAmount(nBalanceInQuestion))); + result.push_back(Pair("orphan blocks found", nOrphansFound)); } return result; } @@ -1588,14 +1592,18 @@ Value repairwallet(const Array& params, bool fHelp) int nMismatchSpent; int64_t nBalanceInQuestion; - pwalletMain->FixSpentCoins(nMismatchSpent, nBalanceInQuestion); + int nOrphansFound; // Fix Orphans + //pwalletMain->FixSpentCoins(nMismatchSpent, nBalanceInQuestion, true); + pwalletMain->FixSpentCoins(nMismatchSpent, nBalanceInQuestion, nOrphansFound, true); Object result; - if (nMismatchSpent == 0) + //if (nMismatchSpent == 0) + if (nMismatchSpent == 0 && nOrphansFound == 0) result.push_back(Pair("wallet check passed", true)); else { result.push_back(Pair("mismatched spent coins", nMismatchSpent)); result.push_back(Pair("amount affected by repair", ValueFromAmount(nBalanceInQuestion))); + result.push_back(Pair("orphan blocks found", nOrphansFound)); } return result; } diff --git a/src/wallet.cpp b/src/wallet.cpp index 24086dd..da22743 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -2541,10 +2541,12 @@ set< set > CWallet::GetAddressGroupings() // ppcoin: check 'spent' consistency between wallet and txindex // ppcoin: fix wallet spent state according to txindex -void CWallet::FixSpentCoins(int& nMismatchFound, int64_t& nBalanceInQuestion, bool fCheckOnly) +// remove orphan Coinbase and Coinstake +void CWallet::FixSpentCoins(int& nMismatchFound, int64_t& nBalanceInQuestion,int& nOrphansFound, bool fCheckOnly) { nMismatchFound = 0; nBalanceInQuestion = 0; + nOrphansFound = 0; LOCK(cs_wallet); vector vCoins; @@ -2555,16 +2557,19 @@ void CWallet::FixSpentCoins(int& nMismatchFound, int64_t& nBalanceInQuestion, bo CTxDB txdb("r"); BOOST_FOREACH(CWalletTx* pcoin, vCoins) { + uint256 hash = pcoin->GetHash(); // Find the corresponding transaction index CTxIndex txindex; - if (!txdb.ReadTxIndex(pcoin->GetHash(), txindex)) + //if (!txdb.ReadTxIndex(pcoin->GetHash(), txindex)) + if (!txdb.ReadTxIndex(hash, txindex) && !(pcoin->IsCoinBase() || pcoin->IsCoinStake())) continue; for (unsigned int n=0; n < pcoin->vout.size(); n++) { + bool fUpdated = false; if (IsMine(pcoin->vout[n]) && pcoin->IsSpent(n) && (txindex.vSpent.size() <= n || txindex.vSpent[n].IsNull())) { LogPrintf("FixSpentCoins found lost coin %s BC %s[%d], %s\n", - FormatMoney(pcoin->vout[n].nValue), pcoin->GetHash().ToString(), n, fCheckOnly? "repair not attempted" : "repairing"); + FormatMoney(pcoin->vout[n].nValue), hash.ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing"); nMismatchFound++; nBalanceInQuestion += pcoin->vout[n].nValue; if (!fCheckOnly) @@ -2576,14 +2581,27 @@ void CWallet::FixSpentCoins(int& nMismatchFound, int64_t& nBalanceInQuestion, bo else if (IsMine(pcoin->vout[n]) && !pcoin->IsSpent(n) && (txindex.vSpent.size() > n && !txindex.vSpent[n].IsNull())) { LogPrintf("FixSpentCoins found spent coin %s BC %s[%d], %s\n", - FormatMoney(pcoin->vout[n].nValue), pcoin->GetHash().ToString(), n, fCheckOnly? "repair not attempted" : "repairing"); + FormatMoney(pcoin->vout[n].nValue), hash.ToString().c_str(), n, fCheckOnly? "repair not attempted" : "repairing"); nMismatchFound++; nBalanceInQuestion += pcoin->vout[n].nValue; if (!fCheckOnly) { + fUpdated = true; pcoin->MarkSpent(n); pcoin->WriteToDisk(); } + if (fUpdated) + NotifyTransactionChanged(this, hash, CT_UPDATED); + } + if((pcoin->IsCoinBase() || pcoin->IsCoinStake()) && pcoin->GetDepthInMainChain() <= 0) + { + nOrphansFound++; + if (!fCheckOnly) + { + EraseFromWallet(hash); + NotifyTransactionChanged(this, hash, CT_DELETED); + } + printf("FixSpentCoins %s orphaned generation tx %s\n", fCheckOnly ? "found" : "removed", hash.ToString().c_str()); } } } diff --git a/src/wallet.h b/src/wallet.h index eb3896a..ff674ce 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -348,7 +348,7 @@ class CWallet : public CCryptoKeyStore, public CWalletInterface // get the current wallet format (the oldest client version guaranteed to understand this wallet) int GetVersion() { LOCK(cs_wallet); return nWalletVersion; } - void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, bool fCheckOnly = false); + void FixSpentCoins(int& nMismatchSpent, int64_t& nBalanceInQuestion, int& nOrphansFound, bool fCheckOnly = false); void DisableTransaction(const CTransaction &tx); /** Address book entry changed.