| 12 | #include <util/check.h> |
| 13 | |
| 14 | unsigned 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 | |
| 50 | unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) |
| 51 | { |
no test coverage detected