Branch data Line data Source code
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 : :
5 : : #include <kernel/mempool_entry.h>
6 : : #include <policy/fees.h>
7 : : #include <policy/fees_args.h>
8 : : #include <primitives/transaction.h>
9 : : #include <streams.h>
10 : : #include <test/fuzz/FuzzedDataProvider.h>
11 : : #include <test/fuzz/fuzz.h>
12 : : #include <test/fuzz/util.h>
13 : : #include <test/fuzz/util/mempool.h>
14 : : #include <test/util/setup_common.h>
15 : :
16 : : #include <memory>
17 : : #include <optional>
18 : : #include <vector>
19 : :
20 : : namespace {
21 : : const BasicTestingSetup* g_setup;
22 : : } // namespace
23 : :
24 : 1 : void initialize_policy_estimator()
25 : : {
26 [ + - + - ]: 2 : static const auto testing_setup = MakeNoLogFileContext<>();
27 : 1 : g_setup = testing_setup.get();
28 [ + - ]: 2 : }
29 : :
30 [ + - ]: 970 : FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
31 : : {
32 : 526 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33 : 526 : bool good_data{true};
34 : :
35 [ + - ]: 526 : CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES};
36 : :
37 : 526 : uint32_t current_height{0};
38 : 526 : const auto advance_height{
39 : 855 : [&] { current_height = fuzzed_data_provider.ConsumeIntegralInRange<decltype(current_height)>(current_height, 1 << 30); },
40 : 526 : };
41 : 526 : advance_height();
42 [ + + + + : 23800 : LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
+ + ]
43 : : {
44 [ + - ]: 11402 : CallOneOf(
45 : : fuzzed_data_provider,
46 : 72 : [&] {
47 : 72 : const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
48 [ + + ]: 72 : if (!mtx) {
49 : 14 : good_data = false;
50 [ - + ]: 14 : return;
51 : : }
52 [ + - ]: 58 : const CTransaction tx{*mtx};
53 : 58 : const auto entry{ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height)};
54 : 58 : const auto tx_submitted_in_package = fuzzed_data_provider.ConsumeBool();
55 : 58 : const auto tx_has_mempool_parents = fuzzed_data_provider.ConsumeBool();
56 [ + - ]: 58 : const auto tx_info = NewMempoolTransactionInfo(entry.GetSharedTx(), entry.GetFee(),
57 [ + - ]: 58 : entry.GetTxSize(), entry.GetHeight(),
58 : : /*mempool_limit_bypassed=*/false,
59 : : tx_submitted_in_package,
60 : : /*chainstate_is_current=*/true,
61 [ + - + - : 116 : tx_has_mempool_parents);
+ - ]
62 [ + - ]: 58 : block_policy_estimator.processTransaction(tx_info);
63 [ + + ]: 58 : if (fuzzed_data_provider.ConsumeBool()) {
64 [ + - ]: 14 : (void)block_policy_estimator.removeTx(tx.GetHash());
65 : : }
66 [ + - ]: 188 : },
67 : 329 : [&] {
68 : 329 : std::list<CTxMemPoolEntry> mempool_entries;
69 [ + + + - ]: 4384 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
70 : : {
71 : 4097 : const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
72 [ + + ]: 4097 : if (!mtx) {
73 : 42 : good_data = false;
74 [ - + ]: 42 : break;
75 : : }
76 [ + - ]: 4055 : const CTransaction tx{*mtx};
77 [ + - ]: 4055 : mempool_entries.emplace_back(CTxMemPoolEntry::ExplicitCopy, ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height));
78 [ + - ]: 8152 : }
79 : 329 : std::vector<RemovedMempoolTransactionInfo> txs;
80 [ + - ]: 329 : txs.reserve(mempool_entries.size());
81 [ + + ]: 4384 : for (const CTxMemPoolEntry& mempool_entry : mempool_entries) {
82 [ + - ]: 4055 : txs.emplace_back(mempool_entry);
83 : : }
84 : 329 : advance_height();
85 [ + - ]: 329 : block_policy_estimator.processBlock(txs, current_height);
86 : 329 : },
87 : 78 : [&] {
88 : 78 : (void)block_policy_estimator.removeTx(ConsumeUInt256(fuzzed_data_provider));
89 : 78 : },
90 : 10923 : [&] {
91 : 10923 : block_policy_estimator.FlushUnconfirmed();
92 : 10923 : });
93 [ + - ]: 11402 : (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
94 : 11402 : EstimationResult result;
95 : 11402 : auto conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
96 : 11402 : auto success_threshold = fuzzed_data_provider.ConsumeFloatingPoint<double>();
97 : 11402 : auto horizon = fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS);
98 [ + + ]: 11402 : auto* result_ptr = fuzzed_data_provider.ConsumeBool() ? &result : nullptr;
99 [ + - ]: 11402 : (void)block_policy_estimator.estimateRawFee(conf_target, success_threshold, horizon, result_ptr);
100 : :
101 : 11402 : FeeCalculation fee_calculation;
102 : 11402 : conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
103 [ + + ]: 11402 : auto* fee_calc_ptr = fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr;
104 : 11402 : auto conservative = fuzzed_data_provider.ConsumeBool();
105 [ + - ]: 11402 : (void)block_policy_estimator.estimateSmartFee(conf_target, fee_calc_ptr, conservative);
106 : :
107 [ + - ]: 11402 : (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
108 : : }
109 : 526 : {
110 [ + - ]: 526 : FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
111 [ + - + - ]: 526 : AutoFile fuzzed_auto_file{fuzzed_file_provider.open()};
112 [ + - ]: 526 : block_policy_estimator.Write(fuzzed_auto_file);
113 [ + - ]: 526 : block_policy_estimator.Read(fuzzed_auto_file);
114 : 0 : }
115 : 526 : }
|