Repository navigation
Description
Several wallet paths inherited from Bitcoin Core update the wallet in memory before the matching database write, or spread one logical change over several writes with no transaction. When a write fails (for example a full disk) or the process stops between two writes, memory and disk disagree and nothing repairs it. A retry in the same session then sees the in-memory state, skips the write and reports success.
None of this is new, and none of it is reachable from the network. It was raised in review of #8068, whose unattended DashPay receiving-chain imports make these paths run more often than a manual importdescriptors does. #8068 documents them as limits instead of working around them, so they are collected here.
The paths
-
A descriptor key is kept in memory before it is written.
DescriptorScriptPubKeyMan::AddDescriptorKeyWithDBfillsm_map_keys/m_map_crypted_keys(and the mnemonic maps) and then writes. If the write fails, a retry in the same session hits the "already have this key" early return, never writes the key, and the import succeeds. After a reload the descriptor has no private key.- Fixed upstream by wallet: make encryption state updates atomic bitcoin/bitcoin#35752 (
wallet: publish descriptor keys after writes, merged). Dash needs that backport, with the Dash mnemonic maps handled the same way.
- Fixed upstream by wallet: make encryption state updates atomic bitcoin/bitcoin#35752 (
-
CWallet::AddWalletDescriptoris not atomic. It writes the private key, then the cache entries (TopUp), then theWALLETDESCRIPTORrecord, each in its own batch. A failure or a stop after the key write leaves key or cache records with no descriptor record.- On load, develop asserts on such records (
assert(spk_man)in the cache loop, an unchecked cast in the key loops). The loader side is covered by the wallet: Load database records in a particular order bitcoin/bitcoin#24914 backport, which only reads these records under a known descriptor (fix: reject mismatched mnemonic keys and unreadable transactions when loading wallets #7823 is the narrow version of the same fix). - Even with a tolerant loader the orphan key record stays in the file, and importing the same descriptor again fails on it, because the key is written without overwrite.
- Upstream has the same write order on master. A complete fix needs the import in one database transaction with the in-memory state rolled back on failure; upstream's
TopUpWithDB(wallet: batch all individual spkms setup db writes in a single db txn bitcoin/bitcoin#28894) is the prerequisite for the first half.
- On load, develop asserts on such records (
-
DescriptorScriptPubKeyMan::TopUpcan stop part way. It adds scripts tom_map_script_pub_keysindex by index and throws when a cache write fails, leaving part of the range populated in memory. It also ignores the result of the finalWriteDescriptor, so a grown range may never be saved. A rescan that then succeeds can miss payments to the unpopulated indexes, or find payments in a range that is not saved. -
CWallet::AddToWalletkeeps a transaction in memory before it is written. It inserts intomapWallet,wtxOrderedand the UTXO set, then callsWriteTx. If that fails,AddToWalletIfInvolvingMethrows ("DB error adding transaction to wallet, write failed") and nothing is rolled back. A rescan retried in the same session finds the transaction already present and unchanged, writes nothing and reports success; after a reload the transaction is missing.- Upstream has the same order on master. wallet: Handle or explicitly ignore
WalletBatchwrite failures bitcoin/bitcoin#35998 (open) handles or explicitly ignores moreWalletBatchwrite failures but does not change this order.
- Upstream has the same order on master. wallet: Handle or explicitly ignore
Effect
After a failed write the wallet has to be reloaded before the operation is retried, and a wallet restored from that file may need rescanblockchain to see a payment again. Funds are not at risk; the keys derive from the seed. Path 2 is the exception in severity: until the loader fix lands, the wallet file does not load at all.
Suggested order
- Backport wallet: Load database records in a particular order bitcoin/bitcoin#24914 (in progress) so a file with orphan descriptor records loads.
- Backport wallet: make encryption state updates atomic bitcoin/bitcoin#35752 for path 1.
- Paths 2 to 4 have no upstream fix to take. Prefer fixing them upstream and backporting; a Dash-only fix would have to change code that every wallet backport touches.
Activity