Bitcoin Core  24.99.0
P2P Digital Currency
pow_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <chain.h>
6 #include <chainparams.h>
7 #include <pow.h>
9 
10 #include <boost/test/unit_test.hpp>
11 
12 BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
13 
14 /* Test calculation of next difficulty target with no constraints applying */
15 BOOST_AUTO_TEST_CASE(get_next_work)
16 {
17  const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
18  int64_t nLastRetargetTime = 1261130161; // Block #30240
19  CBlockIndex pindexLast;
20  pindexLast.nHeight = 32255;
21  pindexLast.nTime = 1262152739; // Block #32255
22  pindexLast.nBits = 0x1d00ffff;
23 
24  // Here (and below): expected_nbits is calculated in
25  // CalculateNextWorkRequired(); redoing the calculation here would be just
26  // reimplementing the same code that is written in pow.cpp. Rather than
27  // copy that code, we just hardcode the expected result.
28  unsigned int expected_nbits = 0x1d00d86aU;
29  BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
30  BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
31 }
32 
33 /* Test the constraint on the upper bound for next work */
34 BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
35 {
36  const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
37  int64_t nLastRetargetTime = 1231006505; // Block #0
38  CBlockIndex pindexLast;
39  pindexLast.nHeight = 2015;
40  pindexLast.nTime = 1233061996; // Block #2015
41  pindexLast.nBits = 0x1d00ffff;
42  unsigned int expected_nbits = 0x1d00ffffU;
43  BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
44  BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
45 }
46 
47 /* Test the constraint on the lower bound for actual time taken */
48 BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
49 {
50  const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
51  int64_t nLastRetargetTime = 1279008237; // Block #66528
52  CBlockIndex pindexLast;
53  pindexLast.nHeight = 68543;
54  pindexLast.nTime = 1279297671; // Block #68543
55  pindexLast.nBits = 0x1c05a3f4;
56  unsigned int expected_nbits = 0x1c0168fdU;
57  BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
58  BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
59  // Test that reducing nbits further would not be a PermittedDifficultyTransition.
60  unsigned int invalid_nbits = expected_nbits-1;
61  BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
62 }
63 
64 /* Test the constraint on the upper bound for actual time taken */
65 BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
66 {
67  const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
68  int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
69  CBlockIndex pindexLast;
70  pindexLast.nHeight = 46367;
71  pindexLast.nTime = 1269211443; // Block #46367
72  pindexLast.nBits = 0x1c387f6f;
73  unsigned int expected_nbits = 0x1d00e1fdU;
74  BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);
75  BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));
76  // Test that increasing nbits further would not be a PermittedDifficultyTransition.
77  unsigned int invalid_nbits = expected_nbits+1;
78  BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));
79 }
80 
81 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
82 {
83  const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
84  uint256 hash;
85  unsigned int nBits;
86  nBits = UintToArith256(consensus.powLimit).GetCompact(true);
87  hash.SetHex("0x1");
88  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
89 }
90 
91 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
92 {
93  const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
94  uint256 hash;
95  unsigned int nBits{~0x00800000U};
96  hash.SetHex("0x1");
97  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
98 }
99 
100 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
101 {
102  const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
103  uint256 hash;
104  unsigned int nBits;
105  arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
106  nBits_arith *= 2;
107  nBits = nBits_arith.GetCompact();
108  hash.SetHex("0x1");
109  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
110 }
111 
112 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
113 {
114  const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
115  uint256 hash;
116  unsigned int nBits;
117  arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
118  nBits = hash_arith.GetCompact();
119  hash_arith *= 2; // hash > nBits
120  hash = ArithToUint256(hash_arith);
121  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
122 }
123 
124 BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
125 {
126  const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
127  uint256 hash;
128  unsigned int nBits;
129  arith_uint256 hash_arith{0};
130  nBits = hash_arith.GetCompact();
131  hash = ArithToUint256(hash_arith);
132  BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
133 }
134 
135 BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
136 {
137  const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
138  std::vector<CBlockIndex> blocks(10000);
139  for (int i = 0; i < 10000; i++) {
140  blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
141  blocks[i].nHeight = i;
142  blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
143  blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
144  blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
145  }
146 
147  for (int j = 0; j < 1000; j++) {
148  CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
149  CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
150  CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
151 
152  int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
153  BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
154  }
155 }
156 
157 void sanity_check_chainparams(const ArgsManager& args, std::string chainName)
158 {
159  const auto chainParams = CreateChainParams(args, chainName);
160  const auto consensus = chainParams->GetConsensus();
161 
162  // hash genesis is correct
163  BOOST_CHECK_EQUAL(consensus.hashGenesisBlock, chainParams->GenesisBlock().GetHash());
164 
165  // target timespan is an even multiple of spacing
166  BOOST_CHECK_EQUAL(consensus.nPowTargetTimespan % consensus.nPowTargetSpacing, 0);
167 
168  // genesis nBits is positive, doesn't overflow and is lower than powLimit
169  arith_uint256 pow_compact;
170  bool neg, over;
171  pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over);
172  BOOST_CHECK(!neg && pow_compact != 0);
173  BOOST_CHECK(!over);
174  BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact);
175 
176  // check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()
177  if (!consensus.fPowNoRetargeting) {
178  arith_uint256 targ_max("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
179  targ_max /= consensus.nPowTargetTimespan*4;
180  BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);
181  }
182 }
183 
184 BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity)
185 {
187 }
188 
189 BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity)
190 {
192 }
193 
194 BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity)
195 {
197 }
198 
199 BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity)
200 {
202 }
203 
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:131
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:146
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.
static const std::string REGTEST
static const std::string TESTNET
static const std::string SIGNET
static const std::string MAIN
Chain name strings.
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:152
uint32_t nTime
Definition: chain.h:206
int64_t GetBlockTime() const
Definition: chain.h:284
uint32_t nBits
Definition: chain.h:207
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:164
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:30
256-bit opaque blob.
Definition: uint256.h:119
BOOST_AUTO_TEST_SUITE_END()
ArgsManager args
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
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:76
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:125
unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params &params)
Definition: pow.cpp:49
void sanity_check_chainparams(const ArgsManager &args, std::string chainName)
Definition: pow_tests.cpp:157
BOOST_AUTO_TEST_CASE(get_next_work)
Definition: pow_tests.cpp:15
static uint64_t InsecureRandRange(uint64_t range)
Definition: setup_common.h:75
Basic testing setup.
Definition: setup_common.h:83
ArgsManager * args
Definition: context.h:56