1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
9 #include "test/test_bitcoin.h"
11 #include <boost/test/unit_test.hpp>
15 BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
17 /* Test calculation of next difficulty target with no constraints applying */
18 BOOST_AUTO_TEST_CASE(get_next_work)
20 SelectParams(CBaseChainParams::MAIN);
21 const Consensus::Params& params = Params().GetConsensus();
22 BOOST_CHECK_EQUAL(150, params.PoWTargetSpacing(0));
24 int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
25 int64_t nThisTime = 1000003570;
27 bnAvg.SetCompact(0x1d00ffff);
28 BOOST_CHECK_EQUAL(0x1d011998,
29 CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
33 BOOST_AUTO_TEST_CASE(get_next_work_blossom)
35 const Consensus::Params& params = RegtestActivateBlossom(true);
36 BOOST_CHECK_EQUAL(75, params.PoWTargetSpacing(0));
38 int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
39 int64_t nThisTime = 1000001445;
41 bnAvg.SetCompact(0x1d00ffff);
42 BOOST_CHECK_GT(0x1d011998,
43 CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
45 RegtestDeactivateBlossom();
48 /* Test the constraint on the upper bound for next work */
49 BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
51 SelectParams(CBaseChainParams::MAIN);
52 const Consensus::Params& params = Params().GetConsensus();
54 int64_t nLastRetargetTime = 1231006505;
55 int64_t nThisTime = 1233061996;
57 // TODO change once the harder genesis block is generated
58 bnAvg.SetCompact(KOMODO_MINDIFF_NBITS);
59 BOOST_CHECK_EQUAL(KOMODO_MINDIFF_NBITS,
60 CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params));
63 /* Test the constraint on the lower bound for actual time taken */
64 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
66 SelectParams(CBaseChainParams::MAIN);
67 const Consensus::Params& params = Params().GetConsensus();
69 int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
70 // 17*150*(1 - PoWMaxAdjustUp*PoWDampingFactor) = 918
71 // so we pick 917 to be outside of this window
72 int64_t nThisTime = 100000917;
74 bnAvg.SetCompact(0x1c05a3f4);
75 BOOST_CHECK_EQUAL(0x1c04bceb,
76 CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
79 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual_blossom)
81 const Consensus::Params& params = RegtestActivateBlossom(true);
83 int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
84 int64_t nThisTime = 1000000458;
86 bnAvg.SetCompact(0x1c05a3f4);
87 BOOST_CHECK_EQUAL(0x1c04bceb,
88 CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
90 RegtestDeactivateBlossom();
93 /* Test the constraint on the upper bound for actual time taken */
94 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
96 SelectParams(CBaseChainParams::MAIN);
97 const Consensus::Params& params = Params().GetConsensus();
99 int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
100 // 17*150*(1 + maxAdjustDown*PoWDampingFactor) = 5814
101 int64_t nThisTime = 1000005815;
103 bnAvg.SetCompact(0x1c387f6f);
104 BOOST_CHECK_EQUAL(0x1c4a93bb,
105 CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
108 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual_blossom)
110 const Consensus::Params& params = RegtestActivateBlossom(true);
112 int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
113 int64_t nThisTime = 1000002908;
115 bnAvg.SetCompact(0x1c387f6f);
116 BOOST_CHECK_EQUAL(0x1c4a93bb,
117 CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
119 RegtestDeactivateBlossom();
122 void GetBlockProofEquivalentTimeImpl(const Consensus::Params& params) {
123 std::vector<CBlockIndex> blocks(10000);
124 for (int i = 0; i < 10000; i++) {
125 blocks[i].pprev = i ? &blocks[i - 1] : NULL;
126 blocks[i].SetHeight(i);
127 blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
128 blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
129 blocks[i].chainPower = CChainPower(&blocks[i]);
131 blocks[i].chainPower += GetBlockProof(blocks[i - 1]);
134 for (int j = 0; j < 1000; j++) {
135 CBlockIndex *p1 = &blocks[GetRand(10000)];
136 CBlockIndex *p2 = &blocks[GetRand(10000)];
137 CBlockIndex *p3 = &blocks[GetRand(10000)];
139 int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, params);
140 BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
144 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
146 SelectParams(CBaseChainParams::MAIN);
147 GetBlockProofEquivalentTimeImpl(Params().GetConsensus());
150 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test_blossom)
152 GetBlockProofEquivalentTimeImpl(RegtestActivateBlossom(true));
153 RegtestDeactivateBlossom();
156 BOOST_AUTO_TEST_SUITE_END()