From 113a4228229baedda2a730e097f2d59ad58a4b0d Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Tue, 20 Feb 2024 11:54:35 -0500 Subject: [PATCH] wallet: Add m_cached_from_me to cache "from me" status m_cached_from_me is used to track whether a transaction is "from me", i.e. has any inputs which belong to the wallet. This is held in memory only in the same way that a transaction's balances are. --- src/wallet/receive.cpp | 5 ++++- src/wallet/transaction.h | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wallet/receive.cpp b/src/wallet/receive.cpp index df3fbc0877d..13a25b36326 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 bc6f0ef8450..1dbcdd2d921 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 m_cached_from_me{std::nullopt}; int64_t nOrderPos; //!< position in ordered transaction list std::multimap::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 */