Skip to content

Commit 757216e

Browse files
committed
wallet: don't iter twice when getting the cached debit/credit amount
Instead of calling GetCachableAmount twice, which will result in iterating through all the transaction txins/txouts and calling GetDebit/GetCredit (which lock cs_wallet), just merge the filters and do it once.
1 parent 9fb2a2b commit 757216e

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

src/wallet/receive.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,10 @@ CAmount CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const ism
130130
return 0;
131131

132132
CAmount credit = 0;
133-
if (filter & ISMINE_SPENDABLE) {
133+
const isminefilter get_amount_filter{filter & ISMINE_ALL};
134+
if (get_amount_filter) {
134135
// GetBalance can assume transactions in mapWallet won't change
135-
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, ISMINE_SPENDABLE);
136-
}
137-
if (filter & ISMINE_WATCH_ONLY) {
138-
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, ISMINE_WATCH_ONLY);
136+
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, get_amount_filter);
139137
}
140138
return credit;
141139
}
@@ -146,11 +144,9 @@ CAmount CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const ismi
146144
return 0;
147145

148146
CAmount debit = 0;
149-
if (filter & ISMINE_SPENDABLE) {
150-
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, ISMINE_SPENDABLE);
151-
}
152-
if (filter & ISMINE_WATCH_ONLY) {
153-
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, ISMINE_WATCH_ONLY);
147+
const isminefilter get_amount_filter{filter & ISMINE_ALL};
148+
if (get_amount_filter) {
149+
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, get_amount_filter);
154150
}
155151
return debit;
156152
}

0 commit comments

Comments
 (0)