From 3633b667ffca5a715d9fb27e977515c1e24f600a Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Thu, 20 May 2021 20:20:49 -0400 Subject: Use SelectCoinsSRD if it has less waste Try to find a solution with SelectCoinsSRD. If we do have one, add it to the list of solutions from which we choose the one with the least waste as the solution to use. --- src/wallet/spend.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/wallet/spend.cpp') diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 4a7a268982..1724375f4c 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -387,6 +387,15 @@ bool AttemptSelection(const CWallet& wallet, const CAmount& nTargetValue, const results.emplace_back(std::make_tuple(waste, std::move(knapsack_coins), knapsack_value)); } + // 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 (results.size() == 0) { // No solution found return false; -- cgit v1.2.3