Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
block_policy_estimator.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-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
10#include <streams.h>
12#include <test/fuzz/fuzz.h>
13#include <test/fuzz/util.h>
16
17#include <memory>
18#include <optional>
19#include <vector>
20
21namespace {
23} // namespace
24
26{
27 static const auto testing_setup = MakeNoLogFileContext<>();
28 g_setup = testing_setup.get();
29}
30
32{
33 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
34 bool good_data{true};
35
37
38 uint32_t current_height{0};
39 const auto advance_height{
40 [&] { current_height = fuzzed_data_provider.ConsumeIntegralInRange<decltype(current_height)>(current_height, 1 << 30); },
41 };
42 advance_height();
43 LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {
45 fuzzed_data_provider,
46 [&] {
47 const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
48 if (!mtx) {
49 good_data = false;
50 return;
51 }
52 const CTransaction tx{*mtx};
53 const auto entry{ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height)};
54 const auto tx_submitted_in_package = fuzzed_data_provider.ConsumeBool();
55 const auto tx_has_mempool_parents = fuzzed_data_provider.ConsumeBool();
56 const auto tx_info = NewMempoolTransactionInfo(entry.GetSharedTx(), entry.GetFee(),
57 entry.GetTxSize(), entry.GetHeight(),
58 /*mempool_limit_bypassed=*/false,
59 tx_submitted_in_package,
60 /*chainstate_is_current=*/true,
61 tx_has_mempool_parents);
62 block_policy_estimator.processTransaction(tx_info);
63 if (fuzzed_data_provider.ConsumeBool()) {
64 (void)block_policy_estimator.removeTx(tx.GetHash());
65 }
66 },
67 [&] {
68 std::list<CTxMemPoolEntry> mempool_entries;
69 LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
70 const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
71 if (!mtx) {
72 good_data = false;
73 break;
74 }
75 const CTransaction tx{*mtx};
76 mempool_entries.push_back(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height));
77 }
78 std::vector<RemovedMempoolTransactionInfo> txs;
79 txs.reserve(mempool_entries.size());
80 for (const CTxMemPoolEntry& mempool_entry : mempool_entries) {
81 txs.emplace_back(mempool_entry);
82 }
83 advance_height();
84 block_policy_estimator.processBlock(txs, current_height);
85 },
86 [&] {
87 (void)block_policy_estimator.removeTx(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
88 },
89 [&] {
90 block_policy_estimator.FlushUnconfirmed();
91 });
92 (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
93 EstimationResult result;
94 auto conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
95 auto success_threshold = fuzzed_data_provider.ConsumeFloatingPoint<double>();
96 auto horizon = fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS);
97 auto* result_ptr = fuzzed_data_provider.ConsumeBool() ? &result : nullptr;
98 (void)block_policy_estimator.estimateRawFee(conf_target, success_threshold, horizon, result_ptr);
99
100 FeeCalculation fee_calculation;
101 conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
102 auto* fee_calc_ptr = fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr;
103 auto conservative = fuzzed_data_provider.ConsumeBool();
104 (void)block_policy_estimator.estimateSmartFee(conf_target, fee_calc_ptr, conservative);
105
106 (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
107 }
108 {
109 FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
110 AutoFile fuzzed_auto_file{fuzzed_file_provider.open()};
111 block_policy_estimator.Write(fuzzed_auto_file);
112 block_policy_estimator.Read(fuzzed_auto_file);
113 (void)fuzzed_auto_file.fclose();
114 }
115}
for(unsigned int i=0;i< 256;++i)
const TestingSetup * g_setup
constexpr bool DEFAULT_ACCEPT_STALE_FEE_ESTIMATES
constexpr auto ALL_FEE_ESTIMATE_HORIZONS
Non-refcounted RAII wrapper for FILE*.
Definition streams.h:395
int fclose()
Definition streams.h:429
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
void processTransaction(const NewMempoolTransactionInfo &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Process a transaction accepted to the mempool.
bool removeTx(Txid hash) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Remove a transaction from the mempool tracking stats for non BLOCK removal reasons.
virtual CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Estimate feerate needed to get be included in a block within confTarget blocks.
bool Write(AutoFile &fileout) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Write estimation data to a file.
virtual unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Calculation of highest target that estimates are tracked for.
CFeeRate estimateFee(int confTarget) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
DEPRECATED.
bool Read(AutoFile &filein) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Read estimation data from a file.
void FlushUnconfirmed() EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool.
CFeeRate estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult *result=nullptr) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Return a specific fee estimate calculation with a given success threshold and time horizon,...
void processBlock(const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Process all the transactions that have been included in a block.
The basic transaction that is broadcasted on the network and contained in blocks.
const Txid & GetHash() const LIFETIMEBOUND
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
FILE * open()
Definition util.cpp:251
static transaction_identifier FromUint256(const uint256 &id)
uint32_t current_height
Current height in the simulation.
fs::path BlockPolicyFeeEstPath(const ArgsManager &argsman)
#define FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
Definition basic.cpp:11
constexpr TransactionSerParams TX_WITH_WITNESS
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.
Basic testing setup.
node::NodeContext m_node
NewMempoolTransactionInfo(const CTransactionRef &tx, const CAmount &fee, const int64_t vsize, const unsigned int height, const bool mempool_limit_bypassed, const bool submitted_in_package, const bool chainstate_is_current, const bool has_no_mempool_parents)
ArgsManager * args
Definition context.h:78
void initialize_block_policy_estimator()
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx, uint32_t max_height) noexcept
Definition mempool.cpp:17
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition util.h:195
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition util.h:37
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P &params, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:106