Skip to content

Commit 51a9c00

Browse files
committed
Return SelectionResult from SelectCoinsSRD
Changes SelectCoinsSRD to return a SelectionResult.
1 parent 0ef6184 commit 51a9c00

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

src/wallet/coinselection.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,9 @@ std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_poo
166166
return result;
167167
}
168168

169-
std::optional<std::pair<std::set<CInputCoin>, CAmount>> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value)
169+
std::optional<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value)
170170
{
171-
std::set<CInputCoin> out_set;
172-
CAmount value_ret = 0;
171+
SelectionResult result(target_value);
173172

174173
std::vector<size_t> indexes;
175174
indexes.resize(utxo_pool.size());
@@ -181,10 +180,9 @@ std::optional<std::pair<std::set<CInputCoin>, CAmount>> SelectCoinsSRD(const std
181180
const OutputGroup& group = utxo_pool.at(i);
182181
Assume(group.GetSelectionAmount() > 0);
183182
selected_eff_value += group.GetSelectionAmount();
184-
value_ret += group.m_value;
185-
util::insert(out_set, group.m_outputs);
183+
result.AddInput(group);
186184
if (selected_eff_value >= target_value) {
187-
return std::make_pair(out_set, value_ret);
185+
return result;
188186
}
189187
}
190188
return std::nullopt;

src/wallet/coinselection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_poo
241241
*
242242
* @param[in] utxo_pool The positive effective value OutputGroups eligible for selection
243243
* @param[in] target_value The target value to select for
244-
* @returns If successful, a pair of set of outputs and total selected value, otherwise, std::nullopt
244+
* @returns If successful, a SelectionResult, otherwise, std::nullopt
245245
*/
246-
std::optional<std::pair<std::set<CInputCoin>, CAmount>> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value);
246+
std::optional<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value);
247247

248248
// Original coin selection algorithm as a fallback
249249
std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmount& nTargetValue);

src/wallet/spend.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,9 @@ bool AttemptSelection(const CWallet& wallet, const CAmount& nTargetValue, const
402402
// We include the minimum final change for SRD as we do want to avoid making really small change.
403403
// KnapsackSolver does not need this because it includes MIN_CHANGE internally.
404404
const CAmount srd_target = nTargetValue + coin_selection_params.m_change_fee + MIN_FINAL_CHANGE;
405-
auto srd_result = SelectCoinsSRD(positive_groups, srd_target);
406-
if (srd_result != std::nullopt) {
407-
const auto waste = GetSelectionWaste(srd_result->first, coin_selection_params.m_cost_of_change, srd_target, !coin_selection_params.m_subtract_fee_outputs);
408-
results.emplace_back(std::make_tuple(waste, std::move(srd_result->first), srd_result->second));
405+
if (auto srd_result{SelectCoinsSRD(positive_groups, srd_target)}) {
406+
srd_result->ComputeAndSetWaste(coin_selection_params.m_cost_of_change);
407+
results.emplace_back(std::make_tuple(srd_result->GetWaste(), srd_result->GetInputSet(), srd_result->GetSelectedValue()));
409408
}
410409

411410
if (results.size() == 0) {

0 commit comments

Comments
 (0)