From f1ec42fcb0f843f7b3fcc4dc7c8c5e758e53780b 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 1e8d53de8bb87b..ff4885b3419991 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.fPowAllowMinDifficultyBlocks && params.hashGenesisBlock == uint256S("0x000000008d6faa98083fa55742aa82d4ed249bd1bfc3239c706e0a61ef9e3931")) { + // Use the last non-special-min-difficulty-rules-block + const CBlockIndex* pindex = pindexLast; + const unsigned int pow_min{bnPowLimit.GetCompact()}; + while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == pow_min) + pindex = pindex->pprev; + bnNew.SetCompact(pindex->nBits); + } else { + bnNew.SetCompact(pindexLast->nBits); + } + bnNew *= nActualTimespan; bnNew /= params.nPowTargetTimespan;