wallet: Add m_cached_from_me to cache "from me" status
What changed, and why it matters
This commit adds a small in-memory cache to Bitcoin Core's wallet so it can remember whether a transaction spends coins that belong to the wallet, instead of recalculating that fact every time it is needed. It is a performance and code-clarity change, not a fix for a known security bug. There is no evidence in the commit or supplied references that this addresses an active vulnerability.
No immediate security action required. Review as normal code-quality/performance change. If auditing, verify that MarkDirty() correctly invalidates the cache in all relevant state transitions and that the mutable cache remains thread-safe for wallet access patterns.
Security signals we found
No security-relevant signals present in the diff or commit message
Change is a performance optimization / caching refactor
No bounds changes, input validation changes, or cryptographic changes
No mention of vulnerability, CVE, bug bounty, reporter, or security fix
Evidence from the diff
The change introduces a mutable std::optional
Changed components
src/wallet/receive.cppsrc/wallet/transaction.hCWalletTx cache managementCachedTxIsFromMe wallet helperInspect captured patch +7 / −1
diff --git a/src/wallet/receive.cpp b/src/wallet/receive.cpp
index df3fbc08..13a25b36 100644
--- a/src/wallet/receive.cpp
+++ b/src/wallet/receive.cpp
@@ -195,7 +195,10 @@ void CachedTxGetAmounts(const CWallet& wallet, const CWalletTx& wtx,
bool CachedTxIsFromMe(const CWallet& wallet, const CWalletTx& wtx)
{
- return (CachedTxGetDebit(wallet, wtx, /*avoid_reuse=*/false) > 0);
+ if (!wtx.m_cached_from_me.has_value()) {
+ wtx.m_cached_from_me = wallet.IsFromMe(*wtx.tx);
+ }
+ return wtx.m_cached_from_me.value();
}
// NOLINTNEXTLINE(misc-no-recursion)
diff --git a/src/wallet/transaction.h b/src/wallet/transaction.h
index bc6f0ef8..1dbcdd2d 100644
--- a/src/wallet/transaction.h
+++ b/src/wallet/transaction.h
@@ -232,6 +232,8 @@ public:
* CWallet::ComputeTimeSmart().
*/
unsigned int nTimeSmart;
+ // Cached value for whether the transaction spends any inputs known to the wallet
+ mutable std::optional<bool> m_cached_from_me{std::nullopt};
int64_t nOrderPos; //!< position in ordered transaction list
std::multimap<int64_t, CWalletTx*>::const_iterator m_it_wtxOrdered;
@@ -339,6 +341,7 @@ public:
m_amounts[CREDIT].Reset();
fChangeCached = false;
m_is_cache_empty = true;
+ m_cached_from_me = std::nullopt;
}
/** True if only scriptSigs are different */
Why this scored 19/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.