Skip to content

Commit

Permalink
fuzz: Test optimality of CoinGrinder
Browse files Browse the repository at this point in the history
  • Loading branch information
murchandamus committed Jan 12, 2024
1 parent e1d5f43 commit 22137e2
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/wallet/test/fuzz/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,91 @@ FUZZ_TARGET(coin_grinder)
}
}

FUZZ_TARGET(coin_grinder_is_optimal)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
std::vector<COutput> utxo_pool;

const CAmount target{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};

FastRandomContext fast_random_context{ConsumeUInt256(fuzzed_data_provider)};
CoinSelectionParams coin_params{fast_random_context};
coin_params.m_subtract_fee_outputs = fuzzed_data_provider.ConsumeBool();
coin_params.m_long_term_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
coin_params.m_effective_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
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);
coin_params.m_change_fee = coin_params.m_effective_feerate.GetFee(coin_params.change_output_size);

// Create some coins
CAmount total_balance{0};
CAmount max_spendable{0};
int next_locktime{0};
unsigned max_utxos = 3;
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), max_utxos)
{
const CAmount amount{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
const int n_input_bytes{fuzzed_data_provider.ConsumeIntegralInRange<int>(41, 10000)};
if (total_balance + amount >= MAX_MONEY) {
break;
}
AddCoin(amount, /*n_input=*/0, n_input_bytes, ++next_locktime, utxo_pool, coin_params.m_effective_feerate);
total_balance += amount;
CAmount eff_value = amount - coin_params.m_effective_feerate.GetFee(n_input_bytes);
max_spendable += eff_value;
}
size_t num_utxos = utxo_pool.size();
assert(num_utxos <= max_utxos);

std::vector<OutputGroup> group_pos;
GroupCoins(fuzzed_data_provider, utxo_pool, coin_params, /*positive_only=*/true, group_pos);

// Run coinselection algorithms
CAmount change_target{CHANGE_LOWER + coin_params.m_change_fee}; // In order to ensure that it’s comparable to SRD, we must use the same change_target
auto result_cg = CoinGrinder(group_pos, target, change_target, MAX_STANDARD_TX_WEIGHT);
if (target + change_target > max_spendable || HasErrorMsg(result_cg)) return; // We only need to compare algorithms if CoinGrinder has a solution
assert(result_cg);
if (!result_cg->GetAlgoCompleted()) return; // Bail out if CoinGrinder solution is not optimal

// Brute force optimal solution
{
unsigned i = 0;
std::cout << "Target: " << target << ", change_target: " << change_target << std::endl;
std::cout << "UTXOs: " << std::endl;

for (auto u : utxo_pool) {
std::cout << " "<< i << "-- amount: " << u.txout.nValue << ", fee: " << u.GetFee() << ", effective_amount: " << u.GetEffectiveValue() << ", bytes: " << u.input_bytes << std::endl;
++i;
}
}
CAmount best_amount{MAX_MONEY};
int best_bytes{std::numeric_limits<int>::max()};
for (uint32_t pattern = 1; (pattern >> num_utxos) == 0; ++pattern) {
CAmount subset_amount{0};
int subset_bytes{0};
for (unsigned i = 0; i < num_utxos; ++i) {
if ((pattern >> i) & 1) {
subset_amount += utxo_pool[i].GetEffectiveValue();
subset_bytes += utxo_pool[i].input_bytes;
}
}
if (subset_amount >= target + change_target && (subset_bytes < best_bytes || (subset_bytes == best_bytes && subset_amount < best_amount))) {
best_bytes = subset_bytes;
best_amount = subset_amount;
std::cout << "NEW BEST: best_weight: " << 4*best_bytes << ", best_amount: " << best_amount << std::endl;
}
}

std::cout << "best_weight: " << 4*best_bytes;
std::cout << ", best_amount: " << best_amount;
std::cout << ", result_cg->GetWeight(): " << result_cg->GetWeight();
std::cout << ", result_cg->GetSelectionAmount(): " << result_cg->GetSelectedEffectiveValue() << std::endl;
assert(best_bytes * 4 == result_cg->GetWeight());
// assert(best_amount == result_cg->GetSelectionAmount());
}


FUZZ_TARGET(coinselection)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
Expand Down

0 comments on commit 22137e2

Please sign in to comment.