wallet: Write new descriptor's cache in AddWalletDescriptor
What changed, and why it matters
This change fixes a wallet bug where a newly added descriptor's pre-existing cache was kept in memory but never saved to disk. If the cache is lost, the wallet may need to rescan the blockchain to recover key derivation data, causing delays or apparent balance issues. It is a data-durability fix rather than a remote-exploitable vulnerability.
Review whether any existing wallet that imported descriptors before this fix may have an incomplete on-disk cache and advise users to reload/rescan if balance or descriptor derivation appears inconsistent. No immediate emergency patch action is indicated beyond normal backporting.
Security signals we found
Data-loss / durability bug: in-memory cache not persisted
Wallet descriptor cache omission could affect key/address derivation state
No input validation, cryptographic, or network attack surface visible in diff
Evidence from the diff
In CWallet::AddWalletDescriptor, after adding a new DescriptorScriptPubKeyMan to memory, the patch now writes desc.cache to the wallet database via WalletBatch::WriteDescriptorCacheItems. Previously the cache existed only in memory until some later event persisted it, risking loss on unclean shutdown or wallet reload. The change returns an error if the write fails.
Changed components
src/wallet/wallet.cppCWallet::AddWalletDescriptorWalletBatch::WriteDescriptorCacheItemsInspect captured patch +6 / −0
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 8d502049..3a3eb9e3 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -3801,6 +3801,12 @@ util::Result<std::reference_wrapper<DescriptorScriptPubKeyMan>> CWallet::AddWall
// Save the descriptor to memory
uint256 id = new_spk_man->GetID();
AddScriptPubKeyMan(id, std::move(new_spk_man));
+
+ // Write the existing cache to disk
+ WalletBatch batch(GetDatabase());
+ if (!batch.WriteDescriptorCacheItems(id, desc.cache)) {
+ return util::Error{_("Unable to write descriptor cache")};
+ }
}
// Apply the label if necessary
Why this scored 32/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.