24 return pindexLast->
nBits;
29 int64_t nActualTimespan = 0;
30 int64_t LastBlockTime = 0;
31 int64_t PastBlocksMin = 24;
32 int64_t PastBlocksMax = 24;
33 int64_t CountBlocks = 0;
38 if (BlockLastSolved ==
nullptr || BlockLastSolved->
nHeight == 0 || BlockLastSolved->
nHeight < PastBlocksMin) {
45 const int64_t& nTargetTimespan = consensus.
TargetTimespan(fTimeV2);
47 int64_t nActualSpacing = 0;
50 if (nActualSpacing < 0)
65 bnNew *= ((nInterval - 1) * consensus.
nTargetSpacing + nActualSpacing + nActualSpacing);
68 if (bnNew <= 0 || bnNew > bnTargetLimit)
69 bnNew = bnTargetLimit;
74 for (
unsigned int i = 1; BlockReading && BlockReading->
nHeight > 0; i++) {
75 if (PastBlocksMax > 0 && i > PastBlocksMax) {
80 if (CountBlocks <= PastBlocksMin) {
81 if (CountBlocks == 1) {
84 PastDifficultyAverage = ((PastDifficultyAveragePrev * CountBlocks) + (
arith_uint256().
SetCompact(BlockReading->
nBits))) / (CountBlocks + 1);
86 PastDifficultyAveragePrev = PastDifficultyAverage;
89 if (LastBlockTime > 0) {
90 int64_t Diff = (LastBlockTime - BlockReading->
GetBlockTime());
91 nActualTimespan += Diff;
95 if (BlockReading->
pprev ==
nullptr) {
99 BlockReading = BlockReading->
pprev;
104 int64_t _nTargetTimespan = CountBlocks * consensus.
nTargetSpacing;
106 if (nActualTimespan < _nTargetTimespan / 3)
107 nActualTimespan = _nTargetTimespan / 3;
108 if (nActualTimespan > _nTargetTimespan * 3)
109 nActualTimespan = _nTargetTimespan * 3;
112 bnNew *= nActualTimespan;
113 bnNew /= _nTargetTimespan;
115 if (bnNew > powLimit) {
128 bnTarget.
SetCompact(nBits, &fNegative, &fOverflow);
132 return error(
"CheckProofOfWork() : nBits below minimum work");
136 return error(
"CheckProofOfWork() : hash doesn't match nBits");
147 if (fNegative || fOverflow || bnTarget.
IsNull())
153 return (~bnTarget / (bnTarget + 1)) + 1;
arith_uint256 UintToArith256(const uint256 &a)
const arith_uint256 ARITH_UINT256_ZERO
const CChainParams & Params()
Return the currently selected parameters.
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
int64_t GetBlockTime() const
int nHeight
height of the entry in the chain. The genesis block has height 0
const Consensus::Params & GetConsensus() const
bool IsRegTestNet() const
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
uint32_t GetCompact(bool fNegative=false) const
bool CheckProofOfWork(uint256 hash, unsigned int nBits)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
arith_uint256 GetBlockProof(const CBlockIndex &block)
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock)
Parameters that influence chain consensus.
uint256 ProofOfStakeLimit(const bool fV2) const
bool IsTimeProtocolV2(const int nHeight) const
int64_t TargetTimespan(const bool fV2=true) const
bool NetworkUpgradeActive(int nHeight, Consensus::UpgradeIndex idx) const
Returns true if the given network upgrade is active as of the given block height.
bool error(const char *fmt, const Args &... args)