aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfurszy <matiasfurszyfer@protonmail.com>2022-04-27 11:15:09 -0300
committerfurszy <matiasfurszyfer@protonmail.com>2022-06-08 11:30:26 -0300
commitfd5c996d1609e6f88769f6f3ef0c322e3435b3aa (patch)
tree251fead3ab31d300ed6231be0160e0537d2a5ef1
parent162d4ad10f28c5fa38551d69ce9b296ab3933c77 (diff)
wallet: GetAvailableBalance, remove double walk-through every available coin
Filtering `AvailableCoins` by spendable outputs only and using the retrieved total_amount.
-rw-r--r--src/wallet/spend.cpp22
-rw-r--r--src/wallet/spend.h2
2 files changed, 12 insertions, 12 deletions
diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp
index 63e04c7a9b..1428a8e217 100644
--- a/src/wallet/spend.cpp
+++ b/src/wallet/spend.cpp
@@ -96,7 +96,6 @@ CoinsResult AvailableCoins(const CWallet& wallet,
AssertLockHeld(wallet.cs_wallet);
CoinsResult result;
- CAmount nTotal = 0;
// Either the WALLET_FLAG_AVOID_REUSE flag is not set (in which case we always allow), or we default to avoiding, and only in the case where
// a coin control object is provided, and has the avoid address reuse flag set to false, do we allow already used addresses
bool allow_used_addresses = !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE) || (coinControl && !coinControl->m_avoid_address_reuse);
@@ -206,12 +205,11 @@ CoinsResult AvailableCoins(const CWallet& wallet,
int input_bytes = GetTxSpendSize(wallet, wtx, i, (coinControl && coinControl->fAllowWatchOnly));
result.coins.emplace_back(outpoint, output, nDepth, input_bytes, spendable, solvable, safeTx, wtx.GetTxTime(), tx_from_me, feerate);
+ result.total_amount += output.nValue;
// Checks the sum amount of all UTXO's.
if (nMinimumSumAmount != MAX_MONEY) {
- nTotal += output.nValue;
-
- if (nTotal >= nMinimumSumAmount) {
+ if (result.total_amount >= nMinimumSumAmount) {
return result;
}
}
@@ -234,14 +232,14 @@ CoinsResult AvailableCoinsListUnspent(const CWallet& wallet, const CCoinControl*
CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinControl)
{
LOCK(wallet.cs_wallet);
-
- CAmount balance = 0;
- for (const COutput& out : AvailableCoinsListUnspent(wallet, coinControl).coins) {
- if (out.spendable) {
- balance += out.txout.nValue;
- }
- }
- return balance;
+ return AvailableCoins(wallet,
+ coinControl,
+ std::nullopt, /*feerate=*/
+ 1, /*nMinimumAmount*/
+ MAX_MONEY, /*nMaximumAmount*/
+ MAX_MONEY, /*nMinimumSumAmount*/
+ 0 /*nMaximumCount*/
+ ).total_amount;
}
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output)
diff --git a/src/wallet/spend.h b/src/wallet/spend.h
index 84628517b0..cba42d6fae 100644
--- a/src/wallet/spend.h
+++ b/src/wallet/spend.h
@@ -36,6 +36,8 @@ TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* walle
struct CoinsResult {
std::vector<COutput> coins;
+ // Sum of all the coins amounts
+ CAmount total_amount{0};
};
/**
* Return vector of available COutputs.