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