Bitcoin Core 31.0.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
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
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
56{
57 static const auto testing_setup = MakeNoLogFileContext<FeeEstimatorTestingSetup>();
58 g_setup = testing_setup.get();
59}
60
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;
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));
79 if (target_feerate > node.mempool->m_opts.incremental_relay_feerate &&
80 target_feerate > node.mempool->m_opts.min_relay_feerate) {
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())};
102 }
103
106
107 CCoinControl coin_control;
110 }
112 coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
113 }
116 }
117
122}
123} // namespace
124} // namespace wallet
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition amount.h:15
node::NodeContext m_node
const TestingSetup * g_setup
ChainType
Definition chaintype.h:11
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
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
T ConsumeIntegralInRange(T min, T max)
#define FUZZ_TARGET(...)
Definition fuzz.h:35
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
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
Testing setup that configures a complete environment.
Bilingual messages:
Definition translation.h:24
Options struct containing options for constructing a CTxMemPool.
std::unique_ptr< CBlockPolicyEstimator > fee_estimator
Definition context.h:70
#define LOCK(cs)
Definition sync.h:258
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),...
@ CONSERVATIVE
Force estimateSmartFee to use conservative estimates.
@ ECONOMICAL
Force estimateSmartFee to use non-conservative estimates.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition time.cpp:44
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:38