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 : : #include <policy/fees/estimator_args.h>
7 : : #include <policy/fees/mempool_estimator.h>
8 : : #include <streams.h>
9 : : #include <test/fuzz/FuzzedDataProvider.h>
10 : : #include <test/fuzz/fuzz.h>
11 : : #include <test/fuzz/util.h>
12 : : #include <test/fuzz/util/reachability.h>
13 : : #include <test/util/setup_common.h>
14 : :
15 : : #include <memory>
16 : :
17 : : namespace {
18 : : const TestingSetup* g_setup;
19 : : constexpr ReachabilityGoal BLOCK_POLICY_ESTIMATOR_READ_SUCCEEDS{"block policy estimator reading succeeds"};
20 : : } // namespace
21 : :
22 : 1 : void initialize_policy_estimator_io()
23 : : {
24 : 1 : RegisterReachabilityGoal(BLOCK_POLICY_ESTIMATOR_READ_SUCCEEDS);
25 : 1 : static const auto testing_setup{
26 [ + - + - : 3 : MakeNoLogFileContext<const TestingSetup>(ChainType::REGTEST, TestOpts{.setup_net = false})};
+ - ]
27 : 1 : g_setup = testing_setup.get();
28 : 1 : }
29 : :
30 [ + - ]: 778 : FUZZ_TARGET(policy_estimator_io, .init = initialize_policy_estimator_io)
31 : : {
32 [ + + ]: 302 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
33 [ + + ]: 302 : FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
34 : : // Reuse estimators across runs to avoid costly object creation.
35 : 302 : static CBlockPolicyEstimator block_policy_estimator{
36 [ + + + - : 304 : BlockPolicyFeeEstPath(*g_setup->m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES};
+ - + - ]
37 : 302 : bool block_policy_read_success{false};
38 : 302 : {
39 : 604 : AutoFile fuzzed_auto_file_block_policy{fuzzed_file_provider.open()};
40 [ + - ]: 302 : block_policy_read_success = block_policy_estimator.Read(fuzzed_auto_file_block_policy);
41 [ + + ]: 302 : if (block_policy_read_success) {
42 [ + - ]: 76 : block_policy_estimator.Write(fuzzed_auto_file_block_policy);
43 : : }
44 [ + - ]: 302 : (void)fuzzed_auto_file_block_policy.fclose();
45 : 302 : }
46 : 302 : static MemPoolFeeRateEstimator mempool_feerate_estimator{
47 [ + - ]: 2 : MempoolPolicyEstimatorPath(*g_setup->m_node.args),
48 [ + - ]: 1 : *g_setup->m_node.mempool,
49 [ + + + - : 304 : *g_setup->m_node.chainman};
+ - ]
50 : 302 : {
51 : 604 : AutoFile fuzzed_auto_file_mempool_policy{fuzzed_file_provider.open()};
52 [ + - + + ]: 302 : if (mempool_feerate_estimator.Read(fuzzed_auto_file_mempool_policy)) {
53 [ + - ]: 13 : mempool_feerate_estimator.Write(fuzzed_auto_file_mempool_policy);
54 : : }
55 : : // Write() does not take ownership; close explicitly because it may
56 : : // have written to the fuzzed file.
57 [ + - ]: 302 : (void)fuzzed_auto_file_mempool_policy.fclose();
58 : 302 : }
59 : 302 : ObserveReachabilityGoal(block_policy_read_success, BLOCK_POLICY_ESTIMATOR_READ_SUCCEEDS);
60 : 302 : }
|