Bitcoin ABC  0.26.3
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/setup_common.h>
13 
14 #include <boost/test/unit_test.hpp>
15 
16 BOOST_FIXTURE_TEST_SUITE(eda_tests, BasicTestingSetup)
17 
18 /* Test calculation of next difficulty target with no constraints applying */
19 BOOST_AUTO_TEST_CASE(get_next_work) {
21 
22  int64_t nLastRetargetTime = 1261130161; // Block #30240
23  CBlockIndex pindexLast;
24  pindexLast.nHeight = 32255;
25  pindexLast.nTime = 1262152739; // Block #32255
26  pindexLast.nBits = 0x1d00ffff;
27 
28  // Here (and below): expected_nbits is calculated in
29  // CalculateNextWorkRequired(); redoing the calculation here would be just
30  // reimplementing the same code that is written in pow.cpp. Rather than
31  // copy that code, we just hardcode the expected result.
32  unsigned int expected_nbits = 0x1d00d86aU;
33  auto consensus_params = config.GetChainParams().GetConsensus();
34  BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
35  consensus_params),
36  expected_nbits);
38  PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
39  pindexLast.nBits, expected_nbits));
40 }
41 
42 /* Test the constraint on the upper bound for next work */
43 BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) {
45 
46  int64_t nLastRetargetTime = 1231006505; // Block #0
47  CBlockIndex pindexLast;
48  pindexLast.nHeight = 2015;
49  pindexLast.nTime = 1233061996; // Block #2015
50  pindexLast.nBits = 0x1d00ffff;
51  unsigned int expected_nbits = 0x1d00ffffU;
52  auto consensus_params = config.GetChainParams().GetConsensus();
53  BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
54  consensus_params),
55  expected_nbits);
57  PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
58  pindexLast.nBits, expected_nbits));
59 }
60 
61 /* Test the constraint on the lower bound for actual time taken */
62 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) {
64 
65  int64_t nLastRetargetTime = 1279008237; // Block #66528
66  CBlockIndex pindexLast;
67  pindexLast.nHeight = 68543;
68  pindexLast.nTime = 1279297671; // Block #68543
69  pindexLast.nBits = 0x1c05a3f4;
70  unsigned int expected_nbits = 0x1c0168fdU;
71  auto consensus_params = config.GetChainParams().GetConsensus();
72  BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
73  consensus_params),
74  expected_nbits);
76  PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
77  pindexLast.nBits, expected_nbits));
78  // Test that reducing nbits further would not be a
79  // PermittedDifficultyTransition.
80  unsigned int invalid_nbits = expected_nbits - 1;
82  !PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
83  pindexLast.nBits, invalid_nbits));
84 }
85 
86 /* Test the constraint on the upper bound for actual time taken */
87 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) {
89 
90  int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
91  CBlockIndex pindexLast;
92  pindexLast.nHeight = 46367;
93  pindexLast.nTime = 1269211443; // Block #46367
94  pindexLast.nBits = 0x1c387f6f;
95  unsigned int expected_nbits = 0x1d00e1fdU;
96  auto consensus_params = config.GetChainParams().GetConsensus();
97  BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
98  consensus_params),
99  expected_nbits);
100  BOOST_CHECK(
101  PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
102  pindexLast.nBits, expected_nbits));
103  // Test that increasing nbits further would not be a
104  // PermittedDifficultyTransition.
105  unsigned int invalid_nbits = expected_nbits + 1;
106  BOOST_CHECK(
107  !PermittedDifficultyTransition(consensus_params, pindexLast.nHeight + 1,
108  pindexLast.nBits, invalid_nbits));
109 }
110 
111 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target) {
112  const auto consensus =
113  CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
114  BlockHash hash;
115  unsigned int nBits;
116  nBits = UintToArith256(consensus.powLimit).GetCompact(true);
117  hash.SetHex("0x1");
118  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
119 }
120 
121 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target) {
122  const auto consensus =
123  CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
124  BlockHash hash;
125  unsigned int nBits = ~0x00800000;
126  hash.SetHex("0x1");
127  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
128 }
129 
130 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target) {
131  const auto consensus =
132  CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
133  BlockHash hash;
134  unsigned int nBits;
135  arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
136  nBits_arith *= 2;
137  nBits = nBits_arith.GetCompact();
138  hash.SetHex("0x1");
139  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
140 }
141 
142 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target) {
143  const auto consensus =
144  CreateChainParams(CBaseChainParams::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 
154 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target) {
155  const auto consensus =
156  CreateChainParams(CBaseChainParams::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 
165 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) {
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 +
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[InsecureRandRange(10000)];
183  CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
184  CBlockIndex *p3 = &blocks[InsecureRandRange(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 
192 static 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 
204 BOOST_AUTO_TEST_CASE(retargeting_test) {
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 
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:78
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:93
std::unique_ptr< CChainParams > CreateChainParams(const std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
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:26
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:33
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:52
uint32_t nTime
Definition: blockindex.h:93
int64_t GetBlockTime() const
Definition: blockindex.h:178
uint32_t nBits
Definition: blockindex.h:94
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:39
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:86
const CChainParams & GetChainParams() const override
Definition: config.h:54
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:19
static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval, uint32_t nBits)
Definition: eda_tests.cpp:192
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#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:91
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
BOOST_FIXTURE_TEST_SUITE(stakingrewards_tests, StakingRewardsActivationTestingSetup) BOOST_AUTO_TEST_CASE(isstakingrewardsactivated)
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