mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-13 22:59:34 +02:00
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:
parent
162d4ad10f
commit
fd5c996d16
@ -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)
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user