Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2015-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.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>13#include <boost/test/unit_test.hpp>15BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)17/* Test calculation of next difficulty target with no constraints applying */18BOOST_AUTO_TEST_CASE(get_next_work)19{20const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);21int64_t nLastRetargetTime = 1261130161; // Block #3024022CBlockIndex pindexLast;23pindexLast.nHeight = 32255;24pindexLast.nTime = 1262152739; // Block #3225525pindexLast.nBits = 0x1d00ffff;27// Here (and below): expected_nbits is calculated in28// CalculateNextWorkRequired(); redoing the calculation here would be just29// reimplementing the same code that is written in pow.cpp. Rather than30// copy that code, we just hardcode the expected result.31unsigned int expected_nbits = 0x1d00d86aU;32BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);33BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));34}36/* Test the constraint on the upper bound for next work */37BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)38{39const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);40int64_t nLastRetargetTime = 1231006505; // Block #041CBlockIndex pindexLast;42pindexLast.nHeight = 2015;43pindexLast.nTime = 1233061996; // Block #201544pindexLast.nBits = 0x1d00ffff;45unsigned int expected_nbits = 0x1d00ffffU;46BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);47BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));48}50/* Test the constraint on the lower bound for actual time taken */51BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)52{53const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);54int64_t nLastRetargetTime = 1279008237; // Block #6652855CBlockIndex pindexLast;56pindexLast.nHeight = 68543;57pindexLast.nTime = 1279297671; // Block #6854358pindexLast.nBits = 0x1c05a3f4;59unsigned int expected_nbits = 0x1c0168fdU;60BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);61BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));62// Test that reducing nbits further would not be a PermittedDifficultyTransition.63unsigned int invalid_nbits = expected_nbits-1;64BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));65}67/* Test the constraint on the upper bound for actual time taken */68BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)69{70const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);71int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time72CBlockIndex pindexLast;73pindexLast.nHeight = 46367;74pindexLast.nTime = 1269211443; // Block #4636775pindexLast.nBits = 0x1c387f6f;76unsigned int expected_nbits = 0x1d00e1fdU;77BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), expected_nbits);78BOOST_CHECK(PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, expected_nbits));79// Test that increasing nbits further would not be a PermittedDifficultyTransition.80unsigned int invalid_nbits = expected_nbits+1;81BOOST_CHECK(!PermittedDifficultyTransition(chainParams->GetConsensus(), pindexLast.nHeight+1, pindexLast.nBits, invalid_nbits));82}84BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_negative_target)85{86const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();87uint256 hash;88unsigned int nBits;89nBits = UintToArith256(consensus.powLimit).GetCompact(true);90hash = uint256{1};91BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));92}94BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_overflow_target)95{96const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();97uint256 hash;98unsigned int nBits{~0x00800000U};99hash = uint256{1};100BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));101}103BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_too_easy_target)104{105const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();106uint256 hash;107unsigned int nBits;108arith_uint256 nBits_arith = UintToArith256(consensus.powLimit);109nBits_arith *= 2;110nBits = nBits_arith.GetCompact();111hash = uint256{1};112BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));113}115BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_biger_hash_than_target)116{117const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();118uint256 hash;119unsigned int nBits;120arith_uint256 hash_arith = UintToArith256(consensus.powLimit);121nBits = hash_arith.GetCompact();122hash_arith *= 2; // hash > nBits123hash = ArithToUint256(hash_arith);124BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));125}127BOOST_AUTO_TEST_CASE(CheckProofOfWork_test_zero_target)128{129const auto consensus = CreateChainParams(*m_node.args, ChainType::MAIN)->GetConsensus();130uint256 hash;131unsigned int nBits;132arith_uint256 hash_arith{0};133nBits = hash_arith.GetCompact();134hash = ArithToUint256(hash_arith);135BOOST_CHECK(!CheckProofOfWork(hash, nBits, consensus));136}138BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)139{140const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);141std::vector<CBlockIndex> blocks(10000);142for (int i = 0; i < 10000; i++) {143blocks[i].pprev = i ? &blocks[i - 1] : nullptr;144blocks[i].nHeight = i;145blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;146blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */147blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);148}150for (int j = 0; j < 1000; j++) {151CBlockIndex *p1 = &blocks[m_rng.randrange(10000)];152CBlockIndex *p2 = &blocks[m_rng.randrange(10000)];153CBlockIndex *p3 = &blocks[m_rng.randrange(10000)];155int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());156BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());157}158}160void sanity_check_chainparams(const ArgsManager& args, ChainType chain_type)161{162const auto chainParams = CreateChainParams(args, chain_type);163const auto consensus = chainParams->GetConsensus();165// hash genesis is correct166BOOST_CHECK_EQUAL(consensus.hashGenesisBlock, chainParams->GenesisBlock().GetHash());168// target timespan is an even multiple of spacing169BOOST_CHECK_EQUAL(consensus.nPowTargetTimespan % consensus.nPowTargetSpacing, 0);171// genesis nBits is positive, doesn't overflow and is lower than powLimit172arith_uint256 pow_compact;173bool neg, over;174pow_compact.SetCompact(chainParams->GenesisBlock().nBits, &neg, &over);175BOOST_CHECK(!neg);176BOOST_CHECK(pow_compact != 0);177BOOST_CHECK(!over);178BOOST_CHECK(UintToArith256(consensus.powLimit) >= pow_compact);180// check max target * 4*nPowTargetTimespan doesn't overflow -- see pow.cpp:CalculateNextWorkRequired()181if (!consensus.fPowNoRetargeting) {182arith_uint256 targ_max{UintToArith256(uint256{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"})};183targ_max /= consensus.nPowTargetTimespan*4;184BOOST_CHECK(UintToArith256(consensus.powLimit) < targ_max);185}186}188BOOST_AUTO_TEST_CASE(ChainParams_MAIN_sanity)189{190sanity_check_chainparams(*m_node.args, ChainType::MAIN);191}193BOOST_AUTO_TEST_CASE(ChainParams_REGTEST_sanity)194{195sanity_check_chainparams(*m_node.args, ChainType::REGTEST);196}198BOOST_AUTO_TEST_CASE(ChainParams_TESTNET_sanity)199{200sanity_check_chainparams(*m_node.args, ChainType::TESTNET);201}203BOOST_AUTO_TEST_CASE(ChainParams_TESTNET4_sanity)204{205sanity_check_chainparams(*m_node.args, ChainType::TESTNET4);206}208BOOST_AUTO_TEST_CASE(ChainParams_SIGNET_sanity)209{210sanity_check_chainparams(*m_node.args, ChainType::SIGNET);211}213BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.