| 696 | static bool HasErrorMsg(const util::Result<SelectionResult>& res) { return !util::ErrorString(res).empty(); } |
| 697 | |
| 698 | util::Result<SelectionResult> AttemptSelection(interfaces::Chain& chain, const CAmount& nTargetValue, OutputGroupTypeMap& groups, |
| 699 | const CoinSelectionParams& coin_selection_params, bool allow_mixed_output_types) |
| 700 | { |
| 701 | // Run coin selection on each OutputType and compute the Waste Metric |
| 702 | std::vector<SelectionResult> results; |
| 703 | for (auto& [type, group] : groups.groups_by_type) { |
| 704 | auto result{ChooseSelectionResult(chain, nTargetValue, group, coin_selection_params)}; |
| 705 | // If any specific error message appears here, then something particularly wrong happened. |
| 706 | if (HasErrorMsg(result)) return result; // So let's return the specific error. |
| 707 | // Append the favorable result. |
| 708 | if (result) results.push_back(*result); |
| 709 | } |
| 710 | // If we have at least one solution for funding the transaction without mixing, choose the minimum one according to waste metric |
| 711 | // and return the result |
| 712 | if (results.size() > 0) return *std::min_element(results.begin(), results.end()); |
| 713 | |
| 714 | // If we can't fund the transaction from any individual OutputType, run coin selection one last time |
| 715 | // over all available coins, which would allow mixing. |
| 716 | // If TypesCount() <= 1, there is nothing to mix. |
| 717 | if (allow_mixed_output_types && groups.TypesCount() > 1) { |
| 718 | return ChooseSelectionResult(chain, nTargetValue, groups.all_groups, coin_selection_params); |
| 719 | } |
| 720 | // 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 |
| 721 | // find a solution using all available coins |
| 722 | return util::Error(); |
| 723 | }; |
| 724 | |
| 725 | util::Result<SelectionResult> ChooseSelectionResult(interfaces::Chain& chain, const CAmount& nTargetValue, Groups& groups, const CoinSelectionParams& coin_selection_params) |
| 726 | { |
no test coverage detected