MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / AttemptSelection

Function AttemptSelection

src/wallet/spend.cpp:698–723  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

696static bool HasErrorMsg(const util::Result<SelectionResult>& res) { return !util::ErrorString(res).empty(); }
697
698util::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
725util::Result<SelectionResult> ChooseSelectionResult(interfaces::Chain& chain, const CAmount& nTargetValue, Groups& groups, const CoinSelectionParams& coin_selection_params)
726{

Callers 2

AutomaticCoinSelectionFunction · 0.85
CoinSelectionFunction · 0.85

Calls 8

ChooseSelectionResultFunction · 0.85
ErrorClass · 0.85
HasErrorMsgFunction · 0.70
push_backMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
TypesCountMethod · 0.45

Tested by

no test coverage detected