Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
26 changes: 22 additions & 4 deletions src/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2541,10 +2541,12 @@ set< set<CTxDestination> > 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<CWalletTx*> vCoins;
Expand All @@ -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)
Expand All @@ -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());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down