Skip to content

Commit 67df6c6

Browse files
committed
fuzz: Add CoinGrinder fuzz target
1 parent 1502231 commit 67df6c6

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/wallet/test/fuzz/coinselection.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,63 @@ static SelectionResult ManualSelection(std::vector<COutput>& utxos, const CAmoun
7777
// Returns true if the result contains an error and the message is not empty
7878
static bool HasErrorMsg(const util::Result<SelectionResult>& res) { return !util::ErrorString(res).empty(); }
7979

80+
FUZZ_TARGET(coin_grinder)
81+
{
82+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
83+
std::vector<COutput> utxo_pool;
84+
85+
const CAmount target{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
86+
87+
FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
88+
CoinSelectionParams coin_params{fast_random_context};
89+
coin_params.m_subtract_fee_outputs = fuzzed_data_provider.ConsumeBool();
90+
coin_params.m_long_term_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
91+
coin_params.m_effective_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
92+
coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
93+
coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
94+
coin_params.m_cost_of_change= coin_params.m_effective_feerate.GetFee(coin_params.change_output_size) + coin_params.m_long_term_feerate.GetFee(coin_params.change_spend_size);
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
100+
CAmount total_balance{0};
101+
CAmount max_spendable{0};
102+
int next_locktime{0};
103+
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
104+
{
105+
const int n_input{fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 10)};
106+
const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange<int>(41, 10000)};
107+
const CAmount amount{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
108+
if (total_balance + amount >= MAX_MONEY) {
109+
break;
110+
}
111+
AddCoin(amount, n_input, n_input_bytes, ++next_locktime, utxo_pool, coin_params.m_effective_feerate);
112+
total_balance += amount;
113+
CAmount eff_value = amount - coin_params.m_effective_feerate.GetFee(n_input_bytes);
114+
max_spendable += eff_value;
115+
}
116+
117+
std::vector<OutputGroup> group_pos;
118+
GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/true, group_pos);
119+
120+
// Run coinselection algorithms
121+
auto result_cg = CoinGrinder(group_pos, target, coin_params.m_min_change_target, MAX_STANDARD_TX_WEIGHT);
122+
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+
assert(result_cg);
124+
if (!result_cg->GetAlgoCompleted()) return; // Bail out if CoinGrinder solution is not optimal
125+
126+
auto result_srd = SelectCoinsSRD(group_pos, target, coin_params.m_change_fee, fast_random_context, MAX_STANDARD_TX_WEIGHT);
127+
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
128+
assert(result_srd->GetWeight() >= result_cg->GetWeight());
129+
}
130+
131+
auto result_knapsack = KnapsackSolver(group_pos, target, coin_params.m_min_change_target, fast_random_context, MAX_STANDARD_TX_WEIGHT);
132+
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
133+
assert(result_knapsack->GetWeight() >= result_cg->GetWeight());
134+
}
135+
}
136+
80137
FUZZ_TARGET(coinselection)
81138
{
82139
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};

0 commit comments

Comments
 (0)