From 0100907ca168c53e8fe044bdda396f308825162c Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Fri, 3 May 2024 10:05:22 +0900 Subject: [PATCH] testnet: Add Testnet4 difficulty adjustment rules fix --- src/pow.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/pow.cpp b/src/pow.cpp index 1e8d53de8b..50de8946be 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -61,7 +61,19 @@ unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nF // Retarget const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); arith_uint256 bnNew; - bnNew.SetCompact(pindexLast->nBits); + + // Special difficulty rule for Testnet4 + if (params.enforce_BIP94) { + // Here we use the first block of the difficulty period. This way + // the real difficulty is always preserved in the first block as + // it is not allowed to use the min-difficulty exception. + int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1); + const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst); + bnNew.SetCompact(pindexFirst->nBits); + } else { + bnNew.SetCompact(pindexLast->nBits); + } + bnNew *= nActualTimespan; bnNew /= params.nPowTargetTimespan;