aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/spend.cpp
diff options
context:
space:
mode:
authorjosibake <josibake@protonmail.com>2022-07-29 12:42:40 +0200
committerjosibake <josibake@protonmail.com>2022-08-10 15:19:32 +0200
commit8cd21bb2799d37ed00dc9d0490bb5f5f1375932b (patch)
tree87df9e9a5414e7104927de39bb3c2deb5b1ba692 /src/wallet/spend.cpp
parentf47ff717611182da27461e29b3c23933eb22fbce (diff)
downloadbitcoin-8cd21bb2799d37ed00dc9d0490bb5f5f1375932b.tar.xz
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.
Diffstat (limited to 'src/wallet/spend.cpp')
-rw-r--r--src/wallet/spend.cpp25
1 files changed, 13 insertions, 12 deletions
diff --git a/src/wallet/spend.cpp b/src/wallet/spend.cpp
index d161393995..8147ffcb69 100644
--- a/src/wallet/spend.cpp
+++ b/src/wallet/spend.cpp
@@ -481,19 +481,20 @@ std::optional<SelectionResult> AttemptSelection(const CWallet& wallet, const CAm
results.push_back(*result);
}
}
-
- // If we can't fund the transaction from any individual OutputType, run coin selection
- // over all available coins, else pick the best solution from the results
- if (results.size() == 0) {
- if (allow_mixed_output_types) {
- if (auto result{ChooseSelectionResult(wallet, nTargetValue, eligibility_filter, available_coins.All(), coin_selection_params)}) {
- return result;
- }
+ // If we have at least one solution for funding the transaction without mixing, choose the minimum one according to waste metric
+ // and return the result
+ if (results.size() > 0) return *std::min_element(results.begin(), results.end());
+
+ // If we can't fund the transaction from any individual OutputType, run coin selection one last time
+ // over all available coins, which would allow mixing
+ 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>();
- };
- std::optional<SelectionResult> result{*std::min_element(results.begin(), results.end())};
- return result;
+ }
+ // 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
+ // find a solution using all available coins
+ 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)