wallet: believe the chain about what has already been spent - #62
Merged
Conversation
fSpent lives in wallet.dat and is written when this node spends something. A wallet.dat restored from backup, or copied and used on another machine, carries whatever that flag was when the copy was taken -- so it can say "unspent" about coins the chain shows as gone. The balance reads high and nothing contradicts it until a send is attempted against coins that do not exist. #47 is about this shape of problem and #40 is someone living through it; Bitcoin fixed the same thing in 53d508072. On startup, after the block index is loaded and before anything reports a balance, walk the wallet and mark spent whatever the transaction index says is spent. One direction only. Marking spent when the chain says spent is safe; clearing the flag because the chain has not caught up would offer up coins already on their way out. fSpent is one flag per transaction rather than per output, which is how 0.1.0 has always worked -- CommitTransactionSpent already marks a whole previous transaction when it spends any part of it -- so this matches rather than changing the model. It reports every run, including the runs where it finds nothing, with a breakdown of why: RescanSpentFlags() : examined 17, already-spent 0, not-indexed 17, corrected 0 A check that speaks only when it finds something is indistinguishable from one that never ran, and that confusion has cost this project real time. The breakdown earned its place immediately: it is how the test below turned from a guess into an answer. Verified running against a wallet with 17 transactions and against an empty one, with no false corrections in either. The correcting branch itself is NOT exercised by a real case: on this chain no test wallet holds a transaction that is both in the transaction index and spent, and the counters show why -- all 17 were absent from the index, so the comparison never had anything to compare.
Merged
Bitflash-sh
added a commit
that referenced
this pull request
Jul 31, 2026
Six changes since 1.2.7, and the reason not to sit on this is #58: coinbase transactions were not unique, two of them overwrote older ones on the main chain, and 100 BTF stopped being reachable. Nothing in how they were built had changed since, so it could happen again at any time. The height now goes into the coinbase on both paths that build one (#59, #60), which makes a repeat impossible without any coordination. Also in: a node now notices a peer it can no longer hear and stays audible to peers that check the same way (#55); it announces its height in the handshake and says out loud when it falls behind the network (#56); script evaluation has ceilings on operation count and stack depth (#57); the tail of the chain is verified at startup and a bad tip is backed out of instead of being mined on (#61); and the wallet now believes the chain about what it has already spent, which is what a restored backup gets wrong (#62).
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.
Addresses part of #47, and the situation #40 describes living through.
The problem
fSpentlives in wallet.dat and is written when this node spends something. A wallet.dat restored from backup, or copied and used on another machine, carries whatever that flag was at the moment the copy was taken — so it can say "unspent" about coins the chain shows as gone.The balance reads high and nothing contradicts it. The error surfaces only when a send is attempted against coins that no longer exist.
Bitcoin fixed the same thing in
53d508072.What this does
On startup, after the block index is loaded and before anything reports a balance, walk the wallet and mark spent whatever the transaction index says is spent.
One direction only. Marking spent when the chain says spent is safe. Clearing the flag because the chain has not caught up yet would offer up coins that are already on their way out.
fSpentis one flag per transaction rather than per output. That is how 0.1.0 has always worked —CommitTransactionSpentalready marks a whole previous transaction when it spends any part of it — so this matches the existing model rather than changing it.It reports every run
Including the runs that find nothing. A check that speaks only when it finds something is indistinguishable from one that never ran, and that confusion has cost this project real time.
The breakdown earned its place immediately. My first two attempts to test the correcting branch reported "corrected 0" and I could not tell whether the logic was wrong or the test was. The counters answered it in one run: all 17 wallet transactions were absent from the transaction index, so the comparison never had anything to compare.
What is and is not verified
Verified: runs against a 17-transaction wallet and an empty one, no false corrections in either. That is the dangerous direction — a false positive here would hide a user's coins — and it is clean.
Not verified: the correcting branch itself. On this chain no test wallet holds a transaction that is both present in the transaction index and spent, so the branch was never entered by a real case. Saying so rather than implying otherwise.