From 51a9c00b4de707e0a6a1a68ca6f8e38d86c72d94 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Mon, 27 Sep 2021 23:22:34 -0400 Subject: [PATCH] Return SelectionResult from SelectCoinsSRD Changes SelectCoinsSRD to return a SelectionResult. --- src/wallet/coinselection.cpp | 10 ++++------ src/wallet/coinselection.h | 4 ++-- src/wallet/spend.cpp | 7 +++---- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index 97918429e0..a85547f5b2 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -166,10 +166,9 @@ std::optional SelectCoinsBnB(std::vector& utxo_poo return result; } -std::optional, CAmount>> SelectCoinsSRD(const std::vector& utxo_pool, CAmount target_value) +std::optional SelectCoinsSRD(const std::vector& utxo_pool, CAmount target_value) { - std::set out_set; - CAmount value_ret = 0; + SelectionResult result(target_value); std::vector indexes; indexes.resize(utxo_pool.size()); @@ -181,10 +180,9 @@ std::optional, CAmount>> SelectCoinsSRD(const std const OutputGroup& group = utxo_pool.at(i); Assume(group.GetSelectionAmount() > 0); selected_eff_value += group.GetSelectionAmount(); - value_ret += group.m_value; - util::insert(out_set, group.m_outputs); + result.AddInput(group); if (selected_eff_value >= target_value) { - return std::make_pair(out_set, value_ret); + return result; } } return std::nullopt; diff --git a/src/wallet/coinselection.h b/src/wallet/coinselection.h index 23bdff73c2..aa5ec822ec 100644 --- a/src/wallet/coinselection.h +++ b/src/wallet/coinselection.h @@ -241,9 +241,9 @@ std::optional SelectCoinsBnB(std::vector& utxo_poo * * @param[in] utxo_pool The positive effective value OutputGroups eligible for selection * @param[in] target_value The target value to select for - * @returns If successful, a pair of set of outputs and total selected value, otherwise, std::nullopt + * @returns If successful, a SelectionResult, otherwise, std::nullopt */ -std::optional, CAmount>> SelectCoinsSRD(const std::vector& utxo_pool, CAmount target_value); +std::optional SelectCoinsSRD(const std::vector& utxo_pool, CAmount target_value); // Original coin selection algorithm as a fallback std::optional KnapsackSolver(std::vector& groups, const CAmount& nTargetValue); diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index e953c83c10..ef8885e738 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -402,10 +402,9 @@ bool AttemptSelection(const CWallet& wallet, const CAmount& nTargetValue, const // We include the minimum final change for SRD as we do want to avoid making really small change. // KnapsackSolver does not need this because it includes MIN_CHANGE internally. const CAmount srd_target = nTargetValue + coin_selection_params.m_change_fee + MIN_FINAL_CHANGE; - auto srd_result = SelectCoinsSRD(positive_groups, srd_target); - if (srd_result != std::nullopt) { - const auto waste = GetSelectionWaste(srd_result->first, coin_selection_params.m_cost_of_change, srd_target, !coin_selection_params.m_subtract_fee_outputs); - results.emplace_back(std::make_tuple(waste, std::move(srd_result->first), srd_result->second)); + if (auto srd_result{SelectCoinsSRD(positive_groups, srd_target)}) { + srd_result->ComputeAndSetWaste(coin_selection_params.m_cost_of_change); + results.emplace_back(std::make_tuple(srd_result->GetWaste(), srd_result->GetInputSet(), srd_result->GetSelectedValue())); } if (results.size() == 0) {