From f134ae040c9245cd4c8a818e8190be44a080b727 Mon Sep 17 00:00:00 2001 From: Murch Date: Fri, 5 Jan 2024 13:52:02 -0500 Subject: [PATCH] Use while loop instead of for loop --- src/wallet/coinselection.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index 88422d325b1cb4..92e76cfe4165ed 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -292,8 +292,10 @@ util::Result CoinGrinder(std::vector& utxo_pool, c }; SelectionResult result(selection_target, SelectionAlgorithm::CG); + bool is_done = false; + while (!is_done) { for (size_t curr_try = 0;;) { - bool should_shift{false}, should_cut{false}, is_done{false}; + bool should_shift{false}, should_cut{false}; // Select `next_utxo` OutputGroup& utxo = utxo_pool[next_utxo]; curr_amount += utxo.GetSelectionAmount(); @@ -302,6 +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. if (curr_amount + lookahead[curr_selection.back()] < selection_target + change_target) { // Insufficient funds with lookahead: CUT should_cut = true; @@ -330,21 +333,21 @@ util::Result CoinGrinder(std::vector& utxo_pool, c break; } if (next_utxo == utxo_pool.size()) { - // Reached end of UTXO pool, nothing left to add: CUT + // Added UTXO was end of UTXO pool, nothing left to add on inclusion or omission branch: CUT should_cut = true; } if (should_cut) { + // Redirect to exploring Omission branch of second to last selected UTXO (i.e. deselect last selected UTXO, then SHIFT) + should_cut = false; deselect_last(); should_shift = true; - should_cut = false; } - if (should_shift) { - // Set `next_utxo` to one after last selected, deselect last selected UTXO + // Set `next_utxo` to one after last selected, then deselect last selected UTXO if (curr_selection.empty()) { - // Exhausted search space before running into limit + // Exhausted search space before running into attempt limit result.SetAlgoCompleted(true); break; } @@ -366,7 +369,6 @@ util::Result CoinGrinder(std::vector& utxo_pool, c if (curr_selection.empty()) { // Exhausted search space before running into limit is_done = true; - std::cout << "FINISHED SEARCH IN CLONE SKIPPING" << std::endl; result.SetAlgoCompleted(true); break; } @@ -374,7 +376,6 @@ util::Result CoinGrinder(std::vector& utxo_pool, c deselect_last(); } } - if (is_done) break; } if (best_selection.empty()) {