Bitcoin Core  22.99.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>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 #include <test/util/setup_common.h>
11 #include <txmempool.h>
12 
13 #include <cstdint>
14 #include <optional>
15 #include <string>
16 #include <vector>
17 
19 {
20  static const auto testing_setup = MakeNoLogFileContext<>();
21 }
22 
24 {
25  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
26  CBlockPolicyEstimator block_policy_estimator;
27  while (fuzzed_data_provider.ConsumeBool()) {
28  CallOneOf(
29  fuzzed_data_provider,
30  [&] {
31  const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
32  if (!mtx) {
33  return;
34  }
35  const CTransaction tx{*mtx};
36  block_policy_estimator.processTransaction(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx), fuzzed_data_provider.ConsumeBool());
37  if (fuzzed_data_provider.ConsumeBool()) {
38  (void)block_policy_estimator.removeTx(tx.GetHash(), /* inBlock */ fuzzed_data_provider.ConsumeBool());
39  }
40  },
41  [&] {
42  std::vector<CTxMemPoolEntry> mempool_entries;
43  while (fuzzed_data_provider.ConsumeBool()) {
44  const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider);
45  if (!mtx) {
46  break;
47  }
48  const CTransaction tx{*mtx};
49  mempool_entries.push_back(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));
50  }
51  std::vector<const CTxMemPoolEntry*> ptrs;
52  ptrs.reserve(mempool_entries.size());
53  for (const CTxMemPoolEntry& mempool_entry : mempool_entries) {
54  ptrs.push_back(&mempool_entry);
55  }
56  block_policy_estimator.processBlock(fuzzed_data_provider.ConsumeIntegral<unsigned int>(), ptrs);
57  },
58  [&] {
59  (void)block_policy_estimator.removeTx(ConsumeUInt256(fuzzed_data_provider), /* inBlock */ fuzzed_data_provider.ConsumeBool());
60  },
61  [&] {
62  block_policy_estimator.FlushUnconfirmed();
63  });
64  (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
65  EstimationResult result;
66  (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);
67  FeeCalculation fee_calculation;
68  (void)block_policy_estimator.estimateSmartFee(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr, fuzzed_data_provider.ConsumeBool());
69  (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
70  }
71  {
72  FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
73  CAutoFile fuzzed_auto_file = fuzzed_auto_file_provider.open();
74  block_policy_estimator.Write(fuzzed_auto_file);
75  block_policy_estimator.Read(fuzzed_auto_file);
76  }
77 }
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:153
CBlockPolicyEstimator::processTransaction
void processTransaction(const CTxMemPoolEntry &entry, bool validFeeEstimate)
Process a transaction accepted to the mempool.
Definition: fees.cpp:538
setup_common.h
transaction.h
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
Definition: policy_estimator.cpp:23
util.h
CBlockPolicyEstimator::removeTx
bool removeTx(uint256 hash, bool inBlock)
Remove a transaction from the mempool tracking stats.
Definition: fees.cpp:493
FuzzedDataProvider::ConsumeFloatingPoint
T ConsumeFloatingPoint()
Definition: FuzzedDataProvider.h:239
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
CBlockPolicyEstimator::Write
bool Write(CAutoFile &fileout) const
Write estimation data to a file.
Definition: fees.cpp:894
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
CBlockPolicyEstimator::FlushUnconfirmed
void FlushUnconfirmed()
Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool.
Definition: fees.cpp:981
txmempool.h
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:564
CBlockPolicyEstimator::estimateSmartFee
CFeeRate estimateSmartFee(int confTarget, FeeCalculation *feeCalc, bool conservative) const
Estimate feerate needed to get be included in a block within confTarget blocks.
Definition: fees.cpp:806
FeeCalculation
Definition: fees.h:75
EstimationResult
Definition: fees.h:67
CBlockPolicyEstimator::estimateRawFee
CFeeRate estimateRawFee(int confTarget, double successThreshold, FeeEstimateHorizon horizon, EstimationResult *result=nullptr) const
Return a specific fee estimate calculation with a given success threshold and time horizon,...
Definition: fees.cpp:662
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
ConsumeTxMemPoolEntry
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx) noexcept
Definition: util.cpp:347
FuzzedDataProvider.h
CBlockPolicyEstimator
Definition: fees.h:131
FuzzedAutoFileProvider
Definition: util.h:306
CBlockPolicyEstimator::HighestTargetTracked
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const
Calculation of highest target that estimates are tracked for.
Definition: fees.cpp:698
fees.h
ConsumeAutoFile
FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:322
fuzz.h
CTxMemPoolEntry
Definition: txmempool.h:79
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
initialize_policy_estimator
void initialize_policy_estimator()
Definition: policy_estimator.cpp:18
CBlockPolicyEstimator::estimateFee
CFeeRate estimateFee(int confTarget) const
DEPRECATED.
Definition: fees.cpp:653
CBlockPolicyEstimator::processBlock
void processBlock(unsigned int nBlockHeight, std::vector< const CTxMemPoolEntry * > &entries)
Process all the transactions that have been included in a block.
Definition: fees.cpp:604
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
CBlockPolicyEstimator::Read
bool Read(CAutoFile &filein)
Read estimation data from a file.
Definition: fees.cpp:919
ALL_FEE_ESTIMATE_HORIZONS
static constexpr auto ALL_FEE_ESTIMATE_HORIZONS
Definition: fees.h:34
FuzzedAutoFileProvider::open
CAutoFile open()
Definition: util.h:316