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>
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()) {
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}
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:565
The BlockPolicyEstimator is used for estimating the feerate needed for a transaction to be included i...
Definition: fees.h:132
bool Read(CAutoFile &filein)
Read estimation data from a file.
Definition: fees.cpp:919
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
bool Write(CAutoFile &fileout) const
Write estimation data to a file.
Definition: fees.cpp:894
bool removeTx(uint256 hash, bool inBlock)
Remove a transaction from the mempool tracking stats.
Definition: fees.cpp:493
void FlushUnconfirmed()
Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool.
Definition: fees.cpp:981
void processTransaction(const CTxMemPoolEntry &entry, bool validFeeEstimate)
Process a transaction accepted to the mempool.
Definition: fees.cpp:538
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
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
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const
Calculation of highest target that estimates are tracked for.
Definition: fees.cpp:698
CFeeRate estimateFee(int confTarget) const
DEPRECATED.
Definition: fees.cpp:653
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:260
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:80
CAutoFile open()
Definition: util.h:316
T PickValueInArray(const T(&array)[size])
static constexpr auto ALL_FEE_ESTIMATE_HORIZONS
Definition: fees.h:34
void initialize_policy_estimator()
FUZZ_TARGET_INIT(policy_estimator, initialize_policy_estimator)
CTxMemPoolEntry ConsumeTxMemPoolEntry(FuzzedDataProvider &fuzzed_data_provider, const CTransaction &tx) noexcept
Definition: util.cpp:348
FuzzedAutoFileProvider ConsumeAutoFile(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:322
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:153
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40