wallet: Require the descriptors flag when the FLAGS record is missing

bitcoin/bitcoin: PR #36887

1 suggestion

Optional improvements

Link

[suggestion] · Missing FLAGS produces migration advice that cannot work

For a SQLite wallet missing FLAGS, the new rejection recommends migratewallet, which instead says it is already a descriptor wallet; occurrence outside deliberately edited test files is unconfirmed.

Location: src/wallet/walletdb.cpp:470 (head).

Details and suggested fix

When FLAGS is missing or cannot be deserialized, this check now returns DBErrors::LEGACY_WALLET for SQLite. PopulateWalletFromDB turns that into advice to run migratewallet, but that RPC rejects the same file because it is not BDB, returning “This wallet is already a descriptor wallet”. The user receives contradictory diagnoses without learning that the flags metadata could not be read. Readable flags lacking DESCRIPTORS already received this advice before the PR; the change newly applies it to failed flags reads, including the files constructed by the new test.

Report a failed FLAGS read separately, for example with a dedicated loading error identifying missing or unreadable wallet flags, and update the functional assertion accordingly. Keep the early rejection and the bdb_ro exemption so the crash prevention and flagless BDB migration inputs remain supported. This should diagnose missing semantic metadata, not infer descriptor semantics from SQLite storage.


Coverage details

Background

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

Documented decisions

Wallet semantics and database storage were deliberately separate concepts; SQLite storage alone did not establish that a wallet used descriptors.

The SQLite introduction explicitly allowed all combinations of legacy/descriptor semantics and BDB/SQLite storage. A later migration change explained that even a blank wallet moved to SQLite would still be treated as legacy unless its descriptors flag was set. This explains why the pinned loader uses WALLET_FLAG_DESCRIPTORS as semantic metadata rather than inferring wallet type solely from the database format.

Source 1 · Source 2

The transition to descriptor-only operation traded continued direct loading of old wallets for a migration path, allowing legacy wallet code and the BDB dependency to be retired.

The deprecation plan tied normally created legacy wallets to BDB and descriptor wallets to SQLite for simplicity, despite their conceptual independence. Historical objections emphasized the value of opening old wallet.dat files and the stress of losing that ability; the response proposed dependency-free migration rather than indefinite normal operation. PR #31250 subsequently disabled legacy creation and loading in preparation for code deletion. The pinned descriptors requirement and bdb_ro exception sit at that operational-versus-migration boundary.

Source 1 · Source 2 · Source 3 · Source 4

Unexpected legacy records in a descriptor wallet were intentionally made a loading error rather than silently ignored.

In #26462, the initial proposal to ignore legacy records was replaced with explicit rejection because mixing the two wallet models was unexpected. The author expressly considered malicious actors distributing crafted wallet files to trigger node crashes; another participant argued for clean failure to avoid possible effects on other node databases and indexes. The pinned LoadLegacyWalletRecords implements this distinction through UNEXPECTED_LEGACY_ENTRY, conditional on the descriptors flag.

Source 1 · Source 2 · Source 3 · Source 4 · Source 5

Last-opened version and client-feature records exist to make automatic wallet upgrades work across upgrade–downgrade–upgrade cycles.

PR #32895 explains that an older client can add material lacking a newer client's automatic upgrades, so recording the last client's capabilities permits those upgrades to run again. These client-feature records are explicitly distinct from wallet feature flags such as the descriptors flag. In the pinned base, successful loading writes this metadata; the current wallet-tool creation path writes it directly when bypassing loading. Its purpose is therefore upgrade bookkeeping, not merely incidental loader output.

Source 1

Ralph's inferences

The missing-FLAGS allowance is best understood as an accommodation for an unfinished creation lifecycle, not as a declaration that an existing SQLite wallet can safely omit its semantic identity.

At the pinned base, bitcoin-wallet create passes the empty database through PopulateWalletFromDB before initializing flags and descriptors, and the loader comments explicitly accommodate creation without FLAGS. At the head, creation initializes records without loading, while LoadWalletFlags applies the descriptor requirement even when FLAGS is absent, retaining the bdb_ro exception. Read together with the historical separation of storage and wallet semantics, this is a lifecycle distinction between initializing a new database and interpreting an existing one—not a Bitcoin consensus or peer-protocol rule. No inspected historical discussion establishes why the original tool chose the load-before-initialize sequence.

Source 1 · Source 2 · Source 3

Unresolved disagreements

The historical record resolved rejection versus silent ignoring for mixed records, but did not resolve whether individual loading checks adequately address wallet-file tampering more generally.

During #26462, furszy questioned a dedicated database error as the broader approach and raised integrity checking as a possible direction. The comment specifically identified modification or erasure of wallet flags, version fields, and script-manager types as other ways to disrupt loading. The inspected thread records adoption of explicit mixed-record rejection, but no resolution of that broader integrity question. This supplies historical threat-model context for flag-dependent loading without establishing a finding about the current PR.

Source 1 · Source 2