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.
pull/25734/head
josibake 2 years ago
parent f47ff71761
commit 8cd21bb279
No known key found for this signature in database
GPG Key ID: 8ADCB558C4F33D65

@ -481,19 +481,20 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
results.push_back(*result); results.push_back(*result);
} }
} }
// If we have at least one solution for funding the transaction without mixing, choose the minimum one according to waste metric
// If we can't fund the transaction from any individual OutputType, run coin selection // and return the result
// over all available coins, else pick the best solution from the results if (results.size() > 0) return *std::min_element(results.begin(), results.end());
if (results.size() == 0) {
if (allow_mixed_output_types) { // If we can't fund the transaction from any individual OutputType, run coin selection one last time
if (auto result{ChooseSelectionResult(wallet, nTargetValue, eligibility_filter, available_coins.All(), coin_selection_params)}) { // over all available coins, which would allow mixing
return result; if (allow_mixed_output_types) {
} if (auto result{ChooseSelectionResult(wallet, nTargetValue, eligibility_filter, available_coins.All(), coin_selection_params)}) {
return result;
} }
return std::optional<SelectionResult>(); }
}; // 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
std::optional<SelectionResult> result{*std::min_element(results.begin(), results.end())}; // find a solution using all available coins
return result; return std::nullopt;
}; };
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) 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)

Loading…
Cancel
Save