Bitcoin Core  24.1.0
P2P Digital Currency
policy_estimator.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 
5 #include <policy/fees.h>
6 #include <policy/fees_args.h>
9 #include <test/fuzz/fuzz.h>
10 #include <test/fuzz/util.h>
11 #include <test/util/setup_common.h>
12 #include <txmempool.h>
13 
14 #include <cstdint>
15 #include <optional>
16 #include <string>
17 #include <vector>
18 
19 namespace {
20 const BasicTestingSetup* g_setup;
21 } // namespace
22 
24 {
25  static const auto testing_setup = MakeNoLogFileContext<>();
26  g_setup = testing_setup.get();
27 }
28 
30 {
31  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
32  CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args)};
33  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
34  CallOneOf(
35  fuzzed_data_provider,
36  [&] {
37  const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
38  if (!mtx) {
39  return;
40  }
41  const CTransaction tx{*mtx};
42  block_policy_estimator.processTransaction(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx), fuzzed_data_provider.ConsumeBool());
43  if (fuzzed_data_provider.ConsumeBool()) {
44  (void)block_policy_estimator.removeTx(tx.GetHash(), /*inBlock=*/fuzzed_data_provider.ConsumeBool());
45  }
46  },
47  [&] {
48  std::vector<CTxMemPoolEntry> mempool_entries;
49  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
50  const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
51  if (!mtx) {
52  break;
53  }
54  const CTransaction tx{*mtx};
55  mempool_entries.push_back(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));
56  }
57  std::vector<const CTxMemPoolEntry*> ptrs;
58  ptrs.reserve(mempool_entries.size());
59  for (const CTxMemPoolEntry& mempool_entry : mempool_entries) {
60  ptrs.push_back(&mempool_entry);
61  }
62  block_policy_estimator.processBlock(fuzzed_data_provider.ConsumeIntegral<unsigned int>(), ptrs);
63  },
64  [&] {
65  (void)block_policy_estimator.removeTx(ConsumeUInt256(fuzzed_data_provider), /*inBlock=*/fuzzed_data_provider.ConsumeBool());
66  },
67  [&] {
68  block_policy_estimator.FlushUnconfirmed();
69  });
70  (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
71  EstimationResult result;
72  (void)block_policy_estimator.estimateRawFee(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeFloatingPoint<double>(), fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS), fuzzed_data_provider.ConsumeBool() ? &result : nullptr);
73  FeeCalculation fee_calculation;
74  (void)block_policy_estimator.estimateSmartFee(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr, fuzzed_data_provider.ConsumeBool());
75  (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
76  }
77  {
78  FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
79  AutoFile fuzzed_auto_file{fuzzed_auto_file_provider.open()};
80  block_policy_estimator.Write(fuzzed_auto_file);
81  block_policy_estimator.Read(fuzzed_auto_file);
82  }
83 }
fs::path FeeestPath(const ArgsManager &argsman)
Definition: fees_args.cpp:9
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx) noexcept
Definition: util.cpp:481
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:474
Basic testing setup.
Definition: setup_common.h:83
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:88
FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:374
static constexpr auto ALL_FEE_ESTIMATE_HORIZONS
Definition: fees.h:35
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:132
AutoFile open()
Definition: util.h:368
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:89
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:287
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:202
void initialize_policy_estimator()