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