Bitcoin Core 31.99.0
P2P Digital Currency
fees.cpp
Go to the documentation of this file.
1// Copyright (c) 2022-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6#include <test/fuzz/fuzz.h>
7#include <test/fuzz/util.h>
9#include <test/util/time.h>
10#include <test/util/txmempool.h>
11#include <validation.h>
12#include <wallet/coincontrol.h>
13#include <wallet/fees.h>
14#include <wallet/test/util.h>
15#include <wallet/wallet.h>
16
17namespace wallet {
18namespace {
19
20struct FeeEstimatorTestingSetup : public TestingSetup {
21 FeeEstimatorTestingSetup(const ChainType chain_type, TestOpts opts) : TestingSetup{chain_type, opts}
22 {
23 }
24
25 ~FeeEstimatorTestingSetup() {
26 m_node.fee_estimator.reset();
27 }
28
29 void SetFeeEstimator(std::unique_ptr<CBlockPolicyEstimator> fee_estimator)
30 {
31 m_node.fee_estimator = std::move(fee_estimator);
32 }
33};
34
35FeeEstimatorTestingSetup* g_setup;
36
37class FuzzedBlockPolicyEstimator : public CBlockPolicyEstimator
38{
40
41public:
42 FuzzedBlockPolicyEstimator(FuzzedDataProvider& provider)
43 : CBlockPolicyEstimator(fs::path{}, false), fuzzed_data_provider(provider) {}
44
45 CFeeRate estimateSmartFee(int confTarget, FeeCalculation* feeCalc, bool conservative) const override
46 {
47 return CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000)};
48 }
49
50 unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const override
51 {
52 return fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1000);
53 }
54};
55
56void initialize_setup()
57{
58 static const auto testing_setup = MakeNoLogFileContext<FeeEstimatorTestingSetup>();
59 g_setup = testing_setup.get();
60}
61
62FUZZ_TARGET(wallet_fees, .init = initialize_setup)
63{
65 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
67 auto& node{g_setup->m_node};
68 Chainstate* chainstate = &node.chainman->ActiveChainstate();
69
70 bilingual_str error;
71 CTxMemPool::Options mempool_opts{
73 .min_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)},
74 .dust_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)}
75 };
76 node.mempool = std::make_unique<CTxMemPool>(mempool_opts, error);
77 std::unique_ptr<CBlockPolicyEstimator> fee_estimator = std::make_unique<FuzzedBlockPolicyEstimator>(fuzzed_data_provider);
78 g_setup->SetFeeEstimator(std::move(fee_estimator));
79 auto target_feerate{CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000)}};
80 if (target_feerate > node.mempool->m_opts.incremental_relay_feerate &&
81 target_feerate > node.mempool->m_opts.min_relay_feerate) {
82 MockMempoolMinFee(target_feerate, *node.mempool);
83 }
84 std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
85 CWallet& wallet{*wallet_ptr};
86 {
87 LOCK(wallet.cs_wallet);
88 wallet.SetLastBlockProcessed(chainstate->m_chain.Height(), chainstate->m_chain.Tip()->GetBlockHash());
89 }
90
92 wallet.m_fallback_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
93 }
94
96 wallet.m_discard_rate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
97 }
99
100 const auto tx_bytes{fuzzed_data_provider.ConsumeIntegralInRange(0, std::numeric_limits<int32_t>::max())};
103 }
104
105 (void)GetRequiredFee(wallet, tx_bytes);
107
108 CCoinControl coin_control;
110 coin_control.m_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
111 }
113 coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
114 }
117 }
118
119 FeeCalculation fee_calculation;
120 FeeCalculation* maybe_fee_calculation{fuzzed_data_provider.ConsumeBool() ? nullptr : &fee_calculation};
121 (void)GetMinimumFeeRate(wallet, coin_control, maybe_fee_calculation);
122 (void)GetMinimumFee(wallet, tx_bytes, coin_control, maybe_fee_calculation);
123}
124} // namespace
125} // namespace wallet
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
node::NodeContext m_node
Definition: bitcoin-gui.cpp:43
const TestingSetup * g_setup
FeeEstimateHorizon
ChainType
Definition: chaintype.h:11
uint256 GetBlockHash() const
Definition: chain.h:198
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:396
int Height() const
Return the maximal height in the chain.
Definition: chain.h:425
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:32
FeePerVSize m_feerate
Fee rate in sats/vB (satoshis per N virtualbytes)
Definition: feerate.h:35
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:551
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:625
T ConsumeIntegralInRange(T min, T max)
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
Definition: messages.h:21
CFeeRate GetRequiredFeeRate(const CWallet &wallet)
Return the minimum required feerate taking into account the minimum relay feerate and user set minimu...
Definition: fees.cpp:24
CAmount GetMinimumFee(const CWallet &wallet, unsigned int nTxBytes, const CCoinControl &coin_control, FeeCalculation *feeCalc)
Estimate the minimum fee considering user set parameters and the required fee.
Definition: fees.cpp:19
CFeeRate GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control, FeeCalculation *feeCalc)
Estimate the minimum fee rate considering user set parameters and the required fee.
Definition: fees.cpp:29
FUZZ_TARGET(coin_grinder)
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:211
CFeeRate GetDiscardRate(const CWallet &wallet)
Return the maximum feerate for discarding change.
Definition: fees.cpp:78
CAmount GetRequiredFee(const CWallet &wallet, unsigned int nTxBytes)
Return the minimum required absolute fee for this size based on the required fee rate.
Definition: fees.cpp:13
node::NodeContext m_node
Definition: setup_common.h:66
Testing setup that configures a complete environment.
Definition: setup_common.h:121
Bilingual messages:
Definition: translation.h:24
Options struct containing options for constructing a CTxMemPool.
CFeeRate incremental_relay_feerate
std::unique_ptr< CBlockPolicyEstimator > fee_estimator
Definition: context.h:70
#define LOCK(cs)
Definition: sync.h:268
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:29
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
void MockMempoolMinFee(const CFeeRate &target_feerate, CTxMemPool &mempool)
Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0),...
Definition: txmempool.cpp:225
@ CONSERVATIVE
Force estimateSmartFee to use conservative estimates.
@ ECONOMICAL
Force estimateSmartFee to use non-conservative estimates.
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:39