3 suggestions
Earlier concerns neither republished nor resolved: 1 minor finding · 1 suggestion. See the Run archive for their assessments.
Optional improvements
[suggestion] · Reconstruction writes can fail without aborting migration
Main-wallet address-book entries and transactions now depend on unchecked writes, so a write failure that leaves the SQLite transaction usable can allow migration to commit with records missing.
Location: src/wallet/migration.cpp:363 (head).
Details and suggested fix
The base first persisted these records through the checked copy loop. After filtering them out, func_store_addr and WriteTxMetadata become their only persistence path, but their return values are ignored and written becomes true regardless. SQLiteBatch::WriteKey can return false after a bind or step failure without recording a transaction-wide failure, and RunWithinTxn otherwise proceeds to commit. Many SQLite I/O errors already abort the transaction, but migration should not rely on every write failure doing so. Return and check success from func_store_addr, check the transaction write result, and propagate errors from ApplyMigrationData so the existing rollback restores the backup. A write-failure injection test would cover this path.
[suggestion] · Interrupted migration can expose a flagless wallet as empty
For a source wallet without flags, interruption after the filtered database swap can leave an empty-looking wallet that loads normally; this crash scenario is unobserved, and recovery still requires .legacy.bak.
Location: src/wallet/migration.cpp:69 (head).
Details and suggested fix
MigrateToSQLite replaces the original database before descriptor derivation and the later ApplyMigrationData commit. An interruption immediately after replacement therefore leaves a database without legacy keys, scripts, transactions or address-book entries. If the source lacked FLAGS, LoadWalletFlags accepts its absence, and there are no remaining legacy records to trigger SetupLegacyDataSPKM's format assertion. Normal loading can succeed, while retrying migratewallet is refused because the file is SQLite. The base also required backup recovery, but its retained legacy records prevented this silent empty-wallet presentation; sources with a FLAGS record reject normal loading in both versions. Write a migration-in-progress marker in the initial SQLite transaction and reject normal loading until the final migration commit clears it.
[suggestion] · CopyMigrationRecords duplicates the entire serialized wallet
CopyMigrationRecords deep-copies every serialized record despite only reading them, unnecessarily increasing peak migration memory.
Location: src/wallet/migration.cpp:64 (head).
Details and suggested fix
MigrateToSQLite passes its local records lvalue to the helper's by-value parameter while retaining the original vector. This duplicates all serialized keys and values, including transactions and key material, on top of the BDB backend's existing in-memory record map. Take the vector by const reference instead. The callback only iterates through const references and runs synchronously, so filtering and write behavior remain unchanged.
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]: The
mapWalletemptiness check still rejects fully transferred history (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
Migration was organized as two distinct transformations: first move the existing database contents from BDB to SQLite, then create descriptors, remove obsolete legacy records, and redistribute wallet contents. The original migration proposal explicitly moved watch-only descriptors, transactions, and labels out of the spendable wallet rather than merely changing its database format.
This explains the historical setting for copying records that would later be deleted: MigrateToSQLite was a storage-conversion stage, while ApplyMigrationData performed semantic conversion and redistribution. The PR moves record-retention decisions into the earlier stage, so preservation must be understood across both stages rather than from the copy loop alone.
A wallet transaction is not fully represented by its canonical serialization. Different valid witnesses can share a txid but have different wtxids and fee rates. The final historical design stores variants separately, keeps the canonical transaction in the backward-compatible tx record, and merges variants back into CWalletTx on load.
Transaction reconstruction must preserve the canonical transaction, its wallet metadata, and the complete known witness set—not just its txid or serialized canonical transaction. In the base, copying every record retained existing variants while main-wallet transaction metadata was rewritten. Watch-only reconstruction explicitly passes GetTxs() and uses WriteFullTx. These are distinct preservation mechanisms affected by skipping TX and WTX_VARIANT records.
The historical address-book rule was to transfer a receive entry that no longer belonged to the main wallet to an owning auxiliary wallet before deleting it from the main wallet. Failure to identify such an owner aborted migration, except for explicitly recognized invalid or non-watched scripts that were not migratable. Other address-book entries were cloned.
Receive-address ownership and preservation of the entire entry were explicit migration invariants. The base retains main-owned receive entries and also copies them to auxiliary wallets; the head instead filters every receive entry by each destination wallet’s IsMine result. The entry being reconstructed includes purpose, label, receive requests, and previously-spent state, not merely an address-label pair.
Batching migration writes was introduced for both performance and consistency: the stated goal was success or failure, not a partially migrated wallet requiring manual restoration. The accompanying discussion also identified a separate consistency hazard if transaction removals changed memory before an enclosing database transaction subsequently aborted.
The base’s copy/delete operations were part of a broader transactional procedure, not independent destructive writes. Its RemoveTxs implementation deferred in-memory removal until commit. Removing those operations changes how state is reconstructed, but the historical guarantee concerns agreement between persisted data and the wallet state ultimately reloaded or discarded.
Failure hardening explicitly aimed to leave the original database untouched until SQLite construction succeeded, and to catch later migration failures so the original wallet could be restored automatically. Cleanup itself had to preserve unrelated user data: historical fixes restricted deletion to specific wallet database files and newly created directories rather than deleting a shared wallets directory.
The pinned replacement contract distinguishes failures before replacement from failures after the original may have been erased or replaced. Before that boundary, MigrateToSQLite may return false without restoration; afterward it throws into rollback. Rollback must retain access to the backup, close created wallets, remove only migration-owned files, and restore the original BDB wallet, including when reloading a migrated wallet fails.
Source 1 · Source 2 · Source 3
Ralph's inferences
Copying all records first provided a preservation baseline: records not explicitly transformed remained available on disk, and deletion could be limited to data successfully replaced or assigned elsewhere. The evidence does not establish copying obsolete records as a protocol requirement or an independently necessary safety mechanism.
The base relies on copied records for more than obsolete keys: it reads the best-block locator from the converted database, detects whether legacy records exist there, and preserves witness variants without rewriting them in the main-wallet metadata pass. The head’s selective-copy architecture shifts those responsibilities to explicit reconstruction or retained in-memory information, including had_legacy_records.
Transaction preservation includes history and classification dependencies, not just raw transactions. Processing in insertion order allows spend lookups to recognize outgoing transactions; transactions relevant to both the main and watch-only wallets can belong in both. Reconstruction also carries confirmation/conflict state, ordering, timestamps, comments, replacement links, messages, and payment requests.
Both pinned versions classify transactions using IsMine or IsFromMe and process wtxOrdered. The watch-only wallet receives the next order position and block locator; an empty locator deliberately permits a rescan. These details explain what must survive when copied transaction records are replaced by reconstructed records.
The migration’s all-or-fail objective is implemented through separate local transactions plus compensating recovery, not one atomic transaction spanning every wallet. An auxiliary wallet can commit before the main wallet’s enclosing transaction commits; a later failure therefore requires cleanup and restoration of the original rather than merely aborting the remaining transaction.
Both base and head commit auxiliary batches inside ApplyMigrationData, while the caller controls the main-wallet transaction. The backup is created before database conversion, and the outer failure path tracks created wallet files even across reload attempts. Thus copying before deletion alone was never the complete recovery guarantee: recoverability also depends on propagating failures to the outer rollback and preserving its backup and cleanup scope.