1 minor finding · 3 suggestions
Findings
[minor] · Unchecked retained-transaction writes can omit migrated history
Skipping DBKeys::TX makes an unchecked WriteTxMetadata failure capable of silently omitting a retained transaction if SQLite still commits; no such error sequence was observed.
Location: src/wallet/migration.cpp:579 (head).
Details and suggested fix
Each retained main-wallet transaction now depends entirely on local_wallet_batch.WriteTxMetadata(*wtx), whose result is ignored. Previously, the checked raw copy left the original transaction row in place if this rewrite failed. A bind or statement failure returns false without recording a failure in the batch. If the transaction remains committable and subsequent migration steps succeed, RunWithinTxn commits and migration reports success. Reloading reads only existing transaction rows, so a missing row alone neither fails loading nor requests a rescan; the copied best-block locator can leave the missing history outside the startup scan. History and balances can consequently be incomplete. A rescan can recover confirmed transactions, but wallet-only metadata or unconfirmed transactions may require recovery from the .legacy.bak backup; keys remain intact. Check WriteTxMetadata and return a migration error containing the txid on failure so the existing abort-and-restore path runs.
Optional improvements
[suggestion] · had_legacy_records also counts skipped transaction records
Malformed TX-only legacy wallets, which normal Bitcoin Core operations do not produce, now select DoMigration without legacy data and reach Assume(legacy_spkm).
Location: src/wallet/migration.cpp:76 (head).
Details and suggested fix
The old HasLegacyRecords checked only DBKeys::LEGACY_TYPES, whereas this flag is also set for DBKeys::TX. A TX-only file can load without creating a LegacyDataSPKM. It now reaches GetDescriptorsForLegacy, which aborts in debug builds or returns an internal-bug error and triggers backup restoration in production builds. The old check selected plain conversion for this input. Rename the flag to describe skipped-record presence, such as skipped_records, and update the MigrateToSQLite documentation, which still promises to move all records, to state the caller's obligation to recreate skipped data. Explicitly reject TX-without-legacy-data input with a clear error before calling DoMigration, rather than relying on Assume. Do not merely exclude TX from the flag, since that would silently drop the skipped transactions.
[suggestion] · Malformed record keys escape migration error handling
A malformed key outside recognized type prefixes can survive normal loading but now make MigrateToSQLite throw; no real-world wallet with this trigger was found.
Location: src/wallet/migration.cpp:74 (head).
Details and suggested fix
The full-database cursor now passes every key through ss_key >> type without catching deserialization errors. A truncated string prefix or non-canonical CompactSize throws std::ios_base::failure, even when normal prefix-based loading never examines that record. Base migration copied these bytes opaquely. The exception occurs before the original BDB file is deleted, so it does not lose data. RPC exception handling reports an error, but the GUI's MigrateWalletActivity::do_migrate callback and migrateWallet interface have no catch, allowing the exception to escape the Qt callback instead of following its normal error-result path. Catch type-deserialization failures here and return a translated migration error before replacing the database.
[suggestion] · Skipped records still incur temporary key/value copies
MigrateToSQLite builds temporary SerializeData copies of skipped legacy and transaction records only to discard them immediately.
Location: src/wallet/migration.cpp:75 (head).
Details and suggested fix
The key and value buffers are constructed before the skip_types.contains(type) check, so skipped records still incur allocation and copying work proportional to their serialized size. This is unnecessary work during the one-time migration, particularly for large transaction values. Read the type through a SpanReader over ss_key, check whether to skip it, and construct the SerializeData buffers only for retained records. The separate reader leaves ss_key unconsumed, preserving the complete serialized key for copying.
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Ralph's inferences
Copy-then-delete reflects a separation between storage-format conversion and semantic wallet migration: first reproduce the database in SQLite, then replace legacy key/script records and redistribute transactions. The inspected evidence does not establish that the temporary copies were themselves a recovery guarantee.
At the comparison base, MigrateToSQLite copies every raw record and commits that copy. ApplyMigrationData subsequently removes legacy records, installs descriptor state, rewrites retained transactions, and removes transactions assigned exclusively to the watch-only wallet. Selective copying changes this intermediate database state; the historical necessity of that state has not been established.
The visible recovery provision is an independent pre-migration backup, rather than preservation of the original records inside SQLite. The old conversion is not an uninterrupted, atomic replacement of the original database.
The base conversion deletes the original database before creating SQLite, and its fatal-error comments explicitly rely on an existing backup. The pinned head still creates a backup before conversion. Thus, recovery reasoning should distinguish the backup from the temporary copied records; the inspected code does not justify treating copy-before-delete as crash-safe recovery by itself.
Migration uses already-loaded wallet state to perform conversion, but its durable result must also survive closing and reloading the resulting wallets. Transaction rewriting serves to persist the post-conversion in-memory representation, not merely duplicate unchanged bytes.
The wallet is loaded before database conversion, descriptor extraction operates on that wallet, and transaction redistribution traverses its in-memory insertion order so spend lookups work. The base explicitly rewrites transactions so in-memory changes persist. After successful migration, the pinned head destroys the temporary wallet objects and, when requested, reloads them; a reload failure is explicitly treated as a migration failure requiring rollback. These are distinct persistence requirements from retaining temporary legacy records.
Transaction records and auxiliary witness-variant records have separate persistence responsibilities. Rewriting a TX record does not, by itself, reproduce all witness variants.
WriteFullTx writes every witness variant, including the canonical one, and then writes the TX record; WriteTxMetadata writes only the TX record. In the base migration, transactions retained in the main wallet use WriteTxMetadata after the raw database copy, whereas the watch-only destination uses WriteFullTx. Consequently, the old copy supplied auxiliary records that the main-wallet rewrite did not regenerate. The supplied patch also shows that the removed EraseTx explicitly deleted associated witness variants to avoid dangling records, but the historical rationale and reload consequences of that cleanup could not be independently retrieved.