MCPcopy Create free account
hub / github.com/ElementsProject/elements / GetNextWorkRequired

Function GetNextWorkRequired

src/pow.cpp:13–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11#include <uint256.h>
12
13unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
14{
15 assert(pindexLast != nullptr);
16 unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
17
18 // Only change once per difficulty adjustment interval
19 if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
20 {
21 if (params.fPowAllowMinDifficultyBlocks)
22 {
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 if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
27 return nProofOfWorkLimit;
28 else
29 {
30 // Return the last non-special-min-difficulty-rules-block
31 const CBlockIndex* pindex = pindexLast;
32 while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
33 pindex = pindex->pprev;
34 return pindex->nBits;
35 }
36 }
37 return pindexLast->nBits;
38 }
39
40 // Go back by what we want to be 14 days worth of blocks
41 int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
42 assert(nHeightFirst >= 0);
43 const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
44 assert(pindexFirst);
45
46 return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
47}
48
49unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
50{

Callers 5

CheckChallengeFunction · 0.85
FUZZ_TARGET_INITFunction · 0.85
DuplicateInputsFunction · 0.85
UpdateTimeFunction · 0.85
CreateNewBlockMethod · 0.85

Calls 6

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

Tested by 1

FUZZ_TARGET_INITFunction · 0.68