From 54b3d964a174535eb41651d478924ace193f999c Mon Sep 17 00:00:00 2001 From: Sunny King Date: Sat, 24 Mar 2012 16:33:53 +0000 Subject: [PATCH] PPCoin: Mark inputs unspent in wallet when disconnecting coinstake --- src/main.cpp | 16 +++++++++++++++- src/wallet.cpp | 23 +++++++++++++++++++++++ src/wallet.h | 1 + 3 files changed, 39 insertions(+), 1 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 60d0704..dae457f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -116,8 +116,18 @@ void static EraseFromWallets(uint256 hash) } // make sure all wallets know about the given transaction, in the given block -void static SyncWithWallets(const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false) +void static SyncWithWallets(const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false, bool fConnect = true) { + if (!fConnect) + { + // ppcoin: wallets need to refund inputs when disconnecting coinstake + if (tx.IsCoinStake()) + BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered) + if (pwallet->IsFromMe(tx)) + pwallet->DisableTransaction(tx); + return; + } + BOOST_FOREACH(CWallet* pwallet, setpwalletRegistered) pwallet->AddToWalletIfInvolvingMe(tx, pblock, fUpdate); } @@ -1038,6 +1048,10 @@ bool CBlock::DisconnectBlock(CTxDB& txdb, CBlockIndex* pindex) return error("DisconnectBlock() : WriteBlockIndex failed"); } + // ppcoin: clean up wallet after disconnecting coinstake + BOOST_FOREACH(CTransaction& tx, vtx) + SyncWithWallets(tx, this, false, false); + return true; } diff --git a/src/wallet.cpp b/src/wallet.cpp index 29cc87a..fd8e9c2 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -1552,6 +1552,29 @@ void CWallet::FixSpentCoins(int& nMismatchFound, int64& nBalanceInQuestion) } } +// ppcoin: disable transaction (only for coinstake) +void CWallet::DisableTransaction(const CTransaction &tx) +{ + if (!tx.IsCoinStake() || !IsFromMe(tx)) + return; // only disconnecting coinstake requires marking input unspent + CRITICAL_BLOCK(cs_wallet) + { + BOOST_FOREACH(const CTxIn& txin, tx.vin) + { + map::iterator mi = mapWallet.find(txin.prevout.hash); + if (mi != mapWallet.end()) + { + CWalletTx& prev = (*mi).second; + if (txin.prevout.n < prev.vout.size() && IsMine(prev.vout[txin.prevout.n])) + { + prev.MarkUnspent(txin.prevout.n); + prev.WriteToDisk(); + } + } + } + } +} + vector CReserveKey::GetReservedKey() { if (nIndex == -1) diff --git a/src/wallet.h b/src/wallet.h index 2fb41db..4beebb8 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -216,6 +216,7 @@ public: bool CheckSpentCoins(int& nMismatchSpent, int64& nBalanceInQuestion); void FixSpentCoins(int& nMismatchSpent, int64& nBalanceInQuestion); + void DisableTransaction(const CTransaction &tx); }; -- 1.7.1