Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
grasberg_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2015-2019 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.
4
5#include <pow/grasberg.h>
6
7#include <chain.h>
8#include <chainparams.h>
9#include <test/lcg.h>
10
11#include <test/util/setup_common.h>
12
13#include <boost/test/unit_test.hpp>
14
15#include <cmath>
16
17using namespace grasberg;
18
20
25 BOOST_CHECK_EQUAL(deterministicExp2(0x000ff1ce), 724359);
26 BOOST_CHECK_EQUAL(deterministicExp2(0x0badf00d), 137991563);
27 BOOST_CHECK_EQUAL(deterministicExp2(0x7fffffff), 1779033703);
28 BOOST_CHECK_EQUAL(deterministicExp2(0x80000000), 1779033704);
29 BOOST_CHECK_EQUAL(deterministicExp2(0xdeadbeef), 3553907236);
30 BOOST_CHECK_EQUAL(deterministicExp2(0xfeedcafe), 4270091087);
31 BOOST_CHECK_EQUAL(deterministicExp2(0xffffffff), 4294967294);
32
33 // 100 randomly picked values.
34 BOOST_CHECK_EQUAL(deterministicExp2(0x087ae9b4), 0x05f22b96);
35 BOOST_CHECK_EQUAL(deterministicExp2(0x5425cdf3), 0x41818942);
36 BOOST_CHECK_EQUAL(deterministicExp2(0x8dbb6e9b), 0x77c0cbd7);
37 BOOST_CHECK_EQUAL(deterministicExp2(0xc72cd267), 0xb6fc25bf);
38 BOOST_CHECK_EQUAL(deterministicExp2(0x9a67c66f), 0x84ded919);
39 BOOST_CHECK_EQUAL(deterministicExp2(0x94667388), 0x7e994692);
40 BOOST_CHECK_EQUAL(deterministicExp2(0x589c28c9), 0x456a0046);
41 BOOST_CHECK_EQUAL(deterministicExp2(0x54a063f8), 0x41ec5164);
42 BOOST_CHECK_EQUAL(deterministicExp2(0xe4741ebb), 0xdb33b0e3);
43 BOOST_CHECK_EQUAL(deterministicExp2(0xed6e1573), 0xe6e49c99);
44 BOOST_CHECK_EQUAL(deterministicExp2(0xd74a77d8), 0xca907219);
45 BOOST_CHECK_EQUAL(deterministicExp2(0xe9615051), 0xe19549ff);
46 BOOST_CHECK_EQUAL(deterministicExp2(0x4b52afdf), 0x39ea41a3);
47 BOOST_CHECK_EQUAL(deterministicExp2(0x9c23c8e3), 0x86b372e0);
48 BOOST_CHECK_EQUAL(deterministicExp2(0x7e3b8bb5), 0x684f7012);
49 BOOST_CHECK_EQUAL(deterministicExp2(0x6e2a4d01), 0x58f8adc7);
50 BOOST_CHECK_EQUAL(deterministicExp2(0xf82b8d68), 0xf5427e75);
51 BOOST_CHECK_EQUAL(deterministicExp2(0x95c28f4c), 0x80028e21);
52 BOOST_CHECK_EQUAL(deterministicExp2(0x7e7814bf), 0x688a837b);
53 BOOST_CHECK_EQUAL(deterministicExp2(0x315df77b), 0x249c68ce);
54 BOOST_CHECK_EQUAL(deterministicExp2(0x64c8d384), 0x5051d61a);
55 BOOST_CHECK_EQUAL(deterministicExp2(0x7e60ec44), 0x6873e8ca);
56 BOOST_CHECK_EQUAL(deterministicExp2(0x3b8b004f), 0x2cc90282);
57 BOOST_CHECK_EQUAL(deterministicExp2(0x444c4ea5), 0x3400274f);
58 BOOST_CHECK_EQUAL(deterministicExp2(0x80759f4a), 0x6a7d4596);
59 BOOST_CHECK_EQUAL(deterministicExp2(0xe7e46027), 0xdfa5919f);
60 BOOST_CHECK_EQUAL(deterministicExp2(0x487b844b), 0x37829001);
61 BOOST_CHECK_EQUAL(deterministicExp2(0xdd45a3f4), 0xd20d0b36);
62 BOOST_CHECK_EQUAL(deterministicExp2(0x3e03f2ea), 0x2ece376f);
63 BOOST_CHECK_EQUAL(deterministicExp2(0xe69e7c49), 0xddff10d2);
64 BOOST_CHECK_EQUAL(deterministicExp2(0x2901bda5), 0x1e0fcf09);
65 BOOST_CHECK_EQUAL(deterministicExp2(0x196a8489), 0x123cc6f8);
66 BOOST_CHECK_EQUAL(deterministicExp2(0x9a19b128), 0x848caaea);
67 BOOST_CHECK_EQUAL(deterministicExp2(0x0398cca8), 0x02815e0a);
68 BOOST_CHECK_EQUAL(deterministicExp2(0xbef03f61), 0xad4da3d5);
69 BOOST_CHECK_EQUAL(deterministicExp2(0x102e1734), 0x0b76e599);
70 BOOST_CHECK_EQUAL(deterministicExp2(0xf363cf7b), 0xeed050f5);
71 BOOST_CHECK_EQUAL(deterministicExp2(0x82c00c2d), 0x6cbe9fc1);
72 BOOST_CHECK_EQUAL(deterministicExp2(0xcffbcde3), 0xc194a25a);
73 BOOST_CHECK_EQUAL(deterministicExp2(0x18d6f2fe), 0x11cf4a02);
74 BOOST_CHECK_EQUAL(deterministicExp2(0xde1ea615), 0xd31f327e);
75 BOOST_CHECK_EQUAL(deterministicExp2(0x0e96d48f), 0x0a509911);
76 BOOST_CHECK_EQUAL(deterministicExp2(0x378fa034), 0x298f481e);
77 BOOST_CHECK_EQUAL(deterministicExp2(0x857061ec), 0x6f68e4c5);
78 BOOST_CHECK_EQUAL(deterministicExp2(0xc8408fdd), 0xb8445ebf);
79 BOOST_CHECK_EQUAL(deterministicExp2(0x09c3db93), 0x06dbe12d);
80 BOOST_CHECK_EQUAL(deterministicExp2(0x128598df), 0x0d2a6e92);
81 BOOST_CHECK_EQUAL(deterministicExp2(0x7fa9d335), 0x69b57700);
82 BOOST_CHECK_EQUAL(deterministicExp2(0x1177f216), 0x0c6630f5);
83 BOOST_CHECK_EQUAL(deterministicExp2(0xa7b1dfcd), 0x931e2c0c);
84 BOOST_CHECK_EQUAL(deterministicExp2(0xb0be2271), 0x9d1d8e6c);
85 BOOST_CHECK_EQUAL(deterministicExp2(0x665c61aa), 0x51c21b91);
86 BOOST_CHECK_EQUAL(deterministicExp2(0xe4472200), 0xdaf9d24c);
87 BOOST_CHECK_EQUAL(deterministicExp2(0x9ba928c3), 0x8631cfcc);
88 BOOST_CHECK_EQUAL(deterministicExp2(0xd1464a98), 0xc327a2cc);
89 BOOST_CHECK_EQUAL(deterministicExp2(0x64a0901d), 0x502d2df6);
90 BOOST_CHECK_EQUAL(deterministicExp2(0x636df03b), 0x4f16881e);
91 BOOST_CHECK_EQUAL(deterministicExp2(0x61bf5d3f), 0x4d90c410);
92 BOOST_CHECK_EQUAL(deterministicExp2(0xa8bf3caa), 0x944498b4);
93 BOOST_CHECK_EQUAL(deterministicExp2(0x683ed475), 0x537c7131);
94 BOOST_CHECK_EQUAL(deterministicExp2(0xb375673c), 0xa02a1e57);
95 BOOST_CHECK_EQUAL(deterministicExp2(0xcd1e7fa3), 0xbe1b716f);
96 BOOST_CHECK_EQUAL(deterministicExp2(0x8d064b3f), 0x7708af2d);
97 BOOST_CHECK_EQUAL(deterministicExp2(0x83148fe1), 0x6d122064);
98 BOOST_CHECK_EQUAL(deterministicExp2(0xbee2c2b4), 0xad3df6c9);
99 BOOST_CHECK_EQUAL(deterministicExp2(0x8f39659e), 0x794630af);
100 BOOST_CHECK_EQUAL(deterministicExp2(0x0cc3e1a5), 0x0900b8f2);
101 BOOST_CHECK_EQUAL(deterministicExp2(0xc41798fb), 0xb355fb23);
102 BOOST_CHECK_EQUAL(deterministicExp2(0x4dc8ef08), 0x3c03bab6);
103 BOOST_CHECK_EQUAL(deterministicExp2(0x00171815), 0x00100270);
104 BOOST_CHECK_EQUAL(deterministicExp2(0xdb6c9075), 0xcfb99180);
105 BOOST_CHECK_EQUAL(deterministicExp2(0x13cd960c), 0x0e19e23a);
106 BOOST_CHECK_EQUAL(deterministicExp2(0x9e19dd37), 0x88c7fe4a);
107 BOOST_CHECK_EQUAL(deterministicExp2(0x3afa11d6), 0x2c53111d);
108 BOOST_CHECK_EQUAL(deterministicExp2(0x03867141), 0x0274851a);
109 BOOST_CHECK_EQUAL(deterministicExp2(0x65b991fe), 0x512d57a0);
110 BOOST_CHECK_EQUAL(deterministicExp2(0x8960fee9), 0x7359a24d);
111 BOOST_CHECK_EQUAL(deterministicExp2(0x06302a65), 0x04534f70);
112 BOOST_CHECK_EQUAL(deterministicExp2(0x9ab4e776), 0x85301748);
113 BOOST_CHECK_EQUAL(deterministicExp2(0xd5c11fed), 0xc8a9149c);
114 BOOST_CHECK_EQUAL(deterministicExp2(0xcd325145), 0xbe33626d);
115 BOOST_CHECK_EQUAL(deterministicExp2(0x45dd5919), 0x354f4f40);
116 BOOST_CHECK_EQUAL(deterministicExp2(0xa5074b00), 0x9037d404);
117 BOOST_CHECK_EQUAL(deterministicExp2(0x19139f48), 0x11fc48dc);
118 BOOST_CHECK_EQUAL(deterministicExp2(0x0ef74095), 0x0a962932);
119 BOOST_CHECK_EQUAL(deterministicExp2(0x6d1ae43b), 0x57fb888b);
120 BOOST_CHECK_EQUAL(deterministicExp2(0x021946f0), 0x017578d9);
121 BOOST_CHECK_EQUAL(deterministicExp2(0xcae745a8), 0xbb7059dd);
122 BOOST_CHECK_EQUAL(deterministicExp2(0x47572522), 0x368c57ef);
123 BOOST_CHECK_EQUAL(deterministicExp2(0x576843af), 0x445b2880);
124 BOOST_CHECK_EQUAL(deterministicExp2(0x5b5734e9), 0x47d4359b);
125 BOOST_CHECK_EQUAL(deterministicExp2(0xb504a24a), 0xa1eced3c);
126 BOOST_CHECK_EQUAL(deterministicExp2(0xa99c6f97), 0x95370102);
127 BOOST_CHECK_EQUAL(deterministicExp2(0xd63094c2), 0xc932f907);
128 BOOST_CHECK_EQUAL(deterministicExp2(0x8339e407), 0x6d370833);
129 BOOST_CHECK_EQUAL(deterministicExp2(0x79056f7b), 0x63430c57);
130 BOOST_CHECK_EQUAL(deterministicExp2(0xee327961), 0xe7e7c8fb);
131 BOOST_CHECK_EQUAL(deterministicExp2(0x89c5e1a0), 0x73bf2027);
132 BOOST_CHECK_EQUAL(deterministicExp2(0x1f28adf5), 0x1688f169);
133 BOOST_CHECK_EQUAL(deterministicExp2(0x74abd815), 0x5f1a2852);
134
135 // Check a ton of random values.
137 for (int i = 0; i < 100000; i++) {
138 static constexpr int64_t POW2_32 = int64_t(1) << 32;
139
140 const uint32_t n = lcg.next();
141 const double d = double(n) / POW2_32;
142
143 const double computed = double(deterministicExp2(n)) / POW2_32;
144 const double expected = exp2(d) - 1;
145
146 BOOST_CHECK(fabs(computed - expected) < 0.0000000075);
147 }
148}
149
151 const auto chainParams =
153 const auto &params = chainParams->GetConsensus();
154
155 const int nHeight = 100000;
157 nHeight * params.nPowTargetSpacing + chainParams->GenesisBlock().nTime;
158
159 CBlockIndex block;
160 block.nHeight = nHeight;
161 block.nTime = expectedBlockTime;
162
163 // When block come on schedule, the block time is what we expect.
164 BOOST_CHECK_EQUAL(computeTargetBlockTime(&block, *chainParams),
165 params.nPowTargetSpacing);
166
167 // As block come later and later, the block time we target gets shorter.
168 int64_t currentTarget = params.nPowTargetSpacing;
169 std::vector<int> downSteps = {
170 2910, 2916, 2921, 2925, 2931, 2935, 2941, 2945, 2950, 2955, 2961,
171 2965, 2970, 2976, 2980, 2986, 2991, 2995, 3001, 3007, 3011, 3016,
172 3022, 3027, 3033, 3037, 3043, 3048, 3054, 3059, 3064, 3069, 3076,
173 3080, 3086, 3091, 3097, 3103, 3107, 3114, 3119, 3124, 3131, 3135,
174 3142, 3147, 3153, 3158, 3165, 3170, 3175, 3182, 3187, 3194, 3199,
175 3205, 3210, 3217, 3223, 3228, 3235, 3241, 3246, 3253, 3259, 3265,
176 };
177
178 for (int step : downSteps) {
180 for (int i = 0; i < step; i++) {
181 block.nTime++;
182 BOOST_CHECK_EQUAL(computeTargetBlockTime(&block, *chainParams),
184 }
185 }
186
187 // Now we reached saturation and the targeted block time will stay where it
188 // is.
190 for (int i = 0; i < 10000; i++) {
191 block.nTime++;
192 BOOST_CHECK_EQUAL(computeTargetBlockTime(&block, *chainParams),
194 }
195
196 // As block come sooner and sooner, the block time we target gets longer.
197 block.nTime = expectedBlockTime;
198 currentTarget = params.nPowTargetSpacing;
199 std::vector<int> upSteps = {
200 2906, 2901, 2896, 2892, 2887, 2882, 2877, 2873, 2867, 2864, 2858,
201 2854, 2849, 2844, 2840, 2835, 2831, 2826, 2822, 2816, 2813, 2808,
202 2803, 2799, 2794, 2790, 2786, 2781, 2776, 2772, 2768, 2764, 2759,
203 2754, 2751, 2746, 2741, 2738, 2733, 2729, 2724, 2721, 2716, 2711,
204 2708, 2704, 2699, 2695, 2691, 2687, 2682, 2679, 2674, 2671, 2666,
205 2662, 2658, 2655, 2650, 2646, 2642, 2638, 2634, 2630, 2626, 2622,
206 2619, 2614, 2611, 2606, 2603, 2599, 2594, 2592,
207 };
208
209 for (int step : upSteps) {
210 for (int i = 0; i < step; i++) {
211 block.nTime--;
212 BOOST_CHECK_EQUAL(computeTargetBlockTime(&block, *chainParams),
214 }
216 }
217
218 // Now we reached saturation and the targeted block time will stay where it
219 // is.
220 for (int i = 0; i < 10000; i++) {
221 block.nTime--;
222 BOOST_CHECK_EQUAL(computeTargetBlockTime(&block, *chainParams),
224 }
225}
226
228 uint32_t nBits) {
229 CBlockIndex block;
230 block.pprev = pindexPrev;
231 block.nHeight = pindexPrev->nHeight + 1;
232 block.nTime = pindexPrev->nTime + nTimeInterval;
233 block.nBits = nBits;
234
235 block.nChainWork = pindexPrev->nChainWork + GetBlockProof(block);
236 return block;
237}
238
240 const auto chainParams =
242 const auto &params = chainParams->GetConsensus();
243
244 std::vector<CBlockIndex> blocks(3000);
245
246 const arith_uint256 powLimit = UintToArith256(params.powLimit);
247 uint32_t powLimitBits = powLimit.GetCompact();
248 arith_uint256 currentPow = powLimit >> 4;
250
251 // Genesis block.
252 blocks[0] = CBlockIndex();
253 blocks[0].nHeight = 0;
254 blocks[0].nTime = 1269211443;
255 blocks[0].nBits = initialBits;
256
257 blocks[0].nChainWork = GetBlockProof(blocks[0]);
258
259 // Check that we can use Grasberg directly from genesis.
261 uint32_t nBits =
262 GetNextGrasbergWorkRequired(&blocks[0], &blkHeaderDummy, *chainParams);
263 BOOST_CHECK_EQUAL(nBits, 0x1c100195);
264
265 // Mine several blocks and check the difficulty.
266 size_t h = 1;
267
268 // Mine regular 600s blocks.
269 std::vector<uint32_t> diffs = {
270 0x1c10032b, 0x1c1004c1, 0x1c100658, 0x1c1007ef, 0x1c100986, 0x1c100b1d,
271 0x1c100cb4, 0x1c100e4b, 0x1c100fe3, 0x1c10117b, 0x1c101313, 0x1c1014ab,
272 0x1c101643, 0x1c1017db, 0x1c101974, 0x1c101b0d, 0x1c101ca6, 0x1c101e3f,
273 0x1c101fd8, 0x1c102171, 0x1c10230a, 0x1c1024a4, 0x1c10263e, 0x1c1027d8,
274 0x1c102972, 0x1c102b0c, 0x1c102ca6, 0x1c102e41, 0x1c102fdc, 0x1c103177,
275 0x1c103312, 0x1c1034ad, 0x1c103648, 0x1c1037e4, 0x1c103980, 0x1c103b1c,
276 0x1c103cb8, 0x1c103e54, 0x1c103ff0, 0x1c10418c, 0x1c104329, 0x1c1044c6,
277 0x1c104663, 0x1c104800, 0x1c10499d, 0x1c104b3a, 0x1c104cd8, 0x1c104e76,
278 0x1c105014, 0x1c1051b2, 0x1c105350, 0x1c1054ee, 0x1c10568d, 0x1c10582c,
279 0x1c1059cb, 0x1c105b6a, 0x1c105d09, 0x1c105ea8, 0x1c106048, 0x1c1061e8,
280 0x1c106388, 0x1c106528, 0x1c1066c8, 0x1c106868, 0x1c106a09, 0x1c106baa,
281 0x1c106d4b, 0x1c106eec, 0x1c10708d, 0x1c10722e, 0x1c1073cf, 0x1c107571,
282 0x1c107713, 0x1c1078b5, 0x1c107a57, 0x1c107bf9, 0x1c107d9b, 0x1c107f3e,
283 0x1c1080e1, 0x1c108284, 0x1c108427, 0x1c1085ca, 0x1c10876d, 0x1c108911,
284 0x1c108ab5, 0x1c108c59, 0x1c108dfd, 0x1c108fa1, 0x1c109145, 0x1c1092ea,
285 0x1c10948f, 0x1c109634, 0x1c1097d9, 0x1c10997e, 0x1c109b23, 0x1c109cc9,
286 0x1c109e6f, 0x1c10a015, 0x1c10a1bb, 0x1c10a361, 0x1c10a507, 0x1c10a6ae,
287 0x1c10a855, 0x1c10a9fc, 0x1c10aba3, 0x1c10ad4a, 0x1c10aef1, 0x1c10b099,
288 0x1c10b241, 0x1c10b3e9, 0x1c10b591, 0x1c10b739, 0x1c10b8e1, 0x1c10ba8a,
289 0x1c10bc33, 0x1c10bddc, 0x1c10bf85, 0x1c10c12e, 0x1c10c2d7, 0x1c10c480,
290 0x1c10c62a, 0x1c10c7d4, 0x1c10c97e, 0x1c10cb28, 0x1c10ccd2, 0x1c10ce7c,
291 0x1c10d027, 0x1c10d1d2, 0x1c10d37d, 0x1c10d528, 0x1c10d6d3, 0x1c10d87e,
292 0x1c10da2a, 0x1c10dbd6, 0x1c10dd82, 0x1c10df2e, 0x1c10e0da, 0x1c10e286,
293 0x1c10e433, 0x1c10e5e0, 0x1c10e78d, 0x1c10e93a, 0x1c10eae7, 0x1c10ec94,
294 0x1c10ee42, 0x1c10eff0, 0x1c10f19e, 0x1c10f34c, 0x1c10f4fa, 0x1c10f6a8,
295 0x1c10f857, 0x1c10fa06, 0x1c10fbb5, 0x1c10fd64, 0x1c10ff13, 0x1c1100c2,
296 0x1c110272, 0x1c110422, 0x1c1105d2, 0x1c110782, 0x1c110932, 0x1c110ae2,
297 0x1c110c93, 0x1c110e44, 0x1c110ff5, 0x1c1111a6, 0x1c111357, 0x1c111508,
298 0x1c1116ba, 0x1c11186c, 0x1c111a1e, 0x1c111bd0, 0x1c111d82, 0x1c111f34,
299 0x1c1120e7, 0x1c11229a, 0x1c11244d, 0x1c112600, 0x1c1127b3, 0x1c112967,
300 0x1c112b1b, 0x1c112ccf, 0x1c112e83, 0x1c113037, 0x1c1131eb, 0x1c1133a0,
301 0x1c113555, 0x1c11370a, 0x1c1138bf, 0x1c113a74, 0x1c113c29, 0x1c113ddf,
302 0x1c113f95, 0x1c11414b, 0x1c114301, 0x1c1144b7, 0x1c11466d, 0x1c114824,
303 0x1c1149db, 0x1c114b92, 0x1c114d49, 0x1c114f00, 0x1c1150b7, 0x1c11526f,
304 0x1c115427, 0x1c1155df, 0x1c115797, 0x1c11594f, 0x1c115b07, 0x1c115cc0,
305 0x1c115e79, 0x1c116032, 0x1c1161eb, 0x1c1163a4, 0x1c11655d, 0x1c116717,
306 0x1c1168d1, 0x1c116a8b, 0x1c116c45, 0x1c116dff, 0x1c116fb9, 0x1c117174,
307 0x1c11732f, 0x1c1174ea, 0x1c1176a5, 0x1c117860, 0x1c117a1c, 0x1c117bd8,
308 0x1c117d94, 0x1c117f50, 0x1c11810c, 0x1c1182c8, 0x1c118485, 0x1c118642,
309 0x1c1187ff, 0x1c1189bc, 0x1c118b79, 0x1c118d36, 0x1c118ef4, 0x1c1190b2,
310 0x1c119270, 0x1c11942e, 0x1c1195ec, 0x1c1197aa, 0x1c119969, 0x1c119b28,
311 0x1c119ce7, 0x1c119ea6, 0x1c11a065, 0x1c11a224, 0x1c11a3e4, 0x1c11a5a4,
312 0x1c11a764, 0x1c11a924, 0x1c11aae4, 0x1c11aca5, 0x1c11ae66, 0x1c11b027,
313 0x1c11b1e8, 0x1c11b3a9, 0x1c11b56a, 0x1c11b72c, 0x1c11b8ee, 0x1c11bab0,
314 0x1c11bc72, 0x1c11be34, 0x1c11bff6, 0x1c11c1b9, 0x1c11c37c, 0x1c11c53f,
315 0x1c11c702, 0x1c11c8c5, 0x1c11ca88, 0x1c11cc4c, 0x1c11ce10, 0x1c11cfd4,
316 0x1c11d198, 0x1c11d35c, 0x1c11d521, 0x1c11d6e6, 0x1c11d8ab, 0x1c11da70,
317 0x1c11dc35, 0x1c11ddfa, 0x1c11dfc0, 0x1c11e186, 0x1c11e34c, 0x1c11e512,
318 0x1c11e6d8, 0x1c11e89e, 0x1c11ea65, 0x1c11ec2c, 0x1c11edf3, 0x1c11efba,
319 0x1c11f181, 0x1c11f349, 0x1c11f511, 0x1c11f6d9, 0x1c11f8a1, 0x1c11fa69,
320 };
321
322 for (uint32_t expected : diffs) {
323 blocks[h] = GetBlockIndex(&blocks[h - 1], 600, nBits);
325 *chainParams);
326 BOOST_CHECK_EQUAL(nBits, expected);
327 h++;
328 }
329
330 // Mine faster to raise difficulty.
331 diffs = {
332 0x1c11eee4, 0x1c11e366, 0x1c11d7f0, 0x1c11cc81, 0x1c11c119, 0x1c11b5b9,
333 0x1c11aa60, 0x1c119f0e, 0x1c1193c3, 0x1c118880, 0x1c117d44, 0x1c11720f,
334 0x1c1166e1, 0x1c115bba, 0x1c11509a, 0x1c114582, 0x1c113a71, 0x1c112f67,
335 0x1c112464, 0x1c111968, 0x1c110e73, 0x1c110385, 0x1c10f89e, 0x1c10edbe,
336 0x1c10e2e5, 0x1c10d813, 0x1c10cd48, 0x1c10c284, 0x1c10b7c7, 0x1c10ad11,
337 0x1c10a261, 0x1c1097b8, 0x1c108d16, 0x1c10827b, 0x1c1077e7, 0x1c106d59,
338 0x1c1062d2, 0x1c105852, 0x1c104dd9, 0x1c104366, 0x1c1038fa, 0x1c102e95,
339 0x1c102436, 0x1c1019de, 0x1c100f8d, 0x1c100542, 0x1c0ffafe, 0x1c0ff0c0,
340 0x1c0fe689, 0x1c0fdc59, 0x1c0fd22f, 0x1c0fc80c, 0x1c0fbdef, 0x1c0fb3d9,
341 0x1c0fa9c9, 0x1c0f9fc0, 0x1c0f95bd, 0x1c0f8bc0, 0x1c0f81ca, 0x1c0f77da,
342 0x1c0f6df0, 0x1c0f640d, 0x1c0f5a30, 0x1c0f5059, 0x1c0f4689, 0x1c0f3cbf,
343 0x1c0f32fb, 0x1c0f293e, 0x1c0f1f87, 0x1c0f15d6, 0x1c0f0c2b, 0x1c0f0286,
344 0x1c0ef8e8, 0x1c0eef50, 0x1c0ee5be, 0x1c0edc32, 0x1c0ed2ac, 0x1c0ec92c,
345 0x1c0ebfb2, 0x1c0eb63e, 0x1c0eacd1, 0x1c0ea36a, 0x1c0e9a09, 0x1c0e90ae,
346 0x1c0e8759, 0x1c0e7e0a, 0x1c0e74c1, 0x1c0e6b7d, 0x1c0e623f, 0x1c0e5907,
347 0x1c0e4fd5, 0x1c0e46a9, 0x1c0e3d83, 0x1c0e3463, 0x1c0e2b49, 0x1c0e2235,
348 0x1c0e1926, 0x1c0e101d, 0x1c0e071a, 0x1c0dfe1d, 0x1c0df526, 0x1c0dec34,
349 0x1c0de348, 0x1c0dda62, 0x1c0dd181, 0x1c0dc8a6, 0x1c0dbfd1, 0x1c0db701,
350 0x1c0dae37, 0x1c0da573, 0x1c0d9cb4, 0x1c0d93fb, 0x1c0d8b47, 0x1c0d8299,
351 0x1c0d79f1, 0x1c0d714e, 0x1c0d68b1, 0x1c0d6019, 0x1c0d5787, 0x1c0d4efa,
352 0x1c0d4673, 0x1c0d3df1, 0x1c0d3575, 0x1c0d2cfe, 0x1c0d248c, 0x1c0d1c20,
353 0x1c0d13b9, 0x1c0d0b58, 0x1c0d02fc, 0x1c0cfaa5, 0x1c0cf254, 0x1c0cea08,
354 0x1c0ce1c1, 0x1c0cd980, 0x1c0cd144, 0x1c0cc90d, 0x1c0cc0dc, 0x1c0cb8b0,
355 0x1c0cb089, 0x1c0ca867, 0x1c0ca04a, 0x1c0c9833, 0x1c0c9021, 0x1c0c8814,
356 0x1c0c800c, 0x1c0c7809, 0x1c0c700b, 0x1c0c6813, 0x1c0c6020, 0x1c0c5832,
357 0x1c0c5049, 0x1c0c4865, 0x1c0c4086, 0x1c0c38ac, 0x1c0c30d7, 0x1c0c2907,
358 0x1c0c213c, 0x1c0c1976, 0x1c0c11b5, 0x1c0c09f9, 0x1c0c0242, 0x1c0bfa90,
359 0x1c0bf2e3, 0x1c0beb3b, 0x1c0be398, 0x1c0bdbfa, 0x1c0bd460, 0x1c0bcccb,
360 0x1c0bc53b, 0x1c0bbdb0, 0x1c0bb62a, 0x1c0baea9, 0x1c0ba72c, 0x1c0b9fb4,
361 0x1c0b9841, 0x1c0b90d3, 0x1c0b896a, 0x1c0b8205, 0x1c0b7aa5, 0x1c0b734a,
362 0x1c0b6bf3, 0x1c0b64a1, 0x1c0b5d54, 0x1c0b560c, 0x1c0b4ec8, 0x1c0b4789,
363 0x1c0b404f, 0x1c0b3919, 0x1c0b31e8, 0x1c0b2abb, 0x1c0b2393, 0x1c0b1c70,
364 0x1c0b1551, 0x1c0b0e37, 0x1c0b0721, 0x1c0b0010, 0x1c0af903, 0x1c0af1fb,
365 0x1c0aeaf7, 0x1c0ae3f8, 0x1c0adcfd, 0x1c0ad607, 0x1c0acf15, 0x1c0ac828,
366 0x1c0ac13f, 0x1c0aba5b, 0x1c0ab37b, 0x1c0aac9f, 0x1c0aa5c8, 0x1c0a9ef5,
367 0x1c0a9827, 0x1c0a915d, 0x1c0a8a97, 0x1c0a83d6, 0x1c0a7d19, 0x1c0a7660,
368 0x1c0a6fac, 0x1c0a68fc, 0x1c0a6250, 0x1c0a5ba8, 0x1c0a5505, 0x1c0a4e66,
369 0x1c0a47cb, 0x1c0a4134, 0x1c0a3aa2, 0x1c0a3414, 0x1c0a2d8a, 0x1c0a2704,
370 0x1c0a2082, 0x1c0a1a05, 0x1c0a138c, 0x1c0a0d17, 0x1c0a06a6, 0x1c0a0039,
371 0x1c09f9d0, 0x1c09f36b, 0x1c09ed0a, 0x1c09e6ae, 0x1c09e056, 0x1c09da02,
372 0x1c09d3b2, 0x1c09cd66, 0x1c09c71e, 0x1c09c0da, 0x1c09ba9a, 0x1c09b45e,
373 0x1c09ae26, 0x1c09a7f2, 0x1c09a1c2, 0x1c099b96, 0x1c09956e, 0x1c098f4a,
374 0x1c09892a, 0x1c09830d, 0x1c097cf4, 0x1c0976df, 0x1c0970ce, 0x1c096ac1,
375 0x1c0964b8, 0x1c095eb3, 0x1c0958b2, 0x1c0952b5, 0x1c094cbb, 0x1c0946c5,
376 0x1c0940d3, 0x1c093ae5, 0x1c0934fb, 0x1c092f14, 0x1c092931, 0x1c092352,
377 0x1c091d77, 0x1c09179f, 0x1c0911cb, 0x1c090bfb, 0x1c09062f, 0x1c090066,
378 0x1c08faa1, 0x1c08f4e0, 0x1c08ef22, 0x1c08e968, 0x1c08e3b2, 0x1c08de00,
379 0x1c08d851, 0x1c08d2a6, 0x1c08ccfe, 0x1c08c75a, 0x1c08c1ba, 0x1c08bc1d,
380 0x1c08b684, 0x1c08b0ee, 0x1c08ab5c, 0x1c08a5ce, 0x1c08a043, 0x1c089abc,
381 0x1c089538, 0x1c088fb8, 0x1c088a3b, 0x1c0884c2, 0x1c087f4c, 0x1c0879da,
382 };
383
384 for (uint32_t expected : diffs) {
385 blocks[h] = GetBlockIndex(&blocks[h - 1], 100, nBits);
387 *chainParams);
388 BOOST_CHECK_EQUAL(nBits, expected);
389 h++;
390 }
391
392 // Mine slow blocks to lower and saturate the diffculty.
393 diffs = {
394 0x1c08a0b5, 0x1c08c842, 0x1c08f084, 0x1c09197f, 0x1c094336, 0x1c096dac,
395 0x1c0998e4, 0x1c09c4e3, 0x1c09f1ab, 0x1c0a1f41, 0x1c0a4da8, 0x1c0a7ce3,
396 0x1c0aacf7, 0x1c0adde7, 0x1c0b0fb8, 0x1c0b426d, 0x1c0b760a, 0x1c0baa94,
397 0x1c0be00f, 0x1c0c167f, 0x1c0c4de9, 0x1c0c8651, 0x1c0cbfbb, 0x1c0cfa2c,
398 0x1c0d35a9, 0x1c0d7237, 0x1c0dafdb, 0x1c0dee99, 0x1c0e2e77, 0x1c0e6f7a,
399 0x1c0eb1a7, 0x1c0ef503, 0x1c0f3994, 0x1c0f7f5f, 0x1c0fc66a, 0x1c100ebb,
400 0x1c105857, 0x1c10a345, 0x1c10ef8a, 0x1c113d2d, 0x1c118c34, 0x1c11dca5,
401 0x1c122e87, 0x1c1281e0, 0x1c12d6b7, 0x1c132d13, 0x1c1384fb, 0x1c13de76,
402 0x1c14398b, 0x1c149642, 0x1c14f4a2, 0x1c1554b3, 0x1c15b67c, 0x1c161a05,
403 0x1c167f57, 0x1c16e679, 0x1c174f74, 0x1c17ba50, 0x1c182716, 0x1c1895cf,
404 0x1c190683, 0x1c19793c, 0x1c19ee03, 0x1c1a64e1, 0x1c1adde0, 0x1c1b590a,
405 0x1c1bd668, 0x1c1c5605, 0x1c1cd7eb, 0x1c1d5c25, 0x1c1de2bd, 0x1c1e6bbe,
406 0x1c1ef733, 0x1c1f8527, 0x1c2015a6, 0x1c20a8bb, 0x1c213e73, 0x1c21d6d9,
407 0x1c2271fa, 0x1c230fe2, 0x1c23b09e, 0x1c24543a, 0x1c24fac5, 0x1c25a44b,
408 0x1c2650da, 0x1c270080, 0x1c27b34b, 0x1c28694a, 0x1c29228b, 0x1c29df1e,
409 0x1c2a9f11, 0x1c2b6274, 0x1c2c2957, 0x1c2cf3ca, 0x1c2dc1dd, 0x1c2e93a0,
410 0x1c2f6925, 0x1c30427d, 0x1c311fb9, 0x1c3200eb, 0x1c32e626, 0x1c33cf7c,
411 0x1c34bcff, 0x1c35aec3, 0x1c36a4db, 0x1c379f5c, 0x1c389e59, 0x1c39a1e7,
412 0x1c3aaa1b, 0x1c3bb70a, 0x1c3cc8ca, 0x1c3ddf71, 0x1c3efb15, 0x1c401bcd,
413 0x1c4141b1, 0x1c426cd8, 0x1c439d5b, 0x1c44d352, 0x1c460ed6, 0x1c475000,
414 0x1c4896ea, 0x1c49e3af, 0x1c4b366a, 0x1c4c8f35, 0x1c4dee2d, 0x1c4f536e,
415 0x1c50bf15, 0x1c52313f, 0x1c53aa0a, 0x1c552994, 0x1c56affc, 0x1c583d62,
416 0x1c59d1e6, 0x1c5b6da8, 0x1c5d10ca, 0x1c5ebb6d, 0x1c606db4, 0x1c6227c2,
417 0x1c63e9bb, 0x1c65b3c2, 0x1c6785fd, 0x1c696091, 0x1c6b43a5, 0x1c6d2f60,
418 0x1c6f23e9, 0x1c712169, 0x1c732808, 0x1c7537f1, 0x1c77514e, 0x1c79744a,
419 0x1c7ba112, 0x1c7dd7d2, 0x1d008018, 0x1d008263, 0x1d0084b8, 0x1d008718,
420 0x1d008983, 0x1d008bf9, 0x1d008e7a, 0x1d009107, 0x1d00939f, 0x1d009643,
421 0x1d0098f3, 0x1d009bb0, 0x1d009e79, 0x1d00a14f, 0x1d00a432, 0x1d00a722,
422 0x1d00aa20, 0x1d00ad2b, 0x1d00b044, 0x1d00b36c, 0x1d00b6a2, 0x1d00b9e7,
423 0x1d00bd3b, 0x1d00c09e, 0x1d00c411, 0x1d00c793, 0x1d00cb25, 0x1d00cec8,
424 0x1d00d27b, 0x1d00d63f, 0x1d00da15, 0x1d00ddfc, 0x1d00e1f5, 0x1d00e600,
425 0x1d00ea1e, 0x1d00ee4f, 0x1d00f293, 0x1d00f6eb, 0x1d00fb56, 0x1d00ffd6,
426 0x1d00ffff, 0x1d00ffff, 0x1d00ffff, 0x1d00ffff, 0x1d00ffff, 0x1d00ffff,
427 0x1d00ffff, 0x1d00ffff,
428 };
429
430 for (uint32_t expected : diffs) {
431 blocks[h] = GetBlockIndex(&blocks[h - 1], 3600, nBits);
433 *chainParams);
434 BOOST_CHECK_EQUAL(nBits, expected);
435 h++;
436 }
437
438 // We floored the difficulty.
440
441 // Check for 0 solve time.
442 diffs = {
443 0x1d00ff35, 0x1d00fe6b, 0x1d00fda2, 0x1d00fcda, 0x1d00fc12, 0x1d00fb4b,
444 0x1d00fa84, 0x1d00f9be, 0x1d00f8f9, 0x1d00f834, 0x1d00f770, 0x1d00f6ac,
445 0x1d00f5e9, 0x1d00f527, 0x1d00f465, 0x1d00f3a4, 0x1d00f2e3, 0x1d00f223,
446 0x1d00f164, 0x1d00f0a5, 0x1d00efe7, 0x1d00ef29, 0x1d00ee6c, 0x1d00edb0,
447 0x1d00ecf4, 0x1d00ec39, 0x1d00eb7e, 0x1d00eac4, 0x1d00ea0a, 0x1d00e951,
448 0x1d00e899, 0x1d00e7e1, 0x1d00e72a, 0x1d00e673, 0x1d00e5bd, 0x1d00e507,
449 0x1d00e452, 0x1d00e39d, 0x1d00e2e9, 0x1d00e236, 0x1d00e183, 0x1d00e0d1,
450 0x1d00e01f, 0x1d00df6e, 0x1d00debd, 0x1d00de0d, 0x1d00dd5d, 0x1d00dcae,
451 0x1d00dc00, 0x1d00db52, 0x1d00daa5, 0x1d00d9f8, 0x1d00d94c, 0x1d00d8a0,
452 0x1d00d7f5, 0x1d00d74a, 0x1d00d6a0, 0x1d00d5f6, 0x1d00d54d, 0x1d00d4a4,
453 0x1d00d3fc, 0x1d00d354, 0x1d00d2ad, 0x1d00d206, 0x1d00d160, 0x1d00d0ba,
454 0x1d00d015, 0x1d00cf70, 0x1d00cecc, 0x1d00ce28, 0x1d00cd85, 0x1d00cce2,
455 0x1d00cc40, 0x1d00cb9e, 0x1d00cafd, 0x1d00ca5c, 0x1d00c9bc, 0x1d00c91c,
456 0x1d00c87d, 0x1d00c7de, 0x1d00c740, 0x1d00c6a2, 0x1d00c605, 0x1d00c568,
457 0x1d00c4cc, 0x1d00c430, 0x1d00c395, 0x1d00c2fa, 0x1d00c260, 0x1d00c1c6,
458 0x1d00c12d, 0x1d00c094, 0x1d00bffc, 0x1d00bf64, 0x1d00becd, 0x1d00be36,
459 0x1d00bda0, 0x1d00bd0a, 0x1d00bc74, 0x1d00bbdf, 0x1d00bb4a, 0x1d00bab6,
460 0x1d00ba22, 0x1d00b98f, 0x1d00b8fc, 0x1d00b86a, 0x1d00b7d8, 0x1d00b747,
461 0x1d00b6b6, 0x1d00b625, 0x1d00b595, 0x1d00b505, 0x1d00b476, 0x1d00b3e7,
462 0x1d00b359, 0x1d00b2cb, 0x1d00b23e, 0x1d00b1b1, 0x1d00b124, 0x1d00b098,
463 0x1d00b00c, 0x1d00af81, 0x1d00aef6, 0x1d00ae6c, 0x1d00ade2, 0x1d00ad58,
464 0x1d00accf, 0x1d00ac46, 0x1d00abbe, 0x1d00ab36, 0x1d00aaaf, 0x1d00aa28,
465 0x1d00a9a1, 0x1d00a91b, 0x1d00a895, 0x1d00a810, 0x1d00a78b, 0x1d00a706,
466 0x1d00a682, 0x1d00a5fe, 0x1d00a57b, 0x1d00a4f8, 0x1d00a475, 0x1d00a3f3,
467 0x1d00a371, 0x1d00a2f0, 0x1d00a26f, 0x1d00a1ee, 0x1d00a16e, 0x1d00a0ee,
468 0x1d00a06f, 0x1d009ff0, 0x1d009f71, 0x1d009ef3, 0x1d009e75, 0x1d009df8,
469 0x1d009d7b, 0x1d009cfe, 0x1d009c82, 0x1d009c06, 0x1d009b8a, 0x1d009b0f,
470 0x1d009a94, 0x1d009a1a, 0x1d0099a0, 0x1d009926, 0x1d0098ad, 0x1d009834,
471 0x1d0097bc, 0x1d009744, 0x1d0096cc, 0x1d009655, 0x1d0095de, 0x1d009567,
472 0x1d0094f1, 0x1d00947b, 0x1d009405, 0x1d009390, 0x1d00931b, 0x1d0092a7,
473 0x1d009233, 0x1d0091bf, 0x1d00914c, 0x1d0090d9, 0x1d009066, 0x1d008ff4,
474 0x1d008f82, 0x1d008f10, 0x1d008e9f, 0x1d008e2e, 0x1d008dbd, 0x1d008d4d,
475 0x1d008cdd, 0x1d008c6d, 0x1d008bfe, 0x1d008b8f, 0x1d008b20, 0x1d008ab2,
476 0x1d008a44, 0x1d0089d6, 0x1d008969, 0x1d0088fc, 0x1d00888f, 0x1d008823,
477 0x1d0087b7, 0x1d00874b, 0x1d0086e0, 0x1d008675, 0x1d00860a, 0x1d0085a0,
478 0x1d008536, 0x1d0084cc, 0x1d008463, 0x1d0083fa, 0x1d008391, 0x1d008329,
479 0x1d0082c1, 0x1d008259, 0x1d0081f2, 0x1d00818b, 0x1d008124, 0x1d0080be,
480 0x1d008058, 0x1c7ff2cf, 0x1c7f8dee, 0x1c7f295d, 0x1c7ec51b, 0x1c7e6128,
481 0x1c7dfd84, 0x1c7d9a2e, 0x1c7d3727, 0x1c7cd46e, 0x1c7c7202, 0x1c7c0fe4,
482 0x1c7bae13, 0x1c7b4c90, 0x1c7aeb59, 0x1c7a8a6f, 0x1c7a29d1, 0x1c79c980,
483 0x1c79697b, 0x1c7909c1, 0x1c78aa53, 0x1c784b30, 0x1c77ec58, 0x1c778dcb,
484 0x1c772f88, 0x1c76d190, 0x1c7673e2, 0x1c76167e, 0x1c75b963, 0x1c755c92,
485 0x1c75000a, 0x1c74a3cb, 0x1c7447d4, 0x1c73ec26, 0x1c7390c0, 0x1c7335a2,
486 0x1c72dacc, 0x1c72803e, 0x1c7225f7, 0x1c71cbf7, 0x1c71723e, 0x1c7118cc,
487 0x1c70bfa1, 0x1c7066bc, 0x1c700e1d, 0x1c6fb5c4, 0x1c6f5db1, 0x1c6f05e3,
488 0x1c6eae5a, 0x1c6e5716, 0x1c6e0017, 0x1c6da95d, 0x1c6d52e7, 0x1c6cfcb5,
489 0x1c6ca6c7, 0x1c6c511d, 0x1c6bfbb6, 0x1c6ba693, 0x1c6b51b3, 0x1c6afd16,
490 0x1c6aa8bb, 0x1c6a54a3, 0x1c6a00cd, 0x1c69ad39, 0x1c6959e7, 0x1c6906d7,
491 0x1c68b408, 0x1c68617b, 0x1c680f2f, 0x1c67bd24, 0x1c676b59, 0x1c6719cf,
492 0x1c66c885, 0x1c66777b, 0x1c6626b1, 0x1c65d627, 0x1c6585dc, 0x1c6535d1,
493 };
494
495 for (uint32_t expected : diffs) {
496 blocks[h] = GetBlockIndex(&blocks[h - 1], 0, nBits);
498 *chainParams);
499 BOOST_CHECK_EQUAL(nBits, expected);
500 h++;
501 }
502
503 // Check for negative solve time.
504 diffs = {
505 0x1c64d713, 0x1c6478ae, 0x1c641aa1, 0x1c63bcec, 0x1c635f8f, 0x1c630289,
506 0x1c62a5da, 0x1c624982, 0x1c61ed81, 0x1c6191d6, 0x1c613681, 0x1c60db81,
507 0x1c6080d6, 0x1c602680, 0x1c5fcc7f, 0x1c5f72d2, 0x1c5f1979, 0x1c5ec073,
508 0x1c5e67c1, 0x1c5e0f62, 0x1c5db756, 0x1c5d5f9c, 0x1c5d0834, 0x1c5cb11e,
509 0x1c5c5a59, 0x1c5c03e6, 0x1c5badc4, 0x1c5b57f2, 0x1c5b0271, 0x1c5aad40,
510 0x1c5a585e, 0x1c5a03cc, 0x1c59af89, 0x1c595b95, 0x1c5907ef, 0x1c58b498,
511 0x1c58618f, 0x1c580ed3, 0x1c57bc65, 0x1c576a44, 0x1c571870, 0x1c56c6e9,
512 0x1c5675ae, 0x1c5624bf, 0x1c55d41c, 0x1c5583c4, 0x1c5533b7, 0x1c54e3f5,
513 0x1c54947e, 0x1c544551, 0x1c53f66e, 0x1c53a7d5, 0x1c535986, 0x1c530b80,
514 0x1c52bdc3, 0x1c52704f, 0x1c522323, 0x1c51d640, 0x1c5189a5, 0x1c513d51,
515 0x1c50f145, 0x1c50a580, 0x1c505a02, 0x1c500ecb, 0x1c4fc3da, 0x1c4f792f,
516 0x1c4f2eca, 0x1c4ee4ab, 0x1c4e9ad1, 0x1c4e513c, 0x1c4e07ec, 0x1c4dbee1,
517 0x1c4d761a, 0x1c4d2d97, 0x1c4ce558, 0x1c4c9d5d, 0x1c4c55a5, 0x1c4c0e30,
518 0x1c4bc6fe, 0x1c4b800f, 0x1c4b3962, 0x1c4af2f7, 0x1c4aacce, 0x1c4a66e7,
519 0x1c4a2141, 0x1c49dbdc, 0x1c4996b8, 0x1c4951d5, 0x1c490d33, 0x1c48c8d1,
520 0x1c4884af, 0x1c4840cd, 0x1c47fd2a, 0x1c47b9c7, 0x1c4776a3, 0x1c4733bd,
521 0x1c46f116, 0x1c46aead, 0x1c466c83, 0x1c462a97, 0x1c45e8e8, 0x1c45a777,
522 0x1c456643, 0x1c45254c, 0x1c44e492, 0x1c44a414, 0x1c4463d3, 0x1c4423ce,
523 0x1c43e405, 0x1c43a478, 0x1c436526, 0x1c43260f, 0x1c42e733, 0x1c42a892,
524 0x1c426a2c, 0x1c422c00, 0x1c41ee0f, 0x1c41b057, 0x1c4172d9, 0x1c413595,
525 0x1c40f88a, 0x1c40bbb8, 0x1c407f1f, 0x1c4042bf, 0x1c400697, 0x1c3fcaa8,
526 0x1c3f8ef1, 0x1c3f5372, 0x1c3f182a, 0x1c3edd1a, 0x1c3ea241, 0x1c3e679f,
527 0x1c3e2d34, 0x1c3df300, 0x1c3db902, 0x1c3d7f3b, 0x1c3d45aa, 0x1c3d0c4f,
528 0x1c3cd329, 0x1c3c9a39, 0x1c3c617e, 0x1c3c28f8, 0x1c3bf0a7, 0x1c3bb88b,
529 0x1c3b80a3, 0x1c3b48f0, 0x1c3b1171, 0x1c3ada26, 0x1c3aa30f, 0x1c3a6c2b,
530 0x1c3a357b, 0x1c39fefe, 0x1c39c8b4, 0x1c39929d, 0x1c395cb8, 0x1c392706,
531 0x1c38f186, 0x1c38bc38, 0x1c38871c, 0x1c385232, 0x1c381d79, 0x1c37e8f1,
532 0x1c37b49b, 0x1c378076, 0x1c374c81, 0x1c3718bd, 0x1c36e52a, 0x1c36b1c7,
533 0x1c367e94, 0x1c364b91, 0x1c3618be, 0x1c35e61a, 0x1c35b3a6, 0x1c358161,
534 0x1c354f4b, 0x1c351d64, 0x1c34ebab, 0x1c34ba21, 0x1c3488c5, 0x1c345798,
535 0x1c342699, 0x1c33f5c7, 0x1c33c523, 0x1c3394ad, 0x1c336464, 0x1c333448,
536 0x1c330459, 0x1c32d497, 0x1c32a502, 0x1c327599, 0x1c32465d, 0x1c32174d,
537 0x1c31e869, 0x1c31b9b1, 0x1c318b25, 0x1c315cc4, 0x1c312e8f, 0x1c310085,
538 0x1c30d2a6, 0x1c30a4f2, 0x1c307769, 0x1c304a0a, 0x1c301cd6, 0x1c2fefcc,
539 0x1c2fc2ec, 0x1c2f9636, 0x1c2f69aa, 0x1c2f3d48, 0x1c2f110f, 0x1c2ee500,
540 0x1c2eb91a, 0x1c2e8d5d, 0x1c2e61c9, 0x1c2e365e, 0x1c2e0b1b, 0x1c2de001,
541 0x1c2db50f, 0x1c2d8a46, 0x1c2d5fa5, 0x1c2d352b, 0x1c2d0ad9, 0x1c2ce0af,
542 0x1c2cb6ac, 0x1c2c8cd1, 0x1c2c631d, 0x1c2c3990, 0x1c2c102a, 0x1c2be6ea,
543 0x1c2bbdd1, 0x1c2b94df, 0x1c2b6c13, 0x1c2b436d, 0x1c2b1aed, 0x1c2af293,
544 0x1c2aca5f, 0x1c2aa250, 0x1c2a7a67, 0x1c2a52a3, 0x1c2a2b05, 0x1c2a038c,
545 0x1c29dc38, 0x1c29b508, 0x1c298dfd, 0x1c296717, 0x1c294055, 0x1c2919b7,
546 0x1c28f33d, 0x1c28cce7, 0x1c28a6b5, 0x1c2880a7, 0x1c285abd, 0x1c2834f6,
547 0x1c280f53, 0x1c27e9d3, 0x1c27c476, 0x1c279f3c, 0x1c277a25, 0x1c275530,
548 0x1c27305e, 0x1c270baf, 0x1c26e722, 0x1c26c2b7, 0x1c269e6e, 0x1c267a47,
549 0x1c265642, 0x1c26325f, 0x1c260e9d, 0x1c25eafd, 0x1c25c77e, 0x1c25a420,
550 0x1c2580e3, 0x1c255dc7, 0x1c253acc, 0x1c2517f2, 0x1c24f539, 0x1c24d2a0,
551 0x1c24b028, 0x1c248dd0, 0x1c246b98, 0x1c244980, 0x1c242788, 0x1c2405b0,
552 0x1c23e3f7, 0x1c23c25e, 0x1c23a0e4, 0x1c237f8a, 0x1c235e4f, 0x1c233d33,
553 0x1c231c36, 0x1c22fb58, 0x1c22da99, 0x1c22b9f8, 0x1c229976, 0x1c227912,
554 0x1c2258cd, 0x1c2238a6, 0x1c22189d, 0x1c21f8b2, 0x1c21d8e5, 0x1c21b936,
555 };
556
557 for (uint32_t expected : diffs) {
558 blocks[h] = GetBlockIndex(&blocks[h - 1], -100, nBits);
560 *chainParams);
561 BOOST_CHECK_EQUAL(nBits, expected);
562 h++;
563 }
564
565 // Check for absurd solve time.
566 blocks[h] = GetBlockIndex(&blocks[h - 1], -3900000, nBits);
567 nBits = GetNextGrasbergWorkRequired(&blocks[h++], &blkHeaderDummy,
568 *chainParams);
569 BOOST_CHECK_EQUAL(nBits, 0x1821b936);
570
571 blocks[h] = GetBlockIndex(&blocks[h - 1], -5000000, nBits);
572 nBits = GetNextGrasbergWorkRequired(&blocks[h++], &blkHeaderDummy,
573 *chainParams);
574 BOOST_CHECK_EQUAL(nBits, 0x1421b936);
575
576 blocks[h] = GetBlockIndex(&blocks[h - 1], 3900000, nBits);
577 nBits = GetNextGrasbergWorkRequired(&blocks[h++], &blkHeaderDummy,
578 *chainParams);
579 BOOST_CHECK_EQUAL(nBits, 0x1821b936);
580
581 blocks[h] = GetBlockIndex(&blocks[h - 1], 5000000, nBits);
582 nBits = GetNextGrasbergWorkRequired(&blocks[h++], &blkHeaderDummy,
583 *chainParams);
584 BOOST_CHECK_EQUAL(nBits, 0x1c21b936);
585
586 blocks[h] = GetBlockIndex(&blocks[h - 1], 9000000, nBits);
587 nBits = GetNextGrasbergWorkRequired(&blocks[h++], &blkHeaderDummy,
588 *chainParams);
589 BOOST_CHECK_EQUAL(nBits, 0x1d00ffff);
590}
591
593 const auto chainParams =
595 const auto &params = chainParams->GetConsensus();
596
597 std::vector<CBlockIndex> blocks(3000);
598
599 const arith_uint256 powLimit = UintToArith256(params.powLimit);
600 uint32_t powLimitBits = powLimit.GetCompact();
601 arith_uint256 currentPow = powLimit >> 4;
603
604 // Genesis block.
605 blocks[0] = CBlockIndex();
606 blocks[0].nHeight = 0;
607 blocks[0].nTime = 1269211443;
608 blocks[0].nBits = initialBits;
609
610 blocks[0].nChainWork = GetBlockProof(blocks[0]);
611
612 // Check that we can use Grasberg directly from genesis.
614 uint32_t nBits =
615 GetNextGrasbergWorkRequired(&blocks[0], &blkHeaderDummy, *chainParams);
616 BOOST_CHECK_EQUAL(nBits, 0x1c0ffe3e);
617
618 // Up to 20 mins, difficulty is unchanged.
619 blkHeaderDummy.nTime = blocks[0].nTime + 2 * params.nPowTargetSpacing;
620 nBits =
621 GetNextGrasbergWorkRequired(&blocks[0], &blkHeaderDummy, *chainParams);
622 BOOST_CHECK_EQUAL(nBits, 0x1c0ffe3e);
623
624 // After 20 mins, difficulty drops.
625 blkHeaderDummy.nTime++;
626 nBits =
627 GetNextGrasbergWorkRequired(&blocks[0], &blkHeaderDummy, *chainParams);
629
630 // Mine several blocks and check the difficulty.
631 size_t h = 1;
632
633 std::vector<uint32_t> diffs = {
634 0x1c100c81, 0x1c101ad2, 0x1c10292f, 0x1c103799, 0x1c104610,
635 0x1c105494, 0x1c106325, 0x1c1071c2, 0x1c10806d, 0x1c108f25,
636 };
637
638 // Mine several blocks at minimal difficulty. Block is skipped to compute
639 // the next difficulty.
640 for (uint32_t expected : diffs) {
641 blocks[h] = GetBlockIndex(
642 &blocks[h - 1], 2 * params.nPowTargetSpacing + 1, powLimitBits);
643 nBits = GetNextGrasbergWorkRequired(&blocks[h++], &blkHeaderDummy,
644 *chainParams);
645 BOOST_CHECK_EQUAL(nBits, expected);
646 }
647
648 // Mine one block at regular difficulty, it will now be the new reference
649 // when skipping over low difficulty blocks.
650 blocks[h] = GetBlockIndex(&blocks[h - 1], 600, nBits);
651 nBits = GetNextGrasbergWorkRequired(&blocks[h++], &blkHeaderDummy,
652 *chainParams);
653 BOOST_CHECK_EQUAL(nBits, 0x1c108d54);
654
655 diffs = {
656 0x1c109c17, 0x1c10aae8, 0x1c10b9c6, 0x1c10c8b1, 0x1c10d7a9,
657 0x1c10e6af, 0x1c10f5c2, 0x1c1104e2, 0x1c111410, 0x1c11234c,
658 };
659
660 // As we mine more blocks with low difficulty, we use our new reference.
661 for (uint32_t expected : diffs) {
662 blocks[h] = GetBlockIndex(
663 &blocks[h - 1], 2 * params.nPowTargetSpacing + 1, powLimitBits);
664 nBits = GetNextGrasbergWorkRequired(&blocks[h++], &blkHeaderDummy,
665 *chainParams);
666 BOOST_CHECK_EQUAL(nBits, expected);
667 }
668}
669
arith_uint256 UintToArith256(const uint256 &a)
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition chain.cpp:74
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
static const std::string TESTNET
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition block.h:23
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition blockindex.h:25
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition blockindex.h:32
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition blockindex.h:51
uint32_t nTime
Definition blockindex.h:92
uint32_t nBits
Definition blockindex.h:93
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition blockindex.h:38
256-bit unsigned big integer.
uint32_t GetCompact(bool fNegative=false) const
static constexpr int64_t POW2_32
Definition grasberg.cpp:17
uint32_t GetNextGrasbergWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const CChainParams &chainParams)
Compute the next required proof of work using a relative target based ASERT algorithm.
Definition grasberg.cpp:71
static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval, uint32_t nBits)
BOOST_AUTO_TEST_CASE(exp2_test)
unsigned int nHeight
int64_t computeTargetBlockTime(const CBlockIndex *pindexPrev, const CChainParams &chainParams)
Compute the block time we are aiming for.
Definition grasberg.cpp:108
uint32_t deterministicExp2(const uint32_t n)
Computes exp2(n) = 2^32 * (2^(n/2^32) - 1)
Definition grasberg.cpp:140
NodeContext & m_node
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:18
#define BOOST_CHECK(expr)
Definition object.cpp:17
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85