Skip to content

Commit

Permalink
fuzz: coinselection, improve min_viable_change
Browse files Browse the repository at this point in the history
Instead of fuzzing it, use same approach from
`CreateTransactionInternal`.
  • Loading branch information
brunoerg committed Aug 29, 2023
1 parent ab42b2e commit 24d2ca5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/wallet/test/fuzz/coinselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ static SelectionResult ManualSelection(std::vector<COutput>& utxos, const CAmoun
return result;
}

static CAmount GetMinViableChange(FuzzedDataProvider& fuzzed_data_provider, const CFeeRate& m_discard_feerate, const CAmount& change_spend_fee)
{
const CTxDestination tx_destination{ConsumeTxDestination(fuzzed_data_provider)};
CScript script_change{GetScriptForDestination(tx_destination)};
CTxOut change_prototype_txout(0, script_change);
const auto dust{GetDustThreshold(change_prototype_txout, m_discard_feerate)};
return std::max(change_spend_fee + 1, dust);
}

// Returns true if the result contains an error and the message is not empty
static bool HasErrorMsg(const util::Result<SelectionResult>& res) { return !util::ErrorString(res).empty(); }

Expand All @@ -86,7 +95,6 @@ FUZZ_TARGET(coinselection)
const CFeeRate effective_fee_rate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
// Discard feerate must be at least dust relay feerate
const CFeeRate discard_fee_rate{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(DUST_RELAY_TX_FEE, COIN)};
const CAmount min_viable_change{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
const CAmount target{fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(1, MAX_MONEY)};
const bool subtract_fee_outputs{fuzzed_data_provider.ConsumeBool()};

Expand All @@ -95,12 +103,13 @@ FUZZ_TARGET(coinselection)
coin_params.m_subtract_fee_outputs = subtract_fee_outputs;
coin_params.m_long_term_feerate = long_term_fee_rate;
coin_params.m_effective_feerate = effective_fee_rate;
coin_params.min_viable_change = min_viable_change;
coin_params.change_output_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(10, 1000);
coin_params.m_change_fee = effective_fee_rate.GetFee(coin_params.change_output_size);
coin_params.m_discard_feerate = discard_fee_rate;
coin_params.change_spend_size = fuzzed_data_provider.ConsumeIntegralInRange<int>(41, 1000);
coin_params.m_cost_of_change = coin_params.m_change_fee + coin_params.m_discard_feerate.GetFee(coin_params.change_spend_size);
const auto change_spend_fee{coin_params.m_discard_feerate.GetFee(coin_params.change_spend_size)};
coin_params.m_cost_of_change = coin_params.m_change_fee + change_spend_fee;
coin_params.min_viable_change = GetMinViableChange(fuzzed_data_provider, discard_fee_rate, change_spend_fee);

int next_locktime{0};
CAmount total_balance{CreateCoins(fuzzed_data_provider, utxo_pool, coin_params, next_locktime)};
Expand Down

0 comments on commit 24d2ca5

Please sign in to comment.