Return SelectionResult from SelectCoinsSRD

Changes SelectCoinsSRD to return a SelectionResult.
This commit is contained in:
Andrew Chow 2021-09-27 23:22:34 -04:00
parent 0ef6184575
commit 51a9c00b4d
3 changed files with 9 additions and 12 deletions

View File

@ -166,10 +166,9 @@ std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_poo
return result; return result;
} }
std::optional<std::pair<std::set<CInputCoin>, CAmount>> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value) std::optional<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value)
{ {
std::set<CInputCoin> out_set; SelectionResult result(target_value);
CAmount value_ret = 0;
std::vector<size_t> indexes; std::vector<size_t> indexes;
indexes.resize(utxo_pool.size()); indexes.resize(utxo_pool.size());
@ -181,10 +180,9 @@ std::optional<std::pair<std::set<CInputCoin>, CAmount>> SelectCoinsSRD(const std
const OutputGroup& group = utxo_pool.at(i); const OutputGroup& group = utxo_pool.at(i);
Assume(group.GetSelectionAmount() > 0); Assume(group.GetSelectionAmount() > 0);
selected_eff_value += group.GetSelectionAmount(); selected_eff_value += group.GetSelectionAmount();
value_ret += group.m_value; result.AddInput(group);
util::insert(out_set, group.m_outputs);
if (selected_eff_value >= target_value) { if (selected_eff_value >= target_value) {
return std::make_pair(out_set, value_ret); return result;
} }
} }
return std::nullopt; return std::nullopt;

View File

@ -241,9 +241,9 @@ std::optional<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_poo
* *
* @param[in] utxo_pool The positive effective value OutputGroups eligible for selection * @param[in] utxo_pool The positive effective value OutputGroups eligible for selection
* @param[in] target_value The target value to select for * @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<std::pair<std::set<CInputCoin>, CAmount>> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value); std::optional<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, CAmount target_value);
// Original coin selection algorithm as a fallback // Original coin selection algorithm as a fallback
std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmount& nTargetValue); std::optional<SelectionResult> KnapsackSolver(std::vector<OutputGroup>& groups, const CAmount& nTargetValue);

View File

@ -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. // 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. // 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; const CAmount srd_target = nTargetValue + coin_selection_params.m_change_fee + MIN_FINAL_CHANGE;
auto srd_result = SelectCoinsSRD(positive_groups, srd_target); if (auto srd_result{SelectCoinsSRD(positive_groups, srd_target)}) {
if (srd_result != std::nullopt) { srd_result->ComputeAndSetWaste(coin_selection_params.m_cost_of_change);
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(srd_result->GetWaste(), srd_result->GetInputSet(), srd_result->GetSelectedValue()));
results.emplace_back(std::make_tuple(waste, std::move(srd_result->first), srd_result->second));
} }
if (results.size() == 0) { if (results.size() == 0) {