Bitcoin Core 29.99.0
P2P Digital Currency
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#include <policy/fees.h>
7#include <policy/fees_args.h>
9#include <streams.h>
11#include <test/fuzz/fuzz.h>
12#include <test/fuzz/util.h>
15
16#include <memory>
17#include <optional>
18#include <vector>
19
20namespace {
21const BasicTestingSetup* g_setup;
22} // namespace
23
25{
26 static const auto testing_setup = MakeNoLogFileContext<>();
27 g_setup = testing_setup.get();
28}
29
31{
32 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33 bool good_data{true};
34
35 CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES};
36
37 uint32_t current_height{0};
38 const auto advance_height{
39 [&] { current_height = fuzzed_data_provider.ConsumeIntegralInRange<decltype(current_height)>(current_height, 1 << 30); },
40 };
41 advance_height();
42 LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
43 {
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 {
71 const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
72 if (!mtx) {
73 good_data = false;
74 break;
75 }
76 const CTransaction tx{*mtx};
77 mempool_entries.emplace_back(CTxMemPoolEntry::ExplicitCopy, ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height));
78 }
79 std::vector<RemovedMempoolTransactionInfo> txs;
80 txs.reserve(mempool_entries.size());
81 for (const CTxMemPoolEntry& mempool_entry : mempool_entries) {
82 txs.emplace_back(mempool_entry);
83 }
84 advance_height();
85 block_policy_estimator.processBlock(txs, current_height);
86 },
87 [&] {
88 (void)block_policy_estimator.removeTx(ConsumeUInt256(fuzzed_data_provider));
89 },
90 [&] {
91 block_policy_estimator.FlushUnconfirmed();
92 });
93 (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
94 EstimationResult result;
95 auto conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
96 auto success_threshold = fuzzed_data_provider.ConsumeFloatingPoint<double>();
97 auto horizon = fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS);
98 auto* result_ptr = fuzzed_data_provider.ConsumeBool() ? &result : nullptr;
99 (void)block_policy_estimator.estimateRawFee(conf_target, success_threshold, horizon, result_ptr);
100
101 FeeCalculation fee_calculation;
102 conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
103 auto* fee_calc_ptr = fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr;
104 auto conservative = fuzzed_data_provider.ConsumeBool();
105 (void)block_policy_estimator.estimateSmartFee(conf_target, fee_calc_ptr, conservative);
106
107 (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
108 }
109 {
110 FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
111 AutoFile fuzzed_auto_file{fuzzed_file_provider.open()};
112 block_policy_estimator.Write(fuzzed_auto_file);
113 block_policy_estimator.Read(fuzzed_auto_file);
114 }
115}
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:392
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:149
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
static constexpr ExplicitCopyTag ExplicitCopy
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
fs::path FeeestPath(const ArgsManager &argsman)
Definition: fees_args.cpp:13
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
static constexpr bool DEFAULT_ACCEPT_STALE_FEE_ESTIMATES
Definition: fees.h:36
static constexpr auto ALL_FEE_ESTIMATE_HORIZONS
Definition: fees.h:51
void initialize_policy_estimator()
FUZZ_TARGET(policy_estimator,.init=initialize_policy_estimator)
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195
Basic testing setup.
Definition: setup_common.h:64
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:171
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35