1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 #include "arith_uint256.h"
10 #include "primitives/block.h"
13 unsigned int GetNextWorkRequired(const CBlockIndex
* pindexLast
, const CBlockHeader
*pblock
, const Consensus::Params
& params
)
15 assert(pindexLast
!= nullptr);
16 unsigned int nProofOfWorkLimit
= UintToArith256(params
.powLimit
).GetCompact();
18 // Only change once per difficulty adjustment interval
19 if ((pindexLast
->nHeight
+1) % params
.DifficultyAdjustmentInterval() != 0)
21 if (params
.fPowAllowMinDifficultyBlocks
)
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
;
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
;
37 return pindexLast
->nBits
;
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
);
46 return CalculateNextWorkRequired(pindexLast
, pindexFirst
->GetBlockTime(), params
);
49 unsigned int CalculateNextWorkRequired(const CBlockIndex
* pindexLast
, int64_t nFirstBlockTime
, const Consensus::Params
& params
)
51 if (params
.fPowNoRetargeting
)
52 return pindexLast
->nBits
;
54 // Limit adjustment step
55 int64_t nActualTimespan
= pindexLast
->GetBlockTime() - nFirstBlockTime
;
56 if (nActualTimespan
< params
.nPowTargetTimespan
/4)
57 nActualTimespan
= params
.nPowTargetTimespan
/4;
58 if (nActualTimespan
> params
.nPowTargetTimespan
*4)
59 nActualTimespan
= params
.nPowTargetTimespan
*4;
62 const arith_uint256 bnPowLimit
= UintToArith256(params
.powLimit
);
64 bnNew
.SetCompact(pindexLast
->nBits
);
65 bnNew
*= nActualTimespan
;
66 bnNew
/= params
.nPowTargetTimespan
;
68 if (bnNew
> bnPowLimit
)
71 return bnNew
.GetCompact();
74 bool CheckProofOfWork(uint256 hash
, unsigned int nBits
, const Consensus::Params
& params
)
78 arith_uint256 bnTarget
;
80 bnTarget
.SetCompact(nBits
, &fNegative
, &fOverflow
);
83 if (fNegative
|| bnTarget
== 0 || fOverflow
|| bnTarget
> UintToArith256(params
.powLimit
))
86 // Check proof of work matches claimed amount
87 if (UintToArith256(hash
) > bnTarget
)