Skip to content

Commit 8cd21bb

Browse files
committed
refactor: improve readability for AttemptSelection
it was pointed out by a few reviewers that the code block at the end of attempt selection was difficult to follow and lacked comments. refactor to get rid of triple nested if statement and improve readibility.
1 parent f47ff71 commit 8cd21bb

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/wallet/spend.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -481,19 +481,20 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
481481
results.push_back(*result);
482482
}
483483
}
484-
485-
// If we can't fund the transaction from any individual OutputType, run coin selection
486-
// over all available coins, else pick the best solution from the results
487-
if (results.size() == 0) {
488-
if (allow_mixed_output_types) {
489-
if (auto result{ChooseSelectionResult(wallet, nTargetValue, eligibility_filter, available_coins.All(), coin_selection_params)}) {
490-
return result;
491-
}
484+
// If we have at least one solution for funding the transaction without mixing, choose the minimum one according to waste metric
485+
// and return the result
486+
if (results.size() > 0) return *std::min_element(results.begin(), results.end());
487+
488+
// If we can't fund the transaction from any individual OutputType, run coin selection one last time
489+
// over all available coins, which would allow mixing
490+
if (allow_mixed_output_types) {
491+
if (auto result{ChooseSelectionResult(wallet, nTargetValue, eligibility_filter, available_coins.All(), coin_selection_params)}) {
492+
return result;
492493
}
493-
return std::optional<SelectionResult>();
494-
};
495-
std::optional<SelectionResult> result{*std::min_element(results.begin(), results.end())};
496-
return result;
494+
}
495+
// Either mixing is not allowed and we couldn't find a solution from any single OutputType, or mixing was allowed and we still couldn't
496+
// find a solution using all available coins
497+
return std::nullopt;
497498
};
498499

499500
std::optional<SelectionResult> ChooseSelectionResult(const CWallet& wallet, const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, const std::vector<COutput>& available_coins, const CoinSelectionParams& coin_selection_params)

0 commit comments

Comments
 (0)