Skip to content

wallet: Require the descriptors flag when the FLAGS record is missing - #36887

Open
kah-ja wants to merge 2 commits into
bitcoin:masterfrom
kah-ja:wallet-load-missing-flags
Open

kah-ja wants to merge 2 commits into
bitcoin:masterfrom
kah-ja:wallet-load-missing-flags

Conversation

@kah-ja

@kah-ja kah-ja commented Nov 16, 2026

Copy link
Copy Markdown

LoadWalletFlags checks for WALLET_FLAG_DESCRIPTORS only inside if (batch.Read(DBKeys::FLAGS, flags)). A SQLite wallet file without a FLAGS record is loaded with no flags set and then handled as a legacy wallet, which ends in an Assert:

  • with a legacy record in the file: SetupLegacyDataSPKM, Assert(m_database->Format() == "bdb_ro" || m_database->Format() == "sqlite-mock")
  • without one: LoadActiveScriptPubKeyMan, Assert(IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS))

loadwallet on such a file aborts the node. If the wallet is in the load on startup list, the node aborts on every start. The existing check for legacy records in a descriptor wallet (UNEXPECTED_LEGACY_ENTRY) does not catch this, because it also depends on the flag.

The second commit does the check whether or not the record exists, so the file is rejected with the legacy wallet error.

The first commit is needed for that: bitcoin-wallet create opened the new, empty database through PopulateWalletFromDB before it wrote the flags, which is the case the comment "The FLAGS key is absent during wallet creation" refers to. CWallet::CreateNew does not load anymore, and now the tool does not either. It writes the version records itself, as CreateNew does.

Behavior change: a wallet database without a FLAGS record no longer loads. bitcoin-wallet create writes the same set of records as before (compared by record type on a new wallet).

Measurement: regtest, RelWithDebInfo, base a527a00, descriptor wallet made with createwallet, then edited with sqlite3.

file master this PR
FLAGS record deleted node aborts, wallet.cpp:3577 error -4, node keeps running
FLAGS record deleted, cscript record added node aborts, wallet.cpp:3404 error -4, node keeps running
unchanged loads loads

Tests: new case in wallet_descriptor.py for both files; on master the node aborts in it. Three unit tests that load a hand written database now write the flags first. Passing locally: test_bitcoin and test_runner.py with the default list. Not run: fuzz targets, lint.

Contributed by turingpoint.

The create command opened the new, empty database through
PopulateWalletFromDB before it set the wallet flags and generated the
descriptors. There is nothing to load at that point, and it made the
loading code accept a database without a FLAGS record.

Skip the load when creating, and write the version records in
WalletCreate as CWallet::CreateNew does, since loading no longer
writes them.

Signed-off-by: Jan Kahmen <[email protected]>
LoadWalletFlags only checked for WALLET_FLAG_DESCRIPTORS when the
database had a FLAGS record. A SQLite wallet file without that record
was loaded with no flags set and then handled as a legacy wallet,
which ends in an Assert:

- with a legacy record, in SetupLegacyDataSPKM, because the database
  is not bdb_ro
- otherwise in LoadActiveScriptPubKeyMan, because the descriptors
  flag is not set

loadwallet aborts the node on such a file, and with the wallet in the
load on startup list the node no longer starts.

Do the check whether or not the record exists, so the file is
rejected as a legacy wallet. Nothing loads a database without the
record anymore now that the wallet tool does not load on create. The
unit tests that load a hand written database write the flags first.

Signed-off-by: Jan Kahmen <[email protected]>
@DrahtBot

DrahtBot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36897 (wallet, migration: Contain all legacy wallet and migration related things in its own subdirectory, namespace, and classes by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LLM Linter (✨ experimental)

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/wallet/test/walletload_tests.cpp: WalletDescriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0)
  • src/wallet/test/walletload_tests.cpp: WalletDescriptor(std::make_shared<DummyDescriptor>(desc), 0, 0, 0, 0)
  • src/wallet/test/walletload_tests.cpp: WalletDescriptor(descriptor, 0, 0, 0, 0)

2026-11-16 06:52:49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants