Repository navigation
Expand file tree
/
Copy pathpow_tests.cpp
More file actions
297 lines (262 loc) · 12.9 KB
/
Copy pathpow_tests.cpp
File metadata and controls
297 lines (262 loc) · 12.9 KB
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
// Copyright (c) 2015-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chain.h>
#include <chainparams.h>
#include <pow.h>
#include <test/util/random.h>
#include <test/util/common.h>
#include <test/util/setup_common.h>
#include <util/chaintype.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)
{
const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
int64_t nLastRetargetTime = 1261130161; // Block #30240
CBlockIndex pindexLast;
pindexLast.nHeight = 32255;
pindexLast.nTime = 1262152739; // Block #32255
pindexLast.nBits = 0x1d00ffff;
// Here (and below): expected_nbits is calculated in
// CalculateNextWorkRequired(); redoing the calculation here would be just
// reimplementing the same code that is written in pow.cpp. Rather than
// copy that code, we just hardcode the expected result.
unsigned int expected_nbits = 0x1d00d86aU;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
}
/* Test the constraint on the upper bound for next work */
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
{
const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
int64_t nLastRetargetTime = 1231006505; // Block #0
CBlockIndex pindexLast;
pindexLast.nHeight = 2015;
pindexLast.nTime = 1233061996; // Block #2015
pindexLast.nBits = 0x1d00ffff;
unsigned int expected_nbits = 0x1d00ffffU;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
}
/* Test the constraint on the lower bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
{
const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
int64_t nLastRetargetTime = 1279008237; // Block #66528
CBlockIndex pindexLast;
pindexLast.nHeight = 68543;
pindexLast.nTime = 1279297671; // Block #68543
pindexLast.nBits = 0x1c05a3f4;
unsigned int expected_nbits = 0x1c0168fdU;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
// Test that reducing nbits further would not be a PermittedDifficultyTransition.
unsigned int invalid_nbits = expected_nbits-1;
BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
}
/* Test that the largest permitted difficulty increase is not itself rejected
* when the new target is not exactly representable in compact form. */
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_rounding)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
CBlockIndex pindexLast;
pindexLast.nHeight = 68543;
pindexLast.nTime = 1279297671;
pindexLast.nBits = 0x1c05a3f5; // Mantissa is not a multiple of four
int64_t nLastRetargetTime = pindexLast.nTime - consensus.nPowTargetTimespan/4;
unsigned int expected_nbits = 0x1c0168fdU;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, consensus), expected_nbits);
BOOST_CHECK(PermittedDifficultyTransition(consensus, pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
}
/* Test the lower bound for a negative actual time taken, which is possible
* because timestamps only have to exceed the median of the previous eleven. */
BOOST_AUTO_TEST_CASE(get_next_work_negative_actual)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
int64_t nLastRetargetTime = 1279297671; // Later than pindexLast.nTime
CBlockIndex pindexLast;
pindexLast.nHeight = 68543;
pindexLast.nTime = 1279008237;
pindexLast.nBits = 0x1c05a3f4;
unsigned int expected_nbits = 0x1c0168fdU;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, consensus), expected_nbits);
BOOST_CHECK(PermittedDifficultyTransition(consensus, pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
}
/* Test the constraint on the upper bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
{
const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
CBlockIndex pindexLast;
pindexLast.nHeight = 46367;
pindexLast.nTime = 1269211443; // Block #46367
pindexLast.nBits = 0x1c387f6f;
unsigned int expected_nbits = 0x1d00e1fdU;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
// Test that increasing nbits further would not be a PermittedDifficultyTransition.
unsigned int invalid_nbits = expected_nbits+1;
BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
}
/* Test that the retarget window starts at the timestamp of the first block
* of the window, not its median time past. */
BOOST_AUTO_TEST_CASE(get_next_work_first_block_time)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
const int64_t interval{consensus.DifficultyAdjustmentInterval()};
// Two retarget windows, so that the first block of the second one has
// ancestors and hence a median time past earlier than its own time.
std::vector<CBlockIndex> blocks(2 * interval);
for (int64_t i = 0; i < 2 * interval; i++) {
blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
blocks[i].nHeight = i;
blocks[i].nTime = 1269211443 + i * consensus.nPowTargetSpacing;
blocks[i].nBits = 0x1c05a3f4;
}
const CBlockIndex& first{blocks[interval]};
const CBlockIndex& last{blocks[2 * interval - 1]};
BOOST_CHECK_NE(first.GetBlockTime(), first.GetMedianTimePast());
unsigned int expected_nbits = 0x1c05a33c;
BOOST_CHECK_EQUAL(GetNextWorkRequired(&last, nullptr, consensus), expected_nbits);
BOOST_CHECK(PermittedDifficultyTransition(consensus, last.nHeight+1, last.nBits, expected_nbits));
}
/* Test that nbits is only permitted to change at a difficulty adjustment. */
BOOST_AUTO_TEST_CASE(permitted_difficulty_transition_no_retarget)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
const int64_t height{consensus.DifficultyAdjustmentInterval() + 1}; // Not a retarget height
const unsigned int nbits{0x1c05a3f4};
BOOST_CHECK(PermittedDifficultyTransition(consensus, height, nbits, nbits));
BOOST_CHECK(!PermittedDifficultyTransition(consensus, height, nbits, nbits - 1));
BOOST_CHECK(!PermittedDifficultyTransition(consensus, height, nbits, nbits + 1));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
uint256 hash;
unsigned int nBits;
nBits = UintToArith256(consensus.powLimit).GetCompact(true);
hash = uint256{1};
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
uint256 hash;
unsigned int nBits{~0x00800000U};
hash = uint256{1};
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
uint256 hash;
unsigned int nBits;
arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
nBits_arith *= 2;
nBits = nBits_arith.GetCompact();
hash = uint256{1};
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
uint256 hash;
unsigned int nBits;
arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
nBits = hash_arith.GetCompact();
hash_arith *= 2; // hash > nBits
hash = ArithToUint256(hash_arith);
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
{
const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
uint256 hash;
unsigned int nBits;
arith_uint256 hash_arith{0};
nBits = hash_arith.GetCompact();
hash = ArithToUint256(hash_arith);
BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
}
BOOST_AUTO_TEST_CASE(GetBitsProof_test)
{
// Targets that are zero, negative or overflow encode no work.
BOOST_CHECK_EQUAL(GetBitsProof(0x00000000), arith_uint256{0});
BOOST_CHECK_EQUAL(GetBitsProof(0x01003456), arith_uint256{0});
BOOST_CHECK_EQUAL(GetBitsProof(0x04923456), arith_uint256{0});
BOOST_CHECK_EQUAL(GetBitsProof(0xff123456), arith_uint256{0});
// The exponent byte is unsigned, so this is a large exponent that overflows.
BOOST_CHECK_EQUAL(GetBitsProof(0x80010203), arith_uint256{0});
// The two overflows above shift every mantissa bit out of 256 bits, so they would
// also give zero work without the overflow check. This one keeps the low bits of
// its mantissa when truncated.
BOOST_CHECK_EQUAL(GetBitsProof(0x21012345), arith_uint256{0});
// The genesis block target.
BOOST_CHECK_EQUAL(GetBitsProof(0x1d00ffff), arith_uint256{0x100010001});
}
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
{
const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
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 * chainParams->GetConsensus().nPowTargetSpacing;
blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
}
for (int j = 0; j < 1000; j++) {
CBlockIndex *p1 = &blocks[m_rng.randrange(10000)];
CBlockIndex *p2 = &blocks[m_rng.randrange(10000)];
CBlockIndex *p3 = &blocks[m_rng.randrange(10000)];
int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
}
}
void sanity_check_chainparams(const ArgsManager& args, ChainType chain_type)
{
const auto chainParams = CreateChainParams(args, chain_type);
const auto consensus = chainParams->GetConsensus();
// hash genesis is correct
BOOST_CHECK_EQUAL(consensus.hashGenesisBlock, chainParams->GenesisBlock().GetHash());
// target timespan is an even multiple of spacing
BOOST_CHECK_EQUAL(consensus.nPowTargetTimespan % consensus.nPowTargetSpacing, 0);
// genesis nBits is positive, doesn't overflow and is lower than powLimit
arith_uint256 pow_compact;
bool neg, over;
pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over);
BOOST_CHECK(!neg);
BOOST_CHECK(pow_compact != 0);
BOOST_CHECK(!over);
BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact);
// check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()
if (!consensus.fPowNoRetargeting) {
arith_uint256 targ_max{UintToArith256(uint256{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"})};
targ_max /= consensus.nPowTargetTimespan*4;
BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);
}
}
BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity)
{
sanity_check_chainparams(*m_node.args, ChainType::MAIN);
}
BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity)
{
sanity_check_chainparams(*m_node.args, ChainType::REGTEST);
}
BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity)
{
sanity_check_chainparams(*m_node.args, ChainType::TESTNET);
}
BOOST_AUTO_TEST_CASE(ChainParams_TESTNET4_sanity)
{
sanity_check_chainparams(*m_node.args, ChainType::TESTNET4);
}
BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity)
{
sanity_check_chainparams(*m_node.args, ChainType::SIGNET);
}
BOOST_AUTO_TEST_SUITE_END()