Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 {
32 m_node.fee_estimator_man.reset();
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{
45 FuzzedDataProvider& fuzzed_data_provider;
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));
54 return FeeRateEstimation{FeeRateEstimatorType::BLOCK_POLICY, feerate, fuzzed_data_provider.ConsumeIntegralInRange<int>(2, 1004)};
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()};
78 FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
79 auto& node{g_setup->m_node};
80 Chainstate* chainstate = &node.chainman->ActiveChainstate();
81
82 bilingual_str error;
83 CTxMemPool::Options mempool_opts{
84 .incremental_relay_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, 1'000'000)},
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
103 if (fuzzed_data_provider.ConsumeBool()) {
104 wallet.m_fallback_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
105 }
106
107 if (fuzzed_data_provider.ConsumeBool()) {
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())};
113 if (fuzzed_data_provider.ConsumeBool()) {
114 wallet.m_min_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
115 }
116
117 (void)GetRequiredFee(wallet, tx_bytes);
118 (void)GetRequiredFeeRate(wallet);
119
120 CCoinControl coin_control;
121 if (fuzzed_data_provider.ConsumeBool()) {
122 coin_control.m_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
123 }
124 if (fuzzed_data_provider.ConsumeBool()) {
125 coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
126 }
127 if (fuzzed_data_provider.ConsumeBool()) {
129 }
131 if (fuzzed_data_provider.ConsumeBool()) {
133 }
134 std::optional<int> returned_target;
135 if (fuzzed_data_provider.ConsumeBool()) {
136 returned_target = fuzzed_data_provider.ConsumeIntegralInRange<int>(1, 999'000);
137 }
138 MinimumFeeRateResult min_fee_rate{
139 CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)},
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
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
FeeEstimateMode m_fee_mode
Fee estimation mode.
std::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition coincontrol.h:99
std::optional< CFeeRate > m_feerate
Override the wallet's fee rate if set.
Definition coincontrol.h:97
kernel::MemPoolOptions Options
Definition txmempool.h:299
CFeeRate m_discard_rate
If the cost to spend a change output at this feerate is greater than the value of the output itself,...
Definition wallet.h:735
CFeeRate m_fallback_fee
If fee estimation does not have enough data to provide estimates, use this fee instead.
Definition wallet.h:731
RecursiveMutex cs_wallet
Main wallet lock.
Definition wallet.h:463
CFeeRate m_min_fee
Definition wallet.h:725
CChain m_chain
Definition validation.h:630
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
Coin Control Features.
Definition coincontrol.h:83
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition wallet.h:310
FeePerUnit< VSizeTag > FeePerVSize
Definition feefrac.h:204
void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Set last block processed height, and write to database.
Definition wallet.cpp:687
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
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
node::NodeContext m_node
#define LOCK(cs)
Definition sync.h:268
SeedRandomStateForTest(SeedRand::ZEROS)
FuzzedDataProvider provider
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.
Definition random.h:19
void MockMempoolMinFee(const CFeeRate &target_feerate, CTxMemPool &mempool)
Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0),...
FeeRateEstimatorType
Identifier for fee rate estimator.
Definition fees.h:37
FeeReason
Definition fees.h:25
@ USER_SPECIFIED
Definition fees.h:28
@ FEE_RATE_ESTIMATOR
Definition fees.h:26
@ REQUIRED
Definition fees.h:30
@ FALLBACK
Definition fees.h:29
@ MEMPOOL_MIN
Definition fees.h:27
@ CONSERVATIVE
Force Fee rate estimator to return conservative estimates.
Definition fees.h:21
@ ECONOMICAL
Force Fee rate estimator to return non-conservative estimates.
Definition fees.h:20