Skip to content

wallet: fix migration with watch-only transactions - #36825

Open
psychemist wants to merge 2 commits into
bitcoin:masterfrom
psychemist:fix-wallet-migration-watch_only
Open

psychemist wants to merge 2 commits into
bitcoin:masterfrom
psychemist:fix-wallet-migration-watch_only

Conversation

@psychemist

@psychemist psychemist commented Nov 10, 2026 •

Copy link
Copy Markdown

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 changes from bad_function_call to 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 moves the check for an empty mapWallet to after RunWithinTxn() commits the database transaction and runs the commit callback, when transactions marked for removal have been deleted.

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.

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.
@DrahtBot

DrahtBot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK cprkrn
Concept ACK Eunovo
Stale ACK Naomi-Gift, Zeegaths

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36823 (wallet, migration: Skip legacy wallet records rather than erasing them, and drop unused record erase functions by achow101)
  • #36767 (wallet: fix crash when external signer wallet creation fails by zerog975)
  • #35716 (wallet: Replace mapWallet and wtxOrdered with a boost::multi_index by achow101)
  • #34909 (wallet, refactor: modularise wallet by extracting out legacy wallet migration by rkrux)
  • #30343 (wallet, logging: Replace WalletLogPrintf() with LogInfo() by ryanofsky)
  • #27865 (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.

@psychemist
psychemist force-pushed the fix-wallet-migration-watch_only branch from 7410d01 to 6ed5058 Compare November 10, 2026 15:27
@psychemist

psychemist commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

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

@psychemist

psychemist commented Nov 10, 2026 •

Copy link
Copy Markdown
Author

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

@Naomi-Gift

Copy link
Copy Markdown

tACK 6ed5058
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.

@Zeegaths

Zeegaths commented Nov 16, 2026 •

Copy link
Copy Markdown

tACK 6ed5058

@Eunovo Eunovo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK 6ed5058

Comment thread test/functional/wallet_migration.py Resolved
Comment thread test/functional/wallet_migration.py Resolved
Comment thread src/wallet/wallet.cpp Resolved
@psychemist

psychemist commented Nov 20, 2026 •

Copy link
Copy Markdown
Author

@Eunovo pushed a commit adding the changes you suggested and updated the PR description to reflect this change
p.s force-pushed to squash last two commits into one since they address the same problem

Removing transactions updates `mapWallet` only after database commit
is successful. Requiring an empty map beforehand causes migration of
legacy wallets containing only watch-only transactions to fail.

Move final address book and transaction map emptiness checks for
legacy wallets with no spendable material to `DoMigration()`, after
`RunWithinTxn()` commits the migration database transaction and the
commit callback registered by `RemoveTxs()` updates memory.

Add regression coverage to verify the successfully migrated wallet
retains the original balance and transaction after unloading and
reloading without rescanning.

Co-authored-by: David Gumberg <[email protected]>
@psychemist
psychemist force-pushed the fix-wallet-migration-watch_only branch from 0fd1311 to b1a356d Compare November 20, 2026 16:57
@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

@cprkrn

cprkrn commented Nov 26, 2026

Copy link
Copy Markdown

tACK b1a356d, wallet migrates with transaction and balance and all wallet tests pass.

we forced a failure after the database save the original wallet was restored unchanged.

on v31.1 / v30.2 without this fix, the wallet gets stuck half-migrated need to restore the backup by hand.

and it needs a rebase onto #34193, will retest after.

Used AI!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wallet: Migration fails for legacy wallet with no keys watching an address

6 participants