LCOV - code coverage report
Current view: top level - src/test - pow_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 59 59 100.0 %
Date: 2015-10-12 22:39:14 Functions: 12 22 54.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2015 The Bitcoin Core developers
       2             : // Distributed under the MIT/X11 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 "random.h"
       9             : #include "util.h"
      10             : #include "test/test_bitcoin.h"
      11             : 
      12             : #include <boost/test/unit_test.hpp>
      13             : 
      14             : using namespace std;
      15             : 
      16           1 : BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
      17             : 
      18             : /* Test calculation of next difficulty target with no constraints applying */
      19           6 : BOOST_AUTO_TEST_CASE(get_next_work)
      20             : {
      21           1 :     SelectParams(CBaseChainParams::MAIN);
      22           2 :     const Consensus::Params& params = Params().GetConsensus();
      23             : 
      24           1 :     int64_t nLastRetargetTime = 1261130161; // Block #30240
      25           1 :     CBlockIndex pindexLast;
      26           1 :     pindexLast.nHeight = 32255;
      27           1 :     pindexLast.nTime = 1262152739;  // Block #32255
      28           1 :     pindexLast.nBits = 0x1d00ffff;
      29           5 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00d86a);
      30           1 : }
      31             : 
      32             : /* Test the constraint on the upper bound for next work */
      33           6 : BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
      34             : {
      35           1 :     SelectParams(CBaseChainParams::MAIN);
      36           2 :     const Consensus::Params& params = Params().GetConsensus();
      37             : 
      38           1 :     int64_t nLastRetargetTime = 1231006505; // Block #0
      39           1 :     CBlockIndex pindexLast;
      40           1 :     pindexLast.nHeight = 2015;
      41           1 :     pindexLast.nTime = 1233061996;  // Block #2015
      42           1 :     pindexLast.nBits = 0x1d00ffff;
      43           5 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00ffff);
      44           1 : }
      45             : 
      46             : /* Test the constraint on the lower bound for actual time taken */
      47           6 : BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
      48             : {
      49           1 :     SelectParams(CBaseChainParams::MAIN);
      50           2 :     const Consensus::Params& params = Params().GetConsensus();
      51             : 
      52           1 :     int64_t nLastRetargetTime = 1279008237; // Block #66528
      53           1 :     CBlockIndex pindexLast;
      54           1 :     pindexLast.nHeight = 68543;
      55           1 :     pindexLast.nTime = 1279297671;  // Block #68543
      56           1 :     pindexLast.nBits = 0x1c05a3f4;
      57           5 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c0168fd);
      58           1 : }
      59             : 
      60             : /* Test the constraint on the upper bound for actual time taken */
      61           6 : BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
      62             : {
      63           1 :     SelectParams(CBaseChainParams::MAIN);
      64           2 :     const Consensus::Params& params = Params().GetConsensus();
      65             : 
      66           1 :     int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
      67           1 :     CBlockIndex pindexLast;
      68           1 :     pindexLast.nHeight = 46367;
      69           1 :     pindexLast.nTime = 1269211443;  // Block #46367
      70           1 :     pindexLast.nBits = 0x1c387f6f;
      71           5 :     BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00e1fd);
      72           1 : }
      73             : 
      74           6 : BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
      75             : {
      76           1 :     SelectParams(CBaseChainParams::MAIN);
      77           2 :     const Consensus::Params& params = Params().GetConsensus();
      78             : 
      79           2 :     std::vector<CBlockIndex> blocks(10000);
      80       10001 :     for (int i = 0; i < 10000; i++) {
      81       30000 :         blocks[i].pprev = i ? &blocks[i - 1] : NULL;
      82       20000 :         blocks[i].nHeight = i;
      83       10000 :         blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
      84       10000 :         blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
      85       59998 :         blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
      86             :     }
      87             : 
      88        1000 :     for (int j = 0; j < 1000; j++) {
      89        2000 :         CBlockIndex *p1 = &blocks[GetRand(10000)];
      90        2000 :         CBlockIndex *p2 = &blocks[GetRand(10000)];
      91        2000 :         CBlockIndex *p3 = &blocks[GetRand(10000)];
      92             : 
      93        1000 :         int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, params);
      94        6000 :         BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
      95             :     }
      96           1 : }
      97             : 
      98           3 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.11