wallet: restore spend tracking when a transaction is confirmed again - #13
Open
MadCatMining wants to merge 1 commit into
Open
wallet: restore spend tracking when a transaction is confirmed again#13MadCatMining wants to merge 1 commit into
MadCatMining wants to merge 1 commit into
Conversation
AddToSpends() only ran when a wallet entry was first inserted, but DisableTransaction() calls RemoveFromSpends() when a coinstake's block is disconnected. A transaction confirmed again after that takes AddToWallet's merge path, which never re-registered its spends, so its inputs stayed marked unspent in mapTxSpends for the rest of the session. The wallet then counted the staked coins twice: once as available balance, because IsSpent() reported the inputs as unspent, and again as stake, because the coinstake was back in the main chain. The total inflated by the staked principal on every affected reorg -- reported as ~140 accumulating over 24 hours on a wallet whose stake rewards are 0.00125 and 0.00000125, which is far too large to be reward accrual. Restarting appeared to fix it because the wallet load path calls AddToSpends() unconditionally and rebuilds mapTxSpends from disk. Register spends on the merge path as well, and make AddToSpends() idempotent since it can now legitimately be called more than once for the same (outpoint, wtxid) pair and mapTxSpends is a multimap. This is not the cache staleness fixed earlier; the credit caches were being invalidated correctly. The spend index itself was losing entries. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the inflated total balance during staking, reported as 3386 displayed vs 3246 after restart — a 140 discrepancy over 24h on a wallet whose stake rewards are 0.00125 and 0.00000125 per stake. That magnitude rules out reward accrual: it is staked principal counted twice.
Cause
AddToSpends(hash)runs at runtime only inside thefInsertedNewbranch ofAddToWallet.DisableTransaction()callsRemoveFromSpends(hash)when a coinstake's block is disconnected. So:AddToSpends→ inputs marked spent. LeavesGetBalance(), entersGetStake(). Correct.RemoveFromSpends→ inputs unmarked → back inGetBalance(). Still correct.mapWallet.insertfinds the txid →fInsertedNew = false→ merge path,AddToSpendsnever runs.GetStake(). Inputs have nomapTxSpendsentry →IsSpent()false → also counted inGetBalance().Total inflates by the staked principal, permanently for the session. Restart appears to fix it because the load path (
wallet.cpp:1117) callsAddToSpendsunconditionally.Note this needs no txid collision — a plain reorg away from and back to a chain containing the coinstake is enough.
Fix
AddToSpends(const COutPoint&, const uint256&)idempotent, since it can now legitimately be called twice for the same pair andmapTxSpendsis a multimap that would otherwise accumulate duplicates.Relationship to #8
Different bug. #8 fixed credit-cache invalidation and the abandonment recursion, and those were working correctly here. This is the spend index losing entries, which no amount of cache invalidation recovers.
🤖 Generated with Claude Code