wallet: check block availability before registering chain notifications in AttachChain

bitcoin/bitcoin: PR #36836

1 major finding · 1 suggestion

Findings

Link

[major] · Late notification registration can skip a block during wallet loading

A runtime wallet load can silently miss a block whose notification completes before registration, leaving an incorrect balance across reloads; the natural race frequency is unmeasured.

Location: src/wallet/wallet.cpp:3132 (head).

Details and suggested fix

Suppose an existing wallet's locator is at H and active-chain block H+1 connects after getHeight() but its BlockConnected event finishes before handleNotifications(). Registration does not replay completed events. AttachChain records the stale H and skips scanning. If a rescan is needed, Scan still stops at GetLastBlockHeight() == H while cs_wallet is held. A later non-historical block that passes the birthday guard and updates a wallet transaction, or reaches a 144-block checkpoint, can persist a locator beyond the missed block. Subsequent loads then trust that locator rather than recover the omitted history. An explicit rescan from the missed block recovers it while its data remains available. The longer interval between the height and hash reads also widens a pre-existing assertion race if the active chain shrinks below H; that secondary abort has not been reproduced.

Keep the availability preflight before registration, but obtain the authoritative tip after subscribing, reading its height and hash together under cs_main. Reconcile the locator fork if the chain changed, and use the refreshed snapshot for the in-memory processed block and rescan decision.


Optional improvements

Link

[suggestion] · Registration ordering lacks deterministic regression coverage

The existing wallet tests do not force either registration-ordering window, so their balance and transaction assertions do not deterministically protect the failed-load fix or the successful-load handoff.

Location: src/wallet/wallet.cpp:3132 (head).

Details and suggested fix

wallet_assumeutxo.py submits blocks before the failed loads and checks the eventual balance, but does not force a relevant callback to be in flight during preflight failure. The AttachChain rescan test completes its chain extension before loading. CreateWallet delays pre-load events until after registration or creates transactions from the post-load hook, so neither phase completes a block notification between the tip read and subscription.

Add controlled scheduling tests for both invariants: an unavailable-block load must leave the stored locator unchanged even when a relevant callback is already in flight if a subscriber exists, and a successful load must find a wallet-relevant block connected and fully dispatched immediately before registration. The latter can use a forwarding interfaces::Chain hook that connects the block and drains notifications before delegating handleNotifications(). Assert the transaction and processed height, then reload after a later locator write to check persistence.


Coverage details