Problem
When loading an existing wallet on a node where required blocks are not yet available on disk (for example, on a pruned node or during assumeutxo background sync before historical blocks are downloaded), CWallet::AttachChain previously registered m_chain_notifications_handler with the chain before validating block availability (rescan_height != block_height).
When the availability check subsequently failed, AttachChain returned false, and CWallet::LoadExisting disconnected the notifications handler. Calling DisconnectChainNotifications() calls chain().waitForNotifications(), which drains pending validation interface callbacks. Any BlockConnected notifications queued on the scheduler or arriving from background synchronization during that window were delivered to the failing wallet instance. If a delivered block matched wallet transactions or fell on a periodic checkpoint, CWallet::blockConnected invoked WriteBestBlock(), persisting an updated CBlockLocator to disk despite the failed wallet load.
As a result, a wallet that failed to load was mutated on disk with an advanced locator. On the next load attempt after node sync completed, the wallet trusted this corrupted locator and skipped rescanning all preceding blocks, silently missing transactions and reporting incorrect balances (e.g. failing with AssertionError: not(15.00000000 == 34) in wallet_assumeutxo.py).
Fixes #36828.
Solution
Perform the block-availability preflight check (verifying chain.havePruned() or chain.hasAssumedValidChain() against haveBlockOnDisk) before registering m_chain_notifications_handler. If required historical blocks are missing, AttachChain exits immediately with return false without ever registering validation callbacks. This ensures that no notifications are queued or delivered to the wallet, preventing any locator corruption or on-disk mutation during a failed load.
Testing
- Built with CMake 4.3.0 and GCC 16.2.1.
- Unit tests: Verified
test_bitcoin --run_test=wallet_tests,rescan_tests,walletload_tests(32/32 test cases passed). - Functional tests: Verified
wallet_assumeutxo.pypasses cleanly (demonstrating that pre-sync failed load does not corrupt the post-sync balance). - Additional functional regressions:
wallet_basic.py,wallet_multiwallet.py,wallet_balance.py,wallet_backup.py,wallet_labels.py, andwallet_importdescriptors.pyall passed.