@@ -397,10 +397,13 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
397
397
398
398
// The knapsack solver has some legacy behavior where it will spend dust outputs. We retain this behavior, so don't filter for positive only here.
399
399
std::vector<OutputGroup> all_groups = GroupOutputs (wallet, coins, coin_selection_params, eligibility_filter, false /* positive_only */ );
400
+ CAmount target_with_change = nTargetValue;
400
401
// While nTargetValue includes the transaction fees for non-input things, it does not include the fee for creating a change output.
401
- // So we need to include that for KnapsackSolver as well, as we are expecting to create a change output.
402
- if (auto knapsack_result{KnapsackSolver (all_groups, nTargetValue + coin_selection_params.m_change_fee ,
403
- coin_selection_params.m_min_change_target , coin_selection_params.rng_fast )}) {
402
+ // So we need to include that for KnapsackSolver and SRD as well, as we are expecting to create a change output.
403
+ if (!coin_selection_params.m_subtract_fee_outputs ) {
404
+ target_with_change += coin_selection_params.m_change_fee ;
405
+ }
406
+ if (auto knapsack_result{KnapsackSolver (all_groups, target_with_change, coin_selection_params.m_min_change_target , coin_selection_params.rng_fast )}) {
404
407
knapsack_result->ComputeAndSetWaste (coin_selection_params.m_cost_of_change );
405
408
results.push_back (*knapsack_result);
406
409
}
@@ -409,7 +412,7 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
409
412
// barely meets the target. Just use the lower bound change target instead of the randomly
410
413
// generated one, since SRD will result in a random change amount anyway; avoid making the
411
414
// target needlessly large.
412
- const CAmount srd_target = nTargetValue + coin_selection_params. m_change_fee + CHANGE_LOWER;
415
+ const CAmount srd_target = target_with_change + CHANGE_LOWER;
413
416
if (auto srd_result{SelectCoinsSRD (positive_groups, srd_target, coin_selection_params.rng_fast )}) {
414
417
srd_result->ComputeAndSetWaste (coin_selection_params.m_cost_of_change );
415
418
results.push_back (*srd_result);
0 commit comments