20 uint64_t PastBlocksMass = 0;
21 int64_t PastRateActualSeconds = 0;
22 int64_t PastRateTargetSeconds = 0;
23 double PastRateAdjustmentRatio = double(1);
26 double EventHorizonDeviation;
27 double EventHorizonDeviationFast;
28 double EventHorizonDeviationSlow;
37 for (
unsigned int i = 1; BlockReading && BlockReading->
nHeight > 0; i++) {
38 if (PastBlocksMax > 0 && i > PastBlocksMax) {
break; }
44 if(PastDifficultyAverage >= PastDifficultyAveragePrev)
45 PastDifficultyAverage = ((PastDifficultyAverage - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev;
47 PastDifficultyAverage = PastDifficultyAveragePrev - ((PastDifficultyAveragePrev - PastDifficultyAverage) / i);
49 PastDifficultyAveragePrev = PastDifficultyAverage;
53 PastRateAdjustmentRatio = double(1);
54 if (PastRateActualSeconds < 0) { PastRateActualSeconds = 0; }
55 if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
56 PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds);
58 EventHorizonDeviation = 1 + (0.7084 * pow((
double(PastBlocksMass)/
double(28.2)), -1.228));
59 EventHorizonDeviationFast = EventHorizonDeviation;
60 EventHorizonDeviationSlow = 1 / EventHorizonDeviation;
62 if (PastBlocksMass >= PastBlocksMin) {
63 if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast))
64 { assert(BlockReading);
break; }
66 if (BlockReading->
pprev == NULL) { assert(BlockReading);
break; }
67 BlockReading = BlockReading->
pprev;
71 if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
72 bnNew *= PastRateActualSeconds;
73 bnNew /= PastRateTargetSeconds;
86 int64_t nPastBlocks = 24;
89 if (!pindexLast || pindexLast->
nHeight < nPastBlocks) {
96 for (
unsigned int nCountBlocks = 1; nCountBlocks <= nPastBlocks; nCountBlocks++) {
98 if (nCountBlocks == 1) {
99 bnPastTargetAvg = bnTarget;
102 bnPastTargetAvg = (bnPastTargetAvg * nCountBlocks + bnTarget) / (nCountBlocks + 1);
105 if(nCountBlocks != nPastBlocks) {
106 assert(pindex->
pprev);
107 pindex = pindex->
pprev;
117 if (nActualTimespan < nTargetTimespan/3)
118 nActualTimespan = nTargetTimespan/3;
119 if (nActualTimespan > nTargetTimespan*3)
120 nActualTimespan = nTargetTimespan*3;
123 bnNew *= nActualTimespan;
124 bnNew /= nTargetTimespan;
126 if (bnNew > bnPowLimit) {
138 if (pindexLast == NULL)
139 return nProofOfWorkLimit;
150 return nProofOfWorkLimit;
156 pindex = pindex->
pprev;
157 return pindex->
nBits;
160 return pindexLast->
nBits;
165 assert(nHeightFirst >= 0);
207 return pindexLast->
nBits;
210 int64_t nActualTimespan = pindexLast->
GetBlockTime() - nFirstBlockTime;
211 LogPrintf(
" nActualTimespan = %d before bounds\n", nActualTimespan);
223 bnNew *= nActualTimespan;
226 if (bnNew > bnPowLimit)
230 LogPrintf(
"GetNextWorkRequired RETARGET\n");
244 bnTarget.
SetCompact(nBits, &fNegative, &fOverflow);
248 return error(
"CheckProofOfWork(): nBits below minimum work");
252 return error(
"CheckProofOfWork(): hash doesn't match nBits");
263 if (fNegative || fOverflow || bnTarget == 0)
269 return (~bnTarget / (bnTarget + 1)) + 1;
284 return sign * std::numeric_limits<int64_t>::max();
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
std::string ToString() const
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params ¶ms)
bool fPowAllowMinDifficultyBlocks
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=NULL, bool *pfOverflow=NULL)
unsigned int GetNextWorkRequiredBTC(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params ¶ms)
uint32_t GetCompact(bool fNegative=false) const
arith_uint256 UintToArith256(const uint256 &a)
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
static bool error(const char *format)
int64_t nPowTargetSpacing
arith_uint256 GetBlockProof(const CBlockIndex &block)
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
int64_t DifficultyAdjustmentInterval() const
unsigned static int DarkGravityWave(const CBlockIndex *pindexLast, const Consensus::Params ¶ms)
int64_t nPowTargetTimespan
unsigned static int KimotoGravityWell(const CBlockIndex *pindexLast, const Consensus::Params ¶ms)
const CChainParams & Params()
uint64_t GetLow64() const
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
static const std::string MAIN
int64_t GetBlockTime() const
CBlockIndex * pprev
pointer to the index of the predecessor of this block
int nHeight
height of the entry in the chain. The genesis block has height 0
unsigned int bits() const
static const std::string REGTEST