Bitcoin Core 30.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>
10#include <validation.h>
11#include <wallet/coincontrol.h>
12#include <wallet/fees.h>
13#include <wallet/test/util.h>
14#include <wallet/wallet.h>
15
16namespace wallet {
17namespace {
18
19struct FeeEstimatorTestingSetup : public TestingSetup {
20 FeeEstimatorTestingSetup(const ChainType chain_type, TestOpts opts) : TestingSetup{chain_type, opts}
21 {
22 }
23
24 ~FeeEstimatorTestingSetup() {
25 m_node.fee_estimator.reset();
26 }
27
28 void SetFeeEstimator(std::unique_ptr<CBlockPolicyEstimator> fee_estimator)
29 {
30 m_node.fee_estimator = std::move(fee_estimator);
31 }
32};
33
34FeeEstimatorTestingSetup* g_setup;
35
36class FuzzedBlockPolicyEstimator : public CBlockPolicyEstimator
37{
39
40public:
41 FuzzedBlockPolicyEstimator(FuzzedDataProvider& provider)
42 : CBlockPolicyEstimator(fs::path{}, false), fuzzed_data_provider(provider) {}
43
44 CFeeRate estimateSmartFee(int confTarget, FeeCalculation* feeCalc, bool conservative) const override
45 {
46 return CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000)};
47 }
48
49 unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const override
50 {
51 return fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1000);
52 }
53};
54
55void initialize_setup()
56{
57 static const auto testing_setup = MakeNoLogFileContext<FeeEstimatorTestingSetup>();
58 g_setup = testing_setup.get();
59}
60
61FUZZ_TARGET(wallet_fees, .init = initialize_setup)
62{
64 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
66 auto& node{g_setup->m_node};
67 Chainstate* chainstate = &node.chainman->ActiveChainstate();
68
69 bilingual_str error;
70 CTxMemPool::Options mempool_opts{
72 .min_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)},
73 .dust_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)}
74 };
75 node.mempool = std::make_unique<CTxMemPool>(mempool_opts, error);
76 std::unique_ptr<CBlockPolicyEstimator> fee_estimator = std::make_unique<FuzzedBlockPolicyEstimator>(fuzzed_data_provider);
77 g_setup->SetFeeEstimator(std::move(fee_estimator));
78 auto target_feerate{CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000)}};
79 if (target_feerate > node.mempool->m_opts.incremental_relay_feerate &&
80 target_feerate > node.mempool->m_opts.min_relay_feerate) {
81 MockMempoolMinFee(target_feerate, *node.mempool);
82 }
83 std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
84 CWallet& wallet{*wallet_ptr};
85 {
86 LOCK(wallet.cs_wallet);
87 wallet.SetLastBlockProcessed(chainstate->m_chain.Height(), chainstate->m_chain.Tip()->GetBlockHash());
88 }
89
91 wallet.m_fallback_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
92 }
93
95 wallet.m_discard_rate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
96 }
98
99 const auto tx_bytes{fuzzed_data_provider.ConsumeIntegralInRange(0, std::numeric_limits<int32_t>::max())};
101 wallet.m_pay_tx_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
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
FeeEstimateHorizon
ChainType
Definition: chaintype.h:11
uint256 GetBlockHash() const
Definition: chain.h:248
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:438
int Height() const
Return the maximal height in the chain.
Definition: chain.h:467
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:35
FeePerVSize m_feerate
Fee rate in sats/vB (satoshis per N virtualbytes)
Definition: feerate.h:38
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:532
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:614
T ConsumeIntegralInRange(T min, T max)
@ CONSERVATIVE
Force estimateSmartFee to use conservative estimates.
@ ECONOMICAL
Force estimateSmartFee to use non-conservative estimates.
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:186
CFeeRate GetDiscardRate(const CWallet &wallet)
Return the maximum feerate for discarding change.
Definition: fees.cpp:84
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
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:259
int64_t 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:223
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:38