Skip to content

Commit a8a7534

Browse files
committed
wallet: SelectCoins, return early if target is covered by preset-inputs
1 parent f41712a commit a8a7534

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/wallet/spend.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,11 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& a
575575
// Deduct preset inputs amount from the search target
576576
CAmount selection_target = nTargetValue - pre_set_inputs.total_amount;
577577

578-
// If automatic coin selection was disabled, we just want to return the preset inputs result
579-
if (!coin_control.m_allow_other_inputs) {
580-
// 'selection_target' is computed on `PreSelectedInputs::Insert` which decides whether to use the effective value
581-
// or the raw output value based on the 'subtract_fee_outputs' flag.
582-
if (selection_target > 0) return std::nullopt;
578+
// Return if automatic coin selection is disabled, and we don't cover the selection target
579+
if (!coin_control.m_allow_other_inputs && selection_target > 0) return std::nullopt;
580+
581+
// Return if we can cover the target only with the preset inputs
582+
if (selection_target <= 0) {
583583
SelectionResult result(nTargetValue, SelectionAlgorithm::MANUAL);
584584
result.AddInputs(pre_set_inputs.coins, coin_selection_params.m_subtract_fee_outputs);
585585
result.ComputeAndSetWaste(coin_selection_params.min_viable_change, coin_selection_params.m_cost_of_change, coin_selection_params.m_change_fee);
@@ -590,12 +590,14 @@ std::optional<SelectionResult> SelectCoins(const CWallet& wallet, CoinsResult& a
590590
auto op_selection_result = AutomaticCoinSelection(wallet, available_coins, selection_target, coin_control, coin_selection_params);
591591
if (!op_selection_result) return op_selection_result;
592592

593-
// Add preset inputs to the automatic coin selection result
594-
SelectionResult preselected(pre_set_inputs.total_amount, SelectionAlgorithm::MANUAL);
595-
preselected.AddInputs(pre_set_inputs.coins, coin_selection_params.m_subtract_fee_outputs);
596-
op_selection_result->Merge(preselected);
597-
if (op_selection_result->GetAlgo() == SelectionAlgorithm::MANUAL) {
598-
op_selection_result->ComputeAndSetWaste(coin_selection_params.min_viable_change, coin_selection_params.m_cost_of_change, coin_selection_params.m_change_fee);
593+
// If needed, add preset inputs to the automatic coin selection result
594+
if (!pre_set_inputs.coins.empty()) {
595+
SelectionResult preselected(pre_set_inputs.total_amount, SelectionAlgorithm::MANUAL);
596+
preselected.AddInputs(pre_set_inputs.coins, coin_selection_params.m_subtract_fee_outputs);
597+
op_selection_result->Merge(preselected);
598+
op_selection_result->ComputeAndSetWaste(coin_selection_params.min_viable_change,
599+
coin_selection_params.m_cost_of_change,
600+
coin_selection_params.m_change_fee);
599601
}
600602
return op_selection_result;
601603
}
@@ -622,9 +624,6 @@ std::optional<SelectionResult> AutomaticCoinSelection(const CWallet& wallet, Coi
622624
// transaction at a target feerate. If an attempt fails, more attempts may be made using a more
623625
// permissive CoinEligibilityFilter.
624626
std::optional<SelectionResult> res = [&] {
625-
// Pre-selected inputs already cover the target amount.
626-
if (value_to_select <= 0) return std::make_optional(SelectionResult(value_to_select, SelectionAlgorithm::MANUAL));
627-
628627
// If possible, fund the transaction with confirmed UTXOs only. Prefer at least six
629628
// confirmations on outputs received from other wallets and only spend confirmed change.
630629
if (auto r1{AttemptSelection(wallet, value_to_select, CoinEligibilityFilter(1, 6, 0), available_coins, coin_selection_params, /*allow_mixed_output_types=*/false)}) return r1;

0 commit comments

Comments
 (0)