Changeset View
Changeset View
Standalone View
Standalone View
src/pow.cpp
Show First 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | if (params.fPowAllowMinDifficultyBlocks) { | ||||
pindex = pindex->pprev; | pindex = pindex->pprev; | ||||
} | } | ||||
nBits = pindex->nBits; | nBits = pindex->nBits; | ||||
} else { | } else { | ||||
nBits = pindexPrev->nBits; | nBits = pindexPrev->nBits; | ||||
} | } | ||||
arith_uint256 nTarget; | |||||
nTarget.SetCompact(nBits); | |||||
// Long-term block production time stabiliser that should rarely | |||||
// trigger. Should miners produce blocks considerably faster than | |||||
// target spacing but collude to fudge timestamps to stay above the 30 | |||||
// minute window, ensure difficulty will rise to compensate anyway. | |||||
if (nHeight > 2016) { | |||||
const CBlockIndex *p2016 = pindexPrev->GetAncestor(nHeight - 2017); | |||||
int64_t t = pindexPrev->GetBlockTime() - p2016->GetBlockTime(); | |||||
// If too fast, increase the target by 1/256. Std deviation over | |||||
// 2016 blocks is 2.2%. | |||||
if (t < params.nPowTargetSpacing * 2016 * 95 / 100) { | |||||
nTarget -= (nTarget >> 8); | |||||
} | |||||
} | |||||
// Determine the MTP difference - the difference between the MTP | // Determine the MTP difference - the difference between the MTP | ||||
// of the previous block, and the block 6 blocks earlier | // of the previous block, and the block 6 blocks earlier | ||||
const CBlockIndex *pindex6 = pindexPrev->GetAncestor(nHeight - 7); | const CBlockIndex *pindex6 = pindexPrev->GetAncestor(nHeight - 7); | ||||
assert(pindex6); | assert(pindex6); | ||||
int64_t mtp6blocks = | int64_t mtp6blocks = | ||||
pindexPrev->GetMedianTimePast() - pindex6->GetMedianTimePast(); | pindexPrev->GetMedianTimePast() - pindex6->GetMedianTimePast(); | ||||
// If too fast (< 30 mins), increase the target by 1/256. | // If too fast (< 30 mins), increase the target by 1/256. | ||||
if (mtp6blocks < params.nPowTargetSpacing * 30 / 10) { | if (mtp6blocks < params.nPowTargetSpacing * 30 / 10) { | ||||
arith_uint256 nTarget; | |||||
nTarget.SetCompact(nBits); | |||||
nTarget -= (nTarget >> 8); | nTarget -= (nTarget >> 8); | ||||
return nTarget.GetCompact(); | |||||
} | } | ||||
// If too slow (> 128 mins), decrease the target by 1/64. | // If too slow (> 128 mins), decrease the target by 1/64. | ||||
if (mtp6blocks > params.nPowTargetSpacing * 128 / 10) { | else if (mtp6blocks > params.nPowTargetSpacing * 128 / 10) { | ||||
arith_uint256 nTarget; | |||||
nTarget.SetCompact(nBits); | |||||
nTarget += (nTarget >> 6); | nTarget += (nTarget >> 6); | ||||
nBits = nTarget.GetCompact(); | |||||
// We can't go below the minimum target | |||||
if (nBits > nProofOfWorkLimit) nBits = nProofOfWorkLimit; | |||||
return nBits; | |||||
} | } | ||||
// Otherwise difficulty is unchanged | nBits = nTarget.GetCompact(); | ||||
return nBits; | |||||
// We can't go below the minimum target | |||||
return std::min(nBits, nProofOfWorkLimit); | |||||
} | } | ||||
/** | /** | ||||
* Compute the next required proof of work using the legacy Bitcoin difficulty | * Compute the next required proof of work using the legacy Bitcoin difficulty | ||||
* adjustement + Emergency Difficulty Adjustement (EDA). | * adjustement + Emergency Difficulty Adjustement (EDA). | ||||
*/ | */ | ||||
static uint32_t GetNextEDAWorkRequired(const CBlockIndex *pindexPrev, | static uint32_t GetNextEDAWorkRequired(const CBlockIndex *pindexPrev, | ||||
const CBlockHeader *pblock, | const CBlockHeader *pblock, | ||||
▲ Show 20 Lines • Show All 133 Lines • Show Last 20 Lines |