17 static void AddCoin(
const CAmount& value,
int n_input,
int n_input_bytes,
int locktime, std::vector<COutput>& coins,
CFeeRate fee_rate)
20 tx.
vout.resize(n_input + 1);
21 tx.
vout[n_input].nValue = value;
23 coins.emplace_back(
COutPoint(tx.
GetHash(), n_input), tx.
vout.at(n_input), 0, n_input_bytes,
true,
true,
true, 0,
true, fee_rate);
30 bool valid_outputgroup{
false};
31 for (
auto& coin : coins) {
32 output_group.Insert(coin, 0, 0, positive_only);
35 valid_outputgroup = !positive_only || output_group.GetSelectionAmount() > 0;
36 if (valid_outputgroup && fuzzed_data_provider.
ConsumeBool()) {
37 output_groups.push_back(output_group);
39 valid_outputgroup =
false;
42 if (valid_outputgroup) output_groups.push_back(output_group);
48 std::vector<COutput> utxo_pool;
54 const bool subtract_fee_outputs{fuzzed_data_provider.ConsumeBool()};
59 coin_params.m_long_term_feerate = long_term_fee_rate;
60 coin_params.m_effective_feerate = effective_fee_rate;
61 coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange<
int>(10, 1000);
62 coin_params.m_change_fee = effective_fee_rate.GetFee(coin_params.change_output_size);
69 const int n_input{fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, 10)};
70 const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange<
int>(100, 10000)};
72 if (total_balance + amount >=
MAX_MONEY) {
75 AddCoin(amount, n_input, n_input_bytes, ++next_locktime, utxo_pool, coin_params.m_effective_feerate);
76 total_balance += amount;
79 std::vector<OutputGroup> group_pos;
80 GroupCoins(fuzzed_data_provider, utxo_pool, coin_params,
true, group_pos);
81 std::vector<OutputGroup> group_all;
82 GroupCoins(fuzzed_data_provider, utxo_pool, coin_params,
false, group_all);
85 const auto result_bnb =
SelectCoinsBnB(group_pos, target, cost_of_change);
87 auto result_srd =
SelectCoinsSRD(group_pos, target, fast_random_context);
88 if (result_srd) result_srd->ComputeAndSetWaste(cost_of_change, cost_of_change, 0);
91 auto result_knapsack =
KnapsackSolver(group_all, target, change_target, fast_random_context);
92 if (result_knapsack) result_knapsack->ComputeAndSetWaste(cost_of_change, cost_of_change, 0);
96 if (total_balance >= target && subtract_fee_outputs) {
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
std::optional< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change)
FUZZ_TARGET(coinselection)
int64_t CAmount
Amount in satoshis (Can be negative)
A group of UTXOs paid to the same output script.
An outpoint - a combination of a transaction hash and an index n into its vout.
std::vector< CTxOut > vout
std::vector< OutputGroup > & GroupCoins(const std::vector< COutput > &available_coins)
Parameters for one iteration of Coin Selection.
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
std::optional< SelectionResult > SelectCoinsSRD(const std::vector< OutputGroup > &utxo_pool, CAmount target_value, FastRandomContext &rng)
Select coins by Single Random Draw.
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
static void AddCoin(const CAmount &value, int n_input, int n_input_bytes, int locktime, std::vector< COutput > &coins, CFeeRate fee_rate)
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
A mutable version of CTransaction.
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
std::optional< SelectionResult > KnapsackSolver(std::vector< OutputGroup > &groups, const CAmount &nTargetValue, CAmount change_target, FastRandomContext &rng)
CAmount GenerateChangeTarget(const CAmount payment_value, const CAmount change_fee, FastRandomContext &rng)
Choose a random change target for each transaction to make it harder to fingerprint the Core wallet b...
static constexpr CAmount COIN
The amount of satoshis in one BTC.