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.
6 #include "chainparams.h"
10 #include "test/test_bitcoin.h"
12 #include <boost/test/unit_test.hpp>
14 BOOST_FIXTURE_TEST_SUITE(pow_tests
, BasicTestingSetup
)
16 /* Test calculation of next difficulty target with no constraints applying */
17 BOOST_AUTO_TEST_CASE(get_next_work
)
19 SelectParams(CBaseChainParams::MAIN
);
20 const Consensus::Params
& params
= Params().GetConsensus();
22 int64_t nLastRetargetTime
= 1261130161; // Block #30240
23 CBlockIndex pindexLast
;
24 pindexLast
.nHeight
= 32255;
25 pindexLast
.nTime
= 1262152739; // Block #32255
26 pindexLast
.nBits
= 0x1d00ffff;
27 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast
, nLastRetargetTime
, params
), 0x1d00d86a);
30 /* Test the constraint on the upper bound for next work */
31 BOOST_AUTO_TEST_CASE(get_next_work_pow_limit
)
33 SelectParams(CBaseChainParams::MAIN
);
34 const Consensus::Params
& params
= Params().GetConsensus();
36 int64_t nLastRetargetTime
= 1231006505; // Block #0
37 CBlockIndex pindexLast
;
38 pindexLast
.nHeight
= 2015;
39 pindexLast
.nTime
= 1233061996; // Block #2015
40 pindexLast
.nBits
= 0x1d00ffff;
41 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast
, nLastRetargetTime
, params
), 0x1d00ffff);
44 /* Test the constraint on the lower bound for actual time taken */
45 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual
)
47 SelectParams(CBaseChainParams::MAIN
);
48 const Consensus::Params
& params
= Params().GetConsensus();
50 int64_t nLastRetargetTime
= 1279008237; // Block #66528
51 CBlockIndex pindexLast
;
52 pindexLast
.nHeight
= 68543;
53 pindexLast
.nTime
= 1279297671; // Block #68543
54 pindexLast
.nBits
= 0x1c05a3f4;
55 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast
, nLastRetargetTime
, params
), 0x1c0168fd);
58 /* Test the constraint on the upper bound for actual time taken */
59 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual
)
61 SelectParams(CBaseChainParams::MAIN
);
62 const Consensus::Params
& params
= Params().GetConsensus();
64 int64_t nLastRetargetTime
= 1263163443; // NOTE: Not an actual block time
65 CBlockIndex pindexLast
;
66 pindexLast
.nHeight
= 46367;
67 pindexLast
.nTime
= 1269211443; // Block #46367
68 pindexLast
.nBits
= 0x1c387f6f;
69 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast
, nLastRetargetTime
, params
), 0x1d00e1fd);
72 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test
)
74 SelectParams(CBaseChainParams::MAIN
);
75 const Consensus::Params
& params
= Params().GetConsensus();
77 std::vector
<CBlockIndex
> blocks(10000);
78 for (int i
= 0; i
< 10000; i
++) {
79 blocks
[i
].pprev
= i
? &blocks
[i
- 1] : NULL
;
80 blocks
[i
].nHeight
= i
;
81 blocks
[i
].nTime
= 1269211443 + i
* params
.nPowTargetSpacing
;
82 blocks
[i
].nBits
= 0x207fffff; /* target 0x7fffff000... */
83 blocks
[i
].nChainWork
= i
? blocks
[i
- 1].nChainWork
+ GetBlockProof(blocks
[i
- 1]) : arith_uint256(0);
86 for (int j
= 0; j
< 1000; j
++) {
87 CBlockIndex
*p1
= &blocks
[GetRand(10000)];
88 CBlockIndex
*p2
= &blocks
[GetRand(10000)];
89 CBlockIndex
*p3
= &blocks
[GetRand(10000)];
91 int64_t tdiff
= GetBlockProofEquivalentTime(*p1
, *p2
, *p3
, params
);
92 BOOST_CHECK_EQUAL(tdiff
, p1
->GetBlockTime() - p2
->GetBlockTime());
96 BOOST_AUTO_TEST_SUITE_END()