diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index 295045bab94692..2d833052e527a7 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -239,7 +239,7 @@ util::Result CoinGrinder(std::vector& utxo_pool, c std::vector best_selection; // best selection UTXO indices CAmount best_selection_amount = MAX_MONEY; - int best_selection_weight = std::numeric_limits::max(); + int best_selection_weight = max_weight; // Tie is fine, because we prefer lower selection amount std::vector curr_selection; // selected UTXO indices CAmount curr_amount = 0; @@ -339,15 +339,9 @@ util::Result CoinGrinder(std::vector& utxo_pool, c if (curr_amount + lookahead.at(utxo_pool_index) < selection_target + change_target) { // Cannot succeed due to insufficient funds in lookahead: CUT (deselect latest, SHIFT) next_op = operations::cut; - } else if (curr_weight > max_weight) { - // max_weight exceeded: CUT if last selected group had minimal weight, else SHIFT - max_tx_weight_exceeded = true; - if (utxo_pool.at(curr_selection.back()).m_weight <= min_tail_weight.at(curr_selection.back())) { - next_op = operations::cut; - } else { - next_op = operations::shift; - } } else if (curr_weight > best_selection_weight) { + // best_selection_weight is initialized to max_weight + if (curr_weight > max_weight) max_tx_weight_exceeded = true; // Worse weight than best solution. More UTXOs only increase weight: // CUT if last selected group had minimal weight, else SHIFT if (utxo_pool.at(curr_selection.back()).m_weight <= min_tail_weight.at(curr_selection.back())) { @@ -358,7 +352,7 @@ util::Result CoinGrinder(std::vector& utxo_pool, c } else if (curr_amount >= selection_target + change_target) { // Target exceeded: check if new best, then SHIFT (deselect latest, select next) next_op = operations::shift; - if (curr_weight < best_selection_weight || (curr_weight == best_selection_weight && curr_amount < best_selection_amount)) { + if (curr_weight < best_selection_weight || curr_amount < best_selection_amount) { best_selection = curr_selection; best_selection_weight = curr_weight; best_selection_amount = curr_amount;