wallet, migration: Skip legacy wallet records rather than erasing them, and drop unused record erase functions

bitcoin/bitcoin: PR #36823

Provider refusals

write_verifier: Provider declined the request because of a cybersecurity restriction. Experiments remain incomplete; static verification can continue.

1 major finding · 3 suggestions

Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive for their assessments.

Findings

Link

[major] · Migration rejects transactions belonging only to the new watch-only wallet

For a private-key-enabled legacy wallet with spendable material and watch-only-only transactions, migratewallet now rejects the transfer and restores the backup instead of completing migration.

Location: src/wallet/wallet.cpp:3983 (base).

Details and suggested fix

After replacing the legacy script manager, ApplyMigrationData computes is_mine from the main wallet's remaining descriptors. A transaction involving only imported watch-only scripts has is_mine == false, even after it is successfully copied into the separate watch-only wallet. Removing the continue therefore sends it into the following rejection, which reports that it cannot belong to any migrated wallet. The existing test_other_watchonly fixture constructs this ownership split and expects successful migration with those transactions only in imports0_watchonly. Restore if (!is_mine) continue; after the successful watch-only write. This avoids both the rejection and rewriting the transaction into the main wallet. No source-side database erasure is needed because MigrateToSQLite now skips DBKeys::TX records.


Optional improvements

Link

[suggestion] · The sole retained transaction write remains unchecked

Skipping DBKeys::TX makes unchecked WriteTxMetadata the sole persistence write for retained transactions; checking its result would harden migration against write failures not caught by commit, though no production instance was demonstrated.

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

Details and suggested fix

Base first copied each original transaction record with an assert-checked write. With that copy removed, a false return from local_wallet_batch.WriteTxMetadata(*wtx) leaves no replacement record, including non-rescannable metadata such as comments and receipt times. SQLiteBatch::WriteKey returns failure without recording a transaction-wide error, while RunWithinTxn proceeds to commit when its callback returns true. If the failed write leaves the transaction committable, migration could therefore report success without that record. Check this result, and the watch-only WriteFullTx result, and return util::Error on failure so the existing abort and backup-restoration path runs. FaultInjectingDatabase::FailNextWrite can cover the error-propagation contract without claiming to reproduce a production SQLite failure.


Link

[suggestion] · MigrateToSQLite still documents a complete record move

The declaration promises a complete move even though a successful call can leave the replacement database missing keys and transactions until reconstruction.

Location: src/wallet/wallet.h:1056 (head).

Details and suggested fix

The comment still says “Move all records,” but the production caller now excludes DBKeys::LEGACY_TYPES and DBKeys::TX. The helper also sets had_legacy_records for any excluded record, including transactions, rather than only legacy key or script records. Document skip_types, the output flag's actual meaning, and the requirement for callers to reconstruct skipped data in the appropriate resulting wallets. This makes clear that successful storage conversion alone does not preserve all wallet data.


Link

[suggestion] · Key-type parsing can throw on previously opaque records

For a crafted or corrupted wallet containing an undecodable record key, MigrateToSQLite now throws where base copied the record; no current legacy-wallet writer producing such keys was found.

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

Details and suggested fix

BerkeleyRODatabase retains key bytes without validating a serialized type string, and wallet loading visits known prefixes, so an undecodable unknown key can reach this full-database cursor. The new ss_key >> type throws std::ios_base::failure for an empty or truncated key instead of returning a migration error. The RPC layer converts the exception, but the GUI worker calling migrateWallet has no local catch, leaving its outcome dependent on global exception handling. The original BDB file remains intact at this point and a backup already exists. Catch the parse failure here and return a clear error through the existing result path.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Moving a transaction to the watch-only wallet must preserve all of its CWalletTx data, not merely recreate the underlying transaction and confirmation state.

Andrew Chow's historical commit explicitly says to make sure that “all of the CWalletTx data follows it.” It replaced AddToWallet with an explicit wallet-transaction copy, preserved the next transaction-order position, and wrote the destination record before source removal. The pinned migration still copies serialized metadata together with GetTxs() and writes the destination using WriteFullTx. This establishes a preservation requirement for reconstruction, rather than a requirement to retain the old erase APIs.

Source 1

Ralph's inferences

The preservation invariant is an ownership-aware replacement: retain the transaction's wallet data in every resulting wallet to which it belongs, and remove the source copy only when it no longer belongs there. Copy-then-erase is one mechanism for achieving that end state.

The pinned base explicitly keeps a transaction in both wallets when both recognize it, schedules source deletion only when the original wallet no longer recognizes it, and rejects transactions that cannot be assigned to either wallet. These checks distinguish preservation and correct partitioning from the physical sequence of copying database records. The inspected history does not make that physical sequence independently mandatory.

Source 1

Deferred in-memory cleanup protects agreement between committed database state and the live wallet; it is not, by itself, a requirement to use the removed callback mechanism in every migration strategy.

The removed implementation defers transaction-map, ordering, spend-index and output-index changes until database commit. Separately, the inspected migration loop processes transactions in insertion order because spend lookups depend on previously available transactions. Together these indicate why deletion must not prematurely destroy working state or leave memory reflecting an aborted database deletion. Reconstruction must respect those dependencies, but the available history does not establish that it must use deferred per-record cleanup.

Witness-variant handling is a record-consistency obligation conditional on transaction ownership, rather than an unconditional requirement to erase variants whenever a transaction is copied to watch-only.

The removed EraseTx implementation explicitly removed witness variants to avoid dangling records. Meanwhile, the pinned transfer copies GetTxs() and uses WriteFullTx for the destination, and the base retains transactions that also belong to the original wallet. The resulting inferred requirement is to preserve the transaction's variants with its destination representation and avoid orphaned source-side variants when the source transaction is removed. No inspected historical source establishes a stronger migration-specific rule.