Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto # 2 : : // Copyright (c) 2009-2018 The Bitcoin Core developers # 3 : : // Distributed under the MIT software license, see the accompanying # 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php. # 5 : : # 6 : : #include <pow.h> # 7 : : # 8 : : #include <arith_uint256.h> # 9 : : #include <chain.h> # 10 : : #include <primitives/block.h> # 11 : : #include <uint256.h> # 12 : : # 13 : : unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) # 14 : 151816 : { # 15 : 151816 : assert(pindexLast != nullptr); # 16 : 0 : unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact(); # 17 : : # 18 : : // Only change once per difficulty adjustment interval # 19 [ + + ]: 151816 : if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0) # 20 : 151810 : { # 21 [ + + ]: 151810 : if (params.fPowAllowMinDifficultyBlocks) # 22 : 151494 : { # 23 : : // Special difficulty rule for testnet: # 24 : : // If the new block's timestamp is more than 2* 10 minutes # 25 : : // then allow mining of a min-difficulty block. # 26 [ + + ]: 151494 : if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2) # 27 : 3982 : return nProofOfWorkLimit; # 28 : 147512 : else # 29 : 147512 : { # 30 : : // Return the last non-special-min-difficulty-rules-block # 31 : 147512 : const CBlockIndex* pindex = pindexLast; # 32 [ + + ][ + + ]: 44887728 : while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) # [ + - ] # 33 : 44740216 : pindex = pindex->pprev; # 34 : 147512 : return pindex->nBits; # 35 : 147512 : } # 36 : 151494 : } # 37 : 316 : return pindexLast->nBits; # 38 : 151810 : } # 39 : : # 40 : : // Go back by what we want to be 14 days worth of blocks # 41 : 6 : int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1); # 42 : 6 : assert(nHeightFirst >= 0); # 43 : 0 : const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst); # 44 : 6 : assert(pindexFirst); # 45 : : # 46 : 0 : return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params); # 47 : 151816 : } # 48 : : # 49 : : unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) # 50 : 14 : { # 51 [ + + ]: 14 : if (params.fPowNoRetargeting) # 52 : 6 : return pindexLast->nBits; # 53 : : # 54 : : // Limit adjustment step # 55 : 8 : int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime; # 56 [ + + ]: 8 : if (nActualTimespan < params.nPowTargetTimespan/4) # 57 : 2 : nActualTimespan = params.nPowTargetTimespan/4; # 58 [ + + ]: 8 : if (nActualTimespan > params.nPowTargetTimespan*4) # 59 : 2 : nActualTimespan = params.nPowTargetTimespan*4; # 60 : : # 61 : : // Retarget # 62 : 8 : const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); # 63 : 8 : arith_uint256 bnNew; # 64 : 8 : bnNew.SetCompact(pindexLast->nBits); # 65 : 8 : bnNew *= nActualTimespan; # 66 : 8 : bnNew /= params.nPowTargetTimespan; # 67 : : # 68 [ + + ]: 8 : if (bnNew > bnPowLimit) # 69 : 2 : bnNew = bnPowLimit; # 70 : : # 71 : 8 : return bnNew.GetCompact(); # 72 : 14 : } # 73 : : # 74 : : bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params) # 75 : 839547 : { # 76 : 839547 : bool fNegative; # 77 : 839547 : bool fOverflow; # 78 : 839547 : arith_uint256 bnTarget; # 79 : : # 80 : 839547 : bnTarget.SetCompact(nBits, &fNegative, &fOverflow); # 81 : : # 82 : : // Check range # 83 [ + + ][ + + ]: 839547 : if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit)) # [ + + ][ - + ] # [ + + ] # 84 : 8 : return false; # 85 : : # 86 : : // Check proof of work matches claimed amount # 87 [ + + ]: 839539 : if (UintToArith256(hash) > bnTarget) # 88 : 463608 : return false; # 89 : : # 90 : 375931 : return true; # 91 : 839539 : }