From ba24bf61cdeb26627bf2ed0bbd6c6e96bb5d67b5 Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Tue, 20 Feb 2024 11:54:37 -0500 Subject: [PATCH] wallet: Replace CachedTxIsFromMe with CheckIsFromMeMap Instead of looking at the cached amounts or searching every input of a transaction each time we want to determine whether it is "from me", use the m_from_me map which stores this value for us. --- src/wallet/receive.cpp | 13 +++++++++---- src/wallet/receive.h | 3 ++- src/wallet/rpc/transactions.cpp | 7 ++++--- src/wallet/spend.cpp | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/wallet/receive.cpp b/src/wallet/receive.cpp index a76b7156a33..c48f7f63997 100644 --- a/src/wallet/receive.cpp +++ b/src/wallet/receive.cpp @@ -203,9 +203,14 @@ void CachedTxGetAmounts(const CWallet& wallet, const CWalletTx& wtx, } -bool CachedTxIsFromMe(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter) +bool CheckIsFromMeMap(const std::map& from_me_map, const isminefilter& filter) { - return (CachedTxGetDebit(wallet, wtx, filter) > 0); + for (const auto& [from_me_filter, from_me] : from_me_map) { + if ((filter & from_me_filter) && from_me) { + return true; + } + } + return false; } // NOLINTNEXTLINE(misc-no-recursion) @@ -226,7 +231,7 @@ bool CachedTxIsTrusted(const CWallet& wallet, const TxState& state, const uint25 assert(wtx); // using wtx's cached debit - if (!wallet.m_spend_zero_conf_change || !CachedTxIsFromMe(wallet, *wtx, ISMINE_ALL)) return false; + if (!wallet.m_spend_zero_conf_change || !CheckIsFromMeMap(wtx->m_from_me, ISMINE_ALL)) return false; // Trusted if all inputs are from us and are in the mempool: for (const CTxIn& txin : wtx->tx->vin) @@ -317,7 +322,7 @@ std::map GetAddressBalances(const CWallet& wallet) if (wallet.IsTXOInImmatureCoinBase(txo)) continue; int nDepth = wallet.GetTxStateDepthInMainChain(txo.GetState()); - if (nDepth < (CachedTxIsFromMe(wallet, txo.GetWalletTx(), ISMINE_ALL) ? 0 : 1)) continue; + if (nDepth < (CheckIsFromMeMap(txo.GetWalletTx().m_from_me, ISMINE_ALL) ? 0 : 1)) continue; CTxDestination addr; Assume(wallet.IsMine(txo.GetTxOut())); diff --git a/src/wallet/receive.h b/src/wallet/receive.h index fd7cafc0776..0871fa2df0e 100644 --- a/src/wallet/receive.h +++ b/src/wallet/receive.h @@ -40,12 +40,13 @@ void CachedTxGetAmounts(const CWallet& wallet, const CWalletTx& wtx, std::list& listSent, CAmount& nFee, const isminefilter& filter, bool include_change); -bool CachedTxIsFromMe(const CWallet& wallet, const CWalletTx& wtx, const isminefilter& filter); bool CachedTxIsTrusted(const CWallet& wallet, const TxState& state, const uint256& txid, std::set& trusted_parents) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet); bool CachedTxIsTrusted(const CWallet& wallet, const TxState& state, const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet); bool CachedTxIsTrusted(const CWallet& wallet, const CWalletTx& wtx, std::set& trusted_parents) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet); bool CachedTxIsTrusted(const CWallet& wallet, const CWalletTx& wtx); +bool CheckIsFromMeMap(const std::map& from_me_map, const isminefilter& filter); + struct Balance { CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending) diff --git a/src/wallet/rpc/transactions.cpp b/src/wallet/rpc/transactions.cpp index 61cf36a6c10..dd647549918 100644 --- a/src/wallet/rpc/transactions.cpp +++ b/src/wallet/rpc/transactions.cpp @@ -330,7 +330,7 @@ static void ListTransactions(const CWallet& wallet, const CWalletTx& wtx, int nM CachedTxGetAmounts(wallet, wtx, listReceived, listSent, nFee, filter_ismine, include_change); - bool involvesWatchonly = CachedTxIsFromMe(wallet, wtx, ISMINE_WATCH_ONLY); + bool involvesWatchonly = CheckIsFromMeMap(wtx.m_from_me, ISMINE_WATCH_ONLY); // Sent if (!filter_label.has_value()) @@ -779,10 +779,11 @@ RPCHelpMan gettransaction() CAmount nCredit = CachedTxGetCredit(*pwallet, wtx, filter); CAmount nDebit = CachedTxGetDebit(*pwallet, wtx, filter); CAmount nNet = nCredit - nDebit; - CAmount nFee = (CachedTxIsFromMe(*pwallet, wtx, filter) ? wtx.tx->GetValueOut() - nDebit : 0); + bool from_me = CheckIsFromMeMap(wtx.m_from_me, filter); + CAmount nFee = (from_me ? wtx.tx->GetValueOut() - nDebit : 0); entry.pushKV("amount", ValueFromAmount(nNet - nFee)); - if (CachedTxIsFromMe(*pwallet, wtx, filter)) + if (from_me) entry.pushKV("fee", ValueFromAmount(nFee)); WalletTxToJSON(*pwallet, wtx, entry); diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 3b99678b468..33746b3c21c 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -415,7 +415,7 @@ CoinsResult AvailableCoins(const CWallet& wallet, continue; } - bool tx_from_me = CachedTxIsFromMe(wallet, wtx, ISMINE_ALL); + bool tx_from_me = CheckIsFromMeMap(wtx.m_from_me, ISMINE_ALL); std::unique_ptr provider = wallet.GetSolvingProvider(output.scriptPubKey);