Skip to content

Commit

Permalink
Merge pull request #13 from RSerhii/scypt2_fork
Browse files Browse the repository at this point in the history
Difficulty multiplier adjustment
  • Loading branch information
phoenixkonsole committed Sep 16, 2020
2 parents 687e9d2 + e1f1f20 commit e866e30
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bool IsScrypt2AdjustmentBlocks(const int nLastBlockHeight)
}

const int difficultyAdjustmentBlock = 668075;
const int multiplierAdjustmentBlock = 668912;

bool IsScrypt2SecondAdjustment(const int nLastBlockHeight)
{
Expand Down Expand Up @@ -107,17 +108,25 @@ unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const CBlockH
bnNew *= nActualTimespan;
bnNew /= nTargetTimespan;

// adjust difficulty
if (pindexLast->nHeight >= difficultyAdjustmentBlock) {
// new difficulty can be maximum 50 times higher than previous
const int64_t lastTimespan = pindexLast->GetBlockTime() - pindexLast->pprev->GetBlockTime();
const int timeMultiplier = lastTimespan / params.nPowTargetSpacing;

// new difficulty can be maximum 40 times higher than previous
const int maxDifMultiplier = 40;
arith_uint256 bnLast = arith_uint256().SetCompact(pindexLast->nBits);
if ((bnLast / bnNew) > maxDifMultiplier) {
bnNew = bnLast / maxDifMultiplier;

int difMult = maxDifMultiplier;
if (pindexLast->nHeight >= multiplierAdjustmentBlock && timeMultiplier > 1) {
difMult /= timeMultiplier;
}

if ((bnLast / bnNew) > difMult) {
bnNew = bnLast / difMult;
}

// significantly reduce the difficulty if the last timespan is too large
const int64_t lastTimespan = pindexLast->GetBlockTime() - pindexLast->pprev->GetBlockTime();
const int timeMultiplier = lastTimespan / params.nPowTargetSpacing;
if (timeMultiplier > maxDifMultiplier) {
bnLast *= (timeMultiplier - (maxDifMultiplier / 2));
if (bnLast > bnNew) {
Expand Down

0 comments on commit e866e30

Please sign in to comment.