wallet: fix migration with watch-only transactions #36825

Open
psychemist wants to merge 2 commits from fix-wallet-migration-watch_only into master AGit
psychemist commented 2026-11-10 12:02:56 +00:00 (Migrated from github.com)

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.

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](https://github.com/bitcoin/bitcoin/issues/36483) 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.
DrahtBot commented 2026-11-10 12:03:05 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36825 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36825.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36825#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [cprkrn](https://github.com/bitcoin/bitcoin/pull/36825#issuecomment-7195811056) | | Concept ACK | [Eunovo](https://github.com/bitcoin/bitcoin/pull/36825#pullrequestreview-5937862910) | | Stale ACK | [Naomi-Gift](https://github.com/bitcoin/bitcoin/pull/36825#issuecomment-6799626987), [Zeegaths](https://github.com/bitcoin/bitcoin/pull/36825#issuecomment-6914277097) | 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: * [#36823](https://github.com/bitcoin/bitcoin/pull/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](https://github.com/bitcoin/bitcoin/pull/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](https://github.com/bitcoin/bitcoin/pull/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](https://github.com/bitcoin/bitcoin/pull/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) * [#30343](https://github.com/bitcoin/bitcoin/pull/30343) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/30343.svg"></sub> (wallet, logging: Replace WalletLogPrintf() with LogInfo() by ryanofsky) * [#27865](https://github.com/bitcoin/bitcoin/pull/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-->
First-time contributor

Earlier concerns remain unresolved; see the Run archive linked from the Review report.

🔵 Suggestion · DbTxnListener requires no-op callbacks for unused events

No current registrant omits a callback, but handling empty callbacks centrally would remove the explicit no-op requirement and the associated std::bad_function_call trap.

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

Review coverage was incomplete.


Reviewed: b1a356dee965 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3705f06c6a27c475c77f2533c4d016ac03c829c839b7b538bba5e4768ccf54f1:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:f498a5a6718d9157b6afb85412c3462439117702 --> <!-- ralph-head:b1a356dee9653922bfad152370c28aea39a0877e --> <!-- ralph-case:a5552f172c3198e8d6d8525c2b4a62a74deacc42a13dd86967044823beebf9e7 --> Earlier concerns remain unresolved; see the Run archive linked from the Review report. **🔵 Suggestion · `DbTxnListener` requires no-op callbacks for unused events** No current registrant omits a callback, but handling empty callbacks centrally would remove the explicit no-op requirement and the associated `std::bad_function_call` trap. Location: [`src/wallet/wallet.cpp:2295`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/b1a356dee9653922bfad152370c28aea39a0877e/src/wallet/wallet.cpp#L2295>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`b1a356dee965`](<https://git.fish.foo/bitcoin/bitcoin/commit/b1a356dee9653922bfad152370c28aea39a0877e>) · [Review report](<https://review.fish.foo/report/36825-b1a356dee9653922bfad152370c28aea39a0877e-2886-4.html>) · [Previous reports](<https://review.fish.foo/traces/36825/>) [permalink](<https://review.fish.foo/report/36825-b1a356dee9653922bfad152370c28aea39a0877e-2886-4.html>)
psychemist commented 2026-11-10 12:46:23 +00:00 (Migrated from github.com)

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

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
psychemist commented 2026-11-10 15:29:20 +00:00 (Migrated from github.com)

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

force-pushed to amend last commit message and add issue author as co-author. no code changes
psychemist commented 2026-11-10 15:44:39 +00:00 (Migrated from github.com)

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

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 commented 2026-11-11 15:16:12 +00:00 (Migrated from github.com)

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.

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.
Zeegaths commented 2026-11-16 10:47:42 +00:00 (Migrated from github.com)

tACK 6ed5058

tACK [6ed5058](https://github.com/bitcoin/bitcoin/commit/6ed50582c17d15aac0252338411c99eb8b6047fc)
Eunovo (Migrated from github.com) reviewed 2026-11-17 15:23:06 +00:00
Eunovo (Migrated from github.com) left a comment
Concept ACK https://github.com/bitcoin/bitcoin/pull/36825/commits/6ed50582c17d15aac0252338411c99eb8b6047fc
Eunovo (Migrated from github.com) commented 2026-11-17 14:12:57 +00:00

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.

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.
Eunovo (Migrated from github.com) commented 2026-11-17 11:50:43 +00:00
https://github.com/bitcoin/bitcoin/pull/36825/commits/6ed50582c17d15aac0252338411c99eb8b6047fc: nit: Is this assertion necessary?
Eunovo (Migrated from github.com) commented 2026-11-17 11:49:58 +00:00

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.

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 (Migrated from github.com) reviewed 2026-11-18 16:20:26 +00:00
psychemist (Migrated from github.com) commented 2026-11-18 16:20:26 +00:00

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

used 2 transactions to show multiple transactions survive migration. will simplify to eliminate looping
psychemist (Migrated from github.com) reviewed 2026-11-18 16:23:09 +00:00
psychemist (Migrated from github.com) commented 2026-11-18 16:23:09 +00:00

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

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
psychemist (Migrated from github.com) reviewed 2026-11-18 17:53:44 +00:00
psychemist (Migrated from github.com) commented 2026-11-18 17:53:44 +00:00

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

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
Eunovo (Migrated from github.com) reviewed 2026-11-19 04:25:22 +00:00
Eunovo (Migrated from github.com) commented 2026-11-19 04:25:22 +00:00

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.

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.
psychemist commented 2026-11-20 10:13:49 +00:00 (Migrated from github.com)

@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

@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
DrahtBot commented 2026-11-24 16:48:02 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
cprkrn commented 2026-11-26 15:07:15 +00:00 (Migrated from github.com)

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!

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 in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36825
No description provided.