wallet: AvailableCoins, don't call 'wtx.tx->vout[i]' multiple times

This commit is contained in:
furszy 2022-04-27 10:34:19 -03:00
parent 4ce235ef8f
commit 9472ca0a65
No known key found for this signature in database
GPG key ID: 5DD23CCC686AA623

View file

@ -165,24 +165,27 @@ CoinsResult AvailableCoins(const CWallet& wallet,
bool tx_from_me = CachedTxIsFromMe(wallet, wtx, ISMINE_ALL);
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
const CTxOut& output = wtx.tx->vout[i];
const COutPoint outpoint(wtxid, i);
// Only consider selected coins if add_inputs is false
if (coinControl && !coinControl->m_add_inputs && !coinControl->IsSelected(COutPoint(entry.first, i))) {
if (coinControl && !coinControl->m_add_inputs && !coinControl->IsSelected(outpoint)) {
continue;
}
if (wtx.tx->vout[i].nValue < nMinimumAmount || wtx.tx->vout[i].nValue > nMaximumAmount)
if (output.nValue < nMinimumAmount || output.nValue > nMaximumAmount)
continue;
if (coinControl && coinControl->HasSelected() && !coinControl->fAllowOtherInputs && !coinControl->IsSelected(COutPoint(entry.first, i)))
if (coinControl && coinControl->HasSelected() && !coinControl->fAllowOtherInputs && !coinControl->IsSelected(outpoint))
continue;
if (wallet.IsLockedCoin(entry.first, i))
if (wallet.IsLockedCoin(wtxid, i))
continue;
if (wallet.IsSpent(wtxid, i))
continue;
isminetype mine = wallet.IsMine(wtx.tx->vout[i]);
isminetype mine = wallet.IsMine(output);
if (mine == ISMINE_NO) {
continue;
@ -192,16 +195,16 @@ CoinsResult AvailableCoins(const CWallet& wallet,
continue;
}
std::unique_ptr<SigningProvider> provider = wallet.GetSolvingProvider(wtx.tx->vout[i].scriptPubKey);
std::unique_ptr<SigningProvider> provider = wallet.GetSolvingProvider(output.scriptPubKey);
bool solvable = provider ? IsSolvable(*provider, wtx.tx->vout[i].scriptPubKey) : false;
bool solvable = provider ? IsSolvable(*provider, output.scriptPubKey) : false;
bool spendable = ((mine & ISMINE_SPENDABLE) != ISMINE_NO) || (((mine & ISMINE_WATCH_ONLY) != ISMINE_NO) && (coinControl && coinControl->fAllowWatchOnly && solvable));
int input_bytes = GetTxSpendSize(wallet, wtx, i, (coinControl && coinControl->fAllowWatchOnly));
result.coins.emplace_back(COutPoint(wtx.GetHash(), i), wtx.tx->vout.at(i), 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);
// Checks the sum amount of all UTXO's.
if (nMinimumSumAmount != MAX_MONEY) {
nTotal += wtx.tx->vout[i].nValue;
nTotal += output.nValue;
if (nTotal >= nMinimumSumAmount) {
return result;