Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
coinselection.cpp
Go to the documentation of this file.
1// Copyright (c) 2022-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <policy/feerate.h>
6#include <policy/policy.h>
9#include <test/fuzz/fuzz.h>
10#include <test/fuzz/util.h>
13
14#include <numeric>
15#include <span>
16#include <vector>
17
18namespace wallet {
19
20static void AddCoin(const CAmount& value, int n_input, int n_input_bytes, int locktime, std::vector<COutput>& coins, CFeeRate fee_rate)
21{
23 tx.vout.resize(n_input + 1);
24 tx.vout[n_input].nValue = value;
25 tx.nLockTime = locktime; // all transactions get different hashes
26 coins.emplace_back(COutPoint(tx.GetHash(), n_input), tx.vout.at(n_input), /*depth=*/0, n_input_bytes, /*solvable=*/true, /*safe=*/true, /*time=*/0, /*from_me=*/true, fee_rate);
27}
28
29// Randomly distribute coins to instances of OutputGroup
30static void GroupCoins(FuzzedDataProvider& fuzzed_data_provider, const std::vector<COutput>& coins, const CoinSelectionParams& coin_params, bool positive_only, std::vector<OutputGroup>& output_groups)
31{
33 bool valid_outputgroup{false};
34 for (auto& coin : coins) {
35 if (!positive_only || (positive_only && coin.GetEffectiveValue() > 0)) {
36 output_group.Insert(std::make_shared<COutput>(coin), /*ancestors=*/0, /*cluster_count=*/0);
37 }
38 // If positive_only was specified, nothing was inserted, leading to an empty output group
39 // that would be invalid for the BnB algorithm
40 valid_outputgroup = !positive_only || output_group.GetSelectionAmount() > 0;
42 output_groups.push_back(output_group);
44 valid_outputgroup = false;
45 }
46 }
48}
49
66
67static SelectionResult ManualSelection(std::vector<COutput>& utxos, const CAmount& total_amount, const bool& subtract_fee_outputs)
68{
69 SelectionResult result(total_amount, SelectionAlgorithm::MANUAL);
71 for (const auto& utxo : utxos) {
72 utxo_pool.insert(std::make_shared<COutput>(utxo));
73 }
75 return result;
76}
77
78// Returns true if the result contains an error and the message is not empty
80
82{
83 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
84 std::vector<COutput> utxo_pool;
85
87
91 coin_params.m_long_term_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
92 coin_params.m_effective_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
93 coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
94 coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
95 coin_params.m_change_fee = coin_params.m_effective_feerate.GetFee(coin_params.change_output_size);
96 // For other results to be comparable to SRD, we must align the change_target with SRD’s hardcoded behavior
97 coin_params.m_min_change_target = CHANGE_LOWER + coin_params.m_change_fee;
98
99 // Create some coins
102 int next_locktime{0};
107 if (total_balance + amount >= MAX_MONEY) {
108 break;
109 }
110 AddCoin(amount, n_input, n_input_bytes, ++next_locktime, utxo_pool, coin_params.m_effective_feerate);
111 total_balance += amount;
112 CAmount eff_value = amount - coin_params.m_effective_feerate.GetFee(n_input_bytes);
114 }
115
116 std::vector<OutputGroup> group_pos;
118
119 // Run coinselection algorithms
121 if (target + coin_params.m_min_change_target > max_spendable || HasErrorMsg(result_cg)) return; // We only need to compare algorithms if CoinGrinder has a solution
123 if (!result_cg->GetAlgoCompleted()) return; // Bail out if CoinGrinder solution is not optimal
124
126 if (result_srd && result_srd->GetChange(CHANGE_LOWER, coin_params.m_change_fee) > 0) { // exclude any srd solutions that don’t have change, err on excluding
127 assert(result_srd->GetWeight() >= result_cg->GetWeight());
128 }
129
131 if (result_knapsack && result_knapsack->GetChange(CHANGE_LOWER, coin_params.m_change_fee) > 0) { // exclude any knapsack solutions that don’t have change, err on excluding
132 assert(result_knapsack->GetWeight() >= result_cg->GetWeight());
133 }
134}
135
137{
138 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
139
143 // Set effective feerate up to MAX_MONEY sats per 1'000'000 vB (2'100'000'000 sat/vB = 21'000 BTC/kvB).
144 coin_params.m_effective_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, MAX_MONEY), 1'000'000};
145 coin_params.m_min_change_target = ConsumeMoney(fuzzed_data_provider);
146
147 // Create some coins
149 int next_locktime{0};
150 static constexpr unsigned max_output_groups{16};
151 std::vector<OutputGroup> group_pos;
153 // With maximum m_effective_feerate and n_input_bytes = 1'000'000, input_fee <= MAX_MONEY.
154 const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 1'000'000)};
155 // Only make UTXOs with positive effective value
156 const CAmount input_fee = coin_params.m_effective_feerate.GetFee(n_input_bytes);
157 // Ensure that each UTXO has at least an effective value of 1 sat
159 const CAmount amount{eff_value + input_fee};
160 std::vector<COutput> temp_utxo_pool;
161
162 AddCoin(amount, /*n_input=*/0, n_input_bytes, ++next_locktime, temp_utxo_pool, coin_params.m_effective_feerate);
164
166 output_group.Insert(std::make_shared<COutput>(temp_utxo_pool.at(0)), /*ancestors=*/0, /*cluster_count=*/0);
167 group_pos.push_back(output_group);
168 }
169 size_t num_groups = group_pos.size();
170 assert(num_groups <= max_output_groups);
171
172 // Only choose targets below max_spendable
174
175 // Brute force optimal solution
177 int best_weight{std::numeric_limits<int>::max()};
178 for (uint32_t pattern = 1; (pattern >> num_groups) == 0; ++pattern) {
180 int subset_weight{0};
181 for (unsigned i = 0; i < num_groups; ++i) {
182 if ((pattern >> i) & 1) {
183 subset_amount += group_pos[i].GetSelectionAmount();
184 subset_weight += group_pos[i].m_weight;
185 }
186 }
190 }
191 }
192
193 if (best_weight < std::numeric_limits<int>::max()) {
194 // Sufficient funds and acceptable weight: CoinGrinder should find at least one solution
195 int high_max_selection_weight = fuzzed_data_provider.ConsumeIntegralInRange<int>(best_weight, std::numeric_limits<int>::max());
196
200 assert(result_cg->GetSelectedEffectiveValue() >= target + coin_params.m_min_change_target);
201 assert(best_weight < result_cg->GetWeight() || (best_weight == result_cg->GetWeight() && best_amount <= result_cg->GetSelectedEffectiveValue()));
202 if (result_cg->GetAlgoCompleted()) {
203 // If CoinGrinder exhausted the search space, it must return the optimal solution
204 assert(best_weight == result_cg->GetWeight());
205 assert(best_amount == result_cg->GetSelectedEffectiveValue());
206 }
207 }
208
209 // CoinGrinder cannot ever find a better solution than the brute-forced best, or there is none in the first place
212 // Max_weight should have been exceeded, or there were insufficient funds
214}
215
217{
218 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
219
223 // Set effective feerate up to 10'000'000 sats per kvB (10'000 sat/vB).
224 coin_params.m_effective_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 10'000'000), 1'000};
225 coin_params.m_long_term_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 10'000'000), 1'000};
226 coin_params.m_discard_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 10'000'000), 1'000};
228
230 coin_params.m_change_fee = coin_params.m_effective_feerate.GetFee(coin_params.change_output_size);
231 coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(41, 1000);
232 const auto change_spend_fee{coin_params.m_discard_feerate.GetFee(coin_params.change_spend_size)};
233 coin_params.m_cost_of_change = coin_params.m_change_fee + change_spend_fee;
234 CScript change_out_script = CScript() << std::vector<unsigned char>(coin_params.change_output_size, OP_TRUE);
235 const auto dust{GetDustThreshold(CTxOut{/*nValueIn=*/0, change_out_script}, coin_params.m_discard_feerate)};
236 coin_params.min_viable_change = std::max(change_spend_fee + 1, dust);
237
238 // Create some coins
240 int next_locktime{0};
241 // Too many output groups (>17?) would make it possible to generate UTXO
242 // pool and target combinations that cannot be completely searched by BnB
243 // before running into the attempt limit (see BnB "Exhaust..." test). The
244 // brute force search also gets exponentially more expensive with bigger
245 // UTXO pools.
246 // Choose 1–16 of 16 provides ample fuzzing space.
247 static constexpr unsigned max_output_groups{16};
248 std::vector<OutputGroup> group_pos;
250 // With maximum m_effective_feerate 10'000 s/vB and n_input_bytes = 20'000 B, input_fee <= MAX_MONEY.
252 const CAmount input_fee = coin_params.m_effective_feerate.GetFee(n_input_bytes);
253 // Ensure that each UTXO has at least an effective value of 1 sat
255 const CAmount amount{eff_value + input_fee};
256 std::vector<COutput> temp_utxo_pool;
257
258 AddCoin(amount, /*n_input=*/0, n_input_bytes, ++next_locktime, temp_utxo_pool, coin_params.m_effective_feerate);
260
262 output_group.Insert(std::make_shared<COutput>(temp_utxo_pool.at(0)), /*ancestors=*/0, /*cluster_count=*/0);
263 group_pos.push_back(output_group);
264 }
265 size_t num_groups = group_pos.size();
266 assert(num_groups <= max_output_groups);
267
268 // Only choose targets below max_spendable
270
271 // Brute force optimal solution (lowest waste, but cannot be superset of another solution)
272 std::vector<uint32_t> solutions;
273 CAmount best_waste{std::numeric_limits<int64_t>::max()};
274 int best_weight{std::numeric_limits<int>::max()};
275 for (uint32_t pattern = 1; (pattern >> num_groups) == 0; ++pattern) {
276 // BnB does not permit adding more inputs to a solution, i.e. a superset of a solution cannot ever be a solution.
277 // The search pattern guarantees that any superset will only be visited after all its subsets have been traversed.
278 bool is_superset = false;
279 for (uint32_t sol : solutions) {
280 if ((pattern & sol) == sol) {
281 is_superset = true;
282 break;
283 }
284 }
285 if (is_superset) {
286 continue;
287 }
288
291 int subset_weight{0};
292 for (unsigned i = 0; i < num_groups; ++i) {
293 if ((pattern >> i) & 1) {
294 subset_amount += group_pos[i].GetSelectionAmount();
295 subset_waste += group_pos[i].fee - group_pos[i].long_term_fee;
296 subset_weight += group_pos[i].m_weight;
297 }
298 }
299 if (subset_amount >= target && subset_amount <= target + coin_params.m_cost_of_change) {
300 solutions.push_back(pattern);
301 // Add the excess (overselection that gets dropped to fees) to waste score
305
306 for (unsigned i = 0; i < num_groups; ++i) {
307 if ((pattern >> i) & 1) {
308 result_bf.AddInput(group_pos[i]);
309 }
310 }
311 if (subset_waste < best_waste) {
313 result_bf.RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
314 assert(result_bf.GetWaste() == best_waste);
316 }
317 }
318 }
319
320 int high_max_selection_weight = fuzzed_data_provider.ConsumeIntegralInRange<int>(best_weight, std::numeric_limits<int>::max());
322
323 if (!solutions.size() || !result_bnb) {
324 // Either both BnB and Brute Force find a solution or neither does.
325 assert(!result_bnb == !solutions.size());
326 } else {
327 // If brute forcing found a solution with an acceptable weight, BnB must find at least one solution with at most 16 output groups
329 result_bnb->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
331 assert(result_bnb->GetSelectedEffectiveValue() >= target);
332 assert(result_bnb->GetSelectedEffectiveValue() <= target + coin_params.m_cost_of_change);
334 if (result_bnb->GetAlgoCompleted()) {
335 // If BnB exhausted the search space, it must return an optimal solution (tied on waste score)
336 assert(best_waste == result_bnb->GetWaste());
337 }
338 }
339}
340
342 BNB,
343 SRD,
344 KNAPSACK,
345};
346
347template<CoinSelectionAlgorithm Algorithm>
348void FuzzCoinSelectionAlgorithm(std::span<const uint8_t> buffer) {
350 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
351 std::vector<COutput> utxo_pool;
352
355 // Discard feerate must be at least dust relay feerate
359
363 coin_params.m_long_term_feerate = long_term_fee_rate;
364 coin_params.m_effective_feerate = effective_fee_rate;
366 coin_params.m_change_fee = effective_fee_rate.GetFee(coin_params.change_output_size);
367 coin_params.m_discard_feerate = discard_fee_rate;
368 coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(41, 1000);
369 const auto change_spend_fee{coin_params.m_discard_feerate.GetFee(coin_params.change_spend_size)};
370 coin_params.m_cost_of_change = coin_params.m_change_fee + change_spend_fee;
371 CScript change_out_script = CScript() << std::vector<unsigned char>(coin_params.change_output_size, OP_TRUE);
372 const auto dust{GetDustThreshold(CTxOut{/*nValueIn=*/0, change_out_script}, coin_params.m_discard_feerate)};
373 coin_params.min_viable_change = std::max(change_spend_fee + 1, dust);
374
375 int next_locktime{0};
377
378 std::vector<OutputGroup> group_pos;
380
381 int max_selection_weight = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max());
382
383 std::optional<SelectionResult> result;
384
385 if constexpr (Algorithm == CoinSelectionAlgorithm::BNB) {
386 if (!coin_params.m_subtract_fee_outputs) {
388 if (result_bnb) {
389 result = *result_bnb;
390 assert(result_bnb->GetChange(coin_params.min_viable_change, coin_params.m_change_fee) == 0);
391 assert(result_bnb->GetSelectedValue() >= target);
392 assert(result_bnb->GetWeight() <= max_selection_weight);
393 (void)result_bnb->GetShuffledInputVector();
394 (void)result_bnb->GetInputSet();
395 }
396 }
397 }
398
399 if constexpr (Algorithm == CoinSelectionAlgorithm::SRD) {
401 if (result_srd) {
402 result = *result_srd;
403 assert(result_srd->GetSelectedValue() >= target);
404 assert(result_srd->GetChange(CHANGE_LOWER, coin_params.m_change_fee) > 0);
405 assert(result_srd->GetWeight() <= max_selection_weight);
406 result_srd->SetBumpFeeDiscount(ConsumeMoney(fuzzed_data_provider));
407 result_srd->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
408 (void)result_srd->GetShuffledInputVector();
409 (void)result_srd->GetInputSet();
410 }
411 }
412
414 std::vector<OutputGroup> group_all;
416
417 for (const OutputGroup& group : group_all) {
419 (void)group.EligibleForSpending(filter);
420 }
421
424 // If the total balance is sufficient for the target and we are not using
425 // effective values, Knapsack should always find a solution (unless the selection exceeded the max tx weight).
428 }
429 if (result_knapsack) {
430 result = *result_knapsack;
431 assert(result_knapsack->GetSelectedValue() >= target);
434 result_knapsack->RecalculateWaste(coin_params.min_viable_change, coin_params.m_cost_of_change, coin_params.m_change_fee);
435 (void)result_knapsack->GetShuffledInputVector();
436 (void)result_knapsack->GetInputSet();
437 }
438 }
439
440 std::vector<COutput> utxos;
442 if (new_total_balance > 0) {
444 for (const auto& utxo : utxos) {
445 new_utxo_pool.insert(std::make_shared<COutput>(utxo));
446 }
447 if (result) {
448 const auto weight{result->GetWeight()};
449 result->AddInputs(new_utxo_pool, subtract_fee_outputs);
450 assert(result->GetWeight() > weight);
451 }
452 }
453
454 std::vector<COutput> manual_inputs;
456 if (manual_balance == 0) return;
458 if (result) {
459 const CAmount old_target{result->GetTarget()};
460 const OutputSet input_set{result->GetInputSet()};
461 const int old_weight{result->GetWeight()};
462 result->Merge(manual_selection);
463 assert(result->GetInputSet().size() == input_set.size() + manual_inputs.size());
464 assert(result->GetTarget() == old_target + manual_selection.GetTarget());
465 assert(result->GetWeight() == old_weight + manual_selection.GetWeight());
466 }
467}
468
472
476
480
481} // namespace wallet
constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition amount.h:26
int64_t CAmount
Amount in satoshis (Can be negative)
Definition amount.h:12
constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition amount.h:15
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition feerate.h:32
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:30
Serialized script, used inside transaction inputs and outputs.
Definition script.h:406
An output of a transaction.
Fast randomness source.
Definition random.h:386
T ConsumeIntegralInRange(T min, T max)
#define FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
bilingual_str ErrorString(const Result< T > &result)
Definition result.h:93
static CAmount CreateCoins(FuzzedDataProvider &fuzzed_data_provider, std::vector< COutput > &utxo_pool, CoinSelectionParams &coin_params, int &next_locktime)
static void AddCoin(const CAmount &value, int n_input, int n_input_bytes, int locktime, std::vector< COutput > &coins, CFeeRate fee_rate)
constexpr CAmount CHANGE_LOWER
lower bound for randomly-chosen target change amount
util::Result< SelectionResult > SelectCoinsBnB(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, const CAmount &cost_of_change, int max_selection_weight)
util::Result< SelectionResult > CoinGrinder(std::vector< OutputGroup > &utxo_pool, const CAmount &selection_target, CAmount change_target, int max_selection_weight)
std::vector< OutputGroup > & GroupCoins(const std::vector< COutput > &available_coins, bool subtract_fee_outputs=false)
void FuzzCoinSelectionAlgorithm(std::span< const uint8_t > buffer)
std::set< std::shared_ptr< COutput >, OutputPtrComparator > OutputSet
util::Result< SelectionResult > KnapsackSolver(std::vector< OutputGroup > &groups, const CAmount &nTargetValue, CAmount change_target, FastRandomContext &rng, int max_selection_weight)
static SelectionResult ManualSelection(std::vector< COutput > &utxos, const CAmount &total_amount, const bool &subtract_fee_outputs)
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 bool HasErrorMsg(const util::Result< SelectionResult > &res)
Definition spend.cpp:697
util::Result< SelectionResult > SelectCoinsSRD(const std::vector< OutputGroup > &utxo_pool, CAmount target_value, CAmount change_fee, FastRandomContext &rng, int max_selection_weight)
Select coins by Single Random Draw (SRD).
CAmount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition policy.cpp:27
constexpr unsigned int DUST_RELAY_TX_FEE
Min feerate for defining dust.
Definition policy.h:68
constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
Definition policy.h:38
constexpr int MAX_SCRIPT_SIZE
Definition script.h:41
@ OP_TRUE
Definition script.h:85
A mutable version of CTransaction.
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
bool empty() const
Definition translation.h:35
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.
A group of UTXOs paid to the same output script.
void AddInputs(const OutputSet &inputs, bool subtract_fee_outputs)
CAmount GetTarget() const
SeedRandomStateForTest(SeedRand::ZEROS)
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition util.cpp:29
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition util.h:195
@ ZEROS
Seed with a compile time constant of zeros.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:46