]> Git Repo - VerusCoin.git/blob - src/test/pow_tests.cpp
Incorporate all Zcash updates through 2.0.7-3 in addition to PBaaS, reserves, etc.
[VerusCoin.git] / src / test / pow_tests.cpp
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 .
4
5 #include "main.h"
6 #include "pow.h"
7 #include "util.h"
8 #include "utiltest.h"
9 #include "test/test_bitcoin.h"
10
11 #include <boost/test/unit_test.hpp>
12
13 using namespace std;
14
15 BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
16
17 /* Test calculation of next difficulty target with no constraints applying */
18 BOOST_AUTO_TEST_CASE(get_next_work)
19 {
20     SelectParams(CBaseChainParams::MAIN);
21     const Consensus::Params& params = Params().GetConsensus();
22     BOOST_CHECK_EQUAL(150, params.PoWTargetSpacing(0));
23
24     int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
25     int64_t nThisTime = 1000003570;
26     arith_uint256 bnAvg;
27     bnAvg.SetCompact(0x1d00ffff);
28     BOOST_CHECK_EQUAL(0x1d011998,
29                       CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
30 }
31
32
33 BOOST_AUTO_TEST_CASE(get_next_work_blossom)
34 {
35     const Consensus::Params& params = RegtestActivateBlossom(true);
36     BOOST_CHECK_EQUAL(75, params.PoWTargetSpacing(0));
37
38     int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
39     int64_t nThisTime = 1000001445;
40     arith_uint256 bnAvg;
41     bnAvg.SetCompact(0x1d00ffff);
42     BOOST_CHECK_GT(0x1d011998,
43                    CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
44
45     RegtestDeactivateBlossom();
46 }
47
48 /* Test the constraint on the upper bound for next work */
49 BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
50 {
51     SelectParams(CBaseChainParams::MAIN);
52     const Consensus::Params& params = Params().GetConsensus();
53
54     int64_t nLastRetargetTime = 1231006505;
55     int64_t nThisTime = 1233061996;
56     arith_uint256 bnAvg;
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));
61 }
62
63 /* Test the constraint on the lower bound for actual time taken */
64 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
65 {
66     SelectParams(CBaseChainParams::MAIN);
67     const Consensus::Params& params = Params().GetConsensus();
68
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;
73     arith_uint256 bnAvg;
74     bnAvg.SetCompact(0x1c05a3f4);
75     BOOST_CHECK_EQUAL(0x1c04bceb,
76                       CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
77 }
78
79 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual_blossom)
80 {
81     const Consensus::Params& params = RegtestActivateBlossom(true);
82
83     int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
84     int64_t nThisTime = 1000000458;
85     arith_uint256 bnAvg;
86     bnAvg.SetCompact(0x1c05a3f4);
87     BOOST_CHECK_EQUAL(0x1c04bceb,
88                       CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
89
90     RegtestDeactivateBlossom();
91 }
92
93 /* Test the constraint on the upper bound for actual time taken */
94 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
95 {
96     SelectParams(CBaseChainParams::MAIN);
97     const Consensus::Params& params = Params().GetConsensus();
98
99     int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
100     // 17*150*(1 + maxAdjustDown*PoWDampingFactor) = 5814
101     int64_t nThisTime = 1000005815;
102     arith_uint256 bnAvg;
103     bnAvg.SetCompact(0x1c387f6f);
104     BOOST_CHECK_EQUAL(0x1c4a93bb,
105                       CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
106 }
107
108 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual_blossom)
109 {
110     const Consensus::Params& params = RegtestActivateBlossom(true);
111
112     int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
113     int64_t nThisTime = 1000002908;
114     arith_uint256 bnAvg;
115     bnAvg.SetCompact(0x1c387f6f);
116     BOOST_CHECK_EQUAL(0x1c4a93bb,
117                       CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
118
119     RegtestDeactivateBlossom();
120 }
121
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]);
130         if (i != 0)
131             blocks[i].chainPower += GetBlockProof(blocks[i - 1]);
132     }
133
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)];
138
139         int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, params);
140         BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
141     }
142 }
143
144 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
145 {
146     SelectParams(CBaseChainParams::MAIN);
147     GetBlockProofEquivalentTimeImpl(Params().GetConsensus());
148 }
149
150 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test_blossom)
151 {
152     GetBlockProofEquivalentTimeImpl(RegtestActivateBlossom(true));
153     RegtestDeactivateBlossom();
154 }
155
156 BOOST_AUTO_TEST_SUITE_END()
This page took 0.038945 seconds and 4 git commands to generate.