wallet: fix migration with watch-only transactions #36825

pull psychemist wants to merge 2 commits into bitcoin:master from psychemist:fix-wallet-migration-watch_only changing 3 files +81 −3
  1. psychemist commented at 12:02 PM on November 10, 2026: none

    Fixes #36483

    As described in the linked issue, migration to descriptor wallets currently fails for legacy wallets that contain no spendable keys and are tracking watch-only transactions.

    When a database transaction containing RemoveTxs() is aborted, the database rolls back and the registered on_abort callback is invoked. The current listener contains an empty std::function so calling it throws std::bad_function_call and masks the actual migration error.

    The first commit changes the callback to a no-op function since no memory cleanup happens on abort. It also adds a unit test to confirm that abort restores the database record and leaves wallet memory unchanged.

    After this commit, the functional test supplied by the issue's author reports Error: Not all transaction records were migrated (-4), exposing the migration error previously masked by the abort callback exception.

    During wallet migration, RemoveTxs() defers deletion of transactions from the original wallet's in-memory mapWallet until after the database commit. The current check that requires an empty map before the database commit causes migration to fail.

    The second commit checks that the transaction IDs left in the map exactly match those already scheduled to be removed in the txids_to_delete vector. If they match, migration continues and the on_commit callback removes those transactions from memory.

    A new functional test is added to cover migration of a blank wallet with watch-only transactions. It checks that the destination watch-only wallet retains both transactions and balance, including after unloading and reloading the wallet, without rescanning the blockchain.

  2. fix(wallet): Handle abort of transaction removal
    Aborting a RemoveTxs() transaction invokes an empty std::function, which
    throws `std::bad_function_call`.
    
    Implement a no-op abort callback since memory changes are deferred until
    after database commit. Extend RemoveTxs coverage to verify rollback
    restores the database record and leaves wallet memory unchanged.
    e518ef26f4
  3. DrahtBot added the label Wallet on Nov 10, 2026
  4. DrahtBot commented at 12:03 PM on November 10, 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.

    Type Reviewers
    ACK Naomi-Gift, Zeegaths
    Concept ACK Eunovo

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36897 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36897.svg"></sub> (wallet, migration: Contain all legacy wallet and migration related things in its own subdirectory, namespace, and classes by achow101)
    • #36823 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36823.svg"></sub> (wallet, migration: Skip legacy wallet records rather than erasing them, and drop unused record erase functions by achow101)
    • #36767 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36767.svg"></sub> (wallet: fix crash when external signer wallet creation fails by zerog975)
    • #35716 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35716.svg"></sub> (wallet: Replace mapWallet and wtxOrdered with a boost::multi_index by achow101)
    • #34909 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34909.svg"></sub> (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux)
    • #27865 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/27865.svg"></sub> (wallet: Track no-longer-spendable TXOs separately by achow101)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  5. fix(wallet): Fix watch-only migration failure
    Removing transactions updates `mapWallet` only after database commit.
    Requiring an empty map beforehand causes migration of legacy wallets
    containing only watch-only transactions to fail.
    
    Require all transactions in map to be those already scheduled for
    removal. Add regression coverage to ensure transactions and balances are
    preserved after migration and wallet reload without having to rescan
    blockchain
    
    Co-authored-by: David Gumberg <davidzgumberg@gmail.com>
    6ed50582c1
  6. psychemist force-pushed on Nov 10, 2026
  7. psychemist commented at 3:29 PM on November 10, 2026: none

    force-pushed to amend last commit message and add issue author as co-author. no code changes

  8. psychemist commented at 3:44 PM on November 10, 2026: none

    the return value of WriteFullTx() is currently ignored. a possible follow-up is to check the return value and add test coverage to verify that the original wallet is preserved when a destination write fails

  9. Naomi-Gift commented at 3:16 PM on November 11, 2026: none

    tACK 6ed50582c1 I ran both tests, and they pass with the fix but fail without it. nit: Use wallet: ... instead of fix(wallet): ... in commit messages. nit: Change .on_abort= {} to .on_abort = [] {} to match scriptpubkeyman.cpp:1040.

  10. Zeegaths commented at 10:47 AM on November 16, 2026: none

    tACK 6ed5058

  11. in test/functional/wallet_migration.py:1193 in 6ed50582c1
    1188 | +        wallet.importaddress(addr)
    1189 | +
    1190 | +        # Migrate blank legacy wallet to a dedicated watch-only descriptor wallet
    1191 | +        # without dropping any watched transaction.
    1192 | +        assert_equal(wallet.getwalletinfo()["private_keys_enabled"], True)
    1193 | +        received_txids = [default.sendtoaddress(addr, amount) for amount in (10, 5)]
    


    Eunovo commented at 11:49 AM on November 17, 2026:

    https://github.com/bitcoin/bitcoin/pull/36825/commits/6ed50582c17d15aac0252338411c99eb8b6047fc:

    Why use 2 txs instead of 1? The test would be simpler and easier to read with one tx, since there would be no need for loops.


    psychemist commented at 4:20 PM on November 18, 2026:

    used 2 transactions to show multiple transactions survive migration. will simplify to eliminate looping

  12. in test/functional/wallet_migration.py:1192 in 6ed50582c1
    1187 | +        addr = key.p2pkh_addr
    1188 | +        wallet.importaddress(addr)
    1189 | +
    1190 | +        # Migrate blank legacy wallet to a dedicated watch-only descriptor wallet
    1191 | +        # without dropping any watched transaction.
    1192 | +        assert_equal(wallet.getwalletinfo()["private_keys_enabled"], True)
    


    Eunovo commented at 11:50 AM on November 17, 2026:

    psychemist commented at 4:23 PM on November 18, 2026:

    i used the assertion to highlight that the watch-only descriptor wallet does not have private keys enabled even though it is migrated from a legacy wallet that does


    Eunovo commented at 4:25 AM on November 19, 2026:

    The presence of spendable material in the legacy wallet is what matters in this test, and this assertion does not test that. I think that line can be removed, but it's up to you.

  13. in src/wallet/wallet.cpp:4067 in 6ed50582c1
    4062 | @@ -4063,7 +4063,13 @@ util::Result<void> CWallet::ApplyMigrationData(WalletBatch& local_wallet_batch,
    4063 |      // migrated records will be presented to the user.
    4064 |      if (!has_spendable_material) {
    4065 |          if (!m_address_book.empty()) return util::Error{_("Error: Not all address book records were migrated")};
    4066 | -        if (!mapWallet.empty()) return util::Error{_("Error: Not all transaction records were migrated")};
    4067 | +
    4068 | +        const std::set<Txid> pending_removals(txids_to_delete.begin(), txids_to_delete.end());
    


    Eunovo commented at 2:12 PM on November 17, 2026:

    https://github.com/bitcoin/bitcoin/pull/36825/commits/6ed50582c17d15aac0252338411c99eb8b6047fc:

    I think it makes sense to move the check to after the DB transaction is committed and the in-memory state is updated. It is safe to do this because a failure after the DB commit still restores the legacy wallet.


    psychemist commented at 5:53 PM on November 18, 2026:

    good catch. moving the check to after the database commits is a more obvious confirmation that all transactions have been removed. i tested this by moving the check to DoMigration() after RunWithinTxn() and the new migration test case still passes

  14. Eunovo commented at 3:23 PM on November 17, 2026: contributor

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