Bitcoin ABC  0.22.12
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;
28  CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
29  config.GetChainParams().GetConsensus()),
30  0x1d00d86aU);
31 }
32 
33 /* Test the constraint on the upper bound for next work */
34 BOOST_AUTO_TEST_CASE(get_next_work_pow_limit) {
36 
37  int64_t nLastRetargetTime = 1231006505; // Block #0
38  CBlockIndex pindexLast;
39  pindexLast.nHeight = 2015;
40  pindexLast.nTime = 1233061996; // Block #2015
41  pindexLast.nBits = 0x1d00ffff;
43  CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
44  config.GetChainParams().GetConsensus()),
45  0x1d00ffffU);
46 }
47 
48 /* Test the constraint on the lower bound for actual time taken */
49 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual) {
51 
52  int64_t nLastRetargetTime = 1279008237; // Block #66528
53  CBlockIndex pindexLast;
54  pindexLast.nHeight = 68543;
55  pindexLast.nTime = 1279297671; // Block #68543
56  pindexLast.nBits = 0x1c05a3f4;
58  CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
59  config.GetChainParams().GetConsensus()),
60  0x1c0168fdU);
61 }
62 
63 /* Test the constraint on the upper bound for actual time taken */
64 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual) {
66 
67  int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
68  CBlockIndex pindexLast;
69  pindexLast.nHeight = 46367;
70  pindexLast.nTime = 1269211443; // Block #46367
71  pindexLast.nBits = 0x1c387f6f;
73  CalculateNextWorkRequired(&pindexLast, nLastRetargetTime,
74  config.GetChainParams().GetConsensus()),
75  0x1d00e1fdU);
76 }
77 
78 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target) {
79  const auto consensus =
81  BlockHash hash;
82  unsigned int nBits;
83  nBits = UintToArith256(consensus.powLimit).GetCompact(true);
84  hash.SetHex("0x1");
85  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
86 }
87 
88 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target) {
89  const auto consensus =
91  BlockHash hash;
92  unsigned int nBits = ~0x00800000;
93  hash.SetHex("0x1");
94  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
95 }
96 
97 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target) {
98  const auto consensus =
100  BlockHash hash;
101  unsigned int nBits;
102  arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
103  nBits_arith *= 2;
104  nBits = nBits_arith.GetCompact();
105  hash.SetHex("0x1");
106  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
107 }
108 
109 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target) {
110  const auto consensus =
111  CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
112  BlockHash hash;
113  unsigned int nBits;
114  arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
115  nBits = hash_arith.GetCompact();
116  hash_arith *= 2; // hash > nBits
117  hash = BlockHash(ArithToUint256(hash_arith));
118  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
119 }
120 
121 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target) {
122  const auto consensus =
123  CreateChainParams(CBaseChainParams::MAIN)->GetConsensus();
124  BlockHash hash;
125  unsigned int nBits;
126  arith_uint256 hash_arith{0};
127  nBits = hash_arith.GetCompact();
128  hash = BlockHash(ArithToUint256(hash_arith));
129  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
130 }
131 
132 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test) {
134 
135  std::vector<CBlockIndex> blocks(10000);
136  for (int i = 0; i < 10000; i++) {
137  blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
138  blocks[i].nHeight = i;
139  blocks[i].nTime =
140  1269211443 +
142  blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
143  blocks[i].nChainWork =
144  i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i])
145  : arith_uint256(0);
146  }
147 
148  for (int j = 0; j < 1000; j++) {
149  CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
150  CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
151  CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
152 
153  int64_t tdiff = GetBlockProofEquivalentTime(
154  *p1, *p2, *p3, config.GetChainParams().GetConsensus());
155  BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
156  }
157 }
158 
159 static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval,
160  uint32_t nBits) {
161  CBlockIndex block;
162  block.pprev = pindexPrev;
163  block.nHeight = pindexPrev->nHeight + 1;
164  block.nTime = pindexPrev->nTime + nTimeInterval;
165  block.nBits = nBits;
166 
167  block.nChainWork = pindexPrev->nChainWork + GetBlockProof(block);
168  return block;
169 }
170 
171 BOOST_AUTO_TEST_CASE(retargeting_test) {
173 
174  std::vector<CBlockIndex> blocks(115);
175 
176  const Consensus::Params &params = config.GetChainParams().GetConsensus();
177  const arith_uint256 powLimit = UintToArith256(params.powLimit);
178  arith_uint256 currentPow = powLimit >> 1;
179  uint32_t initialBits = currentPow.GetCompact();
180 
181  // Genesis block.
182  blocks[0] = CBlockIndex();
183  blocks[0].nHeight = 0;
184  blocks[0].nTime = 1269211443;
185  blocks[0].nBits = initialBits;
186 
187  blocks[0].nChainWork = GetBlockProof(blocks[0]);
188 
189  // Pile up some blocks.
190  for (size_t i = 1; i < 100; i++) {
191  blocks[i] = GetBlockIndex(&blocks[i - 1], params.nPowTargetSpacing,
192  initialBits);
193  }
194 
195  CBlockHeader blkHeaderDummy;
196 
197  // We start getting 2h blocks time. For the first 5 blocks, it doesn't
198  // matter as the MTP is not affected. For the next 5 block, MTP difference
199  // increases but stays below 12h.
200  for (size_t i = 100; i < 110; i++) {
201  blocks[i] = GetBlockIndex(&blocks[i - 1], 2 * 3600, initialBits);
203  GetNextEDAWorkRequired(&blocks[i], &blkHeaderDummy, params),
204  initialBits);
205  }
206 
207  // Now we expect the difficulty to decrease.
208  blocks[110] = GetBlockIndex(&blocks[109], 2 * 3600, initialBits);
209  currentPow.SetCompact(currentPow.GetCompact());
210  currentPow += (currentPow >> 2);
212  GetNextEDAWorkRequired(&blocks[110], &blkHeaderDummy, params),
213  currentPow.GetCompact());
214 
215  // As we continue with 2h blocks, difficulty continue to decrease.
216  blocks[111] =
217  GetBlockIndex(&blocks[110], 2 * 3600, currentPow.GetCompact());
218  currentPow.SetCompact(currentPow.GetCompact());
219  currentPow += (currentPow >> 2);
221  GetNextEDAWorkRequired(&blocks[111], &blkHeaderDummy, params),
222  currentPow.GetCompact());
223 
224  // We decrease again.
225  blocks[112] =
226  GetBlockIndex(&blocks[111], 2 * 3600, currentPow.GetCompact());
227  currentPow.SetCompact(currentPow.GetCompact());
228  currentPow += (currentPow >> 2);
230  GetNextEDAWorkRequired(&blocks[112], &blkHeaderDummy, params),
231  currentPow.GetCompact());
232 
233  // We check that we do not go below the minimal difficulty.
234  blocks[113] =
235  GetBlockIndex(&blocks[112], 2 * 3600, currentPow.GetCompact());
236  currentPow.SetCompact(currentPow.GetCompact());
237  currentPow += (currentPow >> 2);
238  BOOST_CHECK(powLimit.GetCompact() != currentPow.GetCompact());
240  GetNextEDAWorkRequired(&blocks[113], &blkHeaderDummy, params),
241  powLimit.GetCompact());
242 
243  // Once we reached the minimal difficulty, we stick with it.
244  blocks[114] = GetBlockIndex(&blocks[113], 2 * 3600, powLimit.GetCompact());
245  BOOST_CHECK(powLimit.GetCompact() != currentPow.GetCompact());
247  GetNextEDAWorkRequired(&blocks[114], &blkHeaderDummy, params),
248  powLimit.GetCompact());
249 }
250 
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: blockindex.h:49
int64_t GetBlockTime() const
Definition: blockindex.h:160
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:30
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 nTime
Definition: blockindex.h:81
uint32_t GetCompact(bool fNegative=false) const
arith_uint256 UintToArith256(const uint256 &a)
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
uint256 powLimit
Proof of work parameters.
Definition: params.h:100
uint32_t CalculateNextWorkRequired(const CBlockIndex *pindexPrev, int64_t nFirstBlockTime, const Consensus::Params &params)
Do difficulty adjustement Satoshi&#39;s way.
Definition: eda.cpp:14
std::unique_ptr< CChainParams > CreateChainParams(const std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
int64_t nPowTargetSpacing
Definition: params.h:104
static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval, uint32_t nBits)
Definition: eda_tests.cpp:159
Parameters that influence chain consensus.
Definition: params.h:59
256-bit unsigned big integer.
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:45
uint256 ArithToUint256(const arith_uint256 &a)
const CChainParams & GetChainParams() const override
Definition: config.h:61
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
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...
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:80
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:59
void SetHex(const char *psz)
Definition: uint256.cpp:21
BOOST_AUTO_TEST_CASE(get_next_work)
Definition: eda_tests.cpp:19
uint32_t nBits
Definition: blockindex.h:82
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
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:95
#define BOOST_CHECK(expr)
Definition: object.cpp:17