Merge #10953: [Refactor] Combine scriptPubKey and amount as CTxOut in CScriptCheck
[bitcoinplatinum.git] / src / test / pow_tests.cpp
blobb13f2625aada7580d290dec8b566d4288f7d4d1d
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.
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"
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 const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
20 int64_t nLastRetargetTime = 1261130161; // Block #30240
21 CBlockIndex pindexLast;
22 pindexLast.nHeight = 32255;
23 pindexLast.nTime = 1262152739; // Block #32255
24 pindexLast.nBits = 0x1d00ffff;
25 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00d86a);
28 /* Test the constraint on the upper bound for next work */
29 BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
31 const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
32 int64_t nLastRetargetTime = 1231006505; // Block #0
33 CBlockIndex pindexLast;
34 pindexLast.nHeight = 2015;
35 pindexLast.nTime = 1233061996; // Block #2015
36 pindexLast.nBits = 0x1d00ffff;
37 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00ffff);
40 /* Test the constraint on the lower bound for actual time taken */
41 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
43 const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
44 int64_t nLastRetargetTime = 1279008237; // Block #66528
45 CBlockIndex pindexLast;
46 pindexLast.nHeight = 68543;
47 pindexLast.nTime = 1279297671; // Block #68543
48 pindexLast.nBits = 0x1c05a3f4;
49 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1c0168fd);
52 /* Test the constraint on the upper bound for actual time taken */
53 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
55 const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
56 int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
57 CBlockIndex pindexLast;
58 pindexLast.nHeight = 46367;
59 pindexLast.nTime = 1269211443; // Block #46367
60 pindexLast.nBits = 0x1c387f6f;
61 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00e1fd);
64 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
66 const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
67 std::vector<CBlockIndex> blocks(10000);
68 for (int i = 0; i < 10000; i++) {
69 blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
70 blocks[i].nHeight = i;
71 blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
72 blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
73 blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
76 for (int j = 0; j < 1000; j++) {
77 CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
78 CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
79 CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
81 int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
82 BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
86 BOOST_AUTO_TEST_SUITE_END()