85 std::vector<COutput> utxo_pool;
104 int next_locktime{0};
110 if (total_balance + amount >= MAX_MONEY) {
114 total_balance += amount;
116 max_spendable += eff_value;
119 std::vector<OutputGroup> group_pos;
120 GroupCoins(fuzzed_data_provider, utxo_pool, coin_params,
true, group_pos);
126 if (!result_cg->GetAlgoCompleted())
return;
129 if (result_srd && result_srd->GetChange(CHANGE_LOWER, coin_params.
m_change_fee) > 0) {
130 assert(result_srd->GetWeight() >= result_cg->GetWeight());
134 if (result_knapsack && result_knapsack->GetChange(CHANGE_LOWER, coin_params.
m_change_fee) > 0) {
135 assert(result_knapsack->GetWeight() >= result_cg->GetWeight());
152 int next_locktime{0};
153 static constexpr unsigned max_output_groups{16};
154 std::vector<OutputGroup> group_pos;
163 const CAmount amount{eff_value + input_fee};
164 std::vector<COutput> temp_utxo_pool;
167 max_spendable += eff_value;
170 output_group.Insert(std::make_shared<COutput>(temp_utxo_pool.at(0)), 0, 0);
171 group_pos.push_back(output_group);
173 size_t num_groups = group_pos.size();
174 assert(num_groups <= max_output_groups);
180 CAmount best_amount{MAX_MONEY};
181 int best_weight{std::numeric_limits<int>::max()};
182 for (uint32_t pattern = 1; (pattern >> num_groups) == 0; ++pattern) {
184 int subset_weight{0};
185 for (
unsigned i = 0; i < num_groups; ++i) {
186 if ((pattern >> i) & 1) {
187 subset_amount += group_pos[i].GetSelectionAmount();
188 subset_weight += group_pos[i].m_weight;
191 if ((subset_amount >= target + coin_params.
m_min_change_target) && (subset_weight < best_weight || (subset_weight == best_weight && subset_amount < best_amount))) {
192 best_weight = subset_weight;
193 best_amount = subset_amount;
197 if (best_weight < std::numeric_limits<int>::max()) {
199 int high_max_selection_weight = fuzzed_data_provider.
ConsumeIntegralInRange<
int>(best_weight, std::numeric_limits<int>::max());
203 assert(result_cg->GetWeight() <= high_max_selection_weight);
205 assert(best_weight < result_cg->GetWeight() || (best_weight == result_cg->GetWeight() && best_amount <= result_cg->GetSelectedEffectiveValue()));
206 if (result_cg->GetAlgoCompleted()) {
208 assert(best_weight == result_cg->GetWeight());
209 assert(best_amount == result_cg->GetSelectedEffectiveValue());
244 int next_locktime{0};
251 static constexpr unsigned max_output_groups{16};
252 std::vector<OutputGroup> group_pos;
260 const CAmount amount{eff_value + input_fee};
261 std::vector<COutput> temp_utxo_pool;
264 max_spendable += eff_value;
267 output_group.Insert(std::make_shared<COutput>(temp_utxo_pool.at(0)), 0, 0);
268 group_pos.push_back(output_group);
270 size_t num_groups = group_pos.size();
271 assert(num_groups <= max_output_groups);
277 std::vector<uint32_t> solutions;
278 CAmount best_waste{std::numeric_limits<int64_t>::max()};
279 int best_weight{std::numeric_limits<int>::max()};
280 for (uint32_t pattern = 1; (pattern >> num_groups) == 0; ++pattern) {
283 bool is_superset =
false;
284 for (uint32_t sol : solutions) {
285 if ((pattern & sol) == sol) {
296 int subset_weight{0};
297 for (
unsigned i = 0; i < num_groups; ++i) {
298 if ((pattern >> i) & 1) {
299 subset_amount += group_pos[i].GetSelectionAmount();
300 subset_waste += group_pos[i].fee - group_pos[i].long_term_fee;
301 subset_weight += group_pos[i].m_weight;
304 if (subset_amount >= target && subset_amount <= target + coin_params.
m_cost_of_change) {
305 solutions.push_back(pattern);
307 CAmount excess = subset_amount - target;
308 subset_waste += excess;
311 for (
unsigned i = 0; i < num_groups; ++i) {
312 if ((pattern >> i) & 1) {
316 if (subset_waste < best_waste) {
317 best_waste = subset_waste;
320 best_weight = subset_weight;
325 int high_max_selection_weight = fuzzed_data_provider.
ConsumeIntegralInRange<
int>(best_weight, std::numeric_limits<int>::max());
328 if (!solutions.size() || !result_bnb) {
330 assert(!result_bnb == !solutions.size());
335 assert(result_bnb->GetWeight() <= high_max_selection_weight);
336 assert(result_bnb->GetSelectedEffectiveValue() >= target);
338 assert(best_waste <= result_bnb->GetWaste());
339 if (result_bnb->GetAlgoCompleted()) {
341 assert(best_waste == result_bnb->GetWaste());
356 std::vector<COutput> utxo_pool;
363 const bool subtract_fee_outputs{fuzzed_data_provider.
ConsumeBool()};
380 int next_locktime{0};
381 CAmount total_balance{
CreateCoins(fuzzed_data_provider, utxo_pool, coin_params, next_locktime)};
383 std::vector<OutputGroup> group_pos;
384 GroupCoins(fuzzed_data_provider, utxo_pool, coin_params,
true, group_pos);
386 int max_selection_weight = fuzzed_data_provider.
ConsumeIntegralInRange<
int>(0, std::numeric_limits<int>::max());
388 std::optional<SelectionResult> result;
394 result = *result_bnb;
396 assert(result_bnb->GetSelectedValue() >= target);
397 assert(result_bnb->GetWeight() <= max_selection_weight);
398 (void)result_bnb->GetShuffledInputVector();
399 (void)result_bnb->GetInputSet();
407 result = *result_srd;
408 assert(result_srd->GetSelectedValue() >= target);
410 assert(result_srd->GetWeight() <= max_selection_weight);
411 result_srd->SetBumpFeeDiscount(
ConsumeMoney(fuzzed_data_provider));
413 (void)result_srd->GetShuffledInputVector();
414 (void)result_srd->GetInputSet();
419 std::vector<OutputGroup> group_all;
420 GroupCoins(fuzzed_data_provider, utxo_pool, coin_params,
false, group_all);
424 (void)group.EligibleForSpending(filter);
428 auto result_knapsack =
KnapsackSolver(group_all, target, change_target, fast_random_context, max_selection_weight);
431 if (total_balance >= target && subtract_fee_outputs && !
HasErrorMsg(result_knapsack)) {
434 if (result_knapsack) {
435 result = *result_knapsack;
436 assert(result_knapsack->GetSelectedValue() >= target);
437 assert(result_knapsack->GetWeight() <= max_selection_weight);
438 result_knapsack->SetBumpFeeDiscount(
ConsumeMoney(fuzzed_data_provider));
440 (void)result_knapsack->GetShuffledInputVector();
441 (void)result_knapsack->GetInputSet();
445 std::vector<COutput> utxos;
446 CAmount new_total_balance{
CreateCoins(fuzzed_data_provider, utxos, coin_params, next_locktime)};
447 if (new_total_balance > 0) {
449 for (
const auto& utxo : utxos) {
450 new_utxo_pool.insert(std::make_shared<COutput>(utxo));
453 const auto weight{result->GetWeight()};
454 result->AddInputs(new_utxo_pool, subtract_fee_outputs);
455 assert(result->GetWeight() > weight);
459 std::vector<COutput> manual_inputs;
460 CAmount manual_balance{
CreateCoins(fuzzed_data_provider, manual_inputs, coin_params, next_locktime)};
461 if (manual_balance == 0)
return;
464 const CAmount old_target{result->GetTarget()};
465 const OutputSet input_set{result->GetInputSet()};
466 const int old_weight{result->GetWeight()};
467 result->Merge(manual_selection);
468 assert(result->GetInputSet().size() == input_set.size() + manual_inputs.size());
469 assert(result->GetTarget() == old_target + manual_selection.GetTarget());
470 assert(result->GetWeight() == old_weight + manual_selection.GetWeight());