Skip to content

Commit 8ae2808

Browse files
committed
Merge bitcoin/bitcoin#25659: wallet: simplify ListCoins implementation
a2ac6f9 wallet: unify FindNonChangeParentOutput functions (furszy) b3f4e82 wallet: simplify ListCoins implementation (furszy) Pull request description: Focused on the following changes: 1) Removed the entire locked coins lookup that was inside `ListCoins` by including them directly on the `AvailableCoins` result (where we were skipping them before). 2) Unified both `FindNonChangeParentOutput` functions (only called from `ListCoins`) ACKs for top commit: achow101: ACK a2ac6f9 aureleoules: ACK a2ac6f9, LGTM theStack: Code-review ACK a2ac6f9 Tree-SHA512: f72b21ee1600c5992559b5dcd8ff260527afac2ec696737f998343a0850b84d439e7f86ea52a14cc0cddabf132a30bf5b52fb34950578ac323083d4375b937f1
2 parents aef8b4f + a2ac6f9 commit 8ae2808

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

src/wallet/spend.cpp

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -362,67 +362,45 @@ CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinContr
362362
return AvailableCoins(wallet, coinControl).GetTotalAmount();
363363
}
364364

365-
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output)
365+
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint)
366366
{
367367
AssertLockHeld(wallet.cs_wallet);
368-
const CTransaction* ptx = &tx;
369-
int n = output;
368+
const CWalletTx* wtx{Assert(wallet.GetWalletTx(outpoint.hash))};
369+
370+
const CTransaction* ptx = wtx->tx.get();
371+
int n = outpoint.n;
370372
while (OutputIsChange(wallet, ptx->vout[n]) && ptx->vin.size() > 0) {
371373
const COutPoint& prevout = ptx->vin[0].prevout;
372-
auto it = wallet.mapWallet.find(prevout.hash);
373-
if (it == wallet.mapWallet.end() || it->second.tx->vout.size() <= prevout.n ||
374-
!wallet.IsMine(it->second.tx->vout[prevout.n])) {
374+
const CWalletTx* it = wallet.GetWalletTx(prevout.hash);
375+
if (!it || it->tx->vout.size() <= prevout.n ||
376+
!wallet.IsMine(it->tx->vout[prevout.n])) {
375377
break;
376378
}
377-
ptx = it->second.tx.get();
379+
ptx = it->tx.get();
378380
n = prevout.n;
379381
}
380382
return ptx->vout[n];
381383
}
382384

383-
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint)
384-
{
385-
AssertLockHeld(wallet.cs_wallet);
386-
return FindNonChangeParentOutput(wallet, *wallet.GetWalletTx(outpoint.hash)->tx, outpoint.n);
387-
}
388-
389385
std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet)
390386
{
391387
AssertLockHeld(wallet.cs_wallet);
392388

393389
std::map<CTxDestination, std::vector<COutput>> result;
394390

395-
for (COutput& coin : AvailableCoinsListUnspent(wallet).All()) {
391+
CCoinControl coin_control;
392+
// Include watch-only for LegacyScriptPubKeyMan wallets without private keys
393+
coin_control.fAllowWatchOnly = wallet.GetLegacyScriptPubKeyMan() && wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
394+
CoinFilterParams coins_params;
395+
coins_params.only_spendable = false;
396+
coins_params.skip_locked = false;
397+
for (const COutput& coin : AvailableCoins(wallet, &coin_control, /*feerate=*/std::nullopt, coins_params).All()) {
396398
CTxDestination address;
397399
if ((coin.spendable || (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && coin.solvable)) &&
398400
ExtractDestination(FindNonChangeParentOutput(wallet, coin.outpoint).scriptPubKey, address)) {
399-
result[address].emplace_back(std::move(coin));
401+
result[address].emplace_back(coin);
400402
}
401403
}
402-
403-
std::vector<COutPoint> lockedCoins;
404-
wallet.ListLockedCoins(lockedCoins);
405-
// Include watch-only for LegacyScriptPubKeyMan wallets without private keys
406-
const bool include_watch_only = wallet.GetLegacyScriptPubKeyMan() && wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
407-
const isminetype is_mine_filter = include_watch_only ? ISMINE_WATCH_ONLY : ISMINE_SPENDABLE;
408-
for (const COutPoint& output : lockedCoins) {
409-
auto it = wallet.mapWallet.find(output.hash);
410-
if (it != wallet.mapWallet.end()) {
411-
const auto& wtx = it->second;
412-
int depth = wallet.GetTxDepthInMainChain(wtx);
413-
if (depth >= 0 && output.n < wtx.tx->vout.size() &&
414-
wallet.IsMine(wtx.tx->vout[output.n]) == is_mine_filter
415-
) {
416-
CTxDestination address;
417-
if (ExtractDestination(FindNonChangeParentOutput(wallet, *wtx.tx, output.n).scriptPubKey, address)) {
418-
const auto out = wtx.tx->vout.at(output.n);
419-
result[address].emplace_back(
420-
COutPoint(wtx.GetHash(), output.n), out, depth, CalculateMaximumSignedInputSize(out, &wallet, /*coin_control=*/nullptr), /*spendable=*/ true, /*solvable=*/ true, /*safe=*/ false, wtx.GetTxTime(), CachedTxIsFromMe(wallet, wtx, ISMINE_ALL));
421-
}
422-
}
423-
}
424-
}
425-
426404
return result;
427405
}
428406

src/wallet/spend.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ CAmount GetAvailableBalance(const CWallet& wallet, const CCoinControl* coinContr
9999
/**
100100
* Find non-change parent output.
101101
*/
102-
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const CTransaction& tx, int output) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
103102
const CTxOut& FindNonChangeParentOutput(const CWallet& wallet, const COutPoint& outpoint) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet);
104103

105104
/**

0 commit comments

Comments
 (0)