wallet: GetAvailableBalance, remove double walk-through every available coin

Filtering `AvailableCoins` by spendable outputs only and using the retrieved total_amount.
This commit is contained in:
furszy 2022-04-27 11:15:09 -03:00
parent 162d4ad10f
commit fd5c996d16
No known key found for this signature in database
GPG Key ID: 5DD23CCC686AA623
2 changed files with 12 additions and 12 deletions

View File

@ -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)

View File

@ -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.