| 171 | } |
| 172 | |
| 173 | unsigned int BitcoinGetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) |
| 174 | { |
| 175 | assert(pindexLast != nullptr); |
| 176 | unsigned int nProofOfWorkLimit = UintToArith256(params.PowLimit(false)).GetCompact(); |
| 177 | |
| 178 | // Only change once per difficulty adjustment interval |
| 179 | if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0) |
| 180 | { |
| 181 | if (params.fPowAllowMinDifficultyBlocks) |
| 182 | { |
| 183 | // Special difficulty rule for testnet: |
| 184 | // If the new block's timestamp is more than 2* 10 minutes |
| 185 | // then allow mining of a min-difficulty block. |
| 186 | if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2) |
| 187 | return nProofOfWorkLimit; |
| 188 | else |
| 189 | { |
| 190 | // Return the last non-special-min-difficulty-rules-block |
| 191 | const CBlockIndex* pindex = pindexLast; |
| 192 | while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) |
| 193 | pindex = pindex->pprev; |
| 194 | return pindex->nBits; |
| 195 | } |
| 196 | } |
| 197 | return pindexLast->nBits; |
| 198 | } |
| 199 | |
| 200 | // Go back by what we want to be 14 days worth of blocks |
| 201 | int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1); |
| 202 | assert(nHeightFirst >= 0); |
| 203 | const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst); |
| 204 | assert(pindexFirst); |
| 205 | |
| 206 | return BitcoinCalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params); |
| 207 | } |
| 208 | |
| 209 | unsigned int ReduceDifficultyBy(const CBlockIndex* pindexLast, int64_t multiplier, const Consensus::Params& params) { |
| 210 | arith_uint256 target; |
no test coverage detected