Inside Bitcoin
Opening the selected file…
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) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <pow.h>8#include <arith_uint256.h>9#include <chain.h>10#include <primitives/block.h>11#include <uint256.h>12#include <util/check.h>14unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)15{16assert(pindexLast != nullptr);17unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();19// Only change once per difficulty adjustment interval20if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)21{22if (params.fPowAllowMinDifficultyBlocks)23{24// Special difficulty rule for testnet:25// If the new block's timestamp is more than 2* 10 minutes26// then it MUST be a min-difficulty block.27if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)28return nProofOfWorkLimit;29else30{31// Return the last non-special-min-difficulty-rules-block32const CBlockIndex* pindex = pindexLast;33while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)34pindex = pindex->pprev;35return pindex->nBits;36}37}38return pindexLast->nBits;39}41// Go back by what we want to be 14 days worth of blocks42int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);43assert(nHeightFirst >= 0);44const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);45assert(pindexFirst);47return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);48}50unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)51{52if (params.fPowNoRetargeting)53return pindexLast->nBits;55// Limit adjustment step56int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;57if (nActualTimespan < params.nPowTargetTimespan/4)58nActualTimespan = params.nPowTargetTimespan/4;59if (nActualTimespan > params.nPowTargetTimespan*4)60nActualTimespan = params.nPowTargetTimespan*4;62// Retarget63const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);64arith_uint256 bnNew;66// Special difficulty rule for Testnet467if (params.enforce_BIP94) {68// Here we use the first block of the difficulty period. This way69// the real difficulty is always preserved in the first block as70// it is not allowed to use the min-difficulty exception.71int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);72const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);73bnNew.SetCompact(pindexFirst->nBits);74} else {75bnNew.SetCompact(pindexLast->nBits);76}78bnNew *= nActualTimespan;79bnNew /= params.nPowTargetTimespan;81if (bnNew > bnPowLimit)82bnNew = bnPowLimit;84return bnNew.GetCompact();85}87// Check that on difficulty adjustments, the new difficulty does not increase88// or decrease beyond the permitted limits.89bool PermittedDifficultyTransition(const Consensus::Params& params, int64_t height, uint32_t old_nbits, uint32_t new_nbits)90{91if (params.fPowAllowMinDifficultyBlocks) return true;93if (height % params.DifficultyAdjustmentInterval() == 0) {94int64_t smallest_timespan = params.nPowTargetTimespan/4;95int64_t largest_timespan = params.nPowTargetTimespan*4;97const arith_uint256 pow_limit = UintToArith256(params.powLimit);98arith_uint256 observed_new_target;99observed_new_target.SetCompact(new_nbits);101// Calculate the largest difficulty value possible:102arith_uint256 largest_difficulty_target;103largest_difficulty_target.SetCompact(old_nbits);104largest_difficulty_target *= largest_timespan;105largest_difficulty_target /= params.nPowTargetTimespan;107if (largest_difficulty_target > pow_limit) {108largest_difficulty_target = pow_limit;109}111// Round and then compare this new calculated value to what is112// observed.113arith_uint256 maximum_new_target;114maximum_new_target.SetCompact(largest_difficulty_target.GetCompact());115if (maximum_new_target < observed_new_target) return false;117// Calculate the smallest difficulty value possible:118arith_uint256 smallest_difficulty_target;119smallest_difficulty_target.SetCompact(old_nbits);120smallest_difficulty_target *= smallest_timespan;121smallest_difficulty_target /= params.nPowTargetTimespan;123if (smallest_difficulty_target > pow_limit) {124smallest_difficulty_target = pow_limit;125}127// Round and then compare this new calculated value to what is128// observed.129arith_uint256 minimum_new_target;130minimum_new_target.SetCompact(smallest_difficulty_target.GetCompact());131if (minimum_new_target > observed_new_target) return false;132} else if (old_nbits != new_nbits) {133return false;134}135return true;136}138// Bypasses the actual proof of work check during fuzz testing with a simplified validation checking whether139// the most significant bit of the last byte of the hash is set.140bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)141{142if (EnableFuzzDeterminism()) return (hash.data()[31] & 0x80) == 0;143return CheckProofOfWorkImpl(hash, nBits, params);144}146std::optional<arith_uint256> DeriveTarget(unsigned int nBits, const uint256 pow_limit)147{148bool fNegative;149bool fOverflow;150arith_uint256 bnTarget;152bnTarget.SetCompact(nBits, &fNegative, &fOverflow);154// Check range155if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(pow_limit))156return {};158return bnTarget;159}161bool CheckProofOfWorkImpl(uint256 hash, unsigned int nBits, const Consensus::Params& params)162{163auto bnTarget{DeriveTarget(nBits, params.powLimit)};164if (!bnTarget) return false;166// Check proof of work matches claimed amount167if (UintToArith256(hash) > bnTarget)168return false;170return true;171}