Skip to content

Commit 74d6635

Browse files
committed
Merge bitcoin/bitcoin#27585: fuzz: improve coinselection
bf26f97 fuzz: coinselection, fix `m_cost_of_change` (brunoerg) 6d9b26d fuzz: coinselection, BnB should never produce change (brunoerg) b2eb558 fuzz: coinselection, compare `GetSelectedValue` with target (brunoerg) 0df0438 fuzz: coinselection, improve `ComputeAndSetWaste` (brunoerg) 1e351e5 fuzz: coinselection, add coverage for `Merge` (brunoerg) f0244a8 fuzz: coinselection, add coverage for `GetShuffledInputVector`/`GetInputSet` (brunoerg) 808618b fuzz: coinselection, add coverage for `AddInputs` (brunoerg) 90c4e6a fuzz: coinselection, add coverage for `EligibleForSpending` (brunoerg) 2a031cb fuzz: coinselection, add `CreateCoins` (brunoerg) Pull request description: This PR: - Moves coin creation to its own function called `CreateCoins`. - Add coverage for `EligibleForSpending` - Add coverage for `AddInputs`: get result of each algorithm (srd, knapsack and bnb), call `CreateCoins` and add into them. - Add coverage for `GetShuffledInputVector` and `GetInputSet` using the result of each algorithm (srd, knapsack and bnb). - Add coverage for `Merge`: Call SRD with the new utxos and, if successful, try to merge with the previous SRD result. ACKs for top commit: murchandamus: reACK with some minimal fuzzing bf26f97 achow101: ACK bf26f97 furszy: re-ACK bf26f97 Tree-SHA512: bdd2b0a39de37be0a9b21a7c51260b6b8abe538cc0ea74312eb658b90a121a1ae07306c09fb0e75e93b531ce9ea2402feb041b0d852902d07739257f792e64ab
2 parents 1fa6411 + bf26f97 commit 74d6635

File tree

1 file changed

+90
-17
lines changed

1 file changed

+90
-17
lines changed

src/wallet/test/fuzz/coinselection.cpp

Lines changed: 90 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ static void GroupCoins(FuzzedDataProvider& fuzzed_data_provider, const std::vect
4545
if (valid_outputgroup) output_groups.push_back(output_group);
4646
}
4747

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+
4877
// Returns true if the result contains an error and the message is not empty
4978
static bool HasErrorMsg(const util::Result<SelectionResult>& res) { return !util::ErrorString(res).empty(); }
5079

@@ -55,7 +84,9 @@ FUZZ_TARGET(coinselection)
5584

5685
const CFeeRate long_term_fee_rate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
5786
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)};
5990
const CAmount target{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
6091
const bool subtract_fee_outputs{fuzzed_data_provider.ConsumeBool()};
6192

@@ -64,47 +95,89 @@ FUZZ_TARGET(coinselection)
6495
coin_params.m_subtract_fee_outputs = subtract_fee_outputs;
6596
coin_params.m_long_term_feerate = long_term_fee_rate;
6697
coin_params.m_effective_feerate = effective_fee_rate;
98+
coin_params.min_viable_change = min_viable_change;
6799
coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
68100
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);
69104

70-
// Create some coins
71-
CAmount total_balance{0};
72105
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)};
84107

85108
std::vector<OutputGroup> group_pos;
86109
GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/true, group_pos);
87110
std::vector<OutputGroup> group_all;
88111
GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/false, group_all);
89112

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+
90118
// 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+
}
92126

93127
auto result_srd = SelectCoinsSRD(group_pos, target, coin_params.m_change_fee, fast_random_context, MAX_STANDARD_TX_WEIGHT);
94128
if (result_srd) {
129+
assert(result_srd->GetSelectedValue() >= target);
95130
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();
97134
}
98135

99136
CAmount change_target{GenerateChangeTarget(target, coin_params.m_change_fee, fast_random_context)};
100137
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+
}
102144

103145
// If the total balance is sufficient for the target and we are not using
104146
// effective values, Knapsack should always find a solution (unless the selection exceeded the max tx weight).
105147
if (total_balance >= target && subtract_fee_outputs && !HasErrorMsg(result_knapsack)) {
106148
assert(result_knapsack);
107149
}
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+
}
108181
}
109182

110183
} // namespace wallet

0 commit comments

Comments
 (0)