| 11 | #include <uint256.h> |
| 12 | |
| 13 | unsigned 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 | |
| 49 | unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) |
| 50 | { |