Bitcoin Core 32.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
8#include <test/fuzz/fuzz.h>
9#include <test/fuzz/util.h>
11#include <test/util/time.h>
12#include <test/util/txmempool.h>
13#include <util/expected.h>
14#include <util/fees.h>
15#include <validation.h>
16#include <wallet/coincontrol.h>
17#include <wallet/fees.h>
18#include <wallet/test/util.h>
19#include <wallet/wallet.h>
20
21#include <optional>
22
23namespace wallet {
24namespace {
25
26struct FeeEstimatorManTestingSetup : public TestingSetup {
27 FeeEstimatorManTestingSetup(const ChainType chain_type, TestOpts opts) : TestingSetup{chain_type, opts}
28 {
29 }
30
31 ~FeeEstimatorManTestingSetup()
32 {
34 }
35
36 void SetFeeEstimatorMan(std::unique_ptr<FeeRateEstimatorManager> fee_estimator_man)
37 {
38 m_node.fee_estimator_man = std::move(fee_estimator_man);
39 }
40};
41
42FeeEstimatorManTestingSetup* g_setup;
43
44class FuzzedFeeEstimatorMan : public FeeRateEstimatorManager
45{
47
48public:
49 FuzzedFeeEstimatorMan(FuzzedDataProvider& provider, const CTxMemPool& mempool, ChainstateManager& chainman)
50 : FeeRateEstimatorManager(fs::path{}, false, fs::path{}, mempool, chainman), fuzzed_data_provider(provider) {}
51
52 util::Expected<FeeRateEstimation, FeeRateEstimationError> GetFeeRateEstimate(int confTarget, bool conservative) const override
53 {
54 FeePerVSize feerate(ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000), fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1000, 1000000));
56 }
57 util::Expected<FeeRateEstimation, FeeRateEstimationError> GetFeeRateEstimate(FeeRateEstimatorType type, int confTarget, bool conservative) const override
58 {
59 auto res = GetFeeRateEstimate(confTarget, conservative);
60 if (res) res->feerate_estimator = type;
61 return res;
62 }
63 unsigned int MaximumTarget() const override
64 {
65 return fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1004);
66 }
67};
68
69void initialize_setup()
70{
71 static const auto testing_setup = MakeNoLogFileContext<FeeEstimatorManTestingSetup>();
72 g_setup = testing_setup.get();
73}
74
75FUZZ_TARGET(wallet_fees, .init = initialize_setup)
76{
78 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
80 auto& node{g_setup->m_node};
81 Chainstate* chainstate = &node.chainman->ActiveChainstate();
82
83 bilingual_str error;
84 CTxMemPool::Options mempool_opts{
86 .min_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)},
87 .dust_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)}
88 };
89 node.block_template_manager.reset();
90 node.mempool = std::make_unique<CTxMemPool>(mempool_opts, error);
92 std::unique_ptr<FeeRateEstimatorManager> fee_estimator_man = std::make_unique<FuzzedFeeEstimatorMan>(fuzzed_data_provider, *node.mempool, *node.chainman);
93 g_setup->SetFeeEstimatorMan(std::move(fee_estimator_man));
94 auto target_feerate{CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000)}};
95 if (target_feerate > node.mempool->m_opts.incremental_relay_feerate &&
96 target_feerate > node.mempool->m_opts.min_relay_feerate) {
97 MockMempoolMinFee(target_feerate, *node.mempool);
98 }
99 std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
100 CWallet& wallet{*wallet_ptr};
101 {
102 LOCK(wallet.cs_wallet);
103 wallet.SetLastBlockProcessed(chainstate->m_chain.Height(), chainstate->m_chain.Tip()->GetBlockHash());
104 }
105
107 wallet.m_fallback_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
108 }
109
111 wallet.m_discard_rate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
112 }
113 (void)GetDiscardRate(wallet);
114
115 const auto tx_bytes{fuzzed_data_provider.ConsumeIntegralInRange(0, std::numeric_limits<int32_t>::max())};
118 }
119
120 (void)GetRequiredFee(wallet, tx_bytes);
122
123 CCoinControl coin_control;
125 coin_control.m_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
126 }
128 coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
129 }
132 }
136 }
137 std::optional<int> returned_target;
139 returned_target = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 999'000);
140 }
141 MinimumFeeRateResult min_fee_rate{
143 fee_reason,
144 returned_target};
145 (void)GetMinimumFeeRate(wallet, coin_control);
146 (void)GetMinimumFee(min_fee_rate, tx_bytes);
147}
148} // namespace
149} // namespace wallet
constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
node::NodeContext m_node
Definition: bitcoin-gui.cpp:48
const TestingSetup * g_setup
ChainType
Definition: chaintype.h:12
uint256 GetBlockHash() const
Definition: chain.h:198
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
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:188
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:550
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:630
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:950
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:54
Manages fee rate estimators.
Definition: estimator_man.h:31
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
The util::Expected class provides a standard way for low-level functions to return either error value...
Definition: expected.h:44
Definition: basic.cpp:11
Definition: messages.h:21
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase()
Definition: util.cpp:126
CFeeRate GetRequiredFeeRate(const CWallet &wallet)
Return the minimum required feerate taking into account the minimum relay feerate and user set minimu...
Definition: fees.cpp:27
MinimumFeeRateResult GetMinimumFeeRate(const CWallet &wallet, const CCoinControl &coin_control)
Estimate the minimum fee rate considering user set parameters and the required fee.
Definition: fees.cpp:32
CAmount GetMinimumFee(const MinimumFeeRateResult &min_fee_rate, unsigned int nTxBytes)
Return the minimum fee for this size given a fee rate result.
Definition: fees.cpp:22
FUZZ_TARGET(coin_grinder)
CFeeRate GetDiscardRate(const CWallet &wallet)
Return the maximum feerate for discarding change.
Definition: fees.cpp:90
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:16
node::NodeContext m_node
Definition: setup_common.h:60
void CreateBlockTemplateManager()
Create the block template manager.
A successful fee rate estimate returned by a fee rate estimator.
Definition: fees.h:47
Testing setup that configures a complete environment.
Definition: setup_common.h:119
Bilingual messages:
Definition: translation.h:24
Options struct containing options for constructing a CTxMemPool.
CFeeRate incremental_relay_feerate
std::unique_ptr< FeeRateEstimatorManager > fee_estimator_man
Definition: context.h:71
#define LOCK(cs)
Definition: sync.h:268
SeedRandomStateForTest(SeedRand::ZEROS)
FuzzedDataProvider provider
Definition: dbwrapper.cpp:366
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
@ 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
FeeRateEstimatorType
Identifier for fee rate estimator.
Definition: fees.h:37
FeeReason
Definition: fees.h:25
@ FEE_RATE_ESTIMATOR
@ CONSERVATIVE
Force Fee rate estimator to return conservative estimates.
@ ECONOMICAL
Force Fee rate estimator to return non-conservative estimates.
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:46
FakeNodeClock clock