LCOV - code coverage report
Current view: top level - src/test - pow_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 195 195
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 41 41
Branches: 50.6 % 1704 862

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2015-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 <pow.h>
       8                 :             : #include <test/util/random.h>
       9                 :             : #include <test/util/common.h>
      10                 :             : #include <test/util/setup_common.h>
      11                 :             : #include <util/chaintype.h>
      12                 :             : 
      13                 :             : #include <boost/test/unit_test.hpp>
      14                 :             : 
      15                 :             : BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
      16                 :             : 
      17                 :             : /* Test calculation of next difficulty target with no constraints applying */
      18   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      19                 :             : {
      20                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      21                 :           1 :     int64_t nLastRetargetTime = 1261130161; // Block #30240
      22                 :           1 :     CBlockIndex pindexLast;
      23                 :           1 :     pindexLast.nHeight = 32255;
      24                 :           1 :     pindexLast.nTime = 1262152739;  // Block #32255
      25                 :           1 :     pindexLast.nBits = 0x1d00ffff;
      26                 :             : 
      27                 :             :     // Here (and below): expected_nbits is calculated in
      28                 :             :     // CalculateNextWorkRequired(); redoing the calculation here would be just
      29                 :             :     // reimplementing the same code that is written in pow.cpp. Rather than
      30                 :             :     // copy that code, we just hardcode the expected result.
      31                 :           1 :     unsigned int expected_nbits = 0x1d00d86aU;
      32   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
                   +  - ]
      33   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
                   +  - ]
      34                 :           1 : }
      35                 :             : 
      36                 :             : /* Test the constraint on the upper bound for next work */
      37   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      38                 :             : {
      39                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      40                 :           1 :     int64_t nLastRetargetTime = 1231006505; // Block #0
      41                 :           1 :     CBlockIndex pindexLast;
      42                 :           1 :     pindexLast.nHeight = 2015;
      43                 :           1 :     pindexLast.nTime = 1233061996;  // Block #2015
      44                 :           1 :     pindexLast.nBits = 0x1d00ffff;
      45                 :           1 :     unsigned int expected_nbits = 0x1d00ffffU;
      46   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
                   +  - ]
      47   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
                   +  - ]
      48                 :           1 : }
      49                 :             : 
      50                 :             : /* Test the constraint on the lower bound for actual time taken */
      51   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      52                 :             : {
      53                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      54                 :           1 :     int64_t nLastRetargetTime = 1279008237; // Block #66528
      55                 :           1 :     CBlockIndex pindexLast;
      56                 :           1 :     pindexLast.nHeight = 68543;
      57                 :           1 :     pindexLast.nTime = 1279297671;  // Block #68543
      58                 :           1 :     pindexLast.nBits = 0x1c05a3f4;
      59                 :           1 :     unsigned int expected_nbits = 0x1c0168fdU;
      60   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
                   +  - ]
      61   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
             +  -  +  - ]
      62                 :             :     // Test that reducing nbits further would not be a PermittedDifficultyTransition.
      63                 :           1 :     unsigned int invalid_nbits = expected_nbits-1;
      64   [ +  -  +  -  :           2 :     BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
                   +  - ]
      65                 :           1 : }
      66                 :             : 
      67                 :             : /* Test that the largest permitted difficulty increase is not itself rejected
      68                 :             :  * when the new target is not exactly representable in compact form. */
      69   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_rounding)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      70                 :             : {
      71         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
      72                 :           1 :     CBlockIndex pindexLast;
      73                 :           1 :     pindexLast.nHeight = 68543;
      74                 :           1 :     pindexLast.nTime = 1279297671;
      75                 :           1 :     pindexLast.nBits = 0x1c05a3f5; // Mantissa is not a multiple of four
      76                 :           1 :     int64_t nLastRetargetTime = pindexLast.nTime - consensus.nPowTargetTimespan/4;
      77                 :           1 :     unsigned int expected_nbits = 0x1c0168fdU;
      78   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, consensus), expected_nbits);
                   +  - ]
      79   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(consensus, pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
                   +  - ]
      80                 :           1 : }
      81                 :             : 
      82                 :             : /* Test the lower bound for a negative actual time taken, which is possible
      83                 :             :  * because timestamps only have to exceed the median of the previous eleven. */
      84   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_negative_actual)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      85                 :             : {
      86         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
      87                 :           1 :     int64_t nLastRetargetTime = 1279297671; // Later than pindexLast.nTime
      88                 :           1 :     CBlockIndex pindexLast;
      89                 :           1 :     pindexLast.nHeight = 68543;
      90                 :           1 :     pindexLast.nTime = 1279008237;
      91                 :           1 :     pindexLast.nBits = 0x1c05a3f4;
      92                 :           1 :     unsigned int expected_nbits = 0x1c0168fdU;
      93   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, consensus), expected_nbits);
                   +  - ]
      94   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(consensus, pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
                   +  - ]
      95                 :           1 : }
      96                 :             : 
      97                 :             : /* Test the constraint on the upper bound for actual time taken */
      98   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      99                 :             : {
     100                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
     101                 :           1 :     int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
     102                 :           1 :     CBlockIndex pindexLast;
     103                 :           1 :     pindexLast.nHeight = 46367;
     104                 :           1 :     pindexLast.nTime = 1269211443;  // Block #46367
     105                 :           1 :     pindexLast.nBits = 0x1c387f6f;
     106                 :           1 :     unsigned int expected_nbits = 0x1d00e1fdU;
     107   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
                   +  - ]
     108   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
             +  -  +  - ]
     109                 :             :     // Test that increasing nbits further would not be a PermittedDifficultyTransition.
     110                 :           1 :     unsigned int invalid_nbits = expected_nbits+1;
     111   [ +  -  +  -  :           2 :     BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
                   +  - ]
     112                 :           1 : }
     113                 :             : 
     114                 :             : /* Test that the retarget window starts at the timestamp of the first block
     115                 :             :  * of the window, not its median time past. */
     116   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_first_block_time)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     117                 :             : {
     118         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     119         [ +  - ]:           1 :     const int64_t interval{consensus.DifficultyAdjustmentInterval()};
     120                 :             : 
     121                 :             :     // Two retarget windows, so that the first block of the second one has
     122                 :             :     // ancestors and hence a median time past earlier than its own time.
     123         [ +  - ]:           1 :     std::vector<CBlockIndex> blocks(2 * interval);
     124         [ +  + ]:        4033 :     for (int64_t i = 0; i < 2 * interval; i++) {
     125         [ +  + ]:        4032 :         blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
     126                 :        4032 :         blocks[i].nHeight = i;
     127                 :        4032 :         blocks[i].nTime = 1269211443 + i * consensus.nPowTargetSpacing;
     128                 :        4032 :         blocks[i].nBits = 0x1c05a3f4;
     129                 :             :     }
     130                 :             : 
     131         [ +  - ]:           1 :     const CBlockIndex& first{blocks[interval]};
     132                 :           1 :     const CBlockIndex& last{blocks[2 * interval - 1]};
     133   [ +  -  +  - ]:           1 :     BOOST_CHECK_NE(first.GetBlockTime(), first.GetMedianTimePast());
     134                 :           1 :     unsigned int expected_nbits = 0x1c05a33c;
     135   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(GetNextWorkRequired(&last, nullptr, consensus), expected_nbits);
                   +  - ]
     136   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(consensus, last.nHeight+1, last.nBits, expected_nbits));
                   +  - ]
     137                 :           1 : }
     138                 :             : 
     139                 :             : /* Test that nbits is only permitted to change at a difficulty adjustment. */
     140   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(permitted_difficulty_transition_no_retarget)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     141                 :             : {
     142         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     143         [ +  - ]:           1 :     const int64_t height{consensus.DifficultyAdjustmentInterval() + 1}; // Not a retarget height
     144                 :             : 
     145                 :           1 :     const unsigned int nbits{0x1c05a3f4};
     146   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(consensus, height, nbits, nbits));
             +  -  +  - ]
     147   [ +  -  +  -  :           2 :     BOOST_CHECK(!PermittedDifficultyTransition(consensus, height, nbits, nbits - 1));
             +  -  +  - ]
     148   [ +  -  +  -  :           2 :     BOOST_CHECK(!PermittedDifficultyTransition(consensus, height, nbits, nbits + 1));
                   +  - ]
     149                 :           1 : }
     150                 :             : 
     151   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     152                 :             : {
     153         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     154                 :           1 :     uint256 hash;
     155                 :           1 :     unsigned int nBits;
     156   [ +  -  +  - ]:           1 :     nBits = UintToArith256(consensus.powLimit).GetCompact(true);
     157                 :           1 :     hash = uint256{1};
     158   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     159                 :           1 : }
     160                 :             : 
     161   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     162                 :             : {
     163         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     164                 :           1 :     uint256 hash;
     165                 :           1 :     unsigned int nBits{~0x00800000U};
     166                 :           1 :     hash = uint256{1};
     167   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     168                 :           1 : }
     169                 :             : 
     170   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     171                 :             : {
     172         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     173                 :           1 :     uint256 hash;
     174                 :           1 :     unsigned int nBits;
     175         [ +  - ]:           1 :     arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
     176         [ +  - ]:           1 :     nBits_arith *= 2;
     177         [ +  - ]:           1 :     nBits = nBits_arith.GetCompact();
     178                 :           1 :     hash = uint256{1};
     179   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     180                 :           1 : }
     181                 :             : 
     182   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     183                 :             : {
     184         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     185                 :           1 :     uint256 hash;
     186                 :           1 :     unsigned int nBits;
     187         [ +  - ]:           1 :     arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
     188         [ +  - ]:           1 :     nBits = hash_arith.GetCompact();
     189         [ +  - ]:           1 :     hash_arith *= 2; // hash > nBits
     190         [ +  - ]:           1 :     hash = ArithToUint256(hash_arith);
     191   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     192                 :           1 : }
     193                 :             : 
     194   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     195                 :             : {
     196         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     197                 :           1 :     uint256 hash;
     198                 :           1 :     unsigned int nBits;
     199                 :           1 :     arith_uint256 hash_arith{0};
     200         [ +  - ]:           1 :     nBits = hash_arith.GetCompact();
     201         [ +  - ]:           1 :     hash = ArithToUint256(hash_arith);
     202   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     203                 :           1 : }
     204                 :             : 
     205   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(GetBitsProof_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     206                 :             : {
     207                 :             :     // Targets that are zero, negative or overflow encode no work.
     208         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetBitsProof(0x00000000), arith_uint256{0});
     209         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetBitsProof(0x01003456), arith_uint256{0});
     210         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetBitsProof(0x04923456), arith_uint256{0});
     211         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetBitsProof(0xff123456), arith_uint256{0});
     212                 :             :     // The exponent byte is unsigned, so this is a large exponent that overflows.
     213         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetBitsProof(0x80010203), arith_uint256{0});
     214                 :             :     // The two overflows above shift every mantissa bit out of 256 bits, so they would
     215                 :             :     // also give zero work without the overflow check. This one keeps the low bits of
     216                 :             :     // its mantissa when truncated.
     217         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetBitsProof(0x21012345), arith_uint256{0});
     218                 :             :     // The genesis block target.
     219         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetBitsProof(0x1d00ffff), arith_uint256{0x100010001});
     220                 :           1 : }
     221                 :             : 
     222   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     223                 :             : {
     224                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
     225         [ +  - ]:           1 :     std::vector<CBlockIndex> blocks(10000);
     226         [ +  + ]:       10001 :     for (int i = 0; i < 10000; i++) {
     227   [ +  +  +  + ]:       10000 :         blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
     228                 :       10000 :         blocks[i].nHeight = i;
     229         [ +  + ]:       10000 :         blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
     230                 :       10000 :         blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
     231   [ +  +  +  - ]:       10000 :         blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
     232                 :             :     }
     233                 :             : 
     234         [ +  + ]:        1001 :     for (int j = 0; j < 1000; j++) {
     235                 :        1000 :         CBlockIndex *p1 = &blocks[m_rng.randrange(10000)];
     236                 :        1000 :         CBlockIndex *p2 = &blocks[m_rng.randrange(10000)];
     237         [ +  - ]:        1000 :         CBlockIndex *p3 = &blocks[m_rng.randrange(10000)];
     238                 :             : 
     239         [ +  - ]:        1000 :         int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
     240   [ +  -  +  - ]:        1000 :         BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
     241                 :             :     }
     242                 :           1 : }
     243                 :             : 
     244                 :           5 : void sanity_check_chainparams(const ArgsManager& args, ChainType chain_type)
     245                 :             : {
     246                 :           5 :     const auto chainParams = CreateChainParams(args, chain_type);
     247         [ +  - ]:           5 :     const auto consensus = chainParams->GetConsensus();
     248                 :             : 
     249                 :             :     // hash genesis is correct
     250   [ +  -  +  -  :           5 :     BOOST_CHECK_EQUAL(consensus.hashGenesisBlock, chainParams->GenesisBlock().GetHash());
                   +  - ]
     251                 :             : 
     252                 :             :     // target timespan is an even multiple of spacing
     253   [ +  -  +  - ]:           5 :     BOOST_CHECK_EQUAL(consensus.nPowTargetTimespan % consensus.nPowTargetSpacing, 0);
     254                 :             : 
     255                 :             :     // genesis nBits is positive, doesn't overflow and is lower than powLimit
     256                 :           5 :     arith_uint256 pow_compact;
     257                 :           5 :     bool neg, over;
     258         [ +  - ]:           5 :     pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over);
     259   [ +  -  +  -  :          10 :     BOOST_CHECK(!neg);
                   +  - ]
     260   [ +  -  +  -  :          10 :     BOOST_CHECK(pow_compact != 0);
             +  -  +  - ]
     261   [ +  -  +  -  :          10 :     BOOST_CHECK(!over);
                   +  - ]
     262   [ +  -  +  -  :          10 :     BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact);
          +  -  +  -  +  
                      + ]
     263                 :             : 
     264                 :             :     // check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()
     265         [ +  + ]:           5 :     if (!consensus.fPowNoRetargeting) {
     266         [ +  - ]:           4 :         arith_uint256 targ_max{UintToArith256(uint256{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"})};
     267         [ +  - ]:           8 :         targ_max /= consensus.nPowTargetTimespan*4;
     268   [ +  -  +  -  :           8 :         BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);
             +  -  +  - ]
     269                 :             :     }
     270                 :           5 : }
     271                 :             : 
     272   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     273                 :             : {
     274                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::MAIN);
     275                 :           1 : }
     276                 :             : 
     277   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     278                 :             : {
     279                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::REGTEST);
     280                 :           1 : }
     281                 :             : 
     282   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     283                 :             : {
     284                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::TESTNET);
     285                 :           1 : }
     286                 :             : 
     287   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_TESTNET4_sanity)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     288                 :             : {
     289                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::TESTNET4);
     290                 :           1 : }
     291                 :             : 
     292   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     293                 :             : {
     294                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::SIGNET);
     295                 :           1 : }
     296                 :             : 
     297                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1