Skip to content

wallet: restore spend tracking when a transaction is confirmed again - #13

Open
MadCatMining wants to merge 1 commit into
mainfrom
fix-spends-lost-on-reconnect
Open

wallet: restore spend tracking when a transaction is confirmed again#13
MadCatMining wants to merge 1 commit into
mainfrom
fix-spends-lost-on-reconnect

Conversation

@MadCatMining

Copy link
Copy Markdown
Owner

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 the fInsertedNew branch of AddToWallet. DisableTransaction() calls RemoveFromSpends(hash) when a coinstake's block is disconnected. So:

  1. Coinstake mined → inserted → AddToSpends → inputs marked spent. Leaves GetBalance(), enters GetStake(). Correct.
  2. Block orphaned → RemoveFromSpends → inputs unmarked → back in GetBalance(). Still correct.
  3. Same coinstake confirmed again → mapWallet.insert finds the txid → fInsertedNew = falsemerge path, AddToSpends never runs.
  4. Depth > 0 again → counted in GetStake(). Inputs have no mapTxSpends entry → IsSpent() false → also counted in GetBalance().

Total inflates by the staked principal, permanently for the session. Restart appears to fix it because the load path (wallet.cpp:1117) calls AddToSpends unconditionally.

Note this needs no txid collision — a plain reorg away from and back to a chain containing the coinstake is enough.

Fix

  • Register spends on the merge path too.
  • Make AddToSpends(const COutPoint&, const uint256&) idempotent, since it can now legitimately be called twice for the same pair and mapTxSpends is 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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant