1 suggestion
Optional improvements
[suggestion] · Cursor replacement coverage is limited to BerkeleyRO
Both backends now replace reused output streams correctly, but the interface leaves this contract undocumented and the SQLite tests would not detect appending unread bytes to the next record.
Location: src/wallet/test/db_tests.cpp:63 (head).
Details and suggested fix
LoadRecords reuses its key and value streams across records, so replacement must work even when a loader leaves bytes unread. The new test exercises that condition only for BerkeleyRO. The shared db_cursor_prefix_range_test fully consumes both outputs, which makes DataStream::read clear them, while CheckPrefix creates fresh streams on every iteration. Extend the partly-read replacement check to the writable backends returned by TestDatabases, and document on DatabaseCursor::Next in db.h that returning MORE replaces both outputs. Keep the directly populated BerkeleyRO fixture because its read-only batch cannot store records through Write. The comment should not promise replacement on DONE or FAIL, which return before clearing. This makes the existing caller contract explicit and tests the same invariant across backends without changing runtime behavior.
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Documented decisions
The independent BDB reader was designed to preserve access to legacy wallets without retaining the full Berkeley DB dependency. Its author justified reimplementation by its narrow, read-only scope, the fixed file format, and the growing maintenance burden of compiling and patching otherwise largely unused BDB code.
The affected BerkeleyROCursor belongs to this deliberately limited compatibility backend, not a replacement general-purpose database engine. In the pinned code, migration explicitly requests BERKELEY_RO before loading the legacy wallet. Record delivery therefore supports the long-term migration path after ordinary legacy-wallet support has been removed.
Source 1 · Source 2 · Source 3
Legacy-wallet removal was intended to leave behind only the minimum data handling necessary for migration, rather than preserve the old wallet’s complete operational machinery.
The pinned loader places legacy keys, scripts, and metadata in LegacyDataSPKM. This explains why the migration backend still feeds existing wallet-record deserializers: historical access is retained through a restricted loading-and-conversion path, while normal legacy-wallet operations need not survive.
The database reader treats record payloads as arbitrary byte strings; interpreting those bytes is separate from interpreting the BDB container format.
This distinction is explicit in the historical response explaining why BDB endianness handling does not transform record data. In the pinned implementation, BerkeleyROCursor copies stored key/value bytes, while LoadRecords and CKeyMetadata interpret their structure. The affected boundary is consequently between record transport and wallet-schema parsing.
Read-only access and faithful recovery were distinct safety goals. The parser’s author favored avoiding BDB’s incidental writes during dumping, while an objection emphasized that an incorrect dump could itself endanger a user relying on it as a backup. The historical response initially made use of the new reader optional.
These arguments explain the conservatism surrounding a legacy recovery backend: not modifying the source is valuable, but does not by itself establish that records are delivered faithfully. The current change concerns delivery of independent records without altering metadata parsing or the broader migration procedure.
Source 1 · Source 2 · Source 3
Wallet files were treated as input requiring validation, rather than trusted internal state. Historical parser work explicitly challenged assertions on disk input and changed several assertions to exceptions after fuzzing.
This supplies the adversarial assumption for the affected loading path: legacy files may be malformed, and parsing must distinguish usable records from failures. It does not establish that every unknown suffix is acceptable, nor that every deserialization failure proves underlying file corruption.
Ralph's inferences
Record isolation lets a byte-oriented database backend remain independent of how much of a versioned record a particular wallet loader understands. Reading the known portion of one record need not make its remaining bytes input to another record.
This is an architectural inference, not an explicitly documented historical suffix policy. At both pinned revisions, LoadRecords reuses two streams and the metadata loader reads CKeyMetadata without requiring stream exhaustion; CKeyMetadata conditionally reads fields according to its version. The base BDB cursor writes into those streams, while the head clears them before copying each available record, matching the inspected SQLite cursor. The rationale concerns local wallet-storage compatibility, not a Bitcoin consensus rule.