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  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
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  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
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:155
setup_common.h
CBlockPolicyEstimator::Read
bool Read(CAutoFile &filein) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Read estimation data from a file.
Definition: fees.cpp:926
transaction.h
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
Definition: policy_estimator.cpp:23
CBlockPolicyEstimator::estimateFee
CFeeRate estimateFee(int confTarget) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
DEPRECATED.
Definition: fees.cpp:660
util.h
FuzzedDataProvider::ConsumeFloatingPoint
T ConsumeFloatingPoint()
Definition: FuzzedDataProvider.h:239
CBlockPolicyEstimator::processBlock
void processBlock(unsigned int nBlockHeight, std::vector< const CTxMemPoolEntry * > &entries) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Process all the transactions that have been included in a block.
Definition: fees.cpp:611
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition: FuzzedDataProvider.h:303
CBlockPolicyEstimator::estimateSmartFee
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.
Definition: fees.cpp:813
CBlockPolicyEstimator::removeTx
bool removeTx(uint256 hash, bool inBlock) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Remove a transaction from the mempool tracking stats.
Definition: fees.cpp:493
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
txmempool.h
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:544
FeeCalculation
Definition: fees.h:75
EstimationResult
Definition: fees.h:67
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:42
ConsumeTxMemPoolEntry
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx) noexcept
Definition: util.cpp:455
FuzzedDataProvider.h
CBlockPolicyEstimator
Definition: fees.h:131
FuzzedAutoFileProvider
Definition: util.h:297
LIMITED_WHILE
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:18
CBlockPolicyEstimator::Write
bool Write(CAutoFile &fileout) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Write estimation data to a file.
Definition: fees.cpp:901
CBlockPolicyEstimator::estimateRawFee
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,...
Definition: fees.cpp:669
fees.h
ConsumeAutoFile
FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:313
fuzz.h
CTxMemPoolEntry
Definition: txmempool.h:85
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::HighestTargetTracked
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Calculation of highest target that estimates are tracked for.
Definition: fees.cpp:705
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
CBlockPolicyEstimator::FlushUnconfirmed
void FlushUnconfirmed() EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool.
Definition: fees.cpp:988
ALL_FEE_ESTIMATE_HORIZONS
static constexpr auto ALL_FEE_ESTIMATE_HORIZONS
Definition: fees.h:34
FuzzedAutoFileProvider::open
CAutoFile open()
Definition: util.h:307
CBlockPolicyEstimator::processTransaction
void processTransaction(const CTxMemPoolEntry &entry, bool validFeeEstimate) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator)
Process a transaction accepted to the mempool.
Definition: fees.cpp:544