Skip to content

Commit b3f4e82

Browse files
committed
wallet: simplify ListCoins implementation
Can remove the locked coins lookup if we include them directly inside the AvailableCoins result
1 parent cb552c5 commit b3f4e82

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

src/wallet/spend.cpp

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -392,37 +392,19 @@ std::map<CTxDestination, std::vector<COutput>> ListCoins(const CWallet& wallet)
392392

393393
std::map<CTxDestination, std::vector<COutput>> result;
394394

395-
for (COutput& coin : AvailableCoinsListUnspent(wallet).All()) {
395+
CCoinControl coin_control;
396+
// Include watch-only for LegacyScriptPubKeyMan wallets without private keys
397+
coin_control.fAllowWatchOnly = wallet.GetLegacyScriptPubKeyMan() && wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
398+
CoinFilterParams coins_params;
399+
coins_params.only_spendable = false;
400+
coins_params.skip_locked = false;
401+
for (const COutput& coin : AvailableCoins(wallet, &coin_control, /*feerate=*/std::nullopt, coins_params).All()) {
396402
CTxDestination address;
397403
if ((coin.spendable || (wallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && coin.solvable)) &&
398404
ExtractDestination(FindNonChangeParentOutput(wallet, coin.outpoint).scriptPubKey, address)) {
399-
result[address].emplace_back(std::move(coin));
400-
}
401-
}
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-
}
405+
result[address].emplace_back(coin);
423406
}
424407
}
425-
426408
return result;
427409
}
428410

0 commit comments

Comments
 (0)