@@ -45,6 +45,35 @@ static void GroupCoins(FuzzedDataProvider& fuzzed_data_provider, const std::vect
45
45
if (valid_outputgroup) output_groups.push_back (output_group);
46
46
}
47
47
48
+ static CAmount CreateCoins (FuzzedDataProvider& fuzzed_data_provider, std::vector<COutput>& utxo_pool, CoinSelectionParams& coin_params, int & next_locktime)
49
+ {
50
+ CAmount total_balance{0 };
51
+ LIMITED_WHILE (fuzzed_data_provider.ConsumeBool (), 10000 )
52
+ {
53
+ const int n_input{fuzzed_data_provider.ConsumeIntegralInRange <int >(0 , 10 )};
54
+ const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange <int >(41 , 10000 )};
55
+ const CAmount amount{fuzzed_data_provider.ConsumeIntegralInRange <CAmount>(1 , MAX_MONEY)};
56
+ if (total_balance + amount >= MAX_MONEY) {
57
+ break ;
58
+ }
59
+ AddCoin (amount, n_input, n_input_bytes, ++next_locktime, utxo_pool, coin_params.m_effective_feerate );
60
+ total_balance += amount;
61
+ }
62
+
63
+ return total_balance;
64
+ }
65
+
66
+ static SelectionResult ManualSelection (std::vector<COutput>& utxos, const CAmount& total_amount, const bool & subtract_fee_outputs)
67
+ {
68
+ SelectionResult result (total_amount, SelectionAlgorithm::MANUAL);
69
+ std::set<std::shared_ptr<COutput>> utxo_pool;
70
+ for (const auto & utxo : utxos) {
71
+ utxo_pool.insert (std::make_shared<COutput>(utxo));
72
+ }
73
+ result.AddInputs (utxo_pool, subtract_fee_outputs);
74
+ return result;
75
+ }
76
+
48
77
// Returns true if the result contains an error and the message is not empty
49
78
static bool HasErrorMsg (const util::Result<SelectionResult>& res) { return !util::ErrorString (res).empty (); }
50
79
@@ -55,7 +84,9 @@ FUZZ_TARGET(coinselection)
55
84
56
85
const CFeeRate long_term_fee_rate{ConsumeMoney (fuzzed_data_provider, /* max=*/ COIN)};
57
86
const CFeeRate effective_fee_rate{ConsumeMoney (fuzzed_data_provider, /* max=*/ COIN)};
58
- const CAmount cost_of_change{ConsumeMoney (fuzzed_data_provider, /* max=*/ COIN)};
87
+ // Discard feerate must be at least dust relay feerate
88
+ const CFeeRate discard_fee_rate{fuzzed_data_provider.ConsumeIntegralInRange <CAmount>(DUST_RELAY_TX_FEE, COIN)};
89
+ const CAmount min_viable_change{ConsumeMoney (fuzzed_data_provider, /* max=*/ COIN)};
59
90
const CAmount target{fuzzed_data_provider.ConsumeIntegralInRange <CAmount>(1 , MAX_MONEY)};
60
91
const bool subtract_fee_outputs{fuzzed_data_provider.ConsumeBool ()};
61
92
@@ -64,47 +95,89 @@ FUZZ_TARGET(coinselection)
64
95
coin_params.m_subtract_fee_outputs = subtract_fee_outputs;
65
96
coin_params.m_long_term_feerate = long_term_fee_rate;
66
97
coin_params.m_effective_feerate = effective_fee_rate;
98
+ coin_params.min_viable_change = min_viable_change;
67
99
coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange <int >(10 , 1000 );
68
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 );
69
104
70
- // Create some coins
71
- CAmount total_balance{0 };
72
105
int next_locktime{0 };
73
- LIMITED_WHILE (fuzzed_data_provider.ConsumeBool (), 10000 )
74
- {
75
- const int n_input{fuzzed_data_provider.ConsumeIntegralInRange <int >(0 , 10 )};
76
- const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange <int >(100 , 10000 )};
77
- const CAmount amount{fuzzed_data_provider.ConsumeIntegralInRange <CAmount>(1 , MAX_MONEY)};
78
- if (total_balance + amount >= MAX_MONEY) {
79
- break ;
80
- }
81
- AddCoin (amount, n_input, n_input_bytes, ++next_locktime, utxo_pool, coin_params.m_effective_feerate );
82
- total_balance += amount;
83
- }
106
+ CAmount total_balance{CreateCoins (fuzzed_data_provider, utxo_pool, coin_params, next_locktime)};
84
107
85
108
std::vector<OutputGroup> group_pos;
86
109
GroupCoins (fuzzed_data_provider, utxo_pool, coin_params, /* positive_only=*/ true , group_pos);
87
110
std::vector<OutputGroup> group_all;
88
111
GroupCoins (fuzzed_data_provider, utxo_pool, coin_params, /* positive_only=*/ false , group_all);
89
112
113
+ for (const OutputGroup& group : 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);
116
+ }
117
+
90
118
// Run coinselection algorithms
91
- const auto result_bnb = SelectCoinsBnB (group_pos, target, cost_of_change, MAX_STANDARD_TX_WEIGHT);
119
+ auto result_bnb = SelectCoinsBnB (group_pos, target, coin_params.m_cost_of_change , MAX_STANDARD_TX_WEIGHT);
120
+ if (result_bnb) {
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 ();
125
+ }
92
126
93
127
auto result_srd = SelectCoinsSRD (group_pos, target, coin_params.m_change_fee , fast_random_context, MAX_STANDARD_TX_WEIGHT);
94
128
if (result_srd) {
129
+ assert (result_srd->GetSelectedValue () >= target);
95
130
assert (result_srd->GetChange (CHANGE_LOWER, coin_params.m_change_fee ) > 0 ); // Demonstrate that SRD creates change of at least CHANGE_LOWER
96
- result_srd->ComputeAndSetWaste (cost_of_change, cost_of_change, 0 );
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 ();
97
134
}
98
135
99
136
CAmount change_target{GenerateChangeTarget (target, coin_params.m_change_fee , fast_random_context)};
100
137
auto result_knapsack = KnapsackSolver (group_all, target, change_target, fast_random_context, MAX_STANDARD_TX_WEIGHT);
101
- if (result_knapsack) result_knapsack->ComputeAndSetWaste (cost_of_change, cost_of_change, 0 );
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 ();
143
+ }
102
144
103
145
// If the total balance is sufficient for the target and we are not using
104
146
// effective values, Knapsack should always find a solution (unless the selection exceeded the max tx weight).
105
147
if (total_balance >= target && subtract_fee_outputs && !HasErrorMsg (result_knapsack)) {
106
148
assert (result_knapsack);
107
149
}
150
+
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));
158
+ }
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);
164
+ }
165
+ }
166
+
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 ;
173
+ const CAmount old_target{result->GetTarget ()};
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 ());
180
+ }
108
181
}
109
182
110
183
} // namespace wallet
0 commit comments