diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index 4d64a60e414ed..6235f79a3e5f0 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -365,35 +365,31 @@ util::Result CoinGrinder(std::vector& utxo_pool, c } while (!is_done && should_shift) { - if (should_shift) { - // Set `next_utxo` to one after last selected, then deselect last selected UTXO - if (curr_selection.empty()) { - // Exhausted search space before running into attempt limit - is_done = true; - result.SetSelectionsEvaluated(curr_try); - result.SetAlgoCompleted(true); - break; - } - next_utxo = curr_selection.back() + 1; - deselect_last(); - should_shift = false; + // Set `next_utxo` to one after last selected, then deselect last selected UTXO + if (curr_selection.empty()) { + // Exhausted search space before running into attempt limit + is_done = true; + result.SetSelectionsEvaluated(curr_try); + result.SetAlgoCompleted(true); + break; } + next_utxo = curr_selection.back() + 1; + deselect_last(); + should_shift = false; // After SHIFTing to an omission branch, the `next_utxo` might have the same value and same weight as the // UTXO we just omitted (i.e. it is a "clone"). If so, selecting `next_utxo` would produce an equivalent // selection as one we previously evaluated. In that case, increment `next_utxo` until we find a UTXO with a // differing amount or weight. - while (!should_shift && next_utxo > 0 - && (curr_selection.empty() || curr_selection.back() != next_utxo - 1) - && utxo_pool[next_utxo - 1].GetSelectionAmount() == utxo_pool[next_utxo].GetSelectionAmount() + while (utxo_pool[next_utxo - 1].GetSelectionAmount() == utxo_pool[next_utxo].GetSelectionAmount() && utxo_pool[next_utxo - 1].fee == utxo_pool[next_utxo].fee) { - if (next_utxo < utxo_pool.size() - 1) { - // Skip clone: previous UTXO is equivalent and unselected - ++next_utxo; - } else { + if (next_utxo >= utxo_pool.size() - 1) { // Reached end of UTXO pool skipping clones: SHIFT instead should_shift = true; + break; } + // Skip clone: previous UTXO is equivalent and unselected + ++next_utxo; } } }