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 % 139 139
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 31 31
Branches: 50.7 % 1108 562

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2015-2022 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/setup_common.h>
      10                 :             : #include <util/chaintype.h>
      11                 :             : 
      12                 :             : #include <boost/test/unit_test.hpp>
      13                 :             : 
      14                 :             : BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
      15                 :             : 
      16                 :             : /* Test calculation of next difficulty target with no constraints applying */
      17   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      18                 :             : {
      19                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      20                 :           1 :     int64_t nLastRetargetTime = 1261130161; // Block #30240
      21                 :           1 :     CBlockIndex pindexLast;
      22                 :           1 :     pindexLast.nHeight = 32255;
      23                 :           1 :     pindexLast.nTime = 1262152739;  // Block #32255
      24                 :           1 :     pindexLast.nBits = 0x1d00ffff;
      25                 :             : 
      26                 :             :     // Here (and below): expected_nbits is calculated in
      27                 :             :     // CalculateNextWorkRequired(); redoing the calculation here would be just
      28                 :             :     // reimplementing the same code that is written in pow.cpp. Rather than
      29                 :             :     // copy that code, we just hardcode the expected result.
      30                 :           1 :     unsigned int expected_nbits = 0x1d00d86aU;
      31   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
                   +  - ]
      32   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
                   +  - ]
      33                 :           1 : }
      34                 :             : 
      35                 :             : /* Test the constraint on the upper bound for next work */
      36   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      37                 :             : {
      38                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      39                 :           1 :     int64_t nLastRetargetTime = 1231006505; // Block #0
      40                 :           1 :     CBlockIndex pindexLast;
      41                 :           1 :     pindexLast.nHeight = 2015;
      42                 :           1 :     pindexLast.nTime = 1233061996;  // Block #2015
      43                 :           1 :     pindexLast.nBits = 0x1d00ffff;
      44                 :           1 :     unsigned int expected_nbits = 0x1d00ffffU;
      45   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
                   +  - ]
      46   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
                   +  - ]
      47                 :           1 : }
      48                 :             : 
      49                 :             : /* Test the constraint on the lower bound for actual time taken */
      50   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      51                 :             : {
      52                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      53                 :           1 :     int64_t nLastRetargetTime = 1279008237; // Block #66528
      54                 :           1 :     CBlockIndex pindexLast;
      55                 :           1 :     pindexLast.nHeight = 68543;
      56                 :           1 :     pindexLast.nTime = 1279297671;  // Block #68543
      57                 :           1 :     pindexLast.nBits = 0x1c05a3f4;
      58                 :           1 :     unsigned int expected_nbits = 0x1c0168fdU;
      59   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
                   +  - ]
      60   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
             +  -  +  - ]
      61                 :             :     // Test that reducing nbits further would not be a PermittedDifficultyTransition.
      62                 :           1 :     unsigned int invalid_nbits = expected_nbits-1;
      63   [ +  -  +  -  :           2 :     BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
                   +  - ]
      64                 :           1 : }
      65                 :             : 
      66                 :             : /* Test the constraint on the upper bound for actual time taken */
      67   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      68                 :             : {
      69                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      70                 :           1 :     int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
      71                 :           1 :     CBlockIndex pindexLast;
      72                 :           1 :     pindexLast.nHeight = 46367;
      73                 :           1 :     pindexLast.nTime = 1269211443;  // Block #46367
      74                 :           1 :     pindexLast.nBits = 0x1c387f6f;
      75                 :           1 :     unsigned int expected_nbits = 0x1d00e1fdU;
      76   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
                   +  - ]
      77   [ +  -  +  -  :           2 :     BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
             +  -  +  - ]
      78                 :             :     // Test that increasing nbits further would not be a PermittedDifficultyTransition.
      79                 :           1 :     unsigned int invalid_nbits = expected_nbits+1;
      80   [ +  -  +  -  :           2 :     BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
                   +  - ]
      81                 :           1 : }
      82                 :             : 
      83   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      84                 :             : {
      85         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
      86                 :           1 :     uint256 hash;
      87                 :           1 :     unsigned int nBits;
      88   [ +  -  +  - ]:           1 :     nBits = UintToArith256(consensus.powLimit).GetCompact(true);
      89         [ +  - ]:           1 :     hash = uint256{1};
      90   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
      91                 :           1 : }
      92                 :             : 
      93   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      94                 :             : {
      95         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
      96                 :           1 :     uint256 hash;
      97                 :           1 :     unsigned int nBits{~0x00800000U};
      98         [ +  - ]:           1 :     hash = uint256{1};
      99   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     100                 :           1 : }
     101                 :             : 
     102   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     103                 :             : {
     104         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     105                 :           1 :     uint256 hash;
     106                 :           1 :     unsigned int nBits;
     107         [ +  - ]:           1 :     arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
     108         [ +  - ]:           1 :     nBits_arith *= 2;
     109         [ +  - ]:           1 :     nBits = nBits_arith.GetCompact();
     110         [ +  - ]:           1 :     hash = uint256{1};
     111   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     112                 :           1 : }
     113                 :             : 
     114   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     115                 :             : {
     116         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     117                 :           1 :     uint256 hash;
     118                 :           1 :     unsigned int nBits;
     119         [ +  - ]:           1 :     arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
     120         [ +  - ]:           1 :     nBits = hash_arith.GetCompact();
     121         [ +  - ]:           1 :     hash_arith *= 2; // hash > nBits
     122         [ +  - ]:           1 :     hash = ArithToUint256(hash_arith);
     123   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     124                 :           1 : }
     125                 :             : 
     126   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     127                 :             : {
     128         [ +  - ]:           1 :     const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
     129                 :           1 :     uint256 hash;
     130                 :           1 :     unsigned int nBits;
     131                 :           1 :     arith_uint256 hash_arith{0};
     132         [ +  - ]:           1 :     nBits = hash_arith.GetCompact();
     133         [ +  - ]:           1 :     hash = ArithToUint256(hash_arith);
     134   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
                   +  - ]
     135                 :           1 : }
     136                 :             : 
     137   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     138                 :             : {
     139                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
     140         [ +  - ]:           1 :     std::vector<CBlockIndex> blocks(10000);
     141         [ +  + ]:       10001 :     for (int i = 0; i < 10000; i++) {
     142   [ +  +  +  + ]:       10000 :         blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
     143                 :       10000 :         blocks[i].nHeight = i;
     144         [ +  + ]:       10000 :         blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
     145                 :       10000 :         blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
     146   [ +  +  +  -  :       30002 :         blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
                   +  - ]
     147                 :             :     }
     148                 :             : 
     149         [ +  + ]:        1001 :     for (int j = 0; j < 1000; j++) {
     150                 :        1000 :         CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
     151                 :        1000 :         CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
     152         [ +  - ]:        1000 :         CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
     153                 :             : 
     154         [ +  - ]:        1000 :         int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
     155   [ +  -  +  - ]:        1000 :         BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
     156                 :             :     }
     157                 :           1 : }
     158                 :             : 
     159                 :           5 : void sanity_check_chainparams(const ArgsManager& args, ChainType chain_type)
     160                 :             : {
     161                 :           5 :     const auto chainParams = CreateChainParams(args, chain_type);
     162         [ +  - ]:           5 :     const auto consensus = chainParams->GetConsensus();
     163                 :             : 
     164                 :             :     // hash genesis is correct
     165   [ +  -  +  -  :           5 :     BOOST_CHECK_EQUAL(consensus.hashGenesisBlock, chainParams->GenesisBlock().GetHash());
                   +  - ]
     166                 :             : 
     167                 :             :     // target timespan is an even multiple of spacing
     168   [ +  -  +  - ]:           5 :     BOOST_CHECK_EQUAL(consensus.nPowTargetTimespan % consensus.nPowTargetSpacing, 0);
     169                 :             : 
     170                 :             :     // genesis nBits is positive, doesn't overflow and is lower than powLimit
     171                 :           5 :     arith_uint256 pow_compact;
     172                 :           5 :     bool neg, over;
     173         [ +  - ]:           5 :     pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over);
     174   [ +  -  +  -  :          15 :     BOOST_CHECK(!neg && pow_compact != 0);
          -  +  +  -  +  
                      - ]
     175   [ +  -  +  -  :          10 :     BOOST_CHECK(!over);
                   +  - ]
     176   [ +  -  +  -  :          15 :     BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact);
             +  -  +  + ]
     177                 :             : 
     178                 :             :     // check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()
     179         [ +  + ]:           5 :     if (!consensus.fPowNoRetargeting) {
     180         [ +  - ]:           4 :         arith_uint256 targ_max{UintToArith256(uint256{"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"})};
     181         [ +  - ]:           8 :         targ_max /= consensus.nPowTargetTimespan*4;
     182   [ +  -  +  -  :          12 :         BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);
                   +  - ]
     183                 :             :     }
     184                 :           5 : }
     185                 :             : 
     186   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     187                 :             : {
     188                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::MAIN);
     189                 :           1 : }
     190                 :             : 
     191   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     192                 :             : {
     193                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::REGTEST);
     194                 :           1 : }
     195                 :             : 
     196   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     197                 :             : {
     198                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::TESTNET);
     199                 :           1 : }
     200                 :             : 
     201   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_TESTNET4_sanity)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     202                 :             : {
     203                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::TESTNET4);
     204                 :           1 : }
     205                 :             : 
     206   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     207                 :             : {
     208                 :           1 :     sanity_check_chainparams(*m_node.args, ChainType::SIGNET);
     209                 :           1 : }
     210                 :             : 
     211                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1