Repository navigation
fix(scanner): detect chain reorganisations and roll back to the fork point - #21
Merged
Merged
Conversation
…SNB-541) The official vectors only reached Scanner::scan_transaction_full, which has no production caller. The daemon receives through scan_short_block_data -> scan_transaction_short (k = 0, labels enumerated by hand, 8-byte x-only prefixes) and, on a probable match, apply_block_relevant on the external indexer. A false negative in the short step means the block is never fetched: silent loss. - Serve every receiving vector the way the oracle does (taproot outputs' 8-byte x-only prefixes, the 33-byte tweak, the display-order txid) and assert a probable match wherever the vector expects >= 1 output. Pins which k = 0 branch did the work: unlabelled P_0, labels of both output parities, and the change label m = 0. False positives are counted and reported (zero on these vectors). - Drive the real post-match step for the same vectors: the vector tx in a full block through apply_block_relevant, asserting the stored outputs and priv_key_tweaks equal expected.outputs. - Extract the post-match step (txid mapping + apply_block_relevant + stage merge) from scan_block_range into Scanner::apply_matched_block, unchanged, so the test exercises the production code rather than a copy. - K_max through the live path is SNB-540: an ignored test records that it stores 2324 outputs where BIP-352 allows 2323. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The scanner's `owned_outputs` set, which the per-block spent-output check matches the oracle's 8-byte `spent_outputs` prefixes against, had a writer (`add_owned_output`) that no call site ever invoked. The set stayed empty, the spent check never fired, and a block whose only wallet-relevant transaction was a spend (no Silent Payments output back to the wallet) was never fetched. The spending transaction never reached the wallet graph, so blindbit-cli and friglet reported a balance that still included spent coins and Electrum clients never saw the spend. Spends were only noticed by accident, when the spending transaction also paid the wallet (e.g. change). - `Scanner::sync_owned_outputs` rebuilds the owned-output records from the indexer after every applied block and on restore: outpoint, full x-only key, amount, height, and the BIP-352 label `m` the output matched. - A record is marked spent only when a confirmed transaction in the wallet graph spends exactly that outpoint. The 8-byte prefixes, matched through a prefix -> unspent-outpoint map, only decide which blocks to fetch; a foreign key sharing the prefix fetches the block but marks nothing. - The state file's `owned_outputs` array now holds these records (label and spend included). Legacy bare-hex entries are accepted and dropped; the records are re-derived from the indexer on load. - Restore registered labels only after re-indexing the stored transactions, and `load_from_file` blanks the persisted label table, so every labelled output (change included, m = 0) silently dropped out of the index, the balance and spend detection after each restart. Labels are now registered before the changeset is applied. - `apply_matched_block` is extracted from `scan_block_range` (same shape as in #17) so the tests drive the real per-block path without P2P. Tests (in-process, `owned_outputs_tests.rs`): receive then spend in a later block marks the output spent and the balance drops; a colliding foreign prefix marks nothing; records, labels and spends survive save/load/restore and spend detection keeps working after it; a labelled receive records the right m. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
… messages Adds an in-process seam (BlockScanDataStream) between scan_block_range and the scan loop, with no behaviour change, and tests that drive a real Scanner through it. They fail on this commit: an empty-hash block mid-range is treated as an empty block (payments in it are lost and the scan reports success), height gaps and short streams are accepted, and any error status panics the process via unwrap(). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ipping it An oracle answers a height it has not indexed with an OK message carrying an empty block hash and no data. scan_block_range took that as a block with no payments: it warned, advanced the scan height past it and reported success, so any payment in that block was never found (proven by the testkit mutation check unindexed-ok-empty: 80000 sats reported instead of 120600). Separately, every stream message was unwrap()ed, so an error status (NOT_FOUND, UNAVAILABLE) panicked the process, as did failing to open the stream. Now each message must be the next expected height and carry a valid 32-byte, non-zero block hash; otherwise, and on any error status or a stream that ends before the requested end, the scan returns an error naming the height to rescan from. The scan height stays at the last fully processed block, so watch_chain retries from there on its next poll (every 10 s, no spin) and blindbit-cli exits non-zero after saving that height. The example is updated the same way. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ix/reorg-rollback
…point The live scanner had no reorg handling: after a reorganisation it kept every output and spend learned from disconnected blocks. An evicted payment stayed in the balance as a phantom output, a payment that re-confirmed at another height kept its old height, and an output whose spend was disconnected stayed marked spent. The scanner now remembers the block hash of each scanned height for the last REORG_LOOKBACK (144) heights, persisted in the state file as `scanned_block_hashes`. A range scan that continues the scanned chain streams one block early and compares it; watch_chain also runs that check when the oracle has no new block, which catches a same-height tip replacement. On a mismatch the scan walks up from the oldest remembered hash to find the fork point. It rolls back everything above it: graph transactions and anchors from disconnected blocks with their tweaks, the owned-output records and spent marks derived from them, checkpoints, remembered hashes, and Electrum history/SP history/headers. It persists that state and scans the new branch from the fork. A fork at or below the oldest remembered hash is a ReorgTooDeep error. That error changes nothing and repeats on every attempt, and it tells the user to rescan from the wallet birthday. friglet drops its snapshotted checkpoint hashes and cached headers above the fork (and rewrites the header sidecar) so it never serves a header of a disconnected block. blindbit-cli goes through scan_block_range and gets the same check. Tests drive the stream seam with an in-process oracle, mirroring the testkit T2 reorg scenario (fork at 104, 2 disconnected, 3 mined): eviction, re-confirmation at a new height, a disconnected spend, a same-height tip replacement, persistence plus restart mid-rescan, and a reorg deeper than the lookback. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…d-labels
Resolve scanning.rs: keep sync_owned_outputs() after apply_matched_block,
keep both test-module lines, and drop the dead
`if probable_match.spent {}` block that #17's extraction carried over.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s below it; report stalls A wallet whose start height lies below the oracle's first indexed block (the hosted oracle has nothing below 800000 on mainnet and 100000 on signet) stopped at its first height on every poll, forever, with only an error-level log line. Since #20 refuses to scan past an unindexed height, that is the only thing such a wallet ever did. - When `watch_chain`'s scan stops at the wallet's start height because the oracle has not indexed it, the scanner locates the oracle's lowest indexed height with GetBlockHashByHeight lookups and starts there, logs a warning, persists the adjustment (`oracle_floor_start`, optional in the state file) and publishes it in the scan health. - Only heights below the oracle's floor are skipped. A height above the first scanned block, or one with an indexed height below it (a gap in the oracle's index), keeps #20's stop-and-retry. The search is a binary search verified by exponential back-off probes below the boundary, so a gap above the floor does not move the floor up. NOT_FOUND and an empty or zero hash both mean "not indexed". - A scan that cannot get past a height (or cannot reach the oracle) is published as a stall: height, reason, and the time of the first failed attempt at that height. It clears when the scan makes progress. - Stream stops are a typed `ScanStopped` error (same message text) so the loop can tell "not indexed" from other failures. - Health lives in the wallet's Electrum index, which status readers lock without waiting on the scan task. friglet's GetStatus reports it as `StatusInfo::scan_health` (serde default, so old trays and daemons interoperate), and the tray's status window shows a stall line and a note for an adjusted start. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd-labels #20 lands before this PR, so this branch carries it to merge cleanly after it. changeset.rs: keep both imports (#20's OracleFloorStart and this branch's OwnedOutputRecord); the serde_hex module #20 still had is the one this branch replaced. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ends it missed A state file written before owned outputs were recorded still loads, but its wallet never fetched blocks whose only wallet-relevant transaction was a spend, so those spends stay missing and nothing said so. - The state file now carries `format_version` (STATE_FORMAT_VERSION = 1; absent = 0, written before the owned-output records). Every save writes the current version. - Restoring a format-0 state rewinds the scan to just below its oldest unspent output, the lowest height a missed spend can be at. The next scan revisits those blocks and, with the outputs now recorded, fetches the ones that spend them. Re-applying blocks already in the graph is idempotent. With no unspent output there is nothing to re-check and only the version is bumped. - The rewind is logged and published in the scan health (`StateRescan`), shown in friglet's status and the tray, and cleared once the scan is back where the state had been. While it runs, the Electrum tip stays at that height, so clients never see wallet history above the tip. - Test fixture `testdata/state_before_owned_outputs.json` was written by the pre-fix code (integration 93f6a92): payment + change at 100, scanned to 300, spend at 200 missing. The test loads it as written and with legacy bare-hex `owned_outputs` entries, checks the restored records and labels, the rewind to 99, the held Electrum tip, that the rescan finds the spend, and that the upgraded file restores without another rewind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fix/reorg-rollback Keeps this branch a superset of the stack below it. - changeset.rs / scanner.rs: keep both sides' new fields (scanned_block_hashes; format_version, oracle_floor_start). - scanning.rs: keep this branch's scan_range_from / BlockStreamSource; OracleStreams::open now reports a refused stream as #20's typed ScanStopped. watch_chain combines both: the no-new-block hash check from here, and #20's floor start (only for a real range) and stall reporting, with the ReorgTooDeep log kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ing blocks; catch mid-stream oracle reorgs The reorg check streamed the tip block again on every idle poll (~45 KB of compute index every 10 s on mainnet, ~385 MB a day per friglet and the same egress on the oracle), streamed the last scanned block again in front of every new block, and located a fork by streaming the whole 144-block lookback window. - The block below a range is checked with one GetBlockHashByHeight lookup against the remembered hash. An idle poll streams nothing; a new block is streamed once. An empty hash or NOT_FOUND means "cannot check yet", not a reorganisation. - A disagreement is located with a binary search over the remembered heights (lookups only), and the rollback stream starts at the fork point. The oracle disagreeing at the oldest remembered height is ReorgTooDeep without streaming anything, so a halted wallet costs two lookups per poll. - The short stream carries no previous-block hash, so an oracle that switches branches while a stream runs could hand over blocks of both and nothing noticed. When a stream ends, its newest blocks (POST_STREAM_CHECK = 6) and the block below them are checked again; a disagreement is rolled back like any other reorganisation (bounded to 3 restarts per scan). - The per-poll body of watch_chain is `watch_step`, generic over the oracle, so the tests drive the real loop: a ReorgTooDeep is published as a stall (reason says to rescan from the wallet birthday), like any other scan that cannot proceed. - record_scanned_block_hash no longer copies the 144-hash window into the staged changeset on every block (save_to_file writes the current window), and trims the window in place. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n attribute) The status window's error, stall and scan-notes blocks use class="col", whose display: flex beats the user-agent [hidden] rule, so an empty "Last error" label was always shown and the new stall/notes blocks would be too. Seen in a screenshot against a live daemon. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd-labels Picks up the tray CSS fix for hidden status blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Picks up #20's tray CSS fix for hidden status blocks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
setavenger
added a commit
that referenced
this pull request
Oct 1, 2026
…ood/preview Brings in the #17/#20/#19/#21/#31 stack. One conflict, in friglet/src/electrum/mod.rs, resolved keeping both sides: - #26's ChainSource (p2p/network/oracle) and pending-broadcast store, - #21's reorg handling: checkpoints behind a mutex, reorg_rx feeding forget_blocks_above; its test now builds the server state with #26's test helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
setavenger
added a commit
that referenced
this pull request
Oct 1, 2026
Brings in the fix stack (#17, #20, #19, #21) so the T0 harness runs against the receive path that records owned outputs. scanning.rs: both sides extracted apply_matched_block; kept #21's sync_owned_outputs() call after it in the scan loop and its doc line, and kept both test modules (owned_outputs_tests from #21, testkit_t0 from this branch). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
setavenger
added a commit
that referenced
this pull request
Oct 1, 2026
The T0 harness replayed each block through a hand copy of the scan loop's per-block steps, and that copy had already fallen behind: the loop on the fix stack records owned outputs (sync_owned_outputs) after apply_matched_block and the copy did not, so nine spends were carried as KNOWN_SPEND_GAP. process_block now drives Scanner::scan_block_stream, the per-block loop scan_block_range and watch_chain run, with the oracle's message streamed in-process and the full block served in place of the P2P fetch (the stream-safety test hooks from #20/#21). There is no copy left to drift: a step added to the loop is exercised here. State files the loop now saves are removed when a scenario ends. KNOWN_SPEND_GAP and its assertions are gone; all nine scenarios must pass with their spends. Dropping the sync_owned_outputs() call from the loop turns exactly those nine spends red again (k-gap, labelled-multi, spent-outputs, dust-boundary, rescan). Fixture provenance now names testkit 68f9f1c, the merge of testkit PR #25 into master; the fixtures are byte-identical to the export at b1f962e. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since ScanStopped became a struct (22c50bf), blindbit-cli printed a stopped scan as `Error: ScanStopped { height: 104, reason: "...", not_indexed: true }`, the Debug form main uses for a returned error. The message the type renders for the user, "scan stopped at height 104: ...; nothing from height 104 on was scanned, rescan from height 104", no longer appeared. The testkit's mutation checks match that message and went red for stale-view, truncated-stream, close-mid-stream and unindexed-ok-empty. main now runs the command and prints the error with Display. The exit status of a failed command is still 1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd-labels Brings in the blindbit-cli error display fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in the blindbit-cli error display fix from #20. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
setavenger
added a commit
that referenced
this pull request
Oct 1, 2026
Brings in the blindbit-cli error display fix from #20. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 1, 2026
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.
Important
Stacked on #19, which contains #17 and #20. This branch carries the whole stack below it: #17 → #20 → #19 → this PR, each merging cleanly onto the one before (checked with a simulated landing on
integration). Once those land, this PR's diff is only the reorg commits (3ce1404, 9740d66) and merge commits. A follow-up, #31 (atomic state-file writes), is stacked on this PR.What was broken
The live scanner had no reorg handling at all. The testkit's T2 regtest
reorgscenario (fork at 104, 2 blocks disconnected, 3 mined) shows it. A fresh friglet rescan finds the right set. The live friglet kept both outputs of the evicted transaction (evicted-bob,evicted-alice), so it reported a phantom balance and anything built on those outputs failed. The consequences:Fix
REORG_LOOKBACK = 144heights. They are persisted in the state file asscanned_block_hashes. Older state files load with an empty map and start recording on their next scan.start - 1is checked with oneGetBlockHashByHeightlookup (39-byte response) against the remembered hash.watch_chainruns the same check when the oracle has no new block (start = end + 1), which catches a same-height tip replacement; when the oracle is behind, it checks at the oracle's tip. An idle poll streams nothing, and a new block is streamed once. An empty hash orNOT_FOUNDmeans "cannot check yet", not a reorg.Scanner::locate_fork). The rollback stream starts at that fork point, not at the bottom of the 144-block window. Inside any stream, every block with a remembered hash is still compared. If a stream's first block disagrees anyway (the chain changed between the lookups and the stream), the old walk up from the oldest remembered height is the fallback.POST_STREAM_CHECK = 6blocks and the block below them are checked again with lookups. A disagreement is rolled back like any other reorg, with at most 3 restarts per scan. Reorgs happen at the tip, which is where a stream ends, so that window covers them.Scanner::rollback_to). This drops the anchors above the fork point, and every transaction confirmed only in those blocks together with its tweak. The indexer is rebuilt from the remaining changeset (the same path a restart takes). Owned-output records and spent marks are re-derived, so evicted outputs disappear and disconnected spends are undone. It also drops checkpoints and remembered hashes above the fork, prunes the Electrum history, SP history, raw txs and headers of the disconnected blocks, persists, and notifies. The same stream then scans the new branch as new blocks, so a re-confirmed transaction comes back at its new height.Scanner::subscribe_to_reorgs()and, on a reorg, drops the checkpoint hashes it snapshotted at startup and its cached headers above the fork, and rewrites the header sidecar. That way it never serves a header of a disconnected block.scan_block_range, so it gets the same check. A deep reorg is a non-zero exit with the message above.ReorgTooDeep. It changes nothing, in memory or on disk, and says to move the state file away and rescan from the wallet birthday. It is found with lookups only (the oldest remembered height disagrees), so a halted wallet costs two lookups per poll rather than a stream of the 144-block window. In friglet it is published as a stall inGetStatus(scan_health.stall, from fix(scanner): stop at an unscannable oracle block instead of silently skipping it #20) and shown in the tray's status window, with the time it started and the reason. It is no longer only a log line.watch_chain's per-poll body is nowScanner::watch_step, generic over the oracle, so tests drive the real loop.record_scanned_block_hashno longer copies the 144-hash window into the staged changeset on every block (save_to_filewrites the current window), and trims the window in place.Bandwidth and lookups (9740d66)
Payload bytes per response (gRPC message plus its 5-byte frame), measured with a logging proxy in front of the hosted signet oracle. Two friglets ran side by side at the signet tip with fresh throwaway wallets:
3ce1404(this PR before the change) and9740d66.GetInfo23 B + stream of the tip block 10 702 BGetInfo23 B + 1 lookup 39 BGetInfo+ stream 324 499..=324 500, 2 blocks, 24 426 BGetInfo+ 1 lookup + stream of 324 500 only, 13 724 B + 2 lookups (78 B)Mainnet (hosted oracle, measured over the last 20 blocks to 969 449): a block message averages 36 596 B (range 1 728–75 039 B). At a poll every 10 s that projects to:
GetInfoonly)The hosted oracle serves the same bytes per user. The remaining cost of the reorg check over
integrationis one ~39-byte lookup per poll (a single pebble read on the oracle), plus three lookups per new block. Locating a 1-block tip reorg now takes 13 lookups and a 2-block stream, where before it streamed the whole 144-block window (tip_reorg_streams_from_the_fork_point_only).Verification
blindbit-lib/src/scanner/reorg_tests.rsuse the fix(scanner): stop at an unscannable oracle block instead of silently skipping it #20 stream seam, nowBlockStreamSource+OracleProbe. The in-process oracle counts streams, streamed blocks, lookups and bytes. The tests mirror the T2 scenario:reorg_evicting_a_payment_drops_its_output_and_balancereorg_reconfirming_a_payment_keeps_it_at_its_new_heightsame_height_tip_replacement_is_detected_without_a_new_blockreorg_disconnecting_a_spend_makes_the_output_unspent_againrollback_is_persisted_and_restart_mid_rescan_is_safe(restart after the reorg, stream dies right after the rollback, restart again, resume)reorg_deeper_than_the_lookback_is_a_loud_error_and_changes_nothingunchanged_chain_passes_the_check(control)idle_poll_is_one_hash_lookup_and_a_new_block_is_streamed_once(0 streams and 1 lookup per idle poll; 1 block streamed per new block)tip_reorg_streams_from_the_fork_point_onlyoracle_switching_branches_mid_stream_is_rolled_back(the oracle serves 105 from the old branch and 106 from the new one; the payment of old 105 is rolled back and found again at 106')too_deep_reorg_is_a_stall_and_polls_stay_cheap(the stall is published throughwatch_step, keeps its start time, and each poll costs 2 lookups and no stream)left: Some(106) right: None.friglet:electrum::tests::reorg_forgets_hashes_and_headers_above_the_fork,control::tests::status_reports_a_stalled_scan_and_an_adjusted_start.cargo test --workspaceat 9740d66, built in a private target dir: all suites ok. blindbit-lib 49 passed + 1 ignored, friglet 27, friglet-ipc 8+1, friglet-tray 26+9. The head adds only fix(scanner): stop at an unscannable oracle block instead of silently skipping it #20's tray CSS fix on top.cargo clippy --no-deps -p friglet -p friglet-ipc -p friglet-tray --all-targets -- -D warnings(the CI command): clean.cargo clippy -p blindbit-lib --all-targets --features serde: 6 warnings, as on fix: record owned outputs so spends are detected; persist labels #19.Live smoke run (9740d66, hosted signet oracle)
The runs used a throwaway signet SP key and the P2P peer
148.251.42.250:38333(from the signet DNS seed). friglet's oracle URL pointed at a local logging proxy, which forwarded tohttps://signet.oracle.setor.devand could pretend a height is not indexed.WARN the wallet's start height is below the oracle's first indexed block; ... requested_height=99000 oracle_floor=100000 skipped_blocks=1000. Scanning continued from 100 000 (174 494 after 30 s).GetStatus:"start_adjusted": {"requested_height": 99000, "oracle_floor": 100000}. The tray shows "Scanning began at block 100000 instead of 99000: the oracle has no data below it." (screenshot taken under Xvfb).GetInfo height=324499 bytes=23, thenGetBlockHashByHeight h=324499 bytes=39. The 3ce1404 friglet next to it streamed324499..=324499 bytes=10702each poll. Over the same window, new block 324 500 cost 24 426 B against 13 724 B + 3 × 39 B. Totals: 1 237 037 B over 126 polls against 41 651 B over 104 polls.NOT_FOUNDat 324 470. Every poll loggedscan stopped at height 324470: oracle stream error NotFound: height 324470 is not indexed; .... The floor search did not run, because the height is above the wallet's start.GetStatus:"stall": {"height": 324470, "reason": "scan stopped at height 324470: ...", "since_unix": 1790863242}, andsince_unixstayed the same across polls. The tray's status window showed a red "Scan stalled — at height 324470 since …" line (screenshot). Once the fault was removed, the next poll scanned to 324 499 and loggedscan is making progress again, andstallwent back tonull.Out of scope / notes
pending_scripthashespromotion of our own broadcast transactions is not re-done after a reorg. Non-wallet outputs of such a transaction lose their confirmed history entry if its block is disconnected.T2 end-to-end result (at 3ce1404, before the lookup change; not re-run)
Testkit branch
snb-528-t2-regtest,scenarios/reorg.tomlat-tier regtest, with blindbit-rs pinned to this branch (3ce1404). The fork is at 104: 2 blocks disconnected, 3 mined, one transaction re-confirmed and one evicted.Result:
status: PASS. All 4 must-find outputs are found, andevicted-bob/evicted-aliceare absent, exit 0. Before this fix the live friglet kept both evicted outputs.The run needed
FRIGLET_START_HEIGHT=2for the stack's healthcheck friglet only. The oracle at 1b0e113 never indexes height 1 on a fresh chain, and since #20 friglet correctly refuses to scan past an unindexed height. The wallet scanners under test start at 102, so the judged result is unaffected.🤖 Generated with Claude Code