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