Bitcoin Core 29.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>
9#include <validation.h>
10#include <wallet/coincontrol.h>
11#include <wallet/fees.h>
12#include <wallet/test/util.h>
13#include <wallet/wallet.h>
14
15namespace wallet {
16namespace {
17const TestingSetup* g_setup;
18
19void initialize_setup()
20{
21 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
22 g_setup = testing_setup.get();
23}
24
25FUZZ_TARGET(wallet_fees, .init = initialize_setup)
26{
28 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
29 SetMockTime(ConsumeTime(fuzzed_data_provider));
30 const auto& node{g_setup->m_node};
31 Chainstate* chainstate = &node.chainman->ActiveChainstate();
32 std::unique_ptr<CWallet> wallet_ptr{std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase())};
33 CWallet& wallet{*wallet_ptr};
34 {
35 LOCK(wallet.cs_wallet);
36 wallet.SetLastBlockProcessed(chainstate->m_chain.Height(), chainstate->m_chain.Tip()->GetBlockHash());
37 }
38
39 if (fuzzed_data_provider.ConsumeBool()) {
40 wallet.m_fallback_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
41 }
42
43 if (fuzzed_data_provider.ConsumeBool()) {
44 wallet.m_discard_rate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
45 }
47
48 const auto tx_bytes{fuzzed_data_provider.ConsumeIntegral<unsigned int>()};
49
50 if (fuzzed_data_provider.ConsumeBool()) {
51 wallet.m_pay_tx_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
52 wallet.m_min_fee = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
53 }
54
55 (void)GetRequiredFee(wallet, tx_bytes);
57
58 CCoinControl coin_control;
59 if (fuzzed_data_provider.ConsumeBool()) {
60 coin_control.m_feerate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
61 }
62 if (fuzzed_data_provider.ConsumeBool()) {
63 coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
64 }
65 if (fuzzed_data_provider.ConsumeBool()) {
66 coin_control.m_fee_mode = fuzzed_data_provider.ConsumeBool() ? FeeEstimateMode::CONSERVATIVE : FeeEstimateMode::ECONOMICAL;
67 }
68
69 FeeCalculation fee_calculation;
70 FeeCalculation* maybe_fee_calculation{fuzzed_data_provider.ConsumeBool() ? nullptr : &fee_calculation};
71 (void)GetMinimumFeeRate(wallet, coin_control, maybe_fee_calculation);
72 (void)GetMinimumFee(wallet, tx_bytes, coin_control, maybe_fee_calculation);
73}
74} // namespace
75} // namespace wallet
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
uint256 GetBlockHash() const
Definition: chain.h:243
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:433
int Height() const
Return the maximal height in the chain.
Definition: chain.h:462
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:507
CChain m_chain
The current chain of blockheaders we consult and build on.
Definition: validation.h:587
@ CONSERVATIVE
Force estimateSmartFee to use conservative estimates.
@ ECONOMICAL
Force estimateSmartFee to use non-conservative estimates.
Definition: messages.h:20
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
node::NodeContext m_node
Definition: setup_common.h:66
Testing setup that configures a complete environment.
Definition: setup_common.h:121
#define LOCK(cs)
Definition: sync.h:257
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 SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40