Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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>
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 */
19{
21 int64_t nLastRetargetTime = 1261130161; // Block #30240
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;
34}
35
36/* Test the constraint on the upper bound for next work */
38{
40 int64_t nLastRetargetTime = 1231006505; // Block #0
42 pindexLast.nHeight = 2015;
43 pindexLast.nTime = 1233061996; // Block #2015
44 pindexLast.nBits = 0x1d00ffff;
45 unsigned int expected_nbits = 0x1d00ffffU;
48}
49
50/* Test the constraint on the lower bound for actual time taken */
52{
54 int64_t nLastRetargetTime = 1279008237; // Block #66528
56 pindexLast.nHeight = 68543;
57 pindexLast.nTime = 1279297671; // Block #68543
58 pindexLast.nBits = 0x1c05a3f4;
59 unsigned int expected_nbits = 0x1c0168fdU;
62 // Test that reducing nbits further would not be a PermittedDifficultyTransition.
63 unsigned int invalid_nbits = expected_nbits-1;
65}
66
67/* Test that the largest permitted difficulty increase is not itself rejected
68 * when the new target is not exactly representable in compact form. */
70{
71 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
73 pindexLast.nHeight = 68543;
74 pindexLast.nTime = 1279297671;
75 pindexLast.nBits = 0x1c05a3f5; // Mantissa is not a multiple of four
76 int64_t nLastRetargetTime = pindexLast.nTime - consensus.nPowTargetTimespan/4;
77 unsigned int expected_nbits = 0x1c0168fdU;
80}
81
82/* Test the lower bound for a negative actual time taken, which is possible
83 * because timestamps only have to exceed the median of the previous eleven. */
85{
86 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
87 int64_t nLastRetargetTime = 1279297671; // Later than pindexLast.nTime
89 pindexLast.nHeight = 68543;
90 pindexLast.nTime = 1279008237;
91 pindexLast.nBits = 0x1c05a3f4;
92 unsigned int expected_nbits = 0x1c0168fdU;
95}
96
97/* Test the constraint on the upper bound for actual time taken */
99{
101 int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
103 pindexLast.nHeight = 46367;
104 pindexLast.nTime = 1269211443; // Block #46367
105 pindexLast.nBits = 0x1c387f6f;
106 unsigned int expected_nbits = 0x1d00e1fdU;
109 // Test that increasing nbits further would not be a PermittedDifficultyTransition.
110 unsigned int invalid_nbits = expected_nbits+1;
112}
113
114/* Test that the retarget window starts at the timestamp of the first block
115 * of the window, not its median time past. */
117{
118 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
119 const int64_t interval{consensus.DifficultyAdjustmentInterval()};
120
121 // Two retarget windows, so that the first block of the second one has
122 // ancestors and hence a median time past earlier than its own time.
123 std::vector<CBlockIndex> blocks(2 * interval);
124 for (int64_t i = 0; i < 2 * interval; i++) {
125 blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
126 blocks[i].nHeight = i;
127 blocks[i].nTime = 1269211443 + i * consensus.nPowTargetSpacing;
128 blocks[i].nBits = 0x1c05a3f4;
129 }
130
132 const CBlockIndex& last{blocks[2 * interval - 1]};
133 BOOST_CHECK_NE(first.GetBlockTime(), first.GetMedianTimePast());
134 unsigned int expected_nbits = 0x1c05a33c;
137}
138
139/* Test that nbits is only permitted to change at a difficulty adjustment. */
141{
142 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
143 const int64_t height{consensus.DifficultyAdjustmentInterval() + 1}; // Not a retarget height
144
145 const unsigned int nbits{0x1c05a3f4};
147 BOOST_CHECK(!PermittedDifficultyTransition(consensus, height, nbits, nbits - 1));
148 BOOST_CHECK(!PermittedDifficultyTransition(consensus, height, nbits, nbits + 1));
149}
150
152{
153 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
154 uint256 hash;
155 unsigned int nBits;
156 nBits = UintToArith256(consensus.powLimit).GetCompact(true);
157 hash = uint256{1};
158 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
159}
160
162{
163 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
164 uint256 hash;
165 unsigned int nBits{~0x00800000U};
166 hash = uint256{1};
167 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
168}
169
171{
172 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
173 uint256 hash;
174 unsigned int nBits;
175 arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
176 nBits_arith *= 2;
177 nBits = nBits_arith.GetCompact();
178 hash = uint256{1};
179 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
180}
181
183{
184 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
185 uint256 hash;
186 unsigned int nBits;
187 arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
188 nBits = hash_arith.GetCompact();
189 hash_arith *= 2; // hash > nBits
191 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
192}
193
195{
196 const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();
197 uint256 hash;
198 unsigned int nBits;
200 nBits = hash_arith.GetCompact();
202 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
203}
204
206{
207 // Targets that are zero, negative or overflow encode no work.
212 // The exponent byte is unsigned, so this is a large exponent that overflows.
214 // The two overflows above shift every mantissa bit out of 256 bits, so they would
215 // also give zero work without the overflow check. This one keeps the low bits of
216 // its mantissa when truncated.
218 // The genesis block target.
219 BOOST_CHECK_EQUAL(GetBitsProof(0x1d00ffff), arith_uint256{0x100010001});
220}
221
223{
225 std::vector<CBlockIndex> blocks(10000);
226 for (int i = 0; i < 10000; i++) {
227 blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
228 blocks[i].nHeight = i;
229 blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
230 blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
231 blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
232 }
233
234 for (int j = 0; j < 1000; j++) {
235 CBlockIndex *p1 = &blocks[m_rng.randrange(10000)];
236 CBlockIndex *p2 = &blocks[m_rng.randrange(10000)];
237 CBlockIndex *p3 = &blocks[m_rng.randrange(10000)];
238
240 BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
241 }
242}
243
245{
247 const auto consensus = chainParams->GetConsensus();
248
249 // hash genesis is correct
250 BOOST_CHECK_EQUAL(consensus.hashGenesisBlock, chainParams->GenesisBlock().GetHash());
251
252 // target timespan is an even multiple of spacing
253 BOOST_CHECK_EQUAL(consensus.nPowTargetTimespan % consensus.nPowTargetSpacing, 0);
254
255 // genesis nBits is positive, doesn't overflow and is lower than powLimit
257 bool neg, over;
258 pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over);
262 BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact);
263
264 // check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()
265 if (!consensus.fPowNoRetargeting) {
266 arith_uint256 targ_max{UintToArith256(uint256{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"})};
267 targ_max /= consensus.nPowTargetTimespan*4;
268 BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);
269 }
270}
271
276
281
286
291
296
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
node::NodeContext m_node
ArgsManager & args
Definition bitcoind.cpp:280
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:135
arith_uint256 GetBitsProof(uint32_t bits)
Compute how much work an nBits value corresponds to.
Definition chain.cpp:120
arith_uint256 GetBlockProof(const CBlockIndex &block)
Compute how much work a block index entry corresponds to.
Definition chain.h:305
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
ChainType
Definition chaintype.h:12
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition chain.h:94
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition chain.h:106
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
256-bit opaque blob.
Definition uint256.h:196
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#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:89
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition pow.cpp:14
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
unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params &params)
Definition pow.cpp:50
BOOST_AUTO_TEST_CASE(get_next_work)
Definition pow_tests.cpp:18
void sanity_check_chainparams(const ArgsManager &args, ChainType chain_type)
Basic testing setup.
ArgsManager * args
Definition context.h:76
BOOST_CHECK_NE(OneL.ToString(), ArrayToString(ZeroArray, 32))
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82