LCOV - code coverage report
Current view: top level - src/test - versionbits_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 97.8 % 276 270
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 18 18
Branches: 51.2 % 1016 520

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2014-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 <consensus/params.h>
       8                 :             : #include <test/util/random.h>
       9                 :             : #include <test/util/common.h>
      10                 :             : #include <test/util/setup_common.h>
      11                 :             : #include <test/util/versionbits.h>
      12                 :             : #include <util/chaintype.h>
      13                 :             : #include <validation.h>
      14                 :             : #include <versionbits.h>
      15                 :             : #include <versionbits_impl.h>
      16                 :             : 
      17                 :             : #include <boost/test/unit_test.hpp>
      18                 :             : 
      19                 :             : /* Define a virtual block time, one block per 10 minutes after Nov 14 2014, 0:55:36am */
      20                 :        1484 : static int32_t TestTime(int nHeight) { return 1415926536 + 600 * nHeight; }
      21                 :             : 
      22                 :             : class TestConditionChecker final : public VersionBitsConditionChecker
      23                 :             : {
      24                 :             : private:
      25                 :             :     mutable ThresholdConditionCache cache;
      26                 :             : 
      27                 :             : public:
      28                 :             :     // constructor is implicit to allow for easier initialization of vector<TestConditionChecker>
      29                 :         280 :     explicit(false) TestConditionChecker(const Consensus::BIP9Deployment& dep) : VersionBitsConditionChecker{dep} { }
      30   [ +  -  +  -  :        1750 :     ~TestConditionChecker() override = default;
                   +  - ]
      31                 :             : 
      32                 :        6876 :     ThresholdState StateFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateFor(pindexPrev, cache); }
      33   [ +  -  +  -  :       26612 :     int StateSinceHeightFor(const CBlockIndex* pindexPrev) const { return AbstractThresholdConditionChecker::GetStateSinceHeightFor(pindexPrev, cache); }
             +  -  +  - ]
      34                 :        1956 :     void clear() { cache.clear(); }
      35                 :             : };
      36                 :             : 
      37                 :             : namespace {
      38                 :             : struct Deployments
      39                 :             : {
      40                 :             :     const Consensus::BIP9Deployment normal{
      41                 :             :         .bit = 8,
      42                 :             :         .nStartTime = TestTime(10000),
      43                 :             :         .nTimeout = TestTime(20000),
      44                 :             :         .min_activation_height = 0,
      45                 :             :         .period = 1000,
      46                 :             :         .threshold = 900,
      47                 :             :     };
      48                 :             :     Consensus::BIP9Deployment always, never, delayed;
      49                 :          70 :     Deployments()
      50                 :          70 :     {
      51                 :          70 :         delayed = normal; delayed.min_activation_height = 15000;
      52                 :          70 :         always = normal; always.nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;
      53                 :          70 :         never = normal; never.nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE;
      54                 :          70 :     }
      55                 :             : };
      56                 :             : }
      57                 :             : 
      58                 :             : #define CHECKERS 6
      59                 :             : 
      60                 :             : class VersionBitsTester
      61                 :             : {
      62                 :             :     FastRandomContext& m_rng;
      63                 :             :     // A fake blockchain
      64                 :             :     std::vector<CBlockIndex*> vpblock;
      65                 :             : 
      66                 :             :     // Used to automatically set the top bits for manual calls to Mine()
      67                 :             :     const int32_t nVersionBase{0};
      68                 :             : 
      69                 :             :     // Setup BIP9Deployment structs for the checkers
      70                 :             :     const Deployments test_deployments;
      71                 :             : 
      72                 :             :     // 6 independent checkers for the same bit.
      73                 :             :     // The first one performs all checks, the second only 50%, the third only 25%, etc...
      74                 :             :     // This is to test whether lack of cached information leads to the same results.
      75                 :             :     std::vector<TestConditionChecker> checker{CHECKERS, {test_deployments.normal}};
      76                 :             :     // Another 6 that assume delayed activation
      77                 :             :     std::vector<TestConditionChecker> checker_delayed{CHECKERS, {test_deployments.delayed}};
      78                 :             :     // Another 6 that assume always active activation
      79                 :             :     std::vector<TestConditionChecker> checker_always{CHECKERS, {test_deployments.always}};
      80                 :             :     // Another 6 that assume never active activation
      81                 :             :     std::vector<TestConditionChecker> checker_never{CHECKERS, {test_deployments.never}};
      82                 :             : 
      83                 :             :     // Test counter (to identify failures)
      84                 :             :     int num{1000};
      85                 :             : 
      86                 :             : public:
      87   [ +  -  +  -  :         350 :     explicit VersionBitsTester(FastRandomContext& rng, int32_t nVersionBase=0) : m_rng{rng}, nVersionBase{nVersionBase} { }
             +  -  +  - ]
      88                 :             : 
      89                 :         326 :     VersionBitsTester& Reset() {
      90                 :             :         // Have each group of tests be counted by the 1000s part, starting at 1000
      91                 :         326 :         num = num - (num % 1000) + 1000;
      92                 :             : 
      93   [ -  +  +  + ]:     5526838 :         for (unsigned int i = 0; i < vpblock.size(); i++) {
      94         [ +  - ]:     5526512 :             delete vpblock[i];
      95                 :             :         }
      96         [ +  + ]:        2282 :         for (unsigned int  i = 0; i < CHECKERS; i++) {
      97                 :        1956 :             checker[i].clear();
      98                 :        1956 :             checker_delayed[i].clear();
      99                 :        1956 :             checker_always[i].clear();
     100                 :        1956 :             checker_never[i].clear();
     101                 :             :         }
     102         [ +  - ]:         326 :         vpblock.clear();
     103                 :         326 :         return *this;
     104                 :             :     }
     105                 :             : 
     106                 :          70 :     ~VersionBitsTester() {
     107                 :          70 :          Reset();
     108                 :          70 :     }
     109                 :             : 
     110                 :        4597 :     VersionBitsTester& Mine(unsigned int height, int32_t nTime, int32_t nVersion) {
     111   [ -  +  +  + ]:     5531109 :         while (vpblock.size() < height) {
     112                 :     5526512 :             CBlockIndex* pindex = new CBlockIndex();
     113         [ -  + ]:     5526512 :             pindex->nHeight = vpblock.size();
     114                 :     5526512 :             pindex->pprev = Tip();
     115                 :     5526512 :             pindex->nTime = nTime;
     116                 :     5526512 :             pindex->nVersion = (nVersionBase | nVersion);
     117                 :     5526512 :             pindex->BuildSkip();
     118                 :     5526512 :             vpblock.push_back(pindex);
     119                 :             :         }
     120                 :        4597 :         return *this;
     121                 :             :     }
     122                 :             : 
     123                 :        2944 :     VersionBitsTester& TestStateSinceHeight(int height)
     124                 :             :     {
     125                 :        2944 :         return TestStateSinceHeight(height, height);
     126                 :             :     }
     127                 :             : 
     128                 :        3392 :     VersionBitsTester& TestStateSinceHeight(int height, int height_delayed)
     129                 :             :     {
     130                 :        3392 :         const CBlockIndex* tip = Tip();
     131         [ +  + ]:       23744 :         for (int i = 0; i < CHECKERS; i++) {
     132         [ +  + ]:       20352 :             if (m_rng.randbits(i) == 0) {
     133   [ +  -  +  - ]:       13306 :                 BOOST_CHECK_MESSAGE(checker[i].StateSinceHeightFor(tip) == height, strprintf("Test %i for StateSinceHeight", num));
     134   [ +  -  +  - ]:       13306 :                 BOOST_CHECK_MESSAGE(checker_delayed[i].StateSinceHeightFor(tip) == height_delayed, strprintf("Test %i for StateSinceHeight (delayed)", num));
     135   [ +  -  +  - ]:       13306 :                 BOOST_CHECK_MESSAGE(checker_always[i].StateSinceHeightFor(tip) == 0, strprintf("Test %i for StateSinceHeight (always active)", num));
     136   [ +  -  +  - ]:       13306 :                 BOOST_CHECK_MESSAGE(checker_never[i].StateSinceHeightFor(tip) == 0, strprintf("Test %i for StateSinceHeight (never active)", num));
     137                 :             :             }
     138                 :             :         }
     139                 :        3392 :         num++;
     140                 :        3392 :         return *this;
     141                 :             :     }
     142                 :             : 
     143                 :        3200 :     VersionBitsTester& TestState(ThresholdState exp)
     144                 :             :     {
     145                 :        3200 :         return TestState(exp, exp);
     146                 :             :     }
     147                 :             : 
     148                 :        3456 :     VersionBitsTester& TestState(ThresholdState exp, ThresholdState exp_delayed)
     149                 :             :     {
     150         [ +  + ]:        3456 :         if (exp != exp_delayed) {
     151                 :             :             // only expected differences are that delayed stays in locked_in longer
     152         [ +  - ]:         256 :             BOOST_CHECK_EQUAL(exp, ThresholdState::ACTIVE);
     153         [ +  - ]:         256 :             BOOST_CHECK_EQUAL(exp_delayed, ThresholdState::LOCKED_IN);
     154                 :             :         }
     155                 :             : 
     156                 :        3456 :         const CBlockIndex* pindex = Tip();
     157         [ +  + ]:       24192 :         for (int i = 0; i < CHECKERS; i++) {
     158         [ +  + ]:       20736 :             if (m_rng.randbits(i) == 0) {
     159                 :        6876 :                 ThresholdState got = checker[i].StateFor(pindex);
     160                 :        6876 :                 ThresholdState got_delayed = checker_delayed[i].StateFor(pindex);
     161                 :        6876 :                 ThresholdState got_always = checker_always[i].StateFor(pindex);
     162                 :        6876 :                 ThresholdState got_never = checker_never[i].StateFor(pindex);
     163                 :             :                 // nHeight of the next block. If vpblock is empty, the next (ie first)
     164                 :             :                 // block should be the genesis block with nHeight == 0.
     165         [ +  + ]:        6876 :                 int height = pindex == nullptr ? 0 : pindex->nHeight + 1;
     166   [ +  -  +  -  :       13752 :                 BOOST_CHECK_MESSAGE(got == exp, strprintf("Test %i for %s height %d (got %s)", num, StateName(exp), height, StateName(got)));
                   +  - ]
     167   [ +  -  +  -  :       13752 :                 BOOST_CHECK_MESSAGE(got_delayed == exp_delayed, strprintf("Test %i for %s height %d (got %s; delayed case)", num, StateName(exp_delayed), height, StateName(got_delayed)));
                   +  - ]
     168   [ +  -  +  - ]:       13752 :                 BOOST_CHECK_MESSAGE(got_always == ThresholdState::ACTIVE, strprintf("Test %i for ACTIVE height %d (got %s; always active case)", num, height, StateName(got_always)));
     169   [ +  -  +  - ]:       13752 :                 BOOST_CHECK_MESSAGE(got_never == ThresholdState::FAILED, strprintf("Test %i for FAILED height %d (got %s; never active case)", num, height, StateName(got_never)));
     170                 :             :             }
     171                 :             :         }
     172                 :        3456 :         num++;
     173                 :        3456 :         return *this;
     174                 :             :     }
     175                 :             : 
     176                 :        2304 :     VersionBitsTester& TestDefined() { return TestState(ThresholdState::DEFINED); }
     177                 :        1920 :     VersionBitsTester& TestStarted() { return TestState(ThresholdState::STARTED); }
     178                 :         512 :     VersionBitsTester& TestLockedIn() { return TestState(ThresholdState::LOCKED_IN); }
     179                 :         384 :     VersionBitsTester& TestActive() { return TestState(ThresholdState::ACTIVE); }
     180                 :        1280 :     VersionBitsTester& TestFailed() { return TestState(ThresholdState::FAILED); }
     181                 :             : 
     182                 :             :     // non-delayed should be active; delayed should still be locked in
     183                 :         256 :     VersionBitsTester& TestActiveDelayed() { return TestState(ThresholdState::ACTIVE, ThresholdState::LOCKED_IN); }
     184                 :             : 
     185         [ +  + ]:     5534821 :     CBlockIndex* Tip() { return vpblock.empty() ? nullptr : vpblock.back(); }
     186                 :             : };
     187                 :             : 
     188                 :             : BOOST_FIXTURE_TEST_SUITE(versionbits_tests, BasicTestingSetup)
     189                 :             : 
     190   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(versionbits_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     191                 :             : {
     192         [ +  + ]:          65 :     for (int i = 0; i < 64; i++) {
     193                 :             :         // DEFINED -> STARTED after timeout reached -> FAILED
     194   [ +  -  +  - ]:          64 :         VersionBitsTester(m_rng, VERSIONBITS_TOP_BITS).TestDefined().TestStateSinceHeight(0)
     195   [ +  -  +  -  :          64 :                            .Mine(1, TestTime(1), 0x100).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     196   [ +  -  +  -  :          64 :                            .Mine(11, TestTime(11), 0x100).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     197   [ +  -  +  -  :          64 :                            .Mine(989, TestTime(989), 0x100).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     198   [ +  -  +  -  :          64 :                            .Mine(999, TestTime(20000), 0x100).TestDefined().TestStateSinceHeight(0) // Timeout and start time reached simultaneously
                   +  - ]
     199   [ +  -  +  -  :          64 :                            .Mine(1000, TestTime(20000), 0).TestStarted().TestStateSinceHeight(1000) // Hit started, stop signalling
                   +  - ]
     200   [ +  -  +  -  :          64 :                            .Mine(1999, TestTime(30001), 0).TestStarted().TestStateSinceHeight(1000)
                   +  - ]
     201   [ +  -  +  -  :          64 :                            .Mine(2000, TestTime(30002), 0x100).TestFailed().TestStateSinceHeight(2000) // Hit failed, start signalling again
                   +  - ]
     202   [ +  -  +  -  :          64 :                            .Mine(2001, TestTime(30003), 0x100).TestFailed().TestStateSinceHeight(2000)
                   +  - ]
     203   [ +  -  +  -  :          64 :                            .Mine(2999, TestTime(30004), 0x100).TestFailed().TestStateSinceHeight(2000)
                   +  - ]
     204   [ +  -  +  -  :          64 :                            .Mine(3000, TestTime(30005), 0x100).TestFailed().TestStateSinceHeight(2000)
                   +  - ]
     205   [ +  -  +  -  :          64 :                            .Mine(4000, TestTime(30006), 0x100).TestFailed().TestStateSinceHeight(2000)
                   +  - ]
     206                 :             : 
     207                 :             :         // DEFINED -> STARTED -> FAILED
     208   [ +  -  +  - ]:          64 :                            .Reset().TestDefined().TestStateSinceHeight(0)
     209   [ +  -  +  -  :          64 :                            .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     210   [ +  -  +  -  :          64 :                            .Mine(1000, TestTime(10000) - 1, 0x100).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined
                   +  - ]
     211   [ +  -  +  -  :          64 :                            .Mine(2000, TestTime(10000), 0x100).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period
                   +  - ]
     212   [ +  -  +  -  :          64 :                            .Mine(2051, TestTime(10010), 0).TestStarted().TestStateSinceHeight(2000) // 51 old blocks
                   +  - ]
     213   [ +  -  +  -  :          64 :                            .Mine(2950, TestTime(10020), 0x100).TestStarted().TestStateSinceHeight(2000) // 899 new blocks
                   +  - ]
     214   [ +  -  +  -  :          64 :                            .Mine(3000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(3000) // 50 old blocks (so 899 out of the past 1000)
                   +  - ]
     215   [ +  -  +  -  :          64 :                            .Mine(4000, TestTime(20010), 0x100).TestFailed().TestStateSinceHeight(3000)
                   +  - ]
     216                 :             : 
     217                 :             :         // DEFINED -> STARTED -> LOCKEDIN after timeout reached -> ACTIVE
     218   [ +  -  +  - ]:          64 :                            .Reset().TestDefined().TestStateSinceHeight(0)
     219   [ +  -  +  -  :          64 :                            .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     220   [ +  -  +  -  :          64 :                            .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined
                   +  - ]
     221   [ +  -  +  -  :          64 :                            .Mine(2000, TestTime(10000), 0x101).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period
                   +  - ]
     222   [ +  -  +  -  :          64 :                            .Mine(2999, TestTime(30000), 0x100).TestStarted().TestStateSinceHeight(2000) // 999 new blocks
                   +  - ]
     223   [ +  -  +  -  :          64 :                            .Mine(3000, TestTime(30000), 0x100).TestLockedIn().TestStateSinceHeight(3000) // 1 new block (so 1000 out of the past 1000 are new)
                   +  - ]
     224   [ +  -  +  -  :          64 :                            .Mine(3999, TestTime(30001), 0).TestLockedIn().TestStateSinceHeight(3000)
                   +  - ]
     225   [ +  -  +  -  :          64 :                            .Mine(4000, TestTime(30002), 0).TestActiveDelayed().TestStateSinceHeight(4000, 3000)
                   +  - ]
     226   [ +  -  +  -  :          64 :                            .Mine(14333, TestTime(30003), 0).TestActiveDelayed().TestStateSinceHeight(4000, 3000)
                   +  - ]
     227   [ +  -  +  -  :          64 :                            .Mine(24000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000, 15000)
                   +  - ]
     228                 :             : 
     229                 :             :         // DEFINED -> STARTED -> LOCKEDIN before timeout -> ACTIVE
     230         [ +  - ]:          64 :                            .Reset().TestDefined()
     231   [ +  -  +  -  :          64 :                            .Mine(1, TestTime(1), 0).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     232   [ +  -  +  -  :          64 :                            .Mine(1000, TestTime(10000) - 1, 0x101).TestDefined().TestStateSinceHeight(0) // One second more and it would be defined
                   +  - ]
     233   [ +  -  +  -  :          64 :                            .Mine(2000, TestTime(10000), 0x101).TestStarted().TestStateSinceHeight(2000) // So that's what happens the next period
                   +  - ]
     234   [ +  -  +  -  :          64 :                            .Mine(2050, TestTime(10010), 0x200).TestStarted().TestStateSinceHeight(2000) // 50 old blocks
                   +  - ]
     235   [ +  -  +  -  :          64 :                            .Mine(2950, TestTime(10020), 0x100).TestStarted().TestStateSinceHeight(2000) // 900 new blocks
                   +  - ]
     236   [ +  -  +  -  :          64 :                            .Mine(2999, TestTime(19999), 0x200).TestStarted().TestStateSinceHeight(2000) // 49 old blocks
                   +  - ]
     237   [ +  -  +  -  :          64 :                            .Mine(3000, TestTime(29999), 0x200).TestLockedIn().TestStateSinceHeight(3000) // 1 old block (so 900 out of the past 1000)
                   +  - ]
     238   [ +  -  +  -  :          64 :                            .Mine(3999, TestTime(30001), 0).TestLockedIn().TestStateSinceHeight(3000)
                   +  - ]
     239   [ +  -  +  -  :          64 :                            .Mine(4000, TestTime(30002), 0).TestActiveDelayed().TestStateSinceHeight(4000, 3000) // delayed will not become active until height=15000
                   +  - ]
     240   [ +  -  +  -  :          64 :                            .Mine(14333, TestTime(30003), 0).TestActiveDelayed().TestStateSinceHeight(4000, 3000)
                   +  - ]
     241   [ +  -  +  -  :          64 :                            .Mine(15000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000, 15000)
                   +  - ]
     242   [ +  -  +  -  :          64 :                            .Mine(24000, TestTime(40000), 0).TestActive().TestStateSinceHeight(4000, 15000)
                   +  - ]
     243                 :             : 
     244                 :             :         // DEFINED multiple periods -> STARTED multiple periods -> FAILED
     245   [ +  -  +  - ]:          64 :                            .Reset().TestDefined().TestStateSinceHeight(0)
     246   [ +  -  +  -  :          64 :                            .Mine(999, TestTime(999), 0).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     247   [ +  -  +  -  :          64 :                            .Mine(1000, TestTime(1000), 0).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     248   [ +  -  +  -  :          64 :                            .Mine(2000, TestTime(2000), 0).TestDefined().TestStateSinceHeight(0)
                   +  - ]
     249   [ +  -  +  -  :          64 :                            .Mine(3000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000)
                   +  - ]
     250   [ +  -  +  -  :          64 :                            .Mine(4000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000)
                   +  - ]
     251   [ +  -  +  -  :          64 :                            .Mine(5000, TestTime(10000), 0).TestStarted().TestStateSinceHeight(3000)
                   +  - ]
     252   [ +  -  +  -  :          64 :                            .Mine(5999, TestTime(20000), 0).TestStarted().TestStateSinceHeight(3000)
                   +  - ]
     253   [ +  -  +  -  :          64 :                            .Mine(6000, TestTime(20000), 0).TestFailed().TestStateSinceHeight(6000)
                   +  - ]
     254   [ +  -  +  -  :          64 :                            .Mine(7000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000)
                   +  - ]
     255   [ +  -  +  -  :          64 :                            .Mine(24000, TestTime(20000), 0x100).TestFailed().TestStateSinceHeight(6000) // stay in FAILED no matter how much we signal
                   +  - ]
     256                 :             :         ;
     257                 :             :     }
     258                 :           1 : }
     259                 :             : 
     260                 :           2 : struct BlockVersionTest : BasicTestingSetup {
     261                 :             : /** Check that ComputeBlockVersion will set the appropriate bit correctly
     262                 :             :  * Also checks IsActiveAfter() behaviour */
     263                 :           7 : void check_computeblockversion(VersionBitsCache& versionbitscache, const Consensus::Params& params, Consensus::DeploymentPos dep)
     264                 :             : {
     265                 :             :     // Clear the cache every time
     266                 :           7 :     versionbitscache.Clear();
     267                 :             : 
     268                 :           7 :     int64_t bit = params.vDeployments[dep].bit;
     269                 :           7 :     int64_t nStartTime = params.vDeployments[dep].nStartTime;
     270                 :           7 :     int64_t nTimeout = params.vDeployments[dep].nTimeout;
     271                 :           7 :     int min_activation_height = params.vDeployments[dep].min_activation_height;
     272                 :           7 :     uint32_t period = params.vDeployments[dep].period;
     273                 :           7 :     uint32_t threshold = params.vDeployments[dep].threshold;
     274                 :             : 
     275         [ +  - ]:          14 :     BOOST_REQUIRE(period > 0); // no division by zero, thankyou
     276         [ +  - ]:          14 :     BOOST_REQUIRE(0 < threshold); // must be able to have a window that doesn't activate
     277         [ +  - ]:          14 :     BOOST_REQUIRE(threshold < period); // must be able to have a window that does activate
     278                 :             : 
     279                 :             :     // should not be any signalling for first block
     280         [ +  - ]:           7 :     BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(nullptr, params), VERSIONBITS_TOP_BITS);
     281                 :             : 
     282                 :             :     // always/never active deployments shouldn't need to be tested further
     283         [ +  + ]:           7 :     if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE ||
     284                 :             :         nStartTime == Consensus::BIP9Deployment::NEVER_ACTIVE)
     285                 :             :     {
     286         [ -  + ]:           4 :         if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE) {
     287   [ #  #  #  # ]:           0 :             BOOST_CHECK(versionbitscache.IsActiveAfter(nullptr, params, dep));
     288                 :             :         } else {
     289   [ +  -  +  - ]:           8 :             BOOST_CHECK(!versionbitscache.IsActiveAfter(nullptr, params, dep));
     290                 :             :         }
     291         [ +  - ]:           4 :         BOOST_CHECK_EQUAL(min_activation_height, 0);
     292         [ +  - ]:           4 :         BOOST_CHECK_EQUAL(nTimeout, Consensus::BIP9Deployment::NO_TIMEOUT);
     293                 :           4 :         return;
     294                 :             :     }
     295                 :             : 
     296         [ +  - ]:           6 :     BOOST_REQUIRE(nStartTime < nTimeout);
     297         [ +  - ]:           6 :     BOOST_REQUIRE(nStartTime >= 0);
     298         [ +  - ]:           6 :     BOOST_REQUIRE(nTimeout <= std::numeric_limits<uint32_t>::max() || nTimeout == Consensus::BIP9Deployment::NO_TIMEOUT);
     299         [ +  - ]:           6 :     BOOST_REQUIRE(0 <= bit);
     300         [ +  - ]:           6 :     BOOST_REQUIRE(bit < 32);
     301                 :             :     // Make sure that no deployment tries to set an invalid bit.
     302         [ +  - ]:           6 :     BOOST_REQUIRE(((1 << bit) & VERSIONBITS_TOP_MASK) == 0);
     303         [ +  - ]:           6 :     BOOST_REQUIRE(min_activation_height >= 0);
     304                 :             :     // Check min_activation_height is on a retarget boundary
     305         [ +  - ]:           3 :     BOOST_REQUIRE_EQUAL(min_activation_height % period, 0U);
     306                 :             : 
     307                 :             :     // In the first chain, test that the bit is set by CBV until it has failed.
     308                 :             :     // In the second chain, test the bit is set by CBV while STARTED and
     309                 :             :     // LOCKED-IN, and then no longer set while ACTIVE.
     310         [ +  - ]:           3 :     VersionBitsTester firstChain{m_rng}, secondChain{m_rng};
     311                 :             : 
     312                 :           3 :     int64_t nTime = nStartTime;
     313                 :             : 
     314                 :           3 :     const CBlockIndex *lastBlock = nullptr;
     315                 :             : 
     316                 :             :     // Before MedianTimePast of the chain has crossed nStartTime, the bit
     317                 :             :     // should not be set.
     318         [ +  + ]:           3 :     if (nTime == 0) {
     319                 :             :         // since CBlockIndex::nTime is uint32_t we can't represent any
     320                 :             :         // earlier time, so will transition from DEFINED to STARTED at the
     321                 :             :         // end of the first period by mining blocks at nTime == 0
     322         [ +  - ]:           1 :         lastBlock = firstChain.Mine(period - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     323   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
                   +  - ]
     324   [ +  -  +  -  :           2 :         BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
             +  -  +  - ]
     325         [ +  - ]:           1 :         lastBlock = firstChain.Mine(period, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     326   [ +  -  +  -  :           2 :         BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             +  -  +  - ]
     327   [ +  -  +  -  :           2 :         BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     328                 :             :         // then we'll keep mining at nStartTime...
     329                 :             :     } else {
     330                 :             :         // use a time 1s earlier than start time to check we stay DEFINED
     331                 :           2 :         --nTime;
     332                 :             : 
     333                 :             :         // Start generating blocks before nStartTime
     334         [ +  - ]:           2 :         lastBlock = firstChain.Mine(period, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     335   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
                   +  - ]
     336   [ +  -  +  -  :           4 :         BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     337                 :             : 
     338                 :             :         // Mine more blocks (4 less than the adjustment period) at the old time, and check that CBV isn't setting the bit yet.
     339         [ +  + ]:         280 :         for (uint32_t i = 1; i < period - 4; i++) {
     340         [ +  - ]:         278 :             lastBlock = firstChain.Mine(period + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     341   [ +  -  +  -  :         278 :             BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
                   +  - ]
     342   [ +  -  +  -  :         556 :             BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     343                 :             :         }
     344                 :             :         // Now mine 5 more blocks at the start time -- MTP should not have passed yet, so
     345                 :             :         // CBV should still not yet set the bit.
     346                 :          12 :         nTime = nStartTime;
     347         [ +  + ]:          12 :         for (uint32_t i = period - 4; i <= period; i++) {
     348         [ +  - ]:          10 :             lastBlock = firstChain.Mine(period + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     349   [ +  -  +  -  :          10 :             BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
                   +  - ]
     350   [ +  -  +  -  :          20 :             BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     351                 :             :         }
     352                 :             :         // Next we will advance to the next period and transition to STARTED,
     353                 :             :     }
     354                 :             : 
     355         [ +  - ]:           3 :     lastBlock = firstChain.Mine(period * 3, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     356                 :             :     // so ComputeBlockVersion should now set the bit,
     357   [ +  -  +  -  :           6 :     BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             +  -  +  - ]
     358                 :             :     // and should also be using the VERSIONBITS_TOP_BITS.
     359   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
                   +  - ]
     360   [ +  -  +  -  :           6 :     BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     361                 :             : 
     362                 :             :     // Check that ComputeBlockVersion will set the bit until nTimeout
     363                 :           3 :     nTime += 600;
     364                 :           3 :     uint32_t blocksToMine = period * 2; // test blocks for up to 2 time periods
     365                 :           3 :     uint32_t nHeight = period * 3;
     366                 :             :     // These blocks are all before nTimeout is reached.
     367   [ +  -  +  + ]:         867 :     while (nTime < nTimeout && blocksToMine > 0) {
     368         [ +  - ]:         864 :         lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     369   [ +  -  +  -  :        1728 :         BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             +  -  +  - ]
     370   [ +  -  +  -  :         864 :         BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
                   +  - ]
     371   [ +  -  +  -  :        1728 :         BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     372                 :         864 :         blocksToMine--;
     373                 :         864 :         nTime += 600;
     374                 :         864 :         nHeight += 1;
     375                 :             :     }
     376                 :             : 
     377         [ +  + ]:           3 :     if (nTimeout != Consensus::BIP9Deployment::NO_TIMEOUT) {
     378                 :             :         // can reach any nTimeout other than NO_TIMEOUT due to earlier BOOST_REQUIRE
     379                 :             : 
     380                 :           2 :         nTime = nTimeout;
     381                 :             : 
     382                 :             :         // finish the last period before we start timing out
     383         [ -  + ]:           2 :         while (nHeight % period != 0) {
     384         [ #  # ]:           0 :             lastBlock = firstChain.Mine(nHeight+1, nTime - 1, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     385   [ #  #  #  #  :           0 :             BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             #  #  #  # ]
     386   [ #  #  #  #  :           0 :             BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   #  # ]
     387                 :           0 :             nHeight += 1;
     388                 :             :         }
     389                 :             : 
     390                 :             :         // FAILED is only triggered at the end of a period, so CBV should be setting
     391                 :             :         // the bit until the period transition.
     392         [ +  + ]:         288 :         for (uint32_t i = 0; i < period - 1; i++) {
     393         [ +  - ]:         286 :             lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     394   [ +  -  +  -  :         572 :             BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             +  -  +  - ]
     395   [ +  -  +  -  :         572 :             BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     396                 :         286 :             nHeight += 1;
     397                 :             :         }
     398                 :             :         // The next block should trigger no longer setting the bit.
     399         [ +  - ]:           2 :         lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     400   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
                   +  - ]
     401   [ +  -  +  -  :           4 :         BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     402                 :             :     }
     403                 :             : 
     404                 :             :     // On a new chain:
     405                 :             :     // verify that the bit will be set after lock-in, and then stop being set
     406                 :             :     // after activation.
     407                 :           3 :     nTime = nStartTime;
     408                 :             : 
     409                 :             :     // Mine one period worth of blocks, and check that the bit will be on for the
     410                 :             :     // next period.
     411         [ +  - ]:           3 :     lastBlock = secondChain.Mine(period, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     412   [ +  -  +  -  :           6 :     BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             +  -  +  - ]
     413   [ +  -  +  -  :           6 :     BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
             +  -  +  - ]
     414                 :             : 
     415                 :             :     // Mine another period worth of blocks, signaling the new bit.
     416         [ +  - ]:           3 :     lastBlock = secondChain.Mine(period * 2, nTime, VERSIONBITS_TOP_BITS | (1<<bit)).Tip();
     417                 :             :     // After one period of setting the bit on each block, it should have locked in.
     418                 :             :     // We keep setting the bit for one more period though, until activation.
     419   [ +  -  +  -  :           6 :     BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             +  -  +  - ]
     420   [ +  -  +  -  :           6 :     BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
             +  -  +  - ]
     421                 :             : 
     422                 :             :     // Now check that we keep mining the block until the end of this period, and
     423                 :             :     // then stop at the beginning of the next period.
     424         [ +  - ]:           3 :     lastBlock = secondChain.Mine((period * 3) - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     425   [ +  -  +  -  :           6 :     BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             +  -  +  - ]
     426   [ +  -  +  -  :           6 :     BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
             +  -  +  - ]
     427         [ +  - ]:           3 :     lastBlock = secondChain.Mine(period * 3, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     428                 :             : 
     429         [ +  + ]:           3 :     if (lastBlock->nHeight + 1 < min_activation_height) {
     430                 :             :         // check signalling continues while min_activation_height is not reached
     431         [ +  - ]:           1 :         lastBlock = secondChain.Mine(min_activation_height - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     432   [ +  -  +  -  :           2 :         BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
             +  -  +  - ]
     433   [ +  -  +  -  :           2 :         BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
             +  -  +  - ]
     434                 :             :         // then reach min_activation_height, which was already REQUIRE'd to start a new period
     435         [ +  - ]:           1 :         lastBlock = secondChain.Mine(min_activation_height, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
     436                 :             :     }
     437                 :             : 
     438                 :             :     // Check that we don't signal after activation
     439   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
                   +  - ]
     440   [ +  -  +  -  :           6 :     BOOST_CHECK(versionbitscache.IsActiveAfter(lastBlock, params, dep));
                   +  - ]
     441                 :           3 : }
     442                 :             : }; // struct BlockVersionTest
     443                 :             : 
     444   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(blockscriptflags, RegTestingSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     445                 :             : {
     446                 :             :     // RegTest should have all possible flags enabled by default
     447                 :             :     // If this check fails, then either remove the flag from
     448                 :             :     // CONSENSUS_SCRIPT_VERIFY_FLAGS, so that it's never a consensus
     449                 :             :     // flag for any chain, or ensure the deployment is enabled by
     450                 :             :     // default for RegTest.
     451                 :           1 :     auto regtest{CreateChainParams(*m_node.args, ChainType::REGTEST)};
     452   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(ChainstateManager::GetAllConsensusScriptFlags(regtest->GetConsensus()).as_int(), CONSENSUS_SCRIPT_VERIFY_FLAGS.as_int());
                   +  - ]
     453                 :             : 
     454                 :             :     // Check stored value is consistent
     455         [ +  - ]:           1 :     ChainstateManager& chainman = *m_node.chainman;
     456         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(chainman.m_all_consensus_script_flags.as_int(), CONSENSUS_SCRIPT_VERIFY_FLAGS.as_int());
     457                 :             : 
     458                 :             :     // Every other chain should have a subset (though this should already have been checked at compile-time)
     459         [ +  + ]:           5 :     for (const auto& chain_type : {ChainType::MAIN, ChainType::TESTNET, ChainType::TESTNET4, ChainType::SIGNET}) {
     460         [ +  - ]:           4 :         auto params{CreateChainParams(*m_node.args, chain_type)};
     461         [ +  - ]:           4 :         script_verify_flags possible_flags{ChainstateManager::GetAllConsensusScriptFlags(params->GetConsensus())};
     462   [ +  -  +  - ]:           4 :         BOOST_CHECK_EQUAL((possible_flags & CONSENSUS_SCRIPT_VERIFY_FLAGS).as_int(), possible_flags.as_int());
     463                 :           4 :     }
     464                 :           1 : }
     465                 :             : 
     466   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(versionbits_computeblockversion, BlockVersionTest)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     467                 :             : {
     468                 :           1 :     VersionBitsCache vbcache;
     469                 :             : 
     470                 :             :     // check that any deployment on any chain can conceivably reach both
     471                 :             :     // ACTIVE and FAILED states in roughly the way we expect
     472         [ +  + ]:           6 :     for (const auto& chain_type: {ChainType::MAIN, ChainType::TESTNET, ChainType::TESTNET4, ChainType::SIGNET, ChainType::REGTEST}) {
     473         [ +  - ]:           5 :         const auto chainParams = CreateChainParams(*m_node.args, chain_type);
     474                 :             :         uint32_t chain_all_vbits{0};
     475         [ +  + ]:          10 :         for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++i) {
     476                 :           5 :             const auto dep = static_cast<Consensus::DeploymentPos>(i);
     477                 :             :             // Check that no bits are reused (within the same chain). This is
     478                 :             :             // disallowed because the transition to FAILED (on timeout) does
     479                 :             :             // not take precedence over STARTED/LOCKED_IN. So all softforks on
     480                 :             :             // the same bit might overlap, even when non-overlapping start-end
     481                 :             :             // times are picked.
     482         [ +  - ]:           5 :             const auto& dep_info = chainParams->GetConsensus().vDeployments[dep];
     483                 :           5 :             const uint32_t dep_mask{uint32_t{1} << dep_info.bit};
     484   [ +  -  +  -  :          10 :             BOOST_CHECK(!(chain_all_vbits & dep_mask));
                   +  - ]
     485                 :           5 :             chain_all_vbits |= dep_mask;
     486   [ +  -  +  -  :          10 :             BOOST_CHECK(0 <= dep_info.bit);
                   +  - ]
     487   [ +  -  +  -  :          10 :             BOOST_CHECK(dep_info.bit < VERSIONBITS_MAX_NUM_BITS);
                   +  + ]
     488         [ +  + ]:           5 :             if (chain_type != ChainType::REGTEST) {
     489         [ +  - ]:           4 :                 if (dep == Consensus::DEPLOYMENT_TESTDUMMY) {
     490   [ +  -  +  - ]:           4 :                     BOOST_CHECK_EQUAL(dep_info.nStartTime, Consensus::BIP9Deployment::NEVER_ACTIVE);
     491   [ +  -  +  - ]:           4 :                     BOOST_CHECK_EQUAL(dep_info.nTimeout, Consensus::BIP9Deployment::NO_TIMEOUT);
     492                 :             :                 } else {
     493   [ #  #  #  # ]:           0 :                     BOOST_CHECK(dep_info.bit < VERSIONBITS_NUM_BITS);
     494                 :             :                 }
     495                 :             :             }
     496         [ +  - ]:           5 :             check_computeblockversion(vbcache, chainParams->GetConsensus(), dep);
     497                 :             :         }
     498                 :           5 :     }
     499                 :             : 
     500                 :           1 :     {
     501                 :             :         // Use regtest/testdummy to ensure we always exercise some
     502                 :             :         // deployment that's not always/never active
     503         [ +  - ]:           1 :         ArgsManager args;
     504   [ +  -  +  -  :           2 :         args.ForceSetArg("-vbparams", "testdummy:1199145601:1230767999"); // January 1, 2008 - December 31, 2008
                   +  - ]
     505         [ +  - ]:           1 :         const auto chainParams = CreateChainParams(args, ChainType::REGTEST);
     506         [ +  - ]:           1 :         check_computeblockversion(vbcache, chainParams->GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY);
     507                 :           1 :     }
     508                 :             : 
     509                 :           1 :     {
     510                 :             :         // Use regtest/testdummy to ensure we always exercise the
     511                 :             :         // min_activation_height test, even if we're not using that in a
     512                 :             :         // live deployment
     513         [ +  - ]:           1 :         ArgsManager args;
     514   [ +  -  +  -  :           2 :         args.ForceSetArg("-vbparams", "testdummy:1199145601:1230767999:403200"); // January 1, 2008 - December 31, 2008, min act height 403200
                   +  - ]
     515         [ +  - ]:           1 :         const auto chainParams = CreateChainParams(args, ChainType::REGTEST);
     516         [ +  - ]:           1 :         check_computeblockversion(vbcache, chainParams->GetConsensus(), Consensus::DEPLOYMENT_TESTDUMMY);
     517                 :           1 :     }
     518                 :           1 : }
     519                 :             : 
     520                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1