74 const Amount &target_value,
const Amount &cost_of_change,
75 std::set<CInputCoin> &out_set,
Amount &value_ret,
76 const Amount not_input_fees) {
81 std::vector<bool> curr_selection;
82 curr_selection.reserve(utxo_pool.size());
83 Amount actual_target = not_input_fees + target_value;
91 curr_available_value += utxo.effective_value;
93 if (curr_available_value < actual_target) {
98 std::sort(utxo_pool.begin(), utxo_pool.end(),
descending);
101 std::vector<bool> best_selection;
107 bool backtrack =
false;
108 if (curr_value + curr_available_value <
115 (curr_waste > best_waste &&
116 (utxo_pool.at(0).fee - utxo_pool.at(0).long_term_fee) >
124 else if (curr_value >= actual_target) {
133 curr_waste += (curr_value - actual_target);
135 if (curr_waste <= best_waste) {
136 best_selection = curr_selection;
137 best_selection.resize(utxo_pool.size());
138 best_waste = curr_waste;
145 curr_waste -= (curr_value - actual_target);
153 while (!curr_selection.empty() && !curr_selection.back()) {
154 curr_selection.pop_back();
155 curr_available_value +=
156 utxo_pool.at(curr_selection.size()).effective_value;
159 if (curr_selection.empty()) {
166 curr_selection.back() =
false;
167 OutputGroup &utxo = utxo_pool.at(curr_selection.size() - 1);
174 OutputGroup &utxo = utxo_pool.at(curr_selection.size());
183 if (!curr_selection.empty() && !curr_selection.back() &&
185 utxo_pool.at(curr_selection.size() - 1).effective_value &&
186 utxo.
fee == utxo_pool.at(curr_selection.size() - 1).fee) {
187 curr_selection.push_back(
false);
190 curr_selection.push_back(
true);
198 if (best_selection.empty()) {
204 for (
size_t i = 0; i < best_selection.size(); ++i) {
205 if (best_selection.at(i)) {
207 value_ret += utxo_pool.at(i).m_value;
215 const Amount &nTotalLower,
216 const Amount &nTargetValue,
217 std::vector<char> &vfBest,
Amount &nBest,
218 int iterations = 1000) {
219 std::vector<char> vfIncluded;
221 vfBest.assign(groups.size(),
true);
226 for (
int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++) {
227 vfIncluded.assign(groups.size(),
false);
229 bool fReachedTarget =
false;
230 for (
int nPass = 0; nPass < 2 && !fReachedTarget; nPass++) {
231 for (
size_t i = 0; i < groups.size(); i++) {
237 if (nPass == 0 ? insecure_rand.
randbool() : !vfIncluded[i]) {
238 nTotal += groups[i].m_value;
239 vfIncluded[i] =
true;
240 if (nTotal >= nTargetValue) {
241 fReachedTarget =
true;
242 if (nTotal < nBest) {
247 nTotal -= groups[i].m_value;
248 vfIncluded[i] =
false;
257 std::set<CInputCoin> &setCoinsRet,
Amount &nValueRet) {
262 std::optional<OutputGroup> lowest_larger;
263 std::vector<OutputGroup> applicable_groups;
269 if (group.m_value == nTargetValue) {
271 nValueRet += group.m_value;
273 }
else if (group.m_value < nTargetValue +
MIN_CHANGE) {
274 applicable_groups.push_back(group);
275 nTotalLower += group.m_value;
276 }
else if (!lowest_larger || group.m_value < lowest_larger->m_value) {
277 lowest_larger = group;
281 if (nTotalLower == nTargetValue) {
282 for (
const auto &group : applicable_groups) {
284 nValueRet += group.m_value;
289 if (nTotalLower < nTargetValue) {
290 if (!lowest_larger) {
294 nValueRet += lowest_larger->m_value;
299 std::sort(applicable_groups.begin(), applicable_groups.end(),
descending);
300 std::vector<char> vfBest;
305 if (nBest != nTargetValue && nTotalLower >= nTargetValue +
MIN_CHANGE) {
314 ((nBest != nTargetValue && nBest < nTargetValue +
MIN_CHANGE) ||
315 lowest_larger->m_value <= nBest)) {
317 nValueRet += lowest_larger->m_value;
319 for (
size_t i = 0; i < applicable_groups.size(); i++) {
321 util::insert(setCoinsRet, applicable_groups[i].m_outputs);
322 nValueRet += applicable_groups[i].m_value;
329 "SelectCoins() best subset: ");
330 for (
size_t i = 0; i < applicable_groups.size(); i++) {
352 bool positive_only) {
368 coin.
m_fee = coin_fee;
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
bool randbool() noexcept
Generate a random boolean.
static void ApproximateBestSubset(const std::vector< OutputGroup > &groups, const Amount &nTotalLower, const Amount &nTargetValue, std::vector< char > &vfBest, Amount &nBest, int iterations=1000)
static const size_t TOTAL_TRIES
bool KnapsackSolver(const Amount nTargetValue, std::vector< OutputGroup > &groups, std::set< CInputCoin > &setCoinsRet, Amount &nValueRet)
bool SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const Amount &target_value, const Amount &cost_of_change, std::set< CInputCoin > &out_set, Amount &value_ret, const Amount not_input_fees)
This is the Branch and Bound Coin Selection algorithm designed by Murch.
static constexpr Amount MIN_CHANGE
target minimum change amount
static bool LogAcceptCategory(BCLog::LogFlags category)
Return true if log accepts specified category.
#define LogPrint(category,...)
#define LogPrintToBeContinued
std::string FormatMoney(const Amount amt)
Do not use these functions to represent or parse monetary amounts to or from JSON but use AmountFromV...
void insert(Tdst &dst, const Tsrc &src)
Simplification of std insertion.
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
static constexpr Amount zero() noexcept
const int conf_theirs
Minimum number of confirmations for outputs received from a different wallet.
std::vector< CInputCoin > m_outputs
CFeeRate m_long_term_feerate
CFeeRate m_effective_feerate
void Insert(const CInputCoin &output, int depth, bool from_me, bool positive_only)
bool EligibleForSpending(const CoinEligibilityFilter &eligibility_filter) const