Repository: bitcoin/bitcoin
File: src/wallet/test/fuzz/fees.cpp (C++)

Code before:
#include <wallet/fees.h>
#include <wallet/test/util.h>
#include <wallet/wallet.h>

namespace wallet {
namespace {
TestingSetup* 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<TestingSetup>();
    g_setup = testing_setup.get();
}

FUZZ_TARGET(wallet_fees, .init = initialize_setup)
{
    SeedRandomStateForTest(SeedRand::ZEROS);
    FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
    SetMockTime(ConsumeTime(fuzzed_data_provider));
    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);
    node.fee_estimator = std::move(fee_estimator);
    std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};

Review comment (suggestion):
In "fuzz: mock CBlockPolicyEstimator in wallet_fuzz" ff10a37e99271125a9ece92bae571f7b78fb9e22

It would be nice if `FuzzedBlockPolicyEstimator` were renamed to `MockedBlockPolicyEstimator`.
It could then be moved to `src/test/util/`  so that it can be reused by other components that might need a fee rate estimate as well.

Code after:
#include <wallet/coincontrol.h>
#include <wallet/fees.h>
#include <wallet/test/util.h>
#include <wallet/wallet.h>

namespace wallet {
namespace {

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<FeeEstimatorTestingSetup>();
    g_setup = testing_setup.get();
}

FUZZ_TARGET(wallet_fees, .init = initialize_setup)