Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#25544: wallet: don't iter twice when getting th…
Browse files Browse the repository at this point in the history
…e cached debit/credit amount

757216e wallet: don't iter twice when getting the cached debit/credit amount (Antoine Poinsot)

Pull request description:

  A small optimization i stumbled upon while looking at something else. Figured it could be worth a PR.

  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.

ACKs for top commit:
  achow101:
    ACK 757216e
  aureleoules:
    ACK 757216e.

Tree-SHA512: 0dbbdd24231380196e929dce572752e6be1d69457252a7215e279e71d6199483b516f64019ae999a91dbce7fdd86f8bf0336b6e151cca93cbcf51bc854e838a2
  • Loading branch information
fanquake committed Jul 18, 2022
2 parents 4e2929e + 757216e commit c5fa7ed
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/wallet/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,10 @@ CAmount CachedTxGetCredit(const CWallet& wallet, const CWalletTx& wtx, const ism
return 0;

CAmount credit = 0;
if (filter & ISMINE_SPENDABLE) {
const isminefilter get_amount_filter{filter & ISMINE_ALL};
if (get_amount_filter) {
// GetBalance can assume transactions in mapWallet won't change
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, ISMINE_SPENDABLE);
}
if (filter & ISMINE_WATCH_ONLY) {
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, ISMINE_WATCH_ONLY);
credit += GetCachableAmount(wallet, wtx, CWalletTx::CREDIT, get_amount_filter);
}
return credit;
}
Expand All @@ -146,11 +144,9 @@ CAmount CachedTxGetDebit(const CWallet& wallet, const CWalletTx& wtx, const ismi
return 0;

CAmount debit = 0;
if (filter & ISMINE_SPENDABLE) {
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, ISMINE_SPENDABLE);
}
if (filter & ISMINE_WATCH_ONLY) {
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, ISMINE_WATCH_ONLY);
const isminefilter get_amount_filter{filter & ISMINE_ALL};
if (get_amount_filter) {
debit += GetCachableAmount(wallet, wtx, CWalletTx::DEBIT, get_amount_filter);
}
return debit;
}
Expand Down

0 comments on commit c5fa7ed

Please sign in to comment.