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 a527a0077a, 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.