MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / GetNextWorkRequired

Function GetNextWorkRequired

src/pow.cpp:14–48  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12#include <util/check.h>
13
14unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
15{
16 assert(pindexLast != nullptr);
17 unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
18
19 // Only change once per difficulty adjustment interval
20 if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
21 {
22 if (params.fPowAllowMinDifficultyBlocks)
23 {
24 // Special difficulty rule for testnet:
25 // If the new block's timestamp is more than 2* 10 minutes
26 // then it MUST be a min-difficulty block.
27 if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
28 return nProofOfWorkLimit;
29 else
30 {
31 // Return the last non-special-min-difficulty-rules-block
32 const CBlockIndex* pindex = pindexLast;
33 while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
34 pindex = pindex->pprev;
35 return pindex->nBits;
36 }
37 }
38 return pindexLast->nBits;
39 }
40
41 // Go back by what we want to be 14 days worth of blocks
42 int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
43 assert(nHeightFirst >= 0);
44 const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
45 assert(pindexFirst);
46
47 return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
48}
49
50unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
51{

Callers 6

EXCLUSIVE_LOCKS_REQUIREDFunction · 0.85
pow.cppFile · 0.85
NextEmptyBlockIndexFunction · 0.85
DuplicateInputsFunction · 0.85
UpdateTimeFunction · 0.85
CreateNewBlockMethod · 0.85

Calls 6

UintToArith256Function · 0.85
GetCompactMethod · 0.80
GetBlockTimeMethod · 0.45
GetAncestorMethod · 0.45

Tested by

no test coverage detected