Repository navigation
Expand file tree
/
Copy pathpow_tests.cpp
More file actions
194 lines (165 loc) · 6.62 KB
/
Copy pathpow_tests.cpp
File metadata and controls
194 lines (165 loc) · 6.62 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
// 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 <chain.h>
#include <chainparams.h>
#include <config.h>
#include <consensus/activation.h>
#include <pow.h>
#include <random.h>
#include <tinyformat.h>
#include <util/system.h>
#include <test/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <cmath>
#include <memory>
#include <string>
BOOST_FIXTURE_TEST_SUITE(pow_tests, TestingSetup)
/* Test calculation of next difficulty target with no constraints applying */
BOOST_AUTO_TEST_CASE(get_next_work) {
DummyConfig config(CBaseChainParams::MAIN);
int64_t nLastRetargetTime = 1261130161; // Block #30240
CBlockIndex pindexLast;
pindexLast.nHeight = 32255;
pindexLast.nTime = 1261152739; // Block #32255
pindexLast.nBits = 0x1d00ffff;
BOOST_CHECK_EQUAL(
CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
config.GetChainParams().GetConsensus()),
473956288);
}
/* Test the constraint on the upper bound for next work */
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) {
DummyConfig config(CBaseChainParams::MAIN);
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,
config.GetChainParams().GetConsensus()),
0x1d00ffffU);
}
/* Test the constraint on the lower bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) {
DummyConfig config(CBaseChainParams::MAIN);
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,
config.GetChainParams().GetConsensus()),
469938949);
}
/* Test the constraint on the upper bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) {
DummyConfig config(CBaseChainParams::MAIN);
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,
config.GetChainParams().GetConsensus()),
0x1d00e1fdU);
}
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) {
DummyConfig config(CBaseChainParams::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 * config.GetChainParams().GetConsensus().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[InsecureRandRange(10000)];
CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
int64_t tdiff = GetBlockProofEquivalentTime(
*p1, *p2, *p3, config.GetChainParams().GetConsensus());
BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
}
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
{
DummyConfig config(CBaseChainParams::MAIN);
uint256 hash;
unsigned int nBits;
nBits = UintToArith256(config.GetChainParams().GetConsensus().powLimit).GetCompact(true);
hash.SetHex("0x1");
BOOST_CHECK(!CheckProofOfWork(BlockHash(hash), nBits, config.GetChainParams().GetConsensus()));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
{
DummyConfig config(CBaseChainParams::MAIN);
uint256 hash;
unsigned int nBits{~0x00800000U};
hash.SetHex("0x1");
BOOST_CHECK(!CheckProofOfWork(BlockHash(hash), nBits, config.GetChainParams().GetConsensus()));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
{
DummyConfig config(CBaseChainParams::MAIN);
uint256 hash;
unsigned int nBits;
arith_uint256 nBits_arith = UintToArith256(config.GetChainParams().GetConsensus().powLimit);
nBits_arith *= 2;
nBits = nBits_arith.GetCompact();
hash.SetHex("0x1");
BOOST_CHECK(!CheckProofOfWork(BlockHash(hash), nBits, config.GetChainParams().GetConsensus()));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
{
DummyConfig config(CBaseChainParams::MAIN);
uint256 hash;
unsigned int nBits;
arith_uint256 hash_arith = UintToArith256(config.GetChainParams().GetConsensus().powLimit);
nBits = hash_arith.GetCompact();
hash_arith *= 2; // hash > nBits
hash = ArithToUint256(hash_arith);
BOOST_CHECK(!CheckProofOfWork(BlockHash(hash), nBits, config.GetChainParams().GetConsensus()));
}
BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
{
DummyConfig config(CBaseChainParams::MAIN);
uint256 hash;
unsigned int nBits;
arith_uint256 hash_arith{0};
nBits = hash_arith.GetCompact();
hash = ArithToUint256(hash_arith);
BOOST_CHECK(!CheckProofOfWork(BlockHash(hash), nBits, config.GetChainParams().GetConsensus()));
}
double TargetFromBits(const uint32_t nBits) {
return (nBits & 0xff'ff'ff) * pow(256, (nBits >> 24)-3);
}
std::string StrPrintCalcArgs(const arith_uint256 refTarget,
const int64_t targetSpacing,
const int64_t timeDiff,
const int64_t heightDiff,
const arith_uint256 expectedTarget,
const uint32_t expectednBits) {
return strprintf("\n"
"ref= %s\n"
"spacing= %d\n"
"timeDiff= %d\n"
"heightDiff= %d\n"
"expTarget= %s\n"
"exp nBits= 0x%08x\n",
refTarget.ToString(),
targetSpacing,
timeDiff,
heightDiff,
expectedTarget.ToString(),
expectednBits);
}
BOOST_AUTO_TEST_SUITE_END()