18 static void AddCoin(
const CAmount& value,
int n_input,
int n_input_bytes,
int locktime, std::vector<COutput>& coins,
CFeeRate fee_rate)
21 tx.
vout.resize(n_input + 1);
22 tx.
vout[n_input].nValue = value;
24 coins.emplace_back(
COutPoint(tx.
GetHash(), n_input), tx.
vout.at(n_input), 0, n_input_bytes,
true,
true,
true, 0,
true, fee_rate);
31 bool valid_outputgroup{
false};
32 for (
auto& coin : coins) {
33 if (!positive_only || (positive_only && coin.GetEffectiveValue() > 0)) {
34 output_group.Insert(std::make_shared<COutput>(coin), 0, 0);
38 valid_outputgroup = !positive_only || output_group.GetSelectionAmount() > 0;
39 if (valid_outputgroup && fuzzed_data_provider.
ConsumeBool()) {
40 output_groups.push_back(output_group);
42 valid_outputgroup =
false;
45 if (valid_outputgroup) output_groups.push_back(output_group);
56 if (total_balance + amount >=
MAX_MONEY) {
60 total_balance += amount;
69 std::set<std::shared_ptr<COutput>> utxo_pool;
70 for (
const auto& utxo : utxos) {
71 utxo_pool.insert(std::make_shared<COutput>(utxo));
73 result.
AddInputs(utxo_pool, subtract_fee_outputs);
83 std::vector<COutput> utxo_pool;
91 const bool subtract_fee_outputs{fuzzed_data_provider.ConsumeBool()};
96 coin_params.m_long_term_feerate = long_term_fee_rate;
97 coin_params.m_effective_feerate = effective_fee_rate;
98 coin_params.min_viable_change = min_viable_change;
99 coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange<
int>(10, 1000);
100 coin_params.m_change_fee = effective_fee_rate.GetFee(coin_params.change_output_size);
101 coin_params.m_discard_feerate = discard_fee_rate;
102 coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<
int>(41, 1000);
103 coin_params.m_cost_of_change = coin_params.m_change_fee + coin_params.m_discard_feerate.GetFee(coin_params.change_spend_size);
105 int next_locktime{0};
106 CAmount total_balance{
CreateCoins(fuzzed_data_provider, utxo_pool, coin_params, next_locktime)};
108 std::vector<OutputGroup> group_pos;
109 GroupCoins(fuzzed_data_provider, utxo_pool, coin_params,
true, group_pos);
110 std::vector<OutputGroup> group_all;
111 GroupCoins(fuzzed_data_provider, utxo_pool, coin_params,
false, group_all);
114 const CoinEligibilityFilter filter(fuzzed_data_provider.ConsumeIntegral<
int>(), fuzzed_data_provider.ConsumeIntegral<
int>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>());
115 (void)
group.EligibleForSpending(filter);
121 assert(result_bnb->GetChange(coin_params.m_cost_of_change,
CAmount{0}) == 0);
122 assert(result_bnb->GetSelectedValue() >= target);
123 (void)result_bnb->GetShuffledInputVector();
124 (void)result_bnb->GetInputSet();
129 assert(result_srd->GetSelectedValue() >= target);
131 result_srd->ComputeAndSetWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
132 (void)result_srd->GetShuffledInputVector();
133 (void)result_srd->GetInputSet();
138 if (result_knapsack) {
139 assert(result_knapsack->GetSelectedValue() >= target);
140 result_knapsack->ComputeAndSetWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
141 (void)result_knapsack->GetShuffledInputVector();
142 (void)result_knapsack->GetInputSet();
147 if (total_balance >= target && subtract_fee_outputs && !
HasErrorMsg(result_knapsack)) {
151 std::vector<COutput> utxos;
152 std::vector<util::Result<SelectionResult>> results{result_srd, result_knapsack, result_bnb};
153 CAmount new_total_balance{
CreateCoins(fuzzed_data_provider, utxos, coin_params, next_locktime)};
154 if (new_total_balance > 0) {
155 std::set<std::shared_ptr<COutput>> new_utxo_pool;
156 for (
const auto& utxo : utxos) {
157 new_utxo_pool.insert(std::make_shared<COutput>(utxo));
159 for (
auto& result : results) {
160 if (!result)
continue;
161 const auto weight{result->GetWeight()};
162 result->AddInputs(new_utxo_pool, subtract_fee_outputs);
163 assert(result->GetWeight() > weight);
167 std::vector<COutput> manual_inputs;
168 CAmount manual_balance{
CreateCoins(fuzzed_data_provider, manual_inputs, coin_params, next_locktime)};
169 if (manual_balance == 0)
return;
170 auto manual_selection{
ManualSelection(manual_inputs, manual_balance, coin_params.m_subtract_fee_outputs)};
171 for (
auto& result : results) {
172 if (!result)
continue;
174 const std::set<std::shared_ptr<COutput>> input_set{result->GetInputSet()};
175 const int old_weight{result->GetWeight()};
176 result->Merge(manual_selection);
177 assert(result->GetInputSet().size() == input_set.size() + manual_inputs.size());
178 assert(result->GetTarget() == old_target + manual_selection.GetTarget());
179 assert(result->GetWeight() == old_weight + manual_selection.GetWeight());
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
An outpoint - a combination of a transaction hash and an index n into its vout.
T ConsumeIntegralInRange(T min, T max)
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
bilingual_str ErrorString(const Result< T > &result)
static CAmount CreateCoins(FuzzedDataProvider &fuzzed_data_provider, std::vector< COutput > &utxo_pool, CoinSelectionParams &coin_params, int &next_locktime)
util::Result< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, int max_weight)
static void AddCoin(const CAmount &value, int n_input, int n_input_bytes, int locktime, std::vector< COutput > &coins, CFeeRate fee_rate)
static constexpr CAmount CHANGE_LOWER
lower bound for randomly-chosen target change amount
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...
std::vector< OutputGroup > & GroupCoins(const std::vector< COutput > &available_coins, bool subtract_fee_outputs=false)
util::Result< SelectionResult > KnapsackSolver(std::vector< OutputGroup > &groups, const CAmount &nTargetValue, CAmount change_target, FastRandomContext &rng, int max_weight)
static SelectionResult ManualSelection(std::vector< COutput > &utxos, const CAmount &total_amount, const bool &subtract_fee_outputs)
FUZZ_TARGET(coinselection)
util::Result< SelectionResult > SelectCoinsSRD(const std::vector< OutputGroup > &utxo_pool, CAmount target_value, CAmount change_fee, FastRandomContext &rng, int max_weight)
Select coins by Single Random Draw.
static bool HasErrorMsg(const util::Result< SelectionResult > &res)
static constexpr unsigned int DUST_RELAY_TX_FEE
Min feerate for defining dust.
static constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
A mutable version of CTransaction.
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
std::vector< CTxOut > vout
Parameters for filtering which OutputGroups we may use in coin selection.
Parameters for one iteration of Coin Selection.
bool m_subtract_fee_outputs
Indicate that we are subtracting the fee from outputs.
CFeeRate m_effective_feerate
The targeted feerate of the transaction being built.
A group of UTXOs paid to the same output script.
void AddInputs(const std::set< std::shared_ptr< COutput >> &inputs, bool subtract_fee_outputs)
CAmount GetTarget() const
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept