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