Coverage Report

Created: 2024-10-21 15:10

/root/bitcoin/src/test/fuzz/policy_estimator.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2020-2022 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
void initialize_policy_estimator()
25
0
{
26
0
    static const auto testing_setup = MakeNoLogFileContext<>();
27
0
    g_setup = testing_setup.get();
28
0
}
29
30
FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
31
0
{
32
0
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33
0
    bool good_data{true};
34
35
0
    CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES};
36
0
    LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
37
0
    {
38
0
        CallOneOf(
39
0
            fuzzed_data_provider,
40
0
            [&] {
41
0
                const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
42
0
                if (!mtx) {
43
0
                    good_data = false;
44
0
                    return;
45
0
                }
46
0
                const CTransaction tx{*mtx};
47
0
                const CTxMemPoolEntry& entry = ConsumeTxMemPoolEntry(fuzzed_data_provider, tx);
48
0
                const auto tx_submitted_in_package = fuzzed_data_provider.ConsumeBool();
49
0
                const auto tx_has_mempool_parents = fuzzed_data_provider.ConsumeBool();
50
0
                const auto tx_info = NewMempoolTransactionInfo(entry.GetSharedTx(), entry.GetFee(),
51
0
                                                               entry.GetTxSize(), entry.GetHeight(),
52
0
                                                               /*mempool_limit_bypassed=*/false,
53
0
                                                               tx_submitted_in_package,
54
0
                                                               /*chainstate_is_current=*/true,
55
0
                                                               tx_has_mempool_parents);
56
0
                block_policy_estimator.processTransaction(tx_info);
57
0
                if (fuzzed_data_provider.ConsumeBool()) {
58
0
                    (void)block_policy_estimator.removeTx(tx.GetHash());
59
0
                }
60
0
            },
61
0
            [&] {
62
0
                std::list<CTxMemPoolEntry> mempool_entries;
63
0
                LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
64
0
                {
65
0
                    const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
66
0
                    if (!mtx) {
67
0
                        good_data = false;
68
0
                        break;
69
0
                    }
70
0
                    const CTransaction tx{*mtx};
71
0
                    mempool_entries.emplace_back(CTxMemPoolEntry::ExplicitCopy, ConsumeTxMemPoolEntry(fuzzed_data_provider, tx));
72
0
                }
73
0
                std::vector<RemovedMempoolTransactionInfo> txs;
74
0
                txs.reserve(mempool_entries.size());
75
0
                for (const CTxMemPoolEntry& mempool_entry : mempool_entries) {
76
0
                    txs.emplace_back(mempool_entry);
77
0
                }
78
0
                block_policy_estimator.processBlock(txs, fuzzed_data_provider.ConsumeIntegral<unsigned int>());
79
0
            },
80
0
            [&] {
81
0
                (void)block_policy_estimator.removeTx(ConsumeUInt256(fuzzed_data_provider));
82
0
            },
83
0
            [&] {
84
0
                block_policy_estimator.FlushUnconfirmed();
85
0
            });
86
0
        (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
87
0
        EstimationResult result;
88
0
        auto conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
89
0
        auto success_threshold = fuzzed_data_provider.ConsumeFloatingPoint<double>();
90
0
        auto horizon = fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS);
91
0
        auto* result_ptr = fuzzed_data_provider.ConsumeBool() ? &result : nullptr;
92
0
        (void)block_policy_estimator.estimateRawFee(conf_target, success_threshold, horizon, result_ptr);
93
94
0
        FeeCalculation fee_calculation;
95
0
        conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
96
0
        auto* fee_calc_ptr = fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr;
97
0
        auto conservative = fuzzed_data_provider.ConsumeBool();
98
0
        (void)block_policy_estimator.estimateSmartFee(conf_target, fee_calc_ptr, conservative);
99
100
0
        (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
101
0
    }
102
0
    {
103
0
        FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
104
0
        AutoFile fuzzed_auto_file{fuzzed_file_provider.open()};
105
0
        block_policy_estimator.Write(fuzzed_auto_file);
106
0
        block_policy_estimator.Read(fuzzed_auto_file);
107
0
    }
108
0
}