From 3f30d9247254deb4c11768fdc7e2637ffeff54da Mon Sep 17 00:00:00 2001 From: Davide Grilli Date: Fri, 14 Nov 2025 10:35:02 +0100 Subject: [PATCH] fix(pow): simplify testnet difficulty adjustment logic Move the special 20-minute rule check before LWMA calculation for clarity and maintain the same behavior. Remove redundant else block and consolidate the logic for returning last non-special-min-difficulty block. --- src/pow.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/pow.cpp b/src/pow.cpp index 477bf8e..cd01e1e 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -19,6 +19,11 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead if ((pindexLast->nHeight >= 28930) && (pindexLast->nHeight <= 28999)) return nProofOfWorkLimit; + // Special difficulty rule for testnet: If the new block's timestamp is more than 20 minutes + // then allow mining of a min-difficulty block. This rule has priority over LWMA. + if (params.fPowAllowMinDifficultyBlocks && pblock->GetBlockTime() > pindexLast->GetBlockTime() + 20 * 60) + return nProofOfWorkLimit; + // For testnet, use LWMA from beginning to recalculate every block like mainnet if (params.fPowAllowMinDifficultyBlocks && pindexLast->nHeight >= 0) return LwmaCalculateNextWorkRequired(pindexLast, params); @@ -31,19 +36,11 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead { if (params.fPowAllowMinDifficultyBlocks) { - // Special difficulty rule for testnet: - // If the new block's timestamp is more than 20 minutes - // then allow mining of a min-difficulty block. - if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + 20 * 60) - return nProofOfWorkLimit; - else - { - // Return the last non-special-min-difficulty-rules-block - const CBlockIndex* pindex = pindexLast; - while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) - pindex = pindex->pprev; - return pindex->nBits; - } + // Return the last non-special-min-difficulty-rules-block + const CBlockIndex* pindex = pindexLast; + while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) + pindex = pindex->pprev; + return pindex->nBits; } return pindexLast->nBits; }