From 19273d0705fcd2fbde686bc3b5b2375f691e303d Mon Sep 17 00:00:00 2001 From: brunoerg Date: Mon, 11 Aug 2025 14:38:14 -0300 Subject: [PATCH 1/6] fuzz: set mempool options in wallet_fees --- src/wallet/test/fuzz/fees.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/wallet/test/fuzz/fees.cpp b/src/wallet/test/fuzz/fees.cpp index 80458ac89cd..e98dbe02b7e 100644 --- a/src/wallet/test/fuzz/fees.cpp +++ b/src/wallet/test/fuzz/fees.cpp @@ -14,11 +14,11 @@ namespace wallet { namespace { -const TestingSetup* g_setup; +TestingSetup* g_setup; void initialize_setup() { - static const auto testing_setup = MakeNoLogFileContext(); + static const auto testing_setup = MakeNoLogFileContext(); g_setup = testing_setup.get(); } @@ -27,8 +27,16 @@ 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(mempool_opts, error); std::unique_ptr wallet_ptr{std::make_unique(node.chain.get(), "", CreateMockableWalletDatabase())}; CWallet& wallet{*wallet_ptr}; { From f591c3becafcdd7c81722c647865a1f908b6469a Mon Sep 17 00:00:00 2001 From: brunoerg Date: Thu, 14 Aug 2025 16:13:18 -0300 Subject: [PATCH 2/6] fees: make estimateSmartFee/HighestTargetTracked virtual for mocking --- src/policy/fees.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/policy/fees.h b/src/policy/fees.h index b355b65a346..e5820508cc5 100644 --- a/src/policy/fees.h +++ b/src/policy/fees.h @@ -224,7 +224,7 @@ public: * the closest target where one can be given. 'conservative' estimates are * valid over longer time horizons also. */ - CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const + virtual CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator); /** Return a specific fee estimate calculation with a given success @@ -248,7 +248,7 @@ public: EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator); /** Calculation of highest target that estimates are tracked for */ - unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const + virtual unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator); /** Drop still unconfirmed transactions and record current estimations, if the fee estimation file is present. */ From ff10a37e99271125a9ece92bae571f7b78fb9e22 Mon Sep 17 00:00:00 2001 From: brunoerg Date: Thu, 14 Aug 2025 16:30:02 -0300 Subject: [PATCH 3/6] fuzz: mock CBlockPolicyEstimator in wallet_fuzz --- src/wallet/test/fuzz/fees.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/wallet/test/fuzz/fees.cpp b/src/wallet/test/fuzz/fees.cpp index e98dbe02b7e..515c3b19078 100644 --- a/src/wallet/test/fuzz/fees.cpp +++ b/src/wallet/test/fuzz/fees.cpp @@ -16,6 +16,25 @@ 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(1, 1000); + } +}; + void initialize_setup() { static const auto testing_setup = MakeNoLogFileContext(); @@ -37,6 +56,8 @@ FUZZ_TARGET(wallet_fees, .init = initialize_setup) .dust_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)} }; node.mempool = std::make_unique(mempool_opts, error); + std::unique_ptr fee_estimator = std::make_unique(fuzzed_data_provider); + node.fee_estimator = std::move(fee_estimator); std::unique_ptr wallet_ptr{std::make_unique(node.chain.get(), "", CreateMockableWalletDatabase())}; CWallet& wallet{*wallet_ptr}; { From adf67eb21baf39a222b65480e45ae76f093e8f66 Mon Sep 17 00:00:00 2001 From: brunoerg Date: Fri, 15 Aug 2025 10:13:47 -0300 Subject: [PATCH 4/6] fuzz: create FeeEstimatorTestingSetup to set fee_estimator --- src/wallet/test/fuzz/fees.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/wallet/test/fuzz/fees.cpp b/src/wallet/test/fuzz/fees.cpp index 515c3b19078..9cf7e89deb3 100644 --- a/src/wallet/test/fuzz/fees.cpp +++ b/src/wallet/test/fuzz/fees.cpp @@ -14,7 +14,23 @@ namespace wallet { namespace { -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 fee_estimator) + { + m_node.fee_estimator = std::move(fee_estimator); + } +}; + +FeeEstimatorTestingSetup* g_setup; class FuzzedBlockPolicyEstimator : public CBlockPolicyEstimator { @@ -37,7 +53,7 @@ public: void initialize_setup() { - static const auto testing_setup = MakeNoLogFileContext(); + static const auto testing_setup = MakeNoLogFileContext(); g_setup = testing_setup.get(); } @@ -57,7 +73,7 @@ FUZZ_TARGET(wallet_fees, .init = initialize_setup) }; node.mempool = std::make_unique(mempool_opts, error); std::unique_ptr fee_estimator = std::make_unique(fuzzed_data_provider); - node.fee_estimator = std::move(fee_estimator); + g_setup->SetFeeEstimator(std::move(fee_estimator)); std::unique_ptr wallet_ptr{std::make_unique(node.chain.get(), "", CreateMockableWalletDatabase())}; CWallet& wallet{*wallet_ptr}; { From c9a7a198d9e81e99de99a2aaff1687d13d6674e8 Mon Sep 17 00:00:00 2001 From: brunoerg Date: Fri, 29 Aug 2025 11:17:41 -0300 Subject: [PATCH 5/6] test: move MockMempoolMinFee to util/txmempool --- src/test/txpackage_tests.cpp | 6 +++--- src/test/util/setup_common.cpp | 34 -------------------------------- src/test/util/setup_common.h | 11 ----------- src/test/util/txmempool.cpp | 36 ++++++++++++++++++++++++++++++++++ src/test/util/txmempool.h | 13 ++++++++++++ 5 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/test/txpackage_tests.cpp b/src/test/txpackage_tests.cpp index 1f167586fe9..ded13c4cf78 100644 --- a/src/test/txpackage_tests.cpp +++ b/src/test/txpackage_tests.cpp @@ -357,7 +357,7 @@ BOOST_AUTO_TEST_CASE(package_submission_tests) { // Mine blocks to mature coinbases. mineBlocks(3); - MockMempoolMinFee(CFeeRate(5000)); + MockMempoolMinFee(CFeeRate(5000), *m_node.mempool); LOCK(cs_main); unsigned int expected_pool_size = m_node.mempool->size(); CKey parent_key = GenerateRandomKey(); @@ -634,7 +634,7 @@ BOOST_AUTO_TEST_CASE(package_witness_swap_tests) { // Mine blocks to mature coinbases. mineBlocks(5); - MockMempoolMinFee(CFeeRate(5000)); + MockMempoolMinFee(CFeeRate(5000), *m_node.mempool); LOCK(cs_main); // Transactions with a same-txid-different-witness transaction in the mempool should be ignored, @@ -867,7 +867,7 @@ BOOST_AUTO_TEST_CASE(package_witness_swap_tests) BOOST_AUTO_TEST_CASE(package_cpfp_tests) { mineBlocks(5); - MockMempoolMinFee(CFeeRate(5000)); + MockMempoolMinFee(CFeeRate(5000), *m_node.mempool); LOCK(::cs_main); size_t expected_pool_size = m_node.mempool->size(); CKey child_key = GenerateRandomKey(); diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 8ce0a0dbe83..baedcf0b8fc 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -571,40 +571,6 @@ std::vector TestChain100Setup::PopulateMempool(FastRandomContex return mempool_transactions; } -void TestChain100Setup::MockMempoolMinFee(const CFeeRate& target_feerate) -{ - LOCK2(cs_main, m_node.mempool->cs); - // Transactions in the mempool will affect the new minimum feerate. - assert(m_node.mempool->size() == 0); - // The target feerate cannot be too low... - // ...otherwise the transaction's feerate will need to be negative. - assert(target_feerate > m_node.mempool->m_opts.incremental_relay_feerate); - // ...otherwise this is not meaningful. The feerate policy uses the maximum of both feerates. - assert(target_feerate > m_node.mempool->m_opts.min_relay_feerate); - - // Manually create an invalid transaction. Manually set the fee in the CTxMemPoolEntry to - // achieve the exact target feerate. - CMutableTransaction mtx = CMutableTransaction(); - mtx.vin.emplace_back(COutPoint{Txid::FromUint256(m_rng.rand256()), 0}); - mtx.vout.emplace_back(1 * COIN, GetScriptForDestination(WitnessV0ScriptHash(CScript() << OP_TRUE))); - // Set a large size so that the fee evaluated at target_feerate (which is usually in sats/kvB) is an integer. - // Otherwise, GetMinFee() may end up slightly different from target_feerate. - BulkTransaction(mtx, 4000); - const auto tx{MakeTransactionRef(mtx)}; - LockPoints lp; - // The new mempool min feerate is equal to the removed package's feerate + incremental feerate. - const auto tx_fee = target_feerate.GetFee(GetVirtualTransactionSize(*tx)) - - m_node.mempool->m_opts.incremental_relay_feerate.GetFee(GetVirtualTransactionSize(*tx)); - { - auto changeset = m_node.mempool->GetChangeSet(); - changeset->StageAddition(tx, /*fee=*/tx_fee, - /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, - /*spends_coinbase=*/true, /*sigops_cost=*/1, lp); - changeset->Apply(); - } - m_node.mempool->TrimToSize(0); - assert(m_node.mempool->GetMinFee() == target_feerate); -} /** * @returns a real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af) * with 9 txs. diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index 150f50650ba..fdb0951eee3 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -238,17 +238,6 @@ struct TestChain100Setup : public TestingSetup { */ std::vector PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit); - /** Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0), - * simulating the mempool "reaching capacity" and evicting by descendant feerate. Note that - * this clears the mempool, and the new minimum feerate will depend on the maximum feerate of - * transactions removed, so this must be called while the mempool is empty. - * - * @param target_feerate The new mempool minimum feerate after this function returns. - * Must be above max(incremental feerate, min relay feerate), - * or 1sat/vB with default settings. - */ - void MockMempoolMinFee(const CFeeRate& target_feerate); - std::vector m_coinbase_txns; // For convenience, coinbase transactions CKey coinbaseKey; // private/public key needed to spend coinbase transactions }; diff --git a/src/test/util/txmempool.cpp b/src/test/util/txmempool.cpp index 5febb6791c6..441cdbb2256 100644 --- a/src/test/util/txmempool.cpp +++ b/src/test/util/txmempool.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -218,3 +219,38 @@ void AddToMempool(CTxMemPool& tx_pool, const CTxMemPoolEntry& entry) entry.GetSpendsCoinbase(), entry.GetSigOpCost(), entry.GetLockPoints()); changeset->Apply(); } + +void MockMempoolMinFee(const CFeeRate& target_feerate, CTxMemPool& mempool) +{ + LOCK2(cs_main, mempool.cs); + // Transactions in the mempool will affect the new minimum feerate. + assert(mempool.size() == 0); + // The target feerate cannot be too low... + // ...otherwise the transaction's feerate will need to be negative. + assert(target_feerate > mempool.m_opts.incremental_relay_feerate); + // ...otherwise this is not meaningful. The feerate policy uses the maximum of both feerates. + assert(target_feerate > mempool.m_opts.min_relay_feerate); + + // Manually create an invalid transaction. Manually set the fee in the CTxMemPoolEntry to + // achieve the exact target feerate. + CMutableTransaction mtx{}; + mtx.vin.emplace_back(COutPoint{Txid::FromUint256(uint256{123}), 0}); + mtx.vout.emplace_back(1 * COIN, GetScriptForDestination(WitnessV0ScriptHash(CScript() << OP_TRUE))); + // Set a large size so that the fee evaluated at target_feerate (which is usually in sats/kvB) is an integer. + // Otherwise, GetMinFee() may end up slightly different from target_feerate. + BulkTransaction(mtx, 4000); + const auto tx{MakeTransactionRef(mtx)}; + LockPoints lp; + // The new mempool min feerate is equal to the removed package's feerate + incremental feerate. + const auto tx_fee = target_feerate.GetFee(GetVirtualTransactionSize(*tx)) - + mempool.m_opts.incremental_relay_feerate.GetFee(GetVirtualTransactionSize(*tx)); + { + auto changeset = mempool.GetChangeSet(); + changeset->StageAddition(tx, /*fee=*/tx_fee, + /*time=*/0, /*entry_height=*/1, /*entry_sequence=*/0, + /*spends_coinbase=*/true, /*sigops_cost=*/1, lp); + changeset->Apply(); + } + mempool.TrimToSize(0); + assert(mempool.GetMinFee() == target_feerate); +} diff --git a/src/test/util/txmempool.h b/src/test/util/txmempool.h index 36caad2ae1a..731b46a3c7b 100644 --- a/src/test/util/txmempool.h +++ b/src/test/util/txmempool.h @@ -67,4 +67,17 @@ void CheckMempoolTRUCInvariants(const CTxMemPool& tx_pool); * and applying it. */ void AddToMempool(CTxMemPool& tx_pool, const CTxMemPoolEntry& entry); +/** Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0), + * simulating the mempool "reaching capacity" and evicting by descendant feerate. Note that + * this clears the mempool, and the new minimum feerate will depend on the maximum feerate of + * transactions removed, so this must be called while the mempool is empty. + * + * @param target_feerate The new mempool minimum feerate after this function returns. + * Must be above max(incremental feerate, min relay feerate), + * or 1sat/vB with default settings. + * @param mempool The mempool to mock the minimum feerate for. Must be empty + * when called. + */ +void MockMempoolMinFee(const CFeeRate& target_feerate, CTxMemPool& mempool); + #endif // BITCOIN_TEST_UTIL_TXMEMPOOL_H From 5ded99a7f007b142f6b0ec89e0c71ef281b42684 Mon Sep 17 00:00:00 2001 From: brunoerg Date: Fri, 29 Aug 2025 11:30:01 -0300 Subject: [PATCH 6/6] fuzz: MockMempoolMinFee in wallet_fees --- src/wallet/test/fuzz/fees.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wallet/test/fuzz/fees.cpp b/src/wallet/test/fuzz/fees.cpp index 9cf7e89deb3..6d9de0cc0fd 100644 --- a/src/wallet/test/fuzz/fees.cpp +++ b/src/wallet/test/fuzz/fees.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -74,6 +75,11 @@ FUZZ_TARGET(wallet_fees, .init = initialize_setup) node.mempool = std::make_unique(mempool_opts, error); std::unique_ptr fee_estimator = std::make_unique(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 wallet_ptr{std::make_unique(node.chain.get(), "", CreateMockableWalletDatabase())}; CWallet& wallet{*wallet_ptr}; {