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 committed 2022-06-08 11:30:26 -03:00
1 parent 162d4ad10f
commit fd5c996d16
2 files changed
+12 -12

No files matched your search

+10 -12
View File
@@ -96,7 +96,6 @@ CoinsResult AvailableCoins(const CWallet& wallet,
AssertLockHeld(wallet.cs_wallet); AssertLockHeld(wallet.cs_wallet);
CoinsResult result; 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 // 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 // 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); 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)); 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.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. // Checks the sum amount of all UTXO's.
if (nMinimumSumAmount != MAX_MONEY) { if (nMinimumSumAmount != MAX_MONEY) {
nTotal += output.nValue; if (result.total_amount >= nMinimumSumAmount) {
if (nTotal >= nMinimumSumAmount) {
return result; return result;
} }
} }
@@ -234,14 +232,14 @@ CoinsResult AvailableCoinsListUnspent(const CWallet& wallet, const CCoinControl*
CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinControl) CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinControl)
{ {
LOCK(wallet.cs_wallet); LOCK(wallet.cs_wallet);
return AvailableCoins(wallet,
CAmount balance = 0; coinControl,
for (const COutput& out : AvailableCoinsListUnspent(wallet, coinControl).coins) { std::nullopt, /*feerate=*/
if (out.spendable) { 1, /*nMinimumAmount*/
balance += out.txout.nValue; MAX_MONEY, /*nMaximumAmount*/
} MAX_MONEY, /*nMinimumSumAmount*/
} 0 /*nMaximumCount*/
return balance; ).total_amount;
} }
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output) const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output)
+2
View File
@@ -36,6 +36,8 @@ TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* walle
struct CoinsResult { struct CoinsResult {
std::vector<COutput> coins; std::vector<COutput> coins;
// Sum of all the coins amounts
CAmount total_amount{0};
}; };
/** /**
* Return vector of available COutputs. * Return vector of available COutputs.