upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/main_gui.cpp
diff options
context:
space:
mode:
authorBitflash <btf01@proton.me>2026-07-31 08:53:31 -0300
committerGitHub <noreply@github.com>2026-07-31 08:53:31 -0300
commit7066648e6a871bd17984e02c04f22c2f22f2d744 (patch)
tree386f7b4a2c525299ba7ad239890e46bf4365f404 /src/main_gui.cpp
parent3bbfb50ef85feeddad936eb1fe8cce232410b8a6 (diff)
wallet: believe the chain about what has already been spent (#62)
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.
Diffstat (limited to 'src/main_gui.cpp')
-rw-r--r--src/main_gui.cpp4
1 files changed, 4 insertions, 0 deletions
diff --git a/src/main_gui.cpp b/src/main_gui.cpp
index 99bcb6f..c593162 100644
--- a/src/main_gui.cpp
+++ b/src/main_gui.cpp
@@ -243,6 +243,10 @@ int main(int argc, char* argv[])
243 try 243 try
244 { 244 {
245 if (!LoadWallet()) { fprintf(stderr, "LoadWallet failed\n"); return 1; } 245 if (!LoadWallet()) { fprintf(stderr, "LoadWallet failed\n"); return 1; }
246
247 // After the block index, so there is a chain to compare the wallet
248 // against, and before anything reports a balance.
249 RescanSpentFlags();
246 } 250 }
247 catch (const std::exception& e) 251 catch (const std::exception& e)
248 { 252 {