Bitcoin ABC 0.30.5
P2P Digital Currency
eda_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/eda.h>
6#include <pow/pow.h>
7
8#include <chain.h>
9#include <chainparams.h>
10#include <config.h>
11
12#include <test/util/random.h>
13#include <test/util/setup_common.h>
14
15#include <boost/test/unit_test.hpp>
16
17BOOST_FIXTURE_TEST_SUITE(eda_tests, BasicTestingSetup)
18
19/* Test calculation of next difficulty target with no constraints applying */
20BOOST_AUTO_TEST_CASE(get_next_work) {
21 DummyConfig config(CBaseChainParams::MAIN);
22
23 int64_t nLastRetargetTime = 1261130161; // Block #30240
24 CBlockIndex pindexLast;
25 pindexLast.nHeight = 32255;
26 pindexLast.nTime = 1262152739; // Block #32255
27 pindexLast.nBits = 0x1d00ffff;
28
29 // Here (and below): expected_nbits is calculated in
30 // CalculateNextWorkRequired(); redoing the calculation here would be just
31 // reimplementing the same code that is written in pow.cpp. Rather than
32 // copy that code, we just hardcode the expected result.
33 unsigned int expected_nbits = 0x1d00d86aU;
34 auto consensus_params = config.GetChainParams().GetConsensus();
35 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
36 consensus_params),
37 expected_nbits);
39 PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
40 pindexLast.nBits, expected_nbits));
41}
42
43/* Test the constraint on the upper bound for next work */
44BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) {
45 DummyConfig config(CBaseChainParams::MAIN);
46
47 int64_t nLastRetargetTime = 1231006505; // Block #0
48 CBlockIndex pindexLast;
49 pindexLast.nHeight = 2015;
50 pindexLast.nTime = 1233061996; // Block #2015
51 pindexLast.nBits = 0x1d00ffff;
52 unsigned int expected_nbits = 0x1d00ffffU;
53 auto consensus_params = config.GetChainParams().GetConsensus();
54 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
55 consensus_params),
56 expected_nbits);
58 PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
59 pindexLast.nBits, expected_nbits));
60}
61
62/* Test the constraint on the lower bound for actual time taken */
63BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) {
64 DummyConfig config(CBaseChainParams::MAIN);
65
66 int64_t nLastRetargetTime = 1279008237; // Block #66528
67 CBlockIndex pindexLast;
68 pindexLast.nHeight = 68543;
69 pindexLast.nTime = 1279297671; // Block #68543
70 pindexLast.nBits = 0x1c05a3f4;
71 unsigned int expected_nbits = 0x1c0168fdU;
72 auto consensus_params = config.GetChainParams().GetConsensus();
73 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
74 consensus_params),
75 expected_nbits);
77 PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
78 pindexLast.nBits, expected_nbits));
79 // Test that reducing nbits further would not be a
80 // PermittedDifficultyTransition.
81 unsigned int invalid_nbits = expected_nbits - 1;
83 !PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
84 pindexLast.nBits, invalid_nbits));
85}
86
87/* Test the constraint on the upper bound for actual time taken */
88BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) {
89 DummyConfig config(CBaseChainParams::MAIN);
90
91 int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
92 CBlockIndex pindexLast;
93 pindexLast.nHeight = 46367;
94 pindexLast.nTime = 1269211443; // Block #46367
95 pindexLast.nBits = 0x1c387f6f;
96 unsigned int expected_nbits = 0x1d00e1fdU;
97 auto consensus_params = config.GetChainParams().GetConsensus();
98 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
99 consensus_params),
100 expected_nbits);
102 PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
103 pindexLast.nBits, expected_nbits));
104 // Test that increasing nbits further would not be a
105 // PermittedDifficultyTransition.
106 unsigned int invalid_nbits = expected_nbits + 1;
108 !PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
109 pindexLast.nBits, invalid_nbits));
110}
111
112BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target) {
113 const auto consensus =
114 CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
115 BlockHash hash;
116 unsigned int nBits;
117 nBits = UintToArith256(consensus.powLimit).GetCompact(true);
118 hash.SetHex("0x1");
119 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
120}
121
122BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target) {
123 const auto consensus =
124 CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
125 BlockHash hash;
126 unsigned int nBits = ~0x00800000;
127 hash.SetHex("0x1");
128 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
129}
130
131BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target) {
132 const auto consensus =
133 CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
134 BlockHash hash;
135 unsigned int nBits;
136 arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
137 nBits_arith *= 2;
138 nBits = nBits_arith.GetCompact();
139 hash.SetHex("0x1");
140 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
141}
142
143BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target) {
144 const auto consensus =
145 CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
146 BlockHash hash;
147 unsigned int nBits;
148 arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
149 nBits = hash_arith.GetCompact();
150 hash_arith *= 2; // hash > nBits
151 hash = BlockHash(ArithToUint256(hash_arith));
152 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
153}
154
155BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target) {
156 const auto consensus =
157 CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
158 BlockHash hash;
159 unsigned int nBits;
160 arith_uint256 hash_arith{0};
161 nBits = hash_arith.GetCompact();
162 hash = BlockHash(ArithToUint256(hash_arith));
163 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
164}
165
166BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) {
167 DummyConfig config(CBaseChainParams::MAIN);
168
169 std::vector<CBlockIndex> blocks(10000);
170 for (int i = 0; i < 10000; i++) {
171 blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
172 blocks[i].nHeight = i;
173 blocks[i].nTime =
174 1269211443 +
175 i * config.GetChainParams().GetConsensus().nPowTargetSpacing;
176 blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
177 blocks[i].nChainWork =
178 i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i])
179 : arith_uint256(0);
180 }
181
182 for (int j = 0; j < 1000; j++) {
183 CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
184 CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
185 CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
186
187 int64_t tdiff = GetBlockProofEquivalentTime(
188 *p1, *p2, *p3, config.GetChainParams().GetConsensus());
189 BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
190 }
191}
192
193static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval,
194 uint32_t nBits) {
195 CBlockIndex block;
196 block.pprev = pindexPrev;
197 block.nHeight = pindexPrev->nHeight + 1;
198 block.nTime = pindexPrev->nTime + nTimeInterval;
199 block.nBits = nBits;
200
201 block.nChainWork = pindexPrev->nChainWork + GetBlockProof(block);
202 return block;
203}
204
205BOOST_AUTO_TEST_CASE(retargeting_test) {
206 DummyConfig config(CBaseChainParams::MAIN);
207
208 std::vector<CBlockIndex> blocks(115);
209
210 const Consensus::Params &params = config.GetChainParams().GetConsensus();
211 const arith_uint256 powLimit = UintToArith256(params.powLimit);
212 arith_uint256 currentPow = powLimit >> 1;
213 uint32_t initialBits = currentPow.GetCompact();
214
215 // Genesis block.
216 blocks[0] = CBlockIndex();
217 blocks[0].nHeight = 0;
218 blocks[0].nTime = 1269211443;
219 blocks[0].nBits = initialBits;
220
221 blocks[0].nChainWork = GetBlockProof(blocks[0]);
222
223 // Pile up some blocks.
224 for (size_t i = 1; i < 100; i++) {
225 blocks[i] = GetBlockIndex(&blocks[i - 1], params.nPowTargetSpacing,
226 initialBits);
227 }
228
229 CBlockHeader blkHeaderDummy;
230
231 // We start getting 2h blocks time. For the first 5 blocks, it doesn't
232 // matter as the MTP is not affected. For the next 5 block, MTP difference
233 // increases but stays below 12h.
234 for (size_t i = 100; i < 110; i++) {
235 blocks[i] = GetBlockIndex(&blocks[i - 1], 2 * 3600, initialBits);
237 GetNextEDAWorkRequired(&blocks[i], &blkHeaderDummy, params),
238 initialBits);
239 }
240
241 // Now we expect the difficulty to decrease.
242 blocks[110] = GetBlockIndex(&blocks[109], 2 * 3600, initialBits);
243 currentPow.SetCompact(currentPow.GetCompact());
244 currentPow += (currentPow >> 2);
246 GetNextEDAWorkRequired(&blocks[110], &blkHeaderDummy, params),
247 currentPow.GetCompact());
248
249 // As we continue with 2h blocks, difficulty continue to decrease.
250 blocks[111] =
251 GetBlockIndex(&blocks[110], 2 * 3600, currentPow.GetCompact());
252 currentPow.SetCompact(currentPow.GetCompact());
253 currentPow += (currentPow >> 2);
255 GetNextEDAWorkRequired(&blocks[111], &blkHeaderDummy, params),
256 currentPow.GetCompact());
257
258 // We decrease again.
259 blocks[112] =
260 GetBlockIndex(&blocks[111], 2 * 3600, currentPow.GetCompact());
261 currentPow.SetCompact(currentPow.GetCompact());
262 currentPow += (currentPow >> 2);
264 GetNextEDAWorkRequired(&blocks[112], &blkHeaderDummy, params),
265 currentPow.GetCompact());
266
267 // We check that we do not go below the minimal difficulty.
268 blocks[113] =
269 GetBlockIndex(&blocks[112], 2 * 3600, currentPow.GetCompact());
270 currentPow.SetCompact(currentPow.GetCompact());
271 currentPow += (currentPow >> 2);
272 BOOST_CHECK(powLimit.GetCompact() != currentPow.GetCompact());
274 GetNextEDAWorkRequired(&blocks[113], &blkHeaderDummy, params),
275 powLimit.GetCompact());
276
277 // Once we reached the minimal difficulty, we stick with it.
278 blocks[114] = GetBlockIndex(&blocks[113], 2 * 3600, powLimit.GetCompact());
279 BOOST_CHECK(powLimit.GetCompact() != currentPow.GetCompact());
281 GetNextEDAWorkRequired(&blocks[114], &blkHeaderDummy, params),
282 powLimit.GetCompact());
283}
284
285BOOST_AUTO_TEST_SUITE_END()
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:74
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &params)
Return the time it would take to redo the work difference between from and to, assuming the current h...
Definition: chain.cpp:89
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.
Definition: chainparams.cpp:32
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
int64_t GetBlockTime() const
Definition: blockindex.h:180
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.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
uint32_t GetCompact(bool fNegative=false) const
void SetHex(const char *psz)
Definition: uint256.cpp:24
uint32_t GetNextEDAWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params &params)
Compute the next required proof of work using the legacy Bitcoin difficulty adjustment + Emergency Di...
Definition: eda.cpp:45
uint32_t CalculateNextWorkRequired(const CBlockIndex *pindexPrev, int64_t nFirstBlockTime, const Consensus::Params &params)
Do difficulty adjustement Satoshi's way.
Definition: eda.cpp:14
BOOST_AUTO_TEST_CASE(get_next_work)
Definition: eda_tests.cpp:20
static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval, uint32_t nBits)
Definition: eda_tests.cpp:193
NodeContext & m_node
Definition: interfaces.cpp:785
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:87
bool PermittedDifficultyTransition(const Consensus::Params &params, int64_t height, uint32_t old_nbits, uint32_t new_nbits)
Return false if the proof-of-work requirement specified by new_nbits at a given height is not possibl...
Definition: pow.cpp:47
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Parameters that influence chain consensus.
Definition: params.h:34
uint256 powLimit
Proof of work parameters.
Definition: params.h:76
int64_t nPowTargetSpacing
Definition: params.h:80