From f43fe1e2831b0f971d54caef59f15f49f1064984 Mon Sep 17 00:00:00 2001 From: Jeremiah Buddenhagen Date: Tue, 19 Dec 2017 15:28:12 -0800 Subject: [PATCH] More difficulty adjustment progress? --- src/chainparams.cpp | 2 +- src/consensus/params.h | 9 +++++---- src/pow.cpp | 10 ++++++++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 4731bd5ce..2ee4ff9ce 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -110,7 +110,7 @@ public: consensus.nPowTargetSpacing = 40; // 40s block time // V1 consensus.nTargetTimespan_Version1 = 60 * 60; - consensus.nInterval_Version1 = 60 * 60 / 40; + consensus.nInterval_Version1 = consensus.nTargetTimespan_Version1 / consensus.nPowTargetSpacing; consensus.nMaxAdjustUp_Version1 = 75; consensus.nMaxAdjustDown_Version1 = 300; consensus.nAveragingInterval_Version1 = consensus.nInterval_Version1; diff --git a/src/consensus/params.h b/src/consensus/params.h index 269918c1b..7ce770fae 100644 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -94,18 +94,19 @@ struct Params { if (height < nHeight_Difficulty_Version3) return averagingTargetTimespan * (100 + nMaxAdjustDown_Version2) / 100; // V3 - return TargetTimespan(height) * (100 + nMaxAdjustDown_Version3) / 100; + return averagingTargetTimespan * (100 + nMaxAdjustDown_Version3) / 100; } int64_t MinActualTimespan(int height) const { + const int64_t averagingTargetTimespan = AveragingInterval(height) * nPowTargetSpacing; // V1 if (height < nHeight_Difficulty_Version2) - return TargetTimespan(height) * (100 - nMaxAdjustUp_Version1) / 100; + return averagingTargetTimespan * (100 - nMaxAdjustUp_Version1) / 100; // V2 if (height < nHeight_Difficulty_Version3) - return TargetTimespan(height) * (100 - nMaxAdjustUp_Version2) / 100; + return averagingTargetTimespan * (100 - nMaxAdjustUp_Version2) / 100; // V3 - return TargetTimespan(height) * (100 - nMaxAdjustUp_Version3) / 100; + return averagingTargetTimespan * (100 - nMaxAdjustUp_Version3) / 100; } int64_t AveragingInterval(int height) const { diff --git a/src/pow.cpp b/src/pow.cpp index b50531ca4..caa2bcc16 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -24,13 +24,13 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead // Special difficulty rule for testnet: // If the new block's timestamp is more than 2* 10 minutes // then allow mining of a min-difficulty block. - if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2) + if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.TargetTimespan(pindexLast->nHeight+1)*2) return nProofOfWorkLimit; else { // Return the last non-special-min-difficulty-rules-block const CBlockIndex* pindex = pindexLast; - while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval(pindex->nHeight) != 0 && pindex->nBits == nProofOfWorkLimit) + while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval(pindex->nHeight+1) != 0 && pindex->nBits == nProofOfWorkLimit) pindex = pindex->pprev; return pindex->nBits; } @@ -89,6 +89,12 @@ unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nF if (bnNew > bnPowLimit) bnNew = bnPowLimit; + /// debug print + LogPrintf("GetNextWorkRequired RETARGET\n"); + LogPrintf("Params().TargetTimespan() = %d nActualTimespan = %d\n", params.TargetTimespan(pindexLast->nHeight+1), nActualTimespan); + LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString()); + LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString()); + return bnNew.GetCompact(); }