Merge bitcoin/bitcoin#33210: fuzz: enhance wallet_fees by mocking mempool stuff

5ded99a7f0 fuzz: MockMempoolMinFee in wallet_fees (brunoerg)
c9a7a198d9 test: move MockMempoolMinFee to util/txmempool (brunoerg)
adf67eb21b fuzz: create FeeEstimatorTestingSetup to set fee_estimator (brunoerg)
ff10a37e99 fuzz: mock CBlockPolicyEstimator in wallet_fuzz (brunoerg)
f591c3beca fees: make estimateSmartFee/HighestTargetTracked virtual for mocking (brunoerg)
19273d0705 fuzz: set mempool options in wallet_fees (brunoerg)

Pull request description:

  Some functions in `wallet/fees.cpp` (fuzzed by the wallet_fees target) depends on some mempool stuff - e.g. relay current min fee, smart fee and max blocks estimation, relay dust fee and other ones. For better fuzzing of it, it would be great to have these values/interactions. That said, this PR enhances the `wallet_fees` target by:

  - Setting mempool options - `min_relay_feerate`,  `dust_relay_feerate` and `incremental_relay_feerate` - when creating the `CTxMemPool`.
  - Creates a `ConsumeMempoolMinFee` function which is used to have a mempool min fee (similar approach from `MockMempoolMinFee` from unit test).
  - Mock `CBlockPolicyEstimator` - estimateSmartFee/HighestTagretTracket functions, especifically. It's better to mock it then trying to interact to CBlockPolicyEstimator in order to have some effective values due to performance.

  Note that I created `FeeEstimatorTestingSetup` because we cannot set `m_node.fee_estimator` in `ChainTestingSetup` since fae8c73d9e.

ACKs for top commit:
  maflcko:
    re-ACK 5ded99a7f0 🎯
  ismaelsadeeq:
    Code review ACK 5ded99a7f0

Tree-SHA512: 13d2af042098afd237ef349437021ea841069d93d4c3e3a32e1b562c027d00c727f375426709d34421092993398caf7ba8ff19077982cb6f470f8938a44e7754
This commit is contained in:
Ava Chow
2025-10-24 11:43:42 -07:00
7 changed files with 108 additions and 53 deletions

View File

@@ -6,6 +6,7 @@
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <test/util/setup_common.h>
#include <test/util/txmempool.h>
#include <validation.h>
#include <wallet/coincontrol.h>
#include <wallet/fees.h>
@@ -14,11 +15,46 @@
namespace wallet {
namespace {
const TestingSetup* g_setup;
struct FeeEstimatorTestingSetup : public TestingSetup {
FeeEstimatorTestingSetup(const ChainType chain_type, TestOpts opts) : TestingSetup{chain_type, opts}
{
}
~FeeEstimatorTestingSetup() {
m_node.fee_estimator.reset();
}
void SetFeeEstimator(std::unique_ptr<CBlockPolicyEstimator> fee_estimator)
{
m_node.fee_estimator = std::move(fee_estimator);
}
};
FeeEstimatorTestingSetup* g_setup;
class FuzzedBlockPolicyEstimator : public CBlockPolicyEstimator
{
FuzzedDataProvider& fuzzed_data_provider;
public:
FuzzedBlockPolicyEstimator(FuzzedDataProvider& provider)
: CBlockPolicyEstimator(fs::path{}, false), fuzzed_data_provider(provider) {}
CFeeRate estimateSmartFee(int confTarget, FeeCalculation* feeCalc, bool conservative) const override
{
return CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000)};
}
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const override
{
return fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1000);
}
};
void initialize_setup()
{
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
static const auto testing_setup = MakeNoLogFileContext<FeeEstimatorTestingSetup>();
g_setup = testing_setup.get();
}
@@ -27,8 +63,23 @@ FUZZ_TARGET(wallet_fees, .init = initialize_setup)
SeedRandomStateForTest(SeedRand::ZEROS);
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
SetMockTime(ConsumeTime(fuzzed_data_provider));
const auto& node{g_setup->m_node};
auto& node{g_setup->m_node};
Chainstate* chainstate = &node.chainman->ActiveChainstate();
bilingual_str error;
CTxMemPool::Options mempool_opts{
.incremental_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)},
.min_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)},
.dust_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)}
};
node.mempool = std::make_unique<CTxMemPool>(mempool_opts, error);
std::unique_ptr<CBlockPolicyEstimator> fee_estimator = std::make_unique<FuzzedBlockPolicyEstimator>(fuzzed_data_provider);
g_setup->SetFeeEstimator(std::move(fee_estimator));
auto target_feerate{CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000)}};
if (target_feerate > node.mempool->m_opts.incremental_relay_feerate &&
target_feerate > node.mempool->m_opts.min_relay_feerate) {
MockMempoolMinFee(target_feerate, *node.mempool);
}
std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
CWallet& wallet{*wallet_ptr};
{