Skip to content

Commit

Permalink
Use while loop instead of for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
murchandamus committed Jan 5, 2024
1 parent b83d126 commit f134ae0
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/wallet/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,10 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& 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();
Expand All @@ -302,6 +304,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& 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;
Expand Down Expand Up @@ -330,21 +333,21 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& 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;
}
Expand All @@ -366,15 +369,13 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& 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;
}
next_utxo = curr_selection.back() + 1;
deselect_last();
}
}
if (is_done) break;
}

if (best_selection.empty()) {
Expand Down

0 comments on commit f134ae0

Please sign in to comment.