-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathpow_tests.cpp
370 lines (302 loc) · 13.2 KB
/
pow_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (c) 2015 The Bitcoin Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "pow.h"
#include "chain.h"
#include "chainparams.h"
#include "random.h"
#include "test/test_bitcoin.h"
#include "util.h"
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
/* Test calculation of next difficulty target with no constraints applying */
BOOST_AUTO_TEST_CASE(get_next_work) {
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params ¶ms = Params().GetConsensus();
int64_t nLastRetargetTime = 1261130161; // Block #30240
CBlockIndex pindexLast;
pindexLast.nHeight = 32255;
pindexLast.nTime = 1262152739; // Block #32255
pindexLast.nBits = 0x1d00ffff;
BOOST_CHECK_EQUAL(
CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params),
0x1d00d86a);
}
/* Test the constraint on the upper bound for next work */
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) {
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params ¶ms = Params().GetConsensus();
int64_t nLastRetargetTime = 1231006505; // Block #0
CBlockIndex pindexLast;
pindexLast.nHeight = 2015;
pindexLast.nTime = 1233061996; // Block #2015
pindexLast.nBits = 0x1d00ffff;
BOOST_CHECK_EQUAL(
CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params),
0x1d00ffff);
}
/* Test the constraint on the lower bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) {
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params ¶ms = Params().GetConsensus();
int64_t nLastRetargetTime = 1279008237; // Block #66528
CBlockIndex pindexLast;
pindexLast.nHeight = 68543;
pindexLast.nTime = 1279297671; // Block #68543
pindexLast.nBits = 0x1c05a3f4;
BOOST_CHECK_EQUAL(
CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params),
0x1c0168fd);
}
/* Test the constraint on the upper bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) {
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params ¶ms = Params().GetConsensus();
int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
CBlockIndex pindexLast;
pindexLast.nHeight = 46367;
pindexLast.nTime = 1269211443; // Block #46367
pindexLast.nBits = 0x1c387f6f;
BOOST_CHECK_EQUAL(
CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params),
0x1d00e1fd);
}
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) {
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params ¶ms = Params().GetConsensus();
std::vector<CBlockIndex> blocks(10000);
for (int i = 0; i < 10000; i++) {
blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
blocks[i].nHeight = i;
blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
blocks[i].nChainWork =
i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i])
: arith_uint256(0);
}
for (int j = 0; j < 1000; j++) {
CBlockIndex *p1 = &blocks[GetRand(10000)];
CBlockIndex *p2 = &blocks[GetRand(10000)];
CBlockIndex *p3 = &blocks[GetRand(10000)];
int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, params);
BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
}
}
static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval,
uint32_t nBits) {
CBlockIndex block;
block.pprev = pindexPrev;
block.nHeight = pindexPrev->nHeight + 1;
block.nTime = pindexPrev->nTime + nTimeInterval;
block.nBits = nBits;
block.nChainWork = pindexPrev->nChainWork + GetBlockProof(block);
return block;
}
BOOST_AUTO_TEST_CASE(retargeting_test) {
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params ¶ms = Params().GetConsensus();
std::vector<CBlockIndex> blocks(115);
const arith_uint256 powLimit = UintToArith256(params.powLimit);
arith_uint256 currentPow = powLimit >> 1;
uint32_t initialBits = currentPow.GetCompact();
// Genesis block.
blocks[0] = CBlockIndex();
blocks[0].nHeight = 0;
blocks[0].nTime = 1269211443;
blocks[0].nBits = initialBits;
blocks[0].nChainWork = GetBlockProof(blocks[0]);
// Pile up some blocks.
for (size_t i = 1; i < 100; i++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], params.nPowTargetSpacing,
initialBits);
}
CBlockHeader blkHeaderDummy;
// We start getting 2h blocks time. For the first 5 blocks, it doesn't
// matter as the MTP is not affected. For the next 5 block, MTP difference
// increases but stays below 12h.
for (size_t i = 100; i < 110; i++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 2 * 3600, initialBits);
BOOST_CHECK_EQUAL(
GetNextWorkRequired(&blocks[i], &blkHeaderDummy, params),
initialBits);
}
// Now we expect the difficulty to decrease.
blocks[110] = GetBlockIndex(&blocks[109], 2 * 3600, initialBits);
currentPow.SetCompact(currentPow.GetCompact());
currentPow += (currentPow >> 2);
BOOST_CHECK_EQUAL(
GetNextWorkRequired(&blocks[110], &blkHeaderDummy, params),
currentPow.GetCompact());
// As we continue with 2h blocks, difficulty continue to decrease.
blocks[111] =
GetBlockIndex(&blocks[110], 2 * 3600, currentPow.GetCompact());
currentPow.SetCompact(currentPow.GetCompact());
currentPow += (currentPow >> 2);
BOOST_CHECK_EQUAL(
GetNextWorkRequired(&blocks[111], &blkHeaderDummy, params),
currentPow.GetCompact());
// We decrease again.
blocks[112] =
GetBlockIndex(&blocks[111], 2 * 3600, currentPow.GetCompact());
currentPow.SetCompact(currentPow.GetCompact());
currentPow += (currentPow >> 2);
BOOST_CHECK_EQUAL(
GetNextWorkRequired(&blocks[112], &blkHeaderDummy, params),
currentPow.GetCompact());
// We check that we do not go below the minimal difficulty.
blocks[113] =
GetBlockIndex(&blocks[112], 2 * 3600, currentPow.GetCompact());
currentPow.SetCompact(currentPow.GetCompact());
currentPow += (currentPow >> 2);
BOOST_CHECK(powLimit.GetCompact() != currentPow.GetCompact());
BOOST_CHECK_EQUAL(
GetNextWorkRequired(&blocks[113], &blkHeaderDummy, params),
powLimit.GetCompact());
// Once we reached the minimal difficulty, we stick with it.
blocks[114] = GetBlockIndex(&blocks[113], 2 * 3600, powLimit.GetCompact());
BOOST_CHECK(powLimit.GetCompact() != currentPow.GetCompact());
BOOST_CHECK_EQUAL(
GetNextWorkRequired(&blocks[114], &blkHeaderDummy, params),
powLimit.GetCompact());
}
BOOST_AUTO_TEST_CASE(cash_difficulty_test) {
SelectParams(CBaseChainParams::MAIN);
const Consensus::Params ¶ms = Params().GetConsensus();
std::vector<CBlockIndex> blocks(3000);
const arith_uint256 powLimit = UintToArith256(params.powLimit);
uint32_t powLimitBits = powLimit.GetCompact();
arith_uint256 currentPow = powLimit >> 4;
uint32_t initialBits = currentPow.GetCompact();
// Genesis block.
blocks[0] = CBlockIndex();
blocks[0].nHeight = 0;
blocks[0].nTime = 1269211443;
blocks[0].nBits = initialBits;
blocks[0].nChainWork = GetBlockProof(blocks[0]);
// Block counter.
size_t i;
// Pile up some blocks every 10 mins to establish some history.
for (i = 1; i < 2050; i++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 600, initialBits);
}
CBlockHeader blkHeaderDummy;
uint32_t nBits =
GetNextCashWorkRequired(&blocks[2049], &blkHeaderDummy, params);
// Difficulty stays the same as long as we produce a block every 10 mins.
for (size_t j = 0; j < 10; i++, j++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 600, nBits);
BOOST_CHECK_EQUAL(
GetNextCashWorkRequired(&blocks[i], &blkHeaderDummy, params),
nBits);
}
// Make sure we skip over blocks that are out of wack. To do so, we produce
// a block that is far in the future, and then produce a block with the
// expected timestamp.
blocks[i] = GetBlockIndex(&blocks[i - 1], 6000, nBits);
BOOST_CHECK_EQUAL(
GetNextCashWorkRequired(&blocks[i++], &blkHeaderDummy, params), nBits);
blocks[i] = GetBlockIndex(&blocks[i - 1], 2 * 600 - 6000, nBits);
BOOST_CHECK_EQUAL(
GetNextCashWorkRequired(&blocks[i++], &blkHeaderDummy, params), nBits);
// The system should continue unaffected by the block with a bogous
// timestamps.
for (size_t j = 0; j < 20; i++, j++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 600, nBits);
BOOST_CHECK_EQUAL(
GetNextCashWorkRequired(&blocks[i], &blkHeaderDummy, params),
nBits);
}
// We start emitting blocks slightly faster. The first block has no impact.
blocks[i] = GetBlockIndex(&blocks[i - 1], 550, nBits);
BOOST_CHECK_EQUAL(
GetNextCashWorkRequired(&blocks[i++], &blkHeaderDummy, params), nBits);
// Now we should see difficulty increase slowly.
for (size_t j = 0; j < 10; i++, j++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 550, nBits);
const uint32_t nextBits =
GetNextCashWorkRequired(&blocks[i], &blkHeaderDummy, params);
arith_uint256 currentTarget;
currentTarget.SetCompact(nBits);
arith_uint256 nextTarget;
nextTarget.SetCompact(nextBits);
// Make sure that difficulty increases very slowly.
BOOST_CHECK(nextTarget < currentTarget);
BOOST_CHECK((currentTarget - nextTarget) < (currentTarget >> 10));
nBits = nextBits;
}
// Check the actual value.
BOOST_CHECK_EQUAL(nBits, 0x1c0fe7b1);
// If we dramatically shorten block production, difficulty increases faster.
for (size_t j = 0; j < 20; i++, j++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 10, nBits);
const uint32_t nextBits =
GetNextCashWorkRequired(&blocks[i], &blkHeaderDummy, params);
arith_uint256 currentTarget;
currentTarget.SetCompact(nBits);
arith_uint256 nextTarget;
nextTarget.SetCompact(nextBits);
// Make sure that difficulty increases faster.
BOOST_CHECK(nextTarget < currentTarget);
BOOST_CHECK((currentTarget - nextTarget) < (currentTarget >> 4));
nBits = nextBits;
}
// Check the actual value.
BOOST_CHECK_EQUAL(nBits, 0x1c0db19f);
// We start to emit blocks significantly slower. The first block has no
// impact.
blocks[i] = GetBlockIndex(&blocks[i - 1], 6000, nBits);
nBits = GetNextCashWorkRequired(&blocks[i++], &blkHeaderDummy, params);
// Check the actual value.
BOOST_CHECK_EQUAL(nBits, 0x1c0d9222);
// If we dramatically slow down block production, difficulty decreases.
for (size_t j = 0; j < 93; i++, j++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 6000, nBits);
const uint32_t nextBits =
GetNextCashWorkRequired(&blocks[i], &blkHeaderDummy, params);
arith_uint256 currentTarget;
currentTarget.SetCompact(nBits);
arith_uint256 nextTarget;
nextTarget.SetCompact(nextBits);
// Check the difficulty decreases.
BOOST_CHECK(nextTarget <= powLimit);
BOOST_CHECK(nextTarget > currentTarget);
BOOST_CHECK((nextTarget - currentTarget) < (currentTarget >> 3));
nBits = nextBits;
}
// Check the actual value.
BOOST_CHECK_EQUAL(nBits, 0x1c2f13b9);
// Due to the window of time being bounded, next block's difficulty actually
// gets harder.
blocks[i] = GetBlockIndex(&blocks[i - 1], 6000, nBits);
nBits = GetNextCashWorkRequired(&blocks[i++], &blkHeaderDummy, params);
BOOST_CHECK_EQUAL(nBits, 0x1c2ee9bf);
// And goes down again. It takes a while due to the window being bounded and
// the skewed block causes 2 blocks to get out of the window.
for (size_t j = 0; j < 192; i++, j++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 6000, nBits);
const uint32_t nextBits =
GetNextCashWorkRequired(&blocks[i], &blkHeaderDummy, params);
arith_uint256 currentTarget;
currentTarget.SetCompact(nBits);
arith_uint256 nextTarget;
nextTarget.SetCompact(nextBits);
// Check the difficulty decreases.
BOOST_CHECK(nextTarget <= powLimit);
BOOST_CHECK(nextTarget > currentTarget);
BOOST_CHECK((nextTarget - currentTarget) < (currentTarget >> 3));
nBits = nextBits;
}
// Check the actual value.
BOOST_CHECK_EQUAL(nBits, 0x1d00ffff);
// Once the difficulty reached the minimum allowed level, it doesn't get any
// easier.
for (size_t j = 0; j < 5; i++, j++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 6000, nBits);
const uint32_t nextBits =
GetNextCashWorkRequired(&blocks[i], &blkHeaderDummy, params);
// Check the difficulty stays constant.
BOOST_CHECK_EQUAL(nextBits, powLimitBits);
nBits = nextBits;
}
}
BOOST_AUTO_TEST_SUITE_END()