wallet: check block availability before registering chain notifications in AttachChain #36836

pull reginaldalfret wants to merge 1 commits into bitcoin:master from reginaldalfret:contrib-wallet-fix-load-failure-locator changing 1 files +17 −13
  1. reginaldalfret commented at 2:14 PM on November 11, 2026: none

    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.py passes 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, and wallet_importdescriptors.py all passed.
  2. wallet: check block availability before registering chain notifications in AttachChain
    When loading a wallet that requires blocks not currently available on disk (e.g., during assumeutxo background sync or on a pruned node), AttachChain previously registered m_chain_notifications_handler with the chain before checking whether required historical blocks were present.
    
    If the availability check subsequently failed, AttachChain returned false, triggering DisconnectChainNotifications(). Disconnecting notifications drains pending validation interface callbacks, during which queued BlockConnected notifications could be delivered to the failing wallet instance. If a delivered block updated the wallet or matched a checkpoint height, CWallet::blockConnected persisted an updated best-block locator to disk via WriteBestBlock().
    
    Consequently, a failed wallet load could mutate the wallet on disk with an advanced locator. On the next load attempt after node sync completed, the wallet trusted this corrupted locator and skipped rescanning earlier blocks, silently missing transactions and resulting in incorrect balances (as observed in CI on wallet_assumeutxo.py).
    
    Fix this by performing the block-availability preflight check before registering m_chain_notifications_handler. If required blocks are missing, AttachChain returns false without registering validation callbacks, preventing any notification delivery, locator corruption, or on-disk mutation.
    
    Fixes #36828.
    29ce046763
  3. DrahtBot added the label Wallet on Nov 11, 2026
  4. DrahtBot commented at 2:14 PM on November 11, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. pinheadmz commented at 2:18 PM on November 11, 2026: member

    Before we spend precious human resources reviewing your submission, can you guarantee us that you have read our AI Policy and this particular contribution conforms to that policy?

    It's important for us to know that real human users have encountered the problem you are trying to solve and there is a worthwhile motivation for this work.

  6. maflcko commented at 2:20 PM on November 11, 2026: member

    Thx, but LLM output is not accepted if the author can not properly explain, test or could have written the change themselves. Also, LLM generated content is not allowed in comments/communications. See https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md

    The bottleneck in this project has always been review and testing, not writing code. Development here is intentionally conservative and slow, and reviewer attention is the scarcest resource we have. LLMs have made this worse, anyone can now prompt them and post their output as PRs. There is an infinite amount plausible looking "improvements" for LLMs to suggest and work on.

    Unless we fully trust LLMs to both write and review code, humans still have to spend time understanding the proposed changes, which incurs a non-zero cost for every opened PR.

    I understand that contributing to this project can be intimidating, and using LLMs may seem tempting, but it really creates more issues for this project than it solves. The best way to help this project, is to review and test changes. You can use LLMs for this, but you shouldn't solely rely on them, or just post their output.

    I am asking you to reconsider whether it's something you genuinely think the project should pursue, independent of what your LLM suggested.

    See https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md

  7. maflcko closed this on Nov 11, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-19 19:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me