Skip to content

Commit e3210a7

Browse files
committed
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.
1 parent f8e7963 commit e3210a7

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/wallet/spend.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,15 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& a
607607
Shuffle(available_coins.other.begin(), available_coins.other.end(), coin_selection_params.rng_fast);
608608
}
609609

610+
SelectionResult preselected(preset_inputs.GetSelectionAmount(), SelectionAlgorithm::MANUAL);
611+
preselected.AddInput(preset_inputs);
612+
610613
// Coin Selection attempts to select inputs from a pool of eligible UTXOs to fund the
611614
// transaction at a target feerate. If an attempt fails, more attempts may be made using a more
612615
// permissive CoinEligibilityFilter.
613616
std::optional<SelectionResult> res = [&] {
614617
// Pre-selected inputs already cover the target amount.
615-
if (value_to_select <= 0) return std::make_optional(SelectionResult(nTargetValue, SelectionAlgorithm::MANUAL));
618+
if (value_to_select <= 0) return std::make_optional(SelectionResult(value_to_select, SelectionAlgorithm::MANUAL));
616619

617620
// If possible, fund the transaction with confirmed UTXOs only. Prefer at least six
618621
// confirmations on outputs received from other wallets and only spend confirmed change.
@@ -666,7 +669,7 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& a
666669
if (!res) return std::nullopt;
667670

668671
// Add preset inputs to result
669-
res->AddInput(preset_inputs);
672+
res->Merge(preselected);
670673
if (res->GetAlgo() == SelectionAlgorithm::MANUAL) {
671674
res->ComputeAndSetWaste(coin_selection_params.m_cost_of_change);
672675
}

0 commit comments

Comments
 (0)