LCOV - code coverage report
Current view: top level - src/test/fuzz - versionbits.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.9 % 174 172
Test Date: 2026-06-29 07:05:34 Functions: 100.0 % 8 8
Branches: 64.6 % 260 168

             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 <chain.h>
       6                 :             : #include <chainparams.h>
       7                 :             : #include <common/args.h>
       8                 :             : #include <consensus/params.h>
       9                 :             : #include <primitives/block.h>
      10                 :             : #include <util/chaintype.h>
      11                 :             : #include <versionbits.h>
      12                 :             : #include <versionbits_impl.h>
      13                 :             : 
      14                 :             : #include <test/fuzz/FuzzedDataProvider.h>
      15                 :             : #include <test/fuzz/fuzz.h>
      16                 :             : #include <test/fuzz/util.h>
      17                 :             : #include <test/util/versionbits.h>
      18                 :             : 
      19                 :             : #include <cstdint>
      20                 :             : #include <limits>
      21                 :             : #include <memory>
      22                 :             : #include <vector>
      23                 :             : 
      24                 :             : namespace {
      25                 :         254 : class TestConditionChecker : public VersionBitsConditionChecker
      26                 :             : {
      27                 :             : private:
      28                 :             :     mutable ThresholdConditionCache m_cache;
      29                 :             : 
      30                 :             : public:
      31         [ -  + ]:         254 :     TestConditionChecker(const Consensus::BIP9Deployment& dep) : VersionBitsConditionChecker{dep}
      32                 :             :     {
      33         [ -  + ]:         254 :         assert(dep.period > 0);
      34         [ -  + ]:         254 :         assert(dep.threshold <= dep.period);
      35         [ -  + ]:         254 :         assert(0 <= dep.bit && dep.bit < 32 && dep.bit < VERSIONBITS_MAX_NUM_BITS);
      36         [ -  + ]:         254 :         assert(0 <= dep.min_activation_height);
      37                 :         254 :     }
      38                 :             : 
      39                 :        7557 :     ThresholdState GetStateFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateFor(pindexPrev, m_cache); }
      40                 :        7557 :     int GetStateSinceHeightFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor(pindexPrev, m_cache); }
      41                 :             : };
      42                 :             : 
      43                 :             : /** Track blocks mined for test */
      44                 :           0 : class Blocks
      45                 :             : {
      46                 :             : private:
      47                 :             :     std::vector<std::unique_ptr<CBlockIndex>> m_blocks;
      48                 :             :     const uint32_t m_start_time;
      49                 :             :     const uint32_t m_interval;
      50                 :             :     const int32_t m_signal;
      51                 :             :     const int32_t m_no_signal;
      52                 :             : 
      53                 :             : public:
      54                 :         254 :     Blocks(uint32_t start_time, uint32_t interval, int32_t signal, int32_t no_signal)
      55                 :         254 :         : m_start_time{start_time}, m_interval{interval}, m_signal{signal}, m_no_signal{no_signal} {}
      56                 :             : 
      57                 :        6058 :     size_t size() const { return m_blocks.size(); }
      58                 :             : 
      59                 :       97157 :     CBlockIndex* tip() const
      60                 :             :     {
      61         [ +  + ]:       97157 :         return m_blocks.empty() ? nullptr : m_blocks.back().get();
      62                 :             :     }
      63                 :             : 
      64                 :       96928 :     CBlockIndex* mine_block(bool signal)
      65                 :             :     {
      66                 :       96928 :         CBlockHeader header;
      67         [ +  + ]:       96928 :         header.nVersion = signal ? m_signal : m_no_signal;
      68         [ -  + ]:       96928 :         header.nTime = m_start_time + m_blocks.size() * m_interval;
      69                 :       96928 :         header.nBits = 0x1d00ffff;
      70                 :             : 
      71                 :       96928 :         auto current_block = std::make_unique<CBlockIndex>(header);
      72         [ -  + ]:       96928 :         current_block->pprev = tip();
      73   [ -  +  +  - ]:       96928 :         current_block->nHeight = m_blocks.size();
      74         [ +  - ]:       96928 :         current_block->BuildSkip();
      75                 :             : 
      76         [ +  - ]:       96928 :         return m_blocks.emplace_back(std::move(current_block)).get();
      77                 :       96928 :     }
      78                 :             : };
      79                 :             : 
      80                 :             : std::unique_ptr<const CChainParams> g_params;
      81                 :             : 
      82                 :           1 : void initialize()
      83                 :             : {
      84                 :             :     // this is actually comparatively slow, so only do it once
      85         [ +  - ]:           2 :     g_params = CreateChainParams(ArgsManager{}, ChainType::MAIN);
      86         [ -  + ]:           1 :     assert(g_params != nullptr);
      87                 :           1 : }
      88                 :             : 
      89                 :             : constexpr uint32_t MAX_START_TIME = 4102444800; // 2100-01-01
      90                 :             : 
      91         [ +  - ]:         743 : FUZZ_TARGET(versionbits, .init = initialize)
      92                 :             : {
      93         [ -  + ]:         277 :     const CChainParams& params = *g_params;
      94                 :         277 :     const int64_t interval = params.GetConsensus().nPowTargetSpacing;
      95         [ -  + ]:         277 :     assert(interval > 1); // need to be able to halve it
      96         [ -  + ]:         277 :     assert(interval < std::numeric_limits<int32_t>::max());
      97                 :             : 
      98         [ -  + ]:         277 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      99                 :             : 
     100                 :             :     // making period/max_periods larger slows these tests down significantly
     101                 :         277 :     const uint32_t period = 32;
     102                 :         277 :     const size_t max_periods = 16;
     103                 :         277 :     const size_t max_blocks = 2 * period * max_periods;
     104                 :             : 
     105                 :             :     // too many blocks at 10min each might cause uint32_t time to overflow if
     106                 :             :     // block_start_time is at the end of the range above
     107         [ -  + ]:         277 :     assert(std::numeric_limits<uint32_t>::max() - MAX_START_TIME > interval * max_blocks);
     108                 :             : 
     109                 :         277 :     const int64_t block_start_time = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(params.GenesisBlock().nTime, MAX_START_TIME);
     110                 :             : 
     111                 :             :     // what values for version will we use to signal / not signal?
     112                 :         277 :     const int32_t ver_signal = fuzzed_data_provider.ConsumeIntegral<int32_t>();
     113                 :         277 :     const int32_t ver_nosignal = fuzzed_data_provider.ConsumeIntegral<int32_t>();
     114         [ +  + ]:         277 :     if (ver_nosignal < 0) return; // negative values are uninteresting
     115                 :             : 
     116                 :             :     // Now that we have chosen time and versions, setup to mine blocks
     117                 :         254 :     Blocks blocks(block_start_time, interval, ver_signal, ver_nosignal);
     118                 :             : 
     119                 :         254 :     const bool always_active_test = fuzzed_data_provider.ConsumeBool();
     120   [ +  +  +  + ]:         464 :     const bool never_active_test = !always_active_test && fuzzed_data_provider.ConsumeBool();
     121                 :             : 
     122                 :         508 :     const Consensus::BIP9Deployment dep{[&]() {
     123                 :         254 :         Consensus::BIP9Deployment dep;
     124                 :         254 :         dep.period = period;
     125                 :             : 
     126                 :         254 :         dep.threshold = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, period);
     127   [ +  -  -  + ]:         254 :         assert(0 < dep.threshold && dep.threshold <= dep.period); // must be able to both pass and fail threshold!
     128                 :             : 
     129                 :             :         // select deployment parameters: bit, start time, timeout
     130                 :         254 :         dep.bit = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, VERSIONBITS_MAX_NUM_BITS - 1);
     131                 :             : 
     132         [ +  + ]:         254 :         if (always_active_test) {
     133                 :          44 :             dep.nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;
     134         [ +  + ]:          44 :             dep.nTimeout = fuzzed_data_provider.ConsumeBool() ? Consensus::BIP9Deployment::NO_TIMEOUT : fuzzed_data_provider.ConsumeIntegral<int64_t>();
     135         [ +  + ]:         210 :         } else if (never_active_test) {
     136                 :          11 :             dep.nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE;
     137         [ +  + ]:          11 :             dep.nTimeout = fuzzed_data_provider.ConsumeBool() ? Consensus::BIP9Deployment::NO_TIMEOUT : fuzzed_data_provider.ConsumeIntegral<int64_t>();
     138                 :             :         } else {
     139                 :             :             // pick the timestamp to switch based on a block
     140                 :             :             // note states will change *after* these blocks because mediantime lags
     141                 :         199 :             int start_block = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, period * (max_periods - 3));
     142                 :         199 :             int end_block = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, period * (max_periods - 3));
     143                 :             : 
     144                 :         199 :             dep.nStartTime = block_start_time + start_block * interval;
     145                 :         199 :             dep.nTimeout = block_start_time + end_block * interval;
     146                 :             : 
     147                 :             :             // allow for times to not exactly match a block
     148         [ +  + ]:         199 :             if (fuzzed_data_provider.ConsumeBool()) dep.nStartTime += interval / 2;
     149         [ +  + ]:         199 :             if (fuzzed_data_provider.ConsumeBool()) dep.nTimeout += interval / 2;
     150                 :             :         }
     151                 :         254 :         dep.min_activation_height = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, period * max_periods);
     152                 :         254 :         return dep;
     153                 :         254 :     }()};
     154                 :         254 :     TestConditionChecker checker(dep);
     155                 :             : 
     156                 :             :     // Early exit if the versions don't signal sensibly for the deployment
     157         [ +  + ]:         254 :     if (!checker.Condition(ver_signal)) return;
     158         [ +  + ]:         258 :     if (checker.Condition(ver_nosignal)) return;
     159                 :             : 
     160                 :             :     // TOP_BITS should ensure version will be positive and meet min
     161                 :             :     // version requirement
     162         [ -  + ]:         229 :     assert(ver_signal > 0);
     163         [ -  + ]:         229 :     assert(ver_signal >= VERSIONBITS_LAST_OLD_BLOCK_VERSION);
     164                 :             : 
     165                 :             :     /* Strategy:
     166                 :             :      *  * we will mine a final period worth of blocks, with
     167                 :             :      *    randomised signalling according to a mask
     168                 :             :      *  * but before we mine those blocks, we will mine some
     169                 :             :      *    randomised number of prior periods; with either all
     170                 :             :      *    or no blocks in the period signalling
     171                 :             :      *
     172                 :             :      * We establish the mask first, then consume "bools" until
     173                 :             :      * we run out of fuzz data to work out how many prior periods
     174                 :             :      * there are and which ones will signal.
     175                 :             :      */
     176                 :             : 
     177                 :             :     // establish the mask
     178                 :         229 :     const uint32_t signalling_mask = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
     179                 :             : 
     180                 :             :     // mine prior periods
     181         [ +  + ]:        3224 :     while (fuzzed_data_provider.remaining_bytes() > 0) { // early exit; no need for LIMITED_WHILE
     182                 :             :         // all blocks in these periods either do or don't signal
     183                 :        2800 :         bool signal = fuzzed_data_provider.ConsumeBool();
     184         [ +  + ]:       92400 :         for (uint32_t b = 0; b < period; ++b) {
     185         [ +  - ]:       89600 :             blocks.mine_block(signal);
     186                 :             :         }
     187                 :             : 
     188                 :             :         // don't risk exceeding max_blocks or times may wrap around
     189   [ -  +  +  + ]:        2800 :         if (blocks.size() + 2 * period > max_blocks) break;
     190                 :             :     }
     191                 :             :     // NOTE: fuzzed_data_provider may be fully consumed at this point and should not be used further
     192                 :             : 
     193                 :             :     // now we mine the final period and check that everything looks sane
     194                 :             : 
     195                 :             :     // count the number of signalling blocks
     196                 :         229 :     uint32_t blocks_sig = 0;
     197                 :             : 
     198                 :             :     // get the info for the first block of the period
     199                 :         229 :     CBlockIndex* prev = blocks.tip();
     200         [ +  - ]:         229 :     const int exp_since = checker.GetStateSinceHeightFor(prev);
     201         [ +  - ]:         229 :     const ThresholdState exp_state = checker.GetStateFor(prev);
     202                 :             : 
     203                 :             :     // get statistics from end of previous period, then reset
     204                 :         229 :     BIP9Stats last_stats;
     205                 :         229 :     last_stats.period = period;
     206                 :         229 :     last_stats.threshold = dep.threshold;
     207                 :         229 :     last_stats.count = last_stats.elapsed = 0;
     208                 :         229 :     last_stats.possible = (period >= dep.threshold);
     209                 :         229 :     std::vector<bool> last_signals{};
     210                 :             : 
     211         [ +  + ]:         229 :     int prev_next_height = (prev == nullptr ? 0 : prev->nHeight + 1);
     212         [ -  + ]:         229 :     assert(exp_since <= prev_next_height);
     213                 :             : 
     214                 :             :     // mine (period-1) blocks and check state
     215         [ +  + ]:        7328 :     for (uint32_t b = 1; b < period; ++b) {
     216                 :        7099 :         const bool signal = (signalling_mask >> (b % 32)) & 1;
     217         [ +  + ]:        7099 :         if (signal) ++blocks_sig;
     218                 :             : 
     219         [ +  - ]:        7099 :         CBlockIndex* current_block = blocks.mine_block(signal);
     220                 :             : 
     221                 :             :         // verify that signalling attempt was interpreted correctly
     222   [ +  +  -  + ]:       10462 :         assert(checker.Condition(current_block->nVersion) == signal);
     223                 :             : 
     224                 :             :         // state and since don't change within the period
     225         [ +  - ]:        7099 :         const ThresholdState state = checker.GetStateFor(current_block);
     226         [ +  - ]:        7099 :         const int since = checker.GetStateSinceHeightFor(current_block);
     227         [ -  + ]:        7099 :         assert(state == exp_state);
     228         [ -  + ]:        7099 :         assert(since == exp_since);
     229                 :             : 
     230                 :             :         // check that after mining this block stats change as expected
     231                 :        7099 :         std::vector<bool> signals;
     232         [ +  - ]:        7099 :         const BIP9Stats stats = checker.GetStateStatisticsFor(current_block, &signals);
     233         [ +  - ]:        7099 :         const BIP9Stats stats_no_signals = checker.GetStateStatisticsFor(current_block);
     234   [ +  -  +  -  :        7099 :         assert(stats.period == stats_no_signals.period && stats.threshold == stats_no_signals.threshold
          +  -  +  -  -  
                      + ]
     235                 :             :                && stats.elapsed == stats_no_signals.elapsed && stats.count == stats_no_signals.count
     236                 :             :                && stats.possible == stats_no_signals.possible);
     237                 :             : 
     238         [ -  + ]:        7099 :         assert(stats.period == period);
     239         [ -  + ]:        7099 :         assert(stats.threshold == dep.threshold);
     240         [ -  + ]:        7099 :         assert(stats.elapsed == b);
     241   [ +  +  -  + ]:       11383 :         assert(stats.count == last_stats.count + (signal ? 1 : 0));
     242         [ -  + ]:        7099 :         assert(stats.possible == (stats.count + period >= stats.elapsed + dep.threshold));
     243                 :        7099 :         last_stats = stats;
     244                 :             : 
     245         [ -  + ]:        7099 :         assert(signals.size() == last_signals.size() + 1);
     246         [ -  + ]:        7099 :         assert(signals.back() == signal);
     247         [ +  - ]:        7099 :         last_signals.push_back(signal);
     248         [ -  + ]:        7099 :         assert(signals == last_signals);
     249                 :        7099 :     }
     250                 :             : 
     251         [ +  + ]:         229 :     if (exp_state == ThresholdState::STARTED) {
     252                 :             :         // double check that stats.possible is sane
     253   [ +  +  -  + ]:          43 :         if (blocks_sig >= dep.threshold - 1) assert(last_stats.possible);
     254                 :             :     }
     255                 :             : 
     256                 :             :     // mine the final block
     257                 :         229 :     bool signal = (signalling_mask >> (period % 32)) & 1;
     258         [ +  + ]:         229 :     if (signal) ++blocks_sig;
     259         [ +  - ]:         229 :     CBlockIndex* current_block = blocks.mine_block(signal);
     260   [ +  +  -  + ]:         350 :     assert(checker.Condition(current_block->nVersion) == signal);
     261                 :             : 
     262         [ +  - ]:         229 :     const BIP9Stats stats = checker.GetStateStatisticsFor(current_block);
     263         [ -  + ]:         229 :     assert(stats.period == period);
     264         [ -  + ]:         229 :     assert(stats.threshold == dep.threshold);
     265         [ -  + ]:         229 :     assert(stats.elapsed == period);
     266         [ -  + ]:         229 :     assert(stats.count == blocks_sig);
     267         [ -  + ]:         229 :     assert(stats.possible == (stats.count + period >= stats.elapsed + dep.threshold));
     268                 :             : 
     269                 :             :     // More interesting is whether the state changed.
     270         [ +  - ]:         229 :     const ThresholdState state = checker.GetStateFor(current_block);
     271         [ +  - ]:         229 :     const int since = checker.GetStateSinceHeightFor(current_block);
     272                 :             : 
     273                 :             :     // since is straightforward:
     274         [ -  + ]:         229 :     assert(since % period == 0);
     275   [ +  -  -  + ]:         229 :     assert(0 <= since && since <= current_block->nHeight + 1);
     276         [ +  + ]:         229 :     if (state == exp_state) {
     277         [ -  + ]:         176 :         assert(since == exp_since);
     278                 :             :     } else {
     279         [ -  + ]:          53 :         assert(since == current_block->nHeight + 1);
     280                 :             :     }
     281                 :             : 
     282                 :             :     // state is where everything interesting is
     283                 :         458 :     [&]() {
     284   [ +  +  +  +  :         229 :         switch (state) {
                   +  - ]
     285                 :          19 :         case ThresholdState::DEFINED:
     286         [ -  + ]:          19 :             assert(since == 0);
     287         [ -  + ]:          19 :             assert(exp_state == ThresholdState::DEFINED);
     288         [ -  + ]:          19 :             assert(current_block->GetMedianTimePast() < dep.nStartTime);
     289                 :             :             return;
     290                 :          30 :         case ThresholdState::STARTED:
     291         [ -  + ]:          30 :             assert(current_block->GetMedianTimePast() >= dep.nStartTime);
     292         [ +  + ]:          30 :             if (exp_state == ThresholdState::STARTED) {
     293         [ -  + ]:          14 :                 assert(blocks_sig < dep.threshold);
     294         [ -  + ]:          14 :                 assert(current_block->GetMedianTimePast() < dep.nTimeout);
     295                 :             :             } else {
     296         [ -  + ]:          16 :                 assert(exp_state == ThresholdState::DEFINED);
     297                 :             :             }
     298                 :             :             return;
     299                 :          25 :         case ThresholdState::LOCKED_IN:
     300         [ +  + ]:          25 :             if (exp_state == ThresholdState::LOCKED_IN) {
     301         [ -  + ]:           5 :                 assert(current_block->nHeight + 1 < dep.min_activation_height);
     302                 :             :             } else {
     303         [ -  + ]:          20 :                 assert(exp_state == ThresholdState::STARTED);
     304         [ -  + ]:          20 :                 assert(blocks_sig >= dep.threshold);
     305                 :             :             }
     306                 :             :             return;
     307                 :          99 :         case ThresholdState::ACTIVE:
     308   [ +  +  -  + ]:          99 :             assert(always_active_test || dep.min_activation_height <= current_block->nHeight + 1);
     309         [ -  + ]:          99 :             assert(exp_state == ThresholdState::ACTIVE || exp_state == ThresholdState::LOCKED_IN);
     310                 :             :             return;
     311                 :          56 :         case ThresholdState::FAILED:
     312   [ +  +  -  + ]:          56 :             assert(never_active_test || current_block->GetMedianTimePast() >= dep.nTimeout);
     313         [ +  + ]:          56 :             if (exp_state == ThresholdState::STARTED) {
     314         [ -  + ]:           9 :                 assert(blocks_sig < dep.threshold);
     315                 :             :             } else {
     316         [ -  + ]:          47 :                 assert(exp_state == ThresholdState::FAILED);
     317                 :             :             }
     318                 :             :             return;
     319                 :             :         } // no default case, so the compiler can warn about missing cases
     320                 :           0 :         assert(false);
     321                 :         229 :     }();
     322                 :             : 
     323   [ -  +  +  + ]:         229 :     if (blocks.size() >= period * max_periods) {
     324                 :             :         // we chose the timeout (and block times) so that by the time we have this many blocks it's all over
     325         [ -  + ]:          88 :         assert(state == ThresholdState::ACTIVE || state == ThresholdState::FAILED);
     326                 :             :     }
     327                 :             : 
     328         [ +  + ]:         229 :     if (always_active_test) {
     329                 :             :         // "always active" has additional restrictions
     330         [ -  + ]:          26 :         assert(state == ThresholdState::ACTIVE);
     331         [ -  + ]:          26 :         assert(exp_state == ThresholdState::ACTIVE);
     332         [ -  + ]:          26 :         assert(since == 0);
     333         [ +  + ]:         203 :     } else if (never_active_test) {
     334                 :             :         // "never active" does too
     335         [ -  + ]:           7 :         assert(state == ThresholdState::FAILED);
     336         [ -  + ]:           7 :         assert(exp_state == ThresholdState::FAILED);
     337         [ -  + ]:           7 :         assert(since == 0);
     338                 :             :     } else {
     339                 :             :         // for signalled deployments, the initial state is always DEFINED
     340   [ +  +  -  + ]:         196 :         assert(since > 0 || state == ThresholdState::DEFINED);
     341   [ +  +  -  + ]:         196 :         assert(exp_since > 0 || exp_state == ThresholdState::DEFINED);
     342                 :             :     }
     343                 :         254 : }
     344                 :             : } // namespace
        

Generated by: LCOV version 2.0-1