3 minor findings · 1 suggestion
Earlier concerns neither republished nor resolved: 1 minor finding · 2 suggestions. See the Run archive for their assessments.
Findings
[minor] · The sole write of each retained tx record is unchecked
If WriteTxMetadata fails without preventing commit, migration can now succeed with a retained transaction missing; no concrete runtime trigger for that combination was established.
Location: src/wallet/migration.cpp:579 (head).
Details and suggested fix
DBKeys::TX is now skipped during the initial copy, so local_wallet_batch.WriteTxMetadata(*wtx) becomes the only write of each retained canonical transaction record. Its return value is ignored. SQLiteBatch::WriteKey can return false without explicitly aborting, while RunWithinTxn checks only the callback result and commit. Previously, a failed rewrite left the initially copied record present. Under the stated failure condition, the migrated wallet instead lacks the transaction and its wallet-only metadata. The copied best-block locator does not request recovery of that history, and rescanning cannot reconstruct comments or receive times; recovery requires .legacy.bak.
Return a migration error when this write fails so the transaction aborts and the existing backup-restore path runs. Failures that also prevent commit remain detected; this does not establish silent transaction loss for disk-full or I/O errors.
[minor] · Watch-only-only transactions leave orphaned wtxvariant records
Sources containing wtxvariant records now leave orphan variants in the main wallet when transactions move exclusively to the watch-only wallet, although no current producer of such a migration source was found.
Location: src/wallet/migration.cpp:272 (base).
Details and suggested fix
skip_types excludes DBKeys::TX but not DBKeys::WTX_VARIANT. For a transaction belonging only to the watch-only wallet, migration writes WriteFullTx there and then continues without writing a parent tx record in the main wallet. Its copied variants remain behind. Previously, RemoveTxs called EraseTx, which deleted both the canonical record and its variant prefix. The loader reads variants only for existing tx records, so these leftovers remain unused and persist into backups.
Also skip DBKeys::WTX_VARIANT and rebuild retained transactions with a checked WriteFullTx. Skipping variants alone would discard retained variants because WriteTxMetadata writes only the canonical record. Production migration opens read-only BDB sources that predate this record type; the mock benchmark produces variants, but its transactions belong to both wallets. Add a record-level test covering retained, shared, and watch-only-only transactions with variants.
[minor] · Interrupted migration can expose a flagless source as an empty wallet
An interruption after the filtered copy commits can make a source without FLAGS reload as an empty wallet, with keys recoverable from .legacy.bak; this scenario was not observed in execution.
Location: src/wallet/migration.cpp:75 (head).
Details and suggested fix
MigrateToSQLite commits the new SQLite file after omitting all legacy key/script and tx records. Descriptor conversion and ApplyMigrationData complete later. An interruption immediately after that first commit therefore leaves a published wallet file without keys or transaction history. If the source lacked FLAGS, LoadWalletFlags accepts its absence and the remaining load path succeeds without creating any script managers. Retrying migratewallet rejects the file because it is no longer BDB.
The same interruption at base was also ungraceful, but the main file retained the legacy keys and loading it hit SetupLegacyDataSPKM's database-format assertion rather than presenting an empty wallet. Persist a load-blocking migration marker in the initial SQLite transaction and clear it only when the semantic migration commits. The existing .legacy.bak preserves recovery of the original wallet.
Optional improvements
[suggestion] · The mapWallet emptiness check still rejects fully transferred history
A blank legacy wallet with private keys enabled and funded watch-only imports still fails migration because transferred transactions remain in mapWallet; this behavior predates the PR.
Location: src/wallet/migration.cpp:271 (base).
Details and suggested fix
Without a seed or spendable imports, these scripts become watch_descs and has_spendable_material is false. Their transactions are written to the watch-only wallet and reach continue, but remain in the temporary main wallet's mapWallet. The final emptiness check therefore returns an error even though no transaction should remain in the main database. Base already had this problem: RemoveTxs updated memory only in an on_commit listener, after this check had run.
Replace the transaction-map check with an invariant about what migration writes, rejecting a local transaction write when !has_spendable_material. This preserves the safety check without requiring cleanup of the temporary transaction graph. Extend the blank watch-only migration test by funding its imported address before migration; the inspected case currently has no transactions.
Earlier concerns
Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.
- [minor]: Malformed unknown BDB keys now make migration throw (earlier report)
- [suggestion]: Skipped records still incur temporary key and value copies (earlier report)
- [suggestion]: The migration flag conflates legacy records with skipped transactions (earlier report)
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Documented decisions
Witness variants are persistent transaction data, not merely disposable caches. Different valid witnesses can share a txid while having different wtxids. The introducing commit stores them separately under (txid, wtxid), merges them into CWalletTx on load, and retains the old canonical tx-record format so older software can rewrite that record without discarding the variants. Its EraseTx change explicitly removes all associated variants so none remain dangling.
The old migration used EraseTx indirectly when removing transactions transferred exclusively to the watch-only wallet. That operation removed both the canonical transaction record and its associated witness records. The historical persistence contract therefore concerns the whole transaction record family: retained or transferred transactions preserve their variants, while deletion does not leave associated variant records behind. This is distinct from cleanup of the temporary wallet’s in-memory indexes.
Ralph's inferences
Copy-then-delete reflects a separation between storage-format conversion and semantic wallet conversion; the inspected evidence does not establish that copying unwanted records was itself a required safety property. At the base, MigrateToSQLite copies every serialized record without interpreting its ownership. ApplyMigrationData subsequently installs descriptors, deletes legacy records, and redistributes transactions using the new ownership model.
The enduring requirements concern the resulting wallet, not necessarily the intermediate copies: descriptor/key material must be persisted, the descriptor-wallet flag must match the stored representation, and legacy records must not remain in a wallet whose loader rejects them. At the head, selective copying changes the intermediate database contents while migration still uses the already-loaded legacy material to produce descriptors.
Transaction redistribution preserves ownership and dependency information rather than simply moving every transaction to exactly one wallet. Migration processes transactions in insertion order so spend lookups work; a transaction belonging to both wallets is retained in both, while a watch-only-only transaction is excluded from the spendable wallet. Retained transactions are rewritten specifically to persist changes made in memory.
Before the temporary objects disappear, each destination database needs the transactions and metadata appropriate to that wallet, including full witness data for transferred transactions. The base’s deferred removal also leaves the source transaction graph available throughout the ownership-classification loop. Those persistence and classification requirements remain relevant independently of whether unwanted canonical records were copied first.
The removed RemoveTxs implementation provided a live-wallet consistency guarantee in addition to database deletion. As shown in the supplied patch, it deferred memory changes until database commit, then removed ordering entries, input-spend links, derived outputs, and mapWallet entries, emitted deletion notifications, and invalidated cached calculations. Deferring those changes avoided making memory reflect a deletion that had not committed.
The base migration invoked RemoveTxs inside its migration database transaction. These responsibilities explain why it was more than an erase helper. They matter while an object continues to be used: transaction pointers and indexes must remain suitable for subsequent operations, and commit callbacks must execute while their referenced wallet state still exists. They are not themselves a persistent representation that must survive destruction.
Destruction and reload form the decisive persistence boundary. The migration wallet is loaded in an isolated context without signals or normal external users. Migration applies its main changes within a database transaction, commits auxiliary-wallet batches, and then resets the temporary wallet objects before optionally loading fresh objects from their databases.
Before reset, successful migration must have committed the descriptor representation, correct transaction ownership and metadata, and associated witness data; failure must remain recoverable through the backup/cleanup path. Temporary indexes need not survive destruction or be made identical to the eventual reloaded indexes merely for their own sake. They must remain adequate for every operation performed before destruction. Reload cannot recover data that existed only in the temporary objects.