diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index 92e76cfe4165ed..b9c305c53d1e12 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -293,8 +293,8 @@ util::Result CoinGrinder(std::vector& utxo_pool, c SelectionResult result(selection_target, SelectionAlgorithm::CG); bool is_done = false; + size_t curr_try = 0; while (!is_done) { - for (size_t curr_try = 0;;) { bool should_shift{false}, should_cut{false}; // Select `next_utxo` OutputGroup& utxo = utxo_pool[next_utxo]; @@ -304,7 +304,7 @@ util::Result CoinGrinder(std::vector& utxo_pool, c ++curr_try; ++next_utxo; - // EVALUATE the new input set candidate, default to exploring the inclusion branch further, else do exactly one SHIFT or CUT. + // EVALUATE current selection, default to exploring the inclusion branch further, else do exactly one SHIFT or CUT. if (curr_amount + lookahead[curr_selection.back()] < selection_target + change_target) { // Insufficient funds with lookahead: CUT should_cut = true; @@ -327,11 +327,13 @@ util::Result CoinGrinder(std::vector& utxo_pool, c best_selection_amount = curr_amount; } } + if (curr_try >= TOTAL_TRIES) { // Solution is not guaranteed to be optimal if `curr_try` hit TOTAL_TRIES result.SetAlgoCompleted(false); break; } + if (next_utxo == utxo_pool.size()) { // Added UTXO was end of UTXO pool, nothing left to add on inclusion or omission branch: CUT should_cut = true;