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>
16 BOOST_FIXTURE_TEST_SUITE(pow_tests
, BasicTestingSetup
)
18 /* Test calculation of next difficulty target with no constraints applying */
19 BOOST_AUTO_TEST_CASE(get_next_work
)
21 SelectParams(CBaseChainParams::MAIN
);
22 const Consensus::Params
& params
= Params().GetConsensus();
24 int64_t nLastRetargetTime
= 1261130161; // Block #30240
25 CBlockIndex pindexLast
;
26 pindexLast
.nHeight
= 32255;
27 pindexLast
.nTime
= 1262152739; // Block #32255
28 pindexLast
.nBits
= 0x1d00ffff;
29 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast
, nLastRetargetTime
, params
), 0x1d00d86a);
32 /* Test the constraint on the upper bound for next work */
33 BOOST_AUTO_TEST_CASE(get_next_work_pow_limit
)
35 SelectParams(CBaseChainParams::MAIN
);
36 const Consensus::Params
& params
= Params().GetConsensus();
38 int64_t nLastRetargetTime
= 1231006505; // Block #0
39 CBlockIndex pindexLast
;
40 pindexLast
.nHeight
= 2015;
41 pindexLast
.nTime
= 1233061996; // Block #2015
42 pindexLast
.nBits
= 0x1d00ffff;
43 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast
, nLastRetargetTime
, params
), 0x1d00ffff);
46 /* Test the constraint on the lower bound for actual time taken */
47 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual
)
49 SelectParams(CBaseChainParams::MAIN
);
50 const Consensus::Params
& params
= Params().GetConsensus();
52 int64_t nLastRetargetTime
= 1279008237; // Block #66528
53 CBlockIndex pindexLast
;
54 pindexLast
.nHeight
= 68543;
55 pindexLast
.nTime
= 1279297671; // Block #68543
56 pindexLast
.nBits
= 0x1c05a3f4;
57 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast
, nLastRetargetTime
, params
), 0x1c0168fd);
60 /* Test the constraint on the upper bound for actual time taken */
61 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual
)
63 SelectParams(CBaseChainParams::MAIN
);
64 const Consensus::Params
& params
= Params().GetConsensus();
66 int64_t nLastRetargetTime
= 1263163443; // NOTE: Not an actual block time
67 CBlockIndex pindexLast
;
68 pindexLast
.nHeight
= 46367;
69 pindexLast
.nTime
= 1269211443; // Block #46367
70 pindexLast
.nBits
= 0x1c387f6f;
71 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast
, nLastRetargetTime
, params
), 0x1d00e1fd);
74 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test
)
76 SelectParams(CBaseChainParams::MAIN
);
77 const Consensus::Params
& params
= Params().GetConsensus();
79 std::vector
<CBlockIndex
> blocks(10000);
80 for (int i
= 0; i
< 10000; i
++) {
81 blocks
[i
].pprev
= i
? &blocks
[i
- 1] : NULL
;
82 blocks
[i
].nHeight
= i
;
83 blocks
[i
].nTime
= 1269211443 + i
* params
.nPowTargetSpacing
;
84 blocks
[i
].nBits
= 0x207fffff; /* target 0x7fffff000... */
85 blocks
[i
].nChainWork
= i
? blocks
[i
- 1].nChainWork
+ GetBlockProof(blocks
[i
- 1]) : arith_uint256(0);
88 for (int j
= 0; j
< 1000; j
++) {
89 CBlockIndex
*p1
= &blocks
[GetRand(10000)];
90 CBlockIndex
*p2
= &blocks
[GetRand(10000)];
91 CBlockIndex
*p3
= &blocks
[GetRand(10000)];
93 int64_t tdiff
= GetBlockProofEquivalentTime(*p1
, *p2
, *p3
, params
);
94 BOOST_CHECK_EQUAL(tdiff
, p1
->GetBlockTime() - p2
->GetBlockTime());
98 BOOST_AUTO_TEST_SUITE_END()