LCOV - code coverage report
Current view: top level - src/test - pow_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 90 90 100.0 %
Date: 2020-09-26 01:30:44 Functions: 72 72 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2015-2019 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/setup_common.h>
       9             : 
      10             : #include <boost/test/unit_test.hpp>
      11             : 
      12          89 : BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
      13             : 
      14             : /* Test calculation of next difficulty target with no constraints applying */
      15          95 : BOOST_AUTO_TEST_CASE(get_next_work)
      16             : {
      17           1 :     const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
      18             :     int64_t nLastRetargetTime = 1261130161; // Block #30240
      19           1 :     CBlockIndex pindexLast;
      20           1 :     pindexLast.nHeight = 32255;
      21           1 :     pindexLast.nTime = 1262152739;  // Block #32255
      22           1 :     pindexLast.nBits = 0x1d00ffff;
      23           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00d86aU);
      24           1 : }
      25             : 
      26             : /* Test the constraint on the upper bound for next work */
      27          95 : BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
      28             : {
      29           1 :     const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
      30             :     int64_t nLastRetargetTime = 1231006505; // Block #0
      31           1 :     CBlockIndex pindexLast;
      32           1 :     pindexLast.nHeight = 2015;
      33           1 :     pindexLast.nTime = 1233061996;  // Block #2015
      34           1 :     pindexLast.nBits = 0x1d00ffff;
      35           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00ffffU);
      36           1 : }
      37             : 
      38             : /* Test the constraint on the lower bound for actual time taken */
      39          95 : BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
      40             : {
      41           1 :     const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
      42             :     int64_t nLastRetargetTime = 1279008237; // Block #66528
      43           1 :     CBlockIndex pindexLast;
      44           1 :     pindexLast.nHeight = 68543;
      45           1 :     pindexLast.nTime = 1279297671;  // Block #68543
      46           1 :     pindexLast.nBits = 0x1c05a3f4;
      47           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1c0168fdU);
      48           1 : }
      49             : 
      50             : /* Test the constraint on the upper bound for actual time taken */
      51          95 : BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
      52             : {
      53           1 :     const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
      54             :     int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
      55           1 :     CBlockIndex pindexLast;
      56           1 :     pindexLast.nHeight = 46367;
      57           1 :     pindexLast.nTime = 1269211443;  // Block #46367
      58           1 :     pindexLast.nBits = 0x1c387f6f;
      59           1 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00e1fdU);
      60           1 : }
      61             : 
      62          95 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
      63             : {
      64           1 :     const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
      65           1 :     uint256 hash;
      66             :     unsigned int nBits;
      67           1 :     nBits = UintToArith256(consensus.powLimit).GetCompact(true);
      68           1 :     hash.SetHex("0x1");
      69           1 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
      70           1 : }
      71             : 
      72          95 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
      73             : {
      74           1 :     const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
      75           1 :     uint256 hash;
      76             :     unsigned int nBits = ~0x00800000;
      77           1 :     hash.SetHex("0x1");
      78           1 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
      79           1 : }
      80             : 
      81          95 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
      82             : {
      83           1 :     const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
      84           1 :     uint256 hash;
      85             :     unsigned int nBits;
      86           1 :     arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
      87           1 :     nBits_arith *= 2;
      88           1 :     nBits = nBits_arith.GetCompact();
      89           1 :     hash.SetHex("0x1");
      90           1 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
      91           1 : }
      92             : 
      93          95 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
      94             : {
      95           1 :     const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
      96           1 :     uint256 hash;
      97             :     unsigned int nBits;
      98           1 :     arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
      99           1 :     nBits = hash_arith.GetCompact();
     100           1 :     hash_arith *= 2; // hash > nBits
     101           1 :     hash = ArithToUint256(hash_arith);
     102           1 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
     103           1 : }
     104             : 
     105          95 : BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
     106             : {
     107           1 :     const auto consensus = CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
     108           1 :     uint256 hash;
     109             :     unsigned int nBits;
     110           1 :     arith_uint256 hash_arith{0};
     111           1 :     nBits = hash_arith.GetCompact();
     112           1 :     hash = ArithToUint256(hash_arith);
     113           1 :     BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
     114           1 : }
     115             : 
     116          95 : BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
     117             : {
     118           1 :     const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
     119           1 :     std::vector<CBlockIndex> blocks(10000);
     120       10001 :     for (int i = 0; i < 10000; i++) {
     121       10000 :         blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
     122       10000 :         blocks[i].nHeight = i;
     123       10000 :         blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
     124       10000 :         blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
     125       10000 :         blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
     126             :     }
     127             : 
     128        1001 :     for (int j = 0; j < 1000; j++) {
     129        1000 :         CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
     130        1000 :         CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
     131        1000 :         CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
     132             : 
     133        1000 :         int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
     134        1000 :         BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
     135        1000 :     }
     136           1 : }
     137             : 
     138          89 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.15