Skip to content

Commit d9ae6ec

Browse files
committed
Merge bitcoin/bitcoin#21759: wallet: document coin selection code
6ba8921 refactor + document coin selection strategy (glozow) 58ea324 [docs] add doxygen comments to wallet code (glozow) 0c74716 [docs] format existing comments as doxygen (glozow) Pull request description: I think it would help code review to have more documentation + doxygen comments ACKs for top commit: Xekyo: ReACK bitcoin/bitcoin@6ba8921 achow101: ACK 6ba8921 Tree-SHA512: 74a78d9b0e0c1d5659bed566432a5b3511511d8b2432f440565f443da7b8257a1b90e70aa7505a7f8abf618748eeb43d166e84f278bdee3d34ce5d5c37dc573a
2 parents 16d0051 + 6ba8921 commit d9ae6ec

File tree

3 files changed

+143
-32
lines changed

3 files changed

+143
-32
lines changed

src/wallet/coinselection.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static constexpr CAmount MIN_CHANGE{COIN / 100};
1515
//! final minimum change amount after paying for fees
1616
static const CAmount MIN_FINAL_CHANGE = MIN_CHANGE/2;
1717

18+
/** A UTXO under consideration for use in funding a new transaction. */
1819
class CInputCoin {
1920
public:
2021
CInputCoin(const CTransactionRef& tx, unsigned int i)
@@ -56,31 +57,58 @@ class CInputCoin {
5657
}
5758
};
5859

60+
/** Parameters for filtering which OutputGroups we may use in coin selection.
61+
* We start by being very selective and requiring multiple confirmations and
62+
* then get more permissive if we cannot fund the transaction. */
5963
struct CoinEligibilityFilter
6064
{
65+
/** Minimum number of confirmations for outputs that we sent to ourselves.
66+
* We may use unconfirmed UTXOs sent from ourselves, e.g. change outputs. */
6167
const int conf_mine;
68+
/** Minimum number of confirmations for outputs received from a different
69+
* wallet. We never spend unconfirmed foreign outputs as we cannot rely on these funds yet. */
6270
const int conf_theirs;
71+
/** Maximum number of unconfirmed ancestors aggregated across all UTXOs in an OutputGroup. */
6372
const uint64_t max_ancestors;
73+
/** Maximum number of descendants that a single UTXO in the OutputGroup may have. */
6474
const uint64_t max_descendants;
65-
const bool m_include_partial_groups{false}; //! Include partial destination groups when avoid_reuse and there are full groups
75+
/** When avoid_reuse=true and there are full groups (OUTPUT_GROUP_MAX_ENTRIES), whether or not to use any partial groups.*/
76+
const bool m_include_partial_groups{false};
6677

6778
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors) : conf_mine(conf_mine), conf_theirs(conf_theirs), max_ancestors(max_ancestors), max_descendants(max_ancestors) {}
6879
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants) : conf_mine(conf_mine), conf_theirs(conf_theirs), max_ancestors(max_ancestors), max_descendants(max_descendants) {}
6980
CoinEligibilityFilter(int conf_mine, int conf_theirs, uint64_t max_ancestors, uint64_t max_descendants, bool include_partial) : conf_mine(conf_mine), conf_theirs(conf_theirs), max_ancestors(max_ancestors), max_descendants(max_descendants), m_include_partial_groups(include_partial) {}
7081
};
7182

83+
/** A group of UTXOs paid to the same output script. */
7284
struct OutputGroup
7385
{
86+
/** The list of UTXOs contained in this output group. */
7487
std::vector<CInputCoin> m_outputs;
88+
/** Whether the UTXOs were sent by the wallet to itself. This is relevant because we may want at
89+
* least a certain number of confirmations on UTXOs received from outside wallets while trusting
90+
* our own UTXOs more. */
7591
bool m_from_me{true};
92+
/** The total value of the UTXOs in sum. */
7693
CAmount m_value{0};
94+
/** The minimum number of confirmations the UTXOs in the group have. Unconfirmed is 0. */
7795
int m_depth{999};
96+
/** The aggregated count of unconfirmed ancestors of all UTXOs in this
97+
* group. Not deduplicated and may overestimate when ancestors are shared. */
7898
size_t m_ancestors{0};
99+
/** The maximum count of descendants of a single UTXO in this output group. */
79100
size_t m_descendants{0};
101+
/** The value of the UTXOs after deducting the cost of spending them at the effective feerate. */
80102
CAmount effective_value{0};
103+
/** The fee to spend these UTXOs at the effective feerate. */
81104
CAmount fee{0};
105+
/** The target feerate of the transaction we're trying to build. */
82106
CFeeRate m_effective_feerate{0};
107+
/** The fee to spend these UTXOs at the long term feerate. */
83108
CAmount long_term_fee{0};
109+
/** The feerate for spending a created change output eventually (i.e. not urgently, and thus at
110+
* a lower feerate). Calculated using long term fee estimate. This is used to decide whether
111+
* it could be economical to create a change output. */
84112
CFeeRate m_long_term_feerate{0};
85113

86114
OutputGroup() {}

src/wallet/wallet.cpp

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,7 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
24782478
}
24792479
}
24802480

2481-
// remove preset inputs from vCoins
2481+
// remove preset inputs from vCoins so that Coin Selection doesn't pick them.
24822482
for (std::vector<COutput>::iterator it = vCoins.begin(); it != vCoins.end() && coin_control.HasSelected();)
24832483
{
24842484
if (setPresetCoins.count(it->GetInputCoin()))
@@ -2490,9 +2490,9 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
24902490
unsigned int limit_ancestor_count = 0;
24912491
unsigned int limit_descendant_count = 0;
24922492
chain().getPackageLimits(limit_ancestor_count, limit_descendant_count);
2493-
size_t max_ancestors = (size_t)std::max<int64_t>(1, limit_ancestor_count);
2494-
size_t max_descendants = (size_t)std::max<int64_t>(1, limit_descendant_count);
2495-
bool fRejectLongChains = gArgs.GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS);
2493+
const size_t max_ancestors = (size_t)std::max<int64_t>(1, limit_ancestor_count);
2494+
const size_t max_descendants = (size_t)std::max<int64_t>(1, limit_descendant_count);
2495+
const bool fRejectLongChains = gArgs.GetBoolArg("-walletrejectlongchains", DEFAULT_WALLET_REJECT_LONG_CHAINS);
24962496

24972497
// form groups from remaining coins; note that preset coins will not
24982498
// automatically have their associated (same address) coins included
@@ -2502,16 +2502,53 @@ bool CWallet::SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAm
25022502
// explicitly shuffling the outputs before processing
25032503
Shuffle(vCoins.begin(), vCoins.end(), FastRandomContext());
25042504
}
2505-
bool res = value_to_select <= 0 ||
2506-
SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(1, 6, 0), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used) ||
2507-
SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(1, 1, 0), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used) ||
2508-
(m_spend_zero_conf_change && SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, 2), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) ||
2509-
(m_spend_zero_conf_change && SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, std::min((size_t)4, max_ancestors/3), std::min((size_t)4, max_descendants/3)), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) ||
2510-
(m_spend_zero_conf_change && SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, max_ancestors/2, max_descendants/2), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) ||
2511-
(m_spend_zero_conf_change && SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, max_ancestors-1, max_descendants-1, true /* include_partial_groups */), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) ||
2512-
(m_spend_zero_conf_change && !fRejectLongChains && SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max(), true /* include_partial_groups */), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used));
25132505

2514-
// because SelectCoinsMinConf clears the setCoinsRet, we now add the possible inputs to the coinset
2506+
// Coin Selection attempts to select inputs from a pool of eligible UTXOs to fund the
2507+
// transaction at a target feerate. If an attempt fails, more attempts may be made using a more
2508+
// permissive CoinEligibilityFilter.
2509+
const bool res = [&] {
2510+
// Pre-selected inputs already cover the target amount.
2511+
if (value_to_select <= 0) return true;
2512+
2513+
// If possible, fund the transaction with confirmed UTXOs only. Prefer at least six
2514+
// confirmations on outputs received from other wallets and only spend confirmed change.
2515+
if (SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(1, 6, 0), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) return true;
2516+
if (SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(1, 1, 0), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) return true;
2517+
2518+
// Fall back to using zero confirmation change (but with as few ancestors in the mempool as
2519+
// possible) if we cannot fund the transaction otherwise. We never spend unconfirmed
2520+
// outputs received from other wallets.
2521+
if (m_spend_zero_conf_change) {
2522+
if (SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, 2), vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) return true;
2523+
if (SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, std::min((size_t)4, max_ancestors/3), std::min((size_t)4, max_descendants/3)),
2524+
vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) {
2525+
return true;
2526+
}
2527+
if (SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, max_ancestors/2, max_descendants/2),
2528+
vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) {
2529+
return true;
2530+
}
2531+
// If partial groups are allowed, relax the requirement of spending OutputGroups (groups
2532+
// of UTXOs sent to the same address, which are obviously controlled by a single wallet)
2533+
// in their entirety.
2534+
if (SelectCoinsMinConf(value_to_select, CoinEligibilityFilter(0, 1, max_ancestors-1, max_descendants-1, true /* include_partial_groups */),
2535+
vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) {
2536+
return true;
2537+
}
2538+
// Try with unlimited ancestors/descendants. The transaction will still need to meet
2539+
// mempool ancestor/descendant policy to be accepted to mempool and broadcasted, but
2540+
// OutputGroups use heuristics that may overestimate ancestor/descendant counts.
2541+
if (!fRejectLongChains && SelectCoinsMinConf(value_to_select,
2542+
CoinEligibilityFilter(0, 1, std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max(), true /* include_partial_groups */),
2543+
vCoins, setCoinsRet, nValueRet, coin_selection_params, bnb_used)) {
2544+
return true;
2545+
}
2546+
}
2547+
// Coin Selection failed.
2548+
return false;
2549+
}();
2550+
2551+
// SelectCoinsMinConf clears setCoinsRet, so add the preset inputs from coin_control to the coinset
25152552
util::insert(setCoinsRet, setPresetCoins);
25162553

25172554
// add preset inputs to the total value selected

0 commit comments

Comments
 (0)