Skip to content

Commit 34f54a0

Browse files
committed
wallet: decouple outputs grouping process from each ChooseSelectionResult
Another step towards the single OutputGroups calculation goal
1 parent 461f082 commit 34f54a0

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/wallet/coinselection.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ struct OutputGroup
242242
CAmount GetSelectionAmount() const;
243243
};
244244

245+
struct Groups {
246+
// Stores 'OutputGroup' containing only positive UTXOs (value > 0).
247+
std::vector<OutputGroup> positive_group;
248+
// Stores 'OutputGroup' which may contain both positive and negative UTXOs.
249+
std::vector<OutputGroup> mixed_group;
250+
};
251+
245252
/** Compute the waste for this result given the cost of change
246253
* and the opportunity cost of spending these inputs now vs in the future.
247254
* If change exists, waste = change_cost + inputs * (effective_feerate - long_term_feerate)

src/wallet/spend.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,11 @@ util::Result<SelectionResult> AttemptSelection(const CWallet& wallet, const CAmo
499499
{
500500
// Run coin selection on each OutputType and compute the Waste Metric
501501
std::vector<SelectionResult> results;
502-
for (const auto& it : available_coins.coins) {
503-
auto result{ChooseSelectionResult(wallet, nTargetValue, eligibility_filter, it.second, coin_selection_params)};
502+
for (const auto& [type, coins] : available_coins.coins) {
503+
Groups groups;
504+
groups.positive_group = GroupOutputs(wallet, coins, coin_selection_params, eligibility_filter, true /* positive_only */);
505+
groups.mixed_group = GroupOutputs(wallet, coins, coin_selection_params, eligibility_filter, false /* positive_only */);
506+
auto result{ChooseSelectionResult(nTargetValue, groups, coin_selection_params)};
504507
// If any specific error message appears here, then something particularly wrong happened.
505508
if (HasErrorMsg(result)) return result; // So let's return the specific error.
506509
// Append the favorable result.
@@ -514,31 +517,33 @@ util::Result<SelectionResult> AttemptSelection(const CWallet& wallet, const CAmo
514517
// over all available coins, which would allow mixing.
515518
// If TypesCount() <= 1, there is nothing to mix.
516519
if (allow_mixed_output_types && available_coins.TypesCount() > 1) {
517-
return ChooseSelectionResult(wallet, nTargetValue, eligibility_filter, available_coins.All(), coin_selection_params);
520+
const auto& all = available_coins.All();
521+
Groups groups;
522+
groups.positive_group = GroupOutputs(wallet, all, coin_selection_params, eligibility_filter, true /* positive_only */);
523+
groups.mixed_group = GroupOutputs(wallet, all, coin_selection_params, eligibility_filter, false /* positive_only */);
524+
return ChooseSelectionResult(nTargetValue, groups, coin_selection_params);
518525
}
519526
// Either mixing is not allowed and we couldn't find a solution from any single OutputType, or mixing was allowed and we still couldn't
520527
// find a solution using all available coins
521528
return util::Error();
522529
};
523530

524-
util::Result<SelectionResult> ChooseSelectionResult(const CWallet& wallet, const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, const std::vector<COutput>& available_coins, const CoinSelectionParams& coin_selection_params)
531+
util::Result<SelectionResult> ChooseSelectionResult(const CAmount& nTargetValue, Groups& groups, const CoinSelectionParams& coin_selection_params)
525532
{
526533
// Vector of results. We will choose the best one based on waste.
527534
std::vector<SelectionResult> results;
528535

529-
std::vector<OutputGroup> positive_groups = GroupOutputs(wallet, available_coins, coin_selection_params, eligibility_filter, /*positive_only=*/true);
530-
if (auto bnb_result{SelectCoinsBnB(positive_groups, nTargetValue, coin_selection_params.m_cost_of_change)}) {
536+
if (auto bnb_result{SelectCoinsBnB(groups.positive_group, nTargetValue, coin_selection_params.m_cost_of_change)}) {
531537
results.push_back(*bnb_result);
532538
}
533539

534540
// The knapsack solver has some legacy behavior where it will spend dust outputs. We retain this behavior, so don't filter for positive only here.
535-
std::vector<OutputGroup> all_groups = GroupOutputs(wallet, available_coins, coin_selection_params, eligibility_filter, /*positive_only=*/false);
536-
if (auto knapsack_result{KnapsackSolver(all_groups, nTargetValue, coin_selection_params.m_min_change_target, coin_selection_params.rng_fast)}) {
541+
if (auto knapsack_result{KnapsackSolver(groups.mixed_group, nTargetValue, coin_selection_params.m_min_change_target, coin_selection_params.rng_fast)}) {
537542
knapsack_result->ComputeAndSetWaste(coin_selection_params.min_viable_change, coin_selection_params.m_cost_of_change, coin_selection_params.m_change_fee);
538543
results.push_back(*knapsack_result);
539544
}
540545

541-
if (auto srd_result{SelectCoinsSRD(positive_groups, nTargetValue, coin_selection_params.rng_fast)}) {
546+
if (auto srd_result{SelectCoinsSRD(groups.positive_group, nTargetValue, coin_selection_params.rng_fast)}) {
542547
srd_result->ComputeAndSetWaste(coin_selection_params.min_viable_change, coin_selection_params.m_cost_of_change, coin_selection_params.m_change_fee);
543548
results.push_back(*srd_result);
544549
}

src/wallet/spend.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,15 @@ util::Result<SelectionResult> AttemptSelection(const CWallet& wallet, const CAmo
132132
* Multiple coin selection algorithms will be run and the input set that produces the least waste
133133
* (according to the waste metric) will be chosen.
134134
*
135-
* param@[in] wallet The wallet which provides solving data for the coins
136135
* param@[in] nTargetValue The target value
137-
* param@[in] eligilibity_filter A filter containing rules for which coins are allowed to be included in this selection
138-
* param@[in] available_coins The struct of coins, organized by OutputType, available for selection prior to filtering
136+
* param@[in] groups The struct containing the outputs grouped by script and divided by (1) positive only outputs and (2) all outputs (positive + negative).
139137
* param@[in] coin_selection_params Parameters for the coin selection
140138
* returns If successful, a SelectionResult containing the input set
141139
* If failed, returns (1) an empty error message if the target was not reached (general "Insufficient funds")
142140
* or (2) an specific error message if there was something particularly wrong (e.g. a selection
143141
* result that surpassed the tx max weight size).
144142
*/
145-
util::Result<SelectionResult> ChooseSelectionResult(const CWallet& wallet, const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, const std::vector<COutput>& available_coins,
146-
const CoinSelectionParams& coin_selection_params);
143+
util::Result<SelectionResult> ChooseSelectionResult(const CAmount& nTargetValue, Groups& groups, const CoinSelectionParams& coin_selection_params);
147144

148145
// User manually selected inputs that must be part of the transaction
149146
struct PreSelectedInputs

0 commit comments

Comments
 (0)