Bitcoin Core 22.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
13
14/* Test calculation of next difficulty target with no constraints applying */
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 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00d86aU);
24}
25
26/* Test the constraint on the upper bound for next work */
27BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
28{
29 const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
30 int64_t nLastRetargetTime = 1231006505; // Block #0
31 CBlockIndex pindexLast;
32 pindexLast.nHeight = 2015;
33 pindexLast.nTime = 1233061996; // Block #2015
34 pindexLast.nBits = 0x1d00ffff;
35 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00ffffU);
36}
37
38/* Test the constraint on the lower bound for actual time taken */
39BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
40{
41 const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
42 int64_t nLastRetargetTime = 1279008237; // Block #66528
43 CBlockIndex pindexLast;
44 pindexLast.nHeight = 68543;
45 pindexLast.nTime = 1279297671; // Block #68543
46 pindexLast.nBits = 0x1c05a3f4;
47 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1c0168fdU);
48}
49
50/* Test the constraint on the upper bound for actual time taken */
51BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
52{
53 const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
54 int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
55 CBlockIndex pindexLast;
56 pindexLast.nHeight = 46367;
57 pindexLast.nTime = 1269211443; // Block #46367
58 pindexLast.nBits = 0x1c387f6f;
59 BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1d00e1fdU);
60}
61
62BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)
63{
64 const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
65 uint256 hash;
66 unsigned int nBits;
67 nBits = UintToArith256(consensus.powLimit).GetCompact(true);
68 hash.SetHex("0x1");
69 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
70}
71
72BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)
73{
74 const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
75 uint256 hash;
76 unsigned int nBits = ~0x00800000;
77 hash.SetHex("0x1");
78 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
79}
80
81BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)
82{
83 const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
84 uint256 hash;
85 unsigned int nBits;
86 arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);
87 nBits_arith *= 2;
88 nBits = nBits_arith.GetCompact();
89 hash.SetHex("0x1");
90 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
91}
92
93BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)
94{
95 const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
96 uint256 hash;
97 unsigned int nBits;
98 arith_uint256 hash_arith = UintToArith256(consensus.powLimit);
99 nBits = hash_arith.GetCompact();
100 hash_arith *= 2; // hash > nBits
101 hash = ArithToUint256(hash_arith);
102 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
103}
104
105BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)
106{
107 const auto consensus = CreateChainParams(*m_node.args, CBaseChainParams::MAIN)->GetConsensus();
108 uint256 hash;
109 unsigned int nBits;
110 arith_uint256 hash_arith{0};
111 nBits = hash_arith.GetCompact();
112 hash = ArithToUint256(hash_arith);
113 BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));
114}
115
116BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
117{
118 const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
119 std::vector<CBlockIndex> blocks(10000);
120 for (int i = 0; i < 10000; i++) {
121 blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
122 blocks[i].nHeight = i;
123 blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
124 blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
125 blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
126 }
127
128 for (int j = 0; j < 1000; j++) {
129 CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
130 CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
131 CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];
132
133 int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
134 BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
135 }
136}
137
138void sanity_check_chainparams(const ArgsManager& args, std::string chainName)
139{
140 const auto chainParams = CreateChainParams(args, chainName);
141 const auto consensus = chainParams->GetConsensus();
142
143 // hash genesis is correct
144 BOOST_CHECK_EQUAL(consensus.hashGenesisBlock, chainParams->GenesisBlock().GetHash());
145
146 // target timespan is an even multiple of spacing
147 BOOST_CHECK_EQUAL(consensus.nPowTargetTimespan % consensus.nPowTargetSpacing, 0);
148
149 // genesis nBits is positive, doesn't overflow and is lower than powLimit
150 arith_uint256 pow_compact;
151 bool neg, over;
152 pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over);
153 BOOST_CHECK(!neg && pow_compact != 0);
154 BOOST_CHECK(!over);
155 BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact);
156
157 // check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()
158 if (!consensus.fPowNoRetargeting) {
159 arith_uint256 targ_max("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
160 targ_max /= consensus.nPowTargetTimespan*4;
161 BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);
162 }
163}
164
165BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity)
166{
168}
169
170BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity)
171{
173}
174
175BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity)
176{
178}
179
180BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity)
181{
183}
184
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
NodeContext m_node
Definition: bitcoin-gui.cpp:36
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:122
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:137
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:146
uint32_t nTime
Definition: chain.h:200
int64_t GetBlockTime() const
Definition: chain.h:268
uint32_t nBits
Definition: chain.h:201
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:158
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:124
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
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:74
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:138
BOOST_AUTO_TEST_CASE(get_next_work)
Definition: pow_tests.cpp:15
static uint64_t InsecureRandRange(uint64_t range)
Definition: setup_common.h:68
Basic testing setup.
Definition: setup_common.h:76
ArgsManager * args
Definition: context.h:49