LCOV - code coverage report
Current view: top level - src/test/fuzz - policy_estimator.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 75 75
Test Date: 2026-07-15 07:24:03 Functions: 100.0 % 7 7
Branches: 61.4 % 88 54

             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/block_policy_estimator.h>
       7                 :             : #include <policy/fees/block_policy_estimator_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   [ +  -  +  -  :           1 :     static const auto testing_setup = MakeNoLogFileContext<>();
                   +  - ]
      27                 :           1 :     g_setup = testing_setup.get();
      28                 :           1 : }
      29                 :             : 
      30         [ +  - ]:        1480 : FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
      31                 :             : {
      32                 :        1008 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      33                 :        1008 :     bool good_data{true};
      34                 :             : 
      35         [ +  - ]:        1008 :     CBlockPolicyEstimator block_policy_estimator{FeeestPath(*g_setup->m_node.args), DEFAULT_ACCEPT_STALE_FEE_ESTIMATES};
      36                 :             : 
      37                 :        1008 :     uint32_t current_height{0};
      38                 :        1008 :     const auto advance_height{
      39                 :        7608 :         [&] { current_height = fuzzed_data_provider.ConsumeIntegralInRange<decltype(current_height)>(current_height, 1 << 30); },
      40                 :        1008 :     };
      41                 :        1008 :     advance_height();
      42   [ +  +  +  +  :      159626 :     LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {
                   +  + ]
      43         [ +  - ]:       78922 :         CallOneOf(
      44                 :             :             fuzzed_data_provider,
      45                 :       35285 :             [&] {
      46                 :       35285 :                 const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
      47         [ +  + ]:       35285 :                 if (!mtx) {
      48                 :          84 :                     good_data = false;
      49         [ -  + ]:          84 :                     return;
      50                 :             :                 }
      51         [ +  - ]:       35201 :                 const CTransaction tx{*mtx};
      52                 :       35201 :                 const auto entry{ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height)};
      53                 :       35201 :                 const auto tx_submitted_in_package = fuzzed_data_provider.ConsumeBool();
      54                 :       35201 :                 const auto tx_has_mempool_parents = fuzzed_data_provider.ConsumeBool();
      55         [ +  - ]:       35201 :                 const auto tx_info = NewMempoolTransactionInfo(entry.GetSharedTx(), entry.GetFee(),
      56         [ +  - ]:       35201 :                                                                entry.GetTxSize(), entry.GetHeight(),
      57                 :             :                                                                /*mempool_limit_bypassed=*/false,
      58                 :             :                                                                tx_submitted_in_package,
      59                 :             :                                                                /*chainstate_is_current=*/true,
      60   [ +  -  +  -  :       70402 :                                                                tx_has_mempool_parents);
                   +  - ]
      61         [ +  - ]:       35201 :                 block_policy_estimator.processTransaction(tx_info);
      62         [ +  + ]:       35201 :                 if (fuzzed_data_provider.ConsumeBool()) {
      63         [ +  - ]:        4628 :                     (void)block_policy_estimator.removeTx(tx.GetHash());
      64                 :             :                 }
      65         [ +  - ]:      105687 :             },
      66                 :        6600 :             [&] {
      67                 :        6600 :                 std::list<CTxMemPoolEntry> mempool_entries;
      68   [ +  +  +  - ]:       44926 :                 LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
      69                 :       38476 :                     const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
      70         [ +  + ]:       38476 :                     if (!mtx) {
      71                 :         150 :                         good_data = false;
      72         [ -  + ]:         150 :                         break;
      73                 :             :                     }
      74         [ +  - ]:       38326 :                     const CTransaction tx{*mtx};
      75         [ +  - ]:       38326 :                     mempool_entries.push_back(ConsumeTxMemPoolEntry(fuzzed_data_provider, tx, current_height));
      76         [ +  - ]:       76802 :                 }
      77                 :        6600 :                 std::vector<RemovedMempoolTransactionInfo> txs;
      78         [ +  - ]:        6600 :                 txs.reserve(mempool_entries.size());
      79         [ +  + ]:       44926 :                 for (const CTxMemPoolEntry& mempool_entry : mempool_entries) {
      80         [ +  - ]:       38326 :                     txs.emplace_back(mempool_entry);
      81                 :             :                 }
      82                 :        6600 :                 advance_height();
      83         [ +  - ]:        6600 :                 block_policy_estimator.processBlock(txs, current_height);
      84                 :        6600 :             },
      85                 :        2506 :             [&] {
      86                 :        2506 :                 (void)block_policy_estimator.removeTx(Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider)));
      87                 :        2506 :             },
      88                 :       34531 :             [&] {
      89                 :       34531 :                 block_policy_estimator.FlushUnconfirmed();
      90                 :       34531 :             });
      91         [ +  - ]:       78922 :         (void)block_policy_estimator.estimateFee(fuzzed_data_provider.ConsumeIntegral<int>());
      92                 :       78922 :         EstimationResult result;
      93                 :       78922 :         auto conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
      94                 :       78922 :         auto success_threshold = fuzzed_data_provider.ConsumeFloatingPoint<double>();
      95                 :       78922 :         auto horizon = fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS);
      96         [ +  + ]:       78922 :         auto* result_ptr = fuzzed_data_provider.ConsumeBool() ? &result : nullptr;
      97         [ +  - ]:       78922 :         (void)block_policy_estimator.estimateRawFee(conf_target, success_threshold, horizon, result_ptr);
      98                 :             : 
      99                 :       78922 :         FeeCalculation fee_calculation;
     100                 :       78922 :         conf_target = fuzzed_data_provider.ConsumeIntegral<int>();
     101         [ +  + ]:       78922 :         auto* fee_calc_ptr = fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr;
     102                 :       78922 :         auto conservative = fuzzed_data_provider.ConsumeBool();
     103         [ +  - ]:       78922 :         (void)block_policy_estimator.estimateSmartFee(conf_target, fee_calc_ptr, conservative);
     104                 :             : 
     105         [ +  - ]:       78922 :         (void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray(ALL_FEE_ESTIMATE_HORIZONS));
     106                 :             :     }
     107                 :        1008 :     {
     108         [ +  - ]:        1008 :         FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
     109   [ +  -  +  - ]:        2016 :         AutoFile fuzzed_auto_file{fuzzed_file_provider.open()};
     110         [ +  - ]:        1008 :         block_policy_estimator.Write(fuzzed_auto_file);
     111         [ +  - ]:        1008 :         block_policy_estimator.Read(fuzzed_auto_file);
     112         [ +  - ]:        1008 :         (void)fuzzed_auto_file.fclose();
     113                 :        1008 :     }
     114                 :        1008 : }
        

Generated by: LCOV version 2.0-1