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
7#include <test/fuzz/fuzz.h>
8#include <test/fuzz/util.h>
10#include <test/util/time.h>
11#include <test/util/txmempool.h>
12#include <util/expected.h>
13#include <util/fees.h>
14#include <validation.h>
15#include <wallet/coincontrol.h>
16#include <wallet/fees.h>
17#include <wallet/test/util.h>
18#include <wallet/wallet.h>
19
20#include <optional>
21
22namespace wallet {
23namespace {
24
25struct FeeEstimatorManTestingSetup : public TestingSetup {
26 FeeEstimatorManTestingSetup(const ChainType chain_type, TestOpts opts) : TestingSetup{chain_type, opts}
27 {
28 }
29
30 ~FeeEstimatorManTestingSetup()
31 {
33 }
34
35 void SetFeeEstimatorMan(std::unique_ptr<FeeRateEstimatorManager> fee_estimator_man)
36 {
37 m_node.fee_estimator_man = std::move(fee_estimator_man);
38 }
39};
40
41FeeEstimatorManTestingSetup* g_setup;
42
43class FuzzedFeeEstimatorMan : public FeeRateEstimatorManager
44{
46
47public:
48 FuzzedFeeEstimatorMan(FuzzedDataProvider& provider, const CTxMemPool& mempool, ChainstateManager& chainman)
49 : FeeRateEstimatorManager(fs::path{}, false, fs::path{}, mempool, chainman), fuzzed_data_provider(provider) {}
50
51 util::Expected<FeeRateEstimation, FeeRateEstimationError> GetFeeRateEstimate(int confTarget, bool conservative) const override
52 {
53 FeePerVSize feerate(ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000), fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1000, 1000000));
55 }
56 util::Expected<FeeRateEstimation, FeeRateEstimationError> GetFeeRateEstimate(FeeRateEstimatorType type, int confTarget, bool conservative) const override
57 {
58 auto res = GetFeeRateEstimate(confTarget, conservative);
59 if (res) res->feerate_estimator = type;
60 return res;
61 }
62 unsigned int MaximumTarget() const override
63 {
64 return fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1004);
65 }
66};
67
68void initialize_setup()
69{
70 static const auto testing_setup = MakeNoLogFileContext<FeeEstimatorManTestingSetup>();
71 g_setup = testing_setup.get();
72}
73
74FUZZ_TARGET(wallet_fees, .init = initialize_setup)
75{
77 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
79 auto& node{g_setup->m_node};
80 Chainstate* chainstate = &node.chainman->ActiveChainstate();
81
82 bilingual_str error;
83 CTxMemPool::Options mempool_opts{
85 .min_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)},
86 .dust_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)}
87 };
88 node.mempool = std::make_unique<CTxMemPool>(mempool_opts, error);
89 std::unique_ptr<FeeRateEstimatorManager> fee_estimator_man = std::make_unique<FuzzedFeeEstimatorMan>(fuzzed_data_provider, *node.mempool, *node.chainman);
90 g_setup->SetFeeEstimatorMan(std::move(fee_estimator_man));
91 auto target_feerate{CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/1'000'000)}};
92 if (target_feerate > node.mempool->m_opts.incremental_relay_feerate &&
93 target_feerate > node.mempool->m_opts.min_relay_feerate) {
94 MockMempoolMinFee(target_feerate, *node.mempool);
95 }
96 std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
97 CWallet& wallet{*wallet_ptr};
98 {
99 LOCK(wallet.cs_wallet);
100 wallet.SetLastBlockProcessed(chainstate->m_chain.Height(), chainstate->m_chain.Tip()->GetBlockHash());
101 }
102
104 wallet.m_fallback_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
105 }
106
108 wallet.m_discard_rate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
109 }
110 (void)GetDiscardRate(wallet);
111
112 const auto tx_bytes{fuzzed_data_provider.ConsumeIntegralInRange(0, std::numeric_limits<int32_t>::max())};
115 }
116
117 (void)GetRequiredFee(wallet, tx_bytes);
119
120 CCoinControl coin_control;
122 coin_control.m_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
123 }
125 coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
126 }
129 }
133 }
134 std::optional<int> returned_target;
136 returned_target = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 999'000);
137 }
138 MinimumFeeRateResult min_fee_rate{
140 fee_reason,
141 returned_target};
142 (void)GetMinimumFeeRate(wallet, coin_control);
143 (void)GetMinimumFee(min_fee_rate, tx_bytes);
144}
145} // namespace
146} // 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:187
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:554
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:628
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:945
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:120
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
A successful fee rate estimate returned by a fee rate estimator.
Definition: fees.h:46
Testing setup that configures a complete environment.
Definition: setup_common.h:115
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:73
#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:36
FeeReason
Definition: fees.h:24
@ 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:45