From e3210a722542a9cb5f7e4be72470dbe488c281fd Mon Sep 17 00:00:00 2001 From: S3RK <1466284+S3RK@users.noreply.github.com> Date: Wed, 6 Jul 2022 08:15:20 +0200 Subject: [PATCH] wallet: account for preselected inputs in target When we have preselected inputs the coin selection search target is reduced by the sum of (effective) values. This causes incorrect m_target value. Create separate instance of SelectionResult for all the preselected inputs and set the target equal to the sum of (effective) values. Target for preselected SelectionResult is equal to the delta for the search target. To get the final SelectionResult with accurate m_target we merge both SelectionResult instances. --- src/wallet/spend.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp index 223377cf0ec..cbb03dc1778 100644 --- a/src/wallet/spend.cpp +++ b/src/wallet/spend.cpp @@ -607,12 +607,15 @@ std::optional SelectCoins(const CWallet& wallet, CoinsResult& a Shuffle(available_coins.other.begin(), available_coins.other.end(), coin_selection_params.rng_fast); } + SelectionResult preselected(preset_inputs.GetSelectionAmount(), SelectionAlgorithm::MANUAL); + preselected.AddInput(preset_inputs); + // Coin Selection attempts to select inputs from a pool of eligible UTXOs to fund the // transaction at a target feerate. If an attempt fails, more attempts may be made using a more // permissive CoinEligibilityFilter. std::optional res = [&] { // Pre-selected inputs already cover the target amount. - if (value_to_select <= 0) return std::make_optional(SelectionResult(nTargetValue, SelectionAlgorithm::MANUAL)); + if (value_to_select <= 0) return std::make_optional(SelectionResult(value_to_select, SelectionAlgorithm::MANUAL)); // If possible, fund the transaction with confirmed UTXOs only. Prefer at least six // confirmations on outputs received from other wallets and only spend confirmed change. @@ -666,7 +669,7 @@ std::optional SelectCoins(const CWallet& wallet, CoinsResult& a if (!res) return std::nullopt; // Add preset inputs to result - res->AddInput(preset_inputs); + res->Merge(preselected); if (res->GetAlgo() == SelectionAlgorithm::MANUAL) { res->ComputeAndSetWaste(coin_selection_params.m_cost_of_change); }