wallet: add test-only helper to clear in-memory transaction state
What changed, and why it matters
This commit adds a private, test-only helper function to Bitcoin Core's wallet that clears in-memory transaction tracking data. It is explicitly labeled for testing/fuzzing use, requires an internal lock to be held, and does not touch the wallet database, keys, or flags. There is no indication it is used in normal wallet operation or exposed to users.
No security action required. Treat as normal code hygiene for fuzzing infrastructure. If reviewing further, verify that fuzz harnesses using this helper do not accidentally rely on stale database state after clearing in-memory caches.
Security signals we found
Test-only helper with explicit 'ForTest' naming and comment
Requires cs_wallet lock; does not bypass synchronization
Does not modify persistent wallet database or key material
No production callers or external interfaces introduced
No incident or vulnerability disclosure references supplied
Evidence from the diff
CWallet::ClearInMemoryTxStateForTest() is introduced as a guarded, EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) method that resets mapWallet, wtxOrdered, mapTxSpends, m_txos, m_locked_coins, and nOrderPosNext. The commit message and header comment state it exists solely so fuzz harnesses can reuse a single wallet instance across inputs. It deliberately leaves the database, wallet flags, and ScriptPubKeyMans intact. No production code paths call it, and no RPC, GUI, or command-line interface is modified.
Changed components
src/wallet/wallet.cppsrc/wallet/wallet.hInspect captured patch +19 / −0
### src/wallet/wallet.cpp
@@ -2297,6 +2297,17 @@ util::Result<void> CWallet::RemoveTxs(WalletBatch& batch, std::vector<Txid>& txs
return {};
}
+void CWallet::ClearInMemoryTxStateForTest()
+{
+ AssertLockHeld(cs_wallet);
+ mapWallet.clear();
+ wtxOrdered.clear();
+ mapTxSpends.clear();
+ m_txos.clear();
+ m_locked_coins.clear();
+ nOrderPosNext = 0;
+}
+
bool CWallet::SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& address, const std::string& strName, const std::optional<AddressPurpose>& new_purpose)
{
bool fUpdated = false;
### src/wallet/wallet.h
@@ -788,6 +788,14 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
util::Result<void> RemoveTxs(std::vector<Txid>& txs_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
util::Result<void> RemoveTxs(WalletBatch& batch, std::vector<Txid>& txs_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
+ /**
+ * Drop all in-memory transaction state: the transactions themselves, the
+ * outputs derived from them, the spends index and any coin locks. The
+ * database, the wallet flags and the ScriptPubKeyMans are left untouched.
+ * This is test-only function for harnesses that reuse a single wallet.
+ */
+ void ClearInMemoryTxStateForTest() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
+
bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::optional<AddressPurpose>& purpose);
bool DelAddressBook(const CTxDestination& address);Why this scored 15/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.