From 61fb171430fdf057a8eabaedddaebdb5c0634b89 Mon Sep 17 00:00:00 2001 From: bonkcoins Date: Tue, 20 Oct 2026 09:04:23 -0500 Subject: [PATCH] fix(pow): LWMA bootstrap resets to a realistic difficulty, not diff-1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reported by Andy (sha256-mining.shears.co.uk) reviewing v30.3.0: resetting to powLimit (diff ~0.004) for the first N post-fork blocks lets whoever mines at the fork grind ~300 near-free blocks (~150k PERBUG) in minutes before LWMA converges. Reset instead to a realistic level — powLimit / nLwmaStartDifficulty (default 40000, ~1.3 TH/s @ 2.5-min blocks) — a ~150x drop from the stuck 6.4M so the chain still unsticks immediately, but LWMA takes over from a sane point. Also move the fork height 75730 -> 75760 so no node still on v30.3.0 (which has the diff-1 bootstrap) activates the old rule; everyone rebuilds to v30.3.1 first. (Andy's optional LWMA future-time-limit tightening is a sensible follow-up.) Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01Hi9Fj21YnCdsu73fPDkw6o --- src/consensus/params.h | 5 +++++ src/kernel/chainparams.cpp | 3 ++- src/pow.cpp | 7 ++++++- src/test/pow_tests.cpp | 4 +++- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/consensus/params.h b/src/consensus/params.h index 50473c66c5..a35ea9e1aa 100644 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -125,6 +125,11 @@ struct Params { // retarget. 0 = disabled (keep the legacy Bitcoin retarget). int nLwmaHeight{0}; int64_t nLwmaAveragingWindow{90}; + // Bootstrap difficulty for the first nLwmaAveragingWindow blocks after + // the fork, relative to powLimit (diff-1). Resetting to a realistic + // level (not diff-1) stops the miner present at the fork from grinding + // hundreds of near-free blocks before LWMA converges. + int64_t nLwmaStartDifficulty{40000}; std::chrono::seconds PowTargetSpacing() const { return std::chrono::seconds{nPowTargetSpacing}; diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index 9d361aa689..60f7a14f19 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -91,7 +91,8 @@ class CMainParams : public CChainParams { consensus.powLimit = uint256{"00000ffff0000000000000000000000000000000000000000000000000000000"}; consensus.nPowTargetTimespan = 7 * 24 * 60 * 60; // one week at 5-minute spacing, 2016 blocks consensus.nPowTargetSpacing = 2.5 * 60; - consensus.nLwmaHeight = 75730; // LWMA per-block retarget hard fork + consensus.nLwmaHeight = 75760; // LWMA per-block retarget hard fork + consensus.nLwmaStartDifficulty = 40000; // ~1.3 TH/s @ 2.5-min blocks consensus.nLwmaAveragingWindow = 90; consensus.fPowAllowMinDifficultyBlocks = false; consensus.enforce_BIP94 = false; diff --git a/src/pow.cpp b/src/pow.cpp index aa46915f21..a056d357dc 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -185,7 +185,12 @@ unsigned int LwmaGetNextWorkRequired(const CBlockIndex* pindexLast, const Consen const arith_uint256 powLimit = UintToArith256(params.powLimit); if (height < params.nLwmaHeight + N) { - return powLimit.GetCompact(); + // Restart from a realistic difficulty (powLimit / nLwmaStartDifficulty), + // never easier than diff-1, so the chain unsticks at once without + // handing the fork-height miner a flood of near-free blocks. + arith_uint256 startTarget = powLimit / std::max(1, params.nLwmaStartDifficulty); + if (startTarget > powLimit) startTarget = powLimit; + return startTarget.GetCompact(); } arith_uint256 sumTarget(0); diff --git a/src/test/pow_tests.cpp b/src/test/pow_tests.cpp index 9b40e42655..be80a6a82a 100644 --- a/src/test/pow_tests.cpp +++ b/src/test/pow_tests.cpp @@ -217,6 +217,7 @@ BOOST_AUTO_TEST_CASE(lwma_difficulty) const int N = 20; params.nLwmaHeight = FORK; params.nLwmaAveragingWindow = N; + params.nLwmaStartDifficulty = 40000; const int64_t T = params.nPowTargetSpacing; const arith_uint256 powLimit = UintToArith256(params.powLimit); @@ -239,8 +240,9 @@ BOOST_AUTO_TEST_CASE(lwma_difficulty) // stuck chain unsticks immediately. { auto blocks = make_chain(T, startBits); + arith_uint256 expectStart = powLimit / params.nLwmaStartDifficulty; unsigned int got = LwmaGetNextWorkRequired(&(*blocks)[FORK], params); - BOOST_CHECK_EQUAL(got, powLimit.GetCompact()); + BOOST_CHECK_EQUAL(got, expectStart.GetCompact()); } // Slow blocks (3x target spacing) after a full window -> difficulty drops // (target grows ~3x); fast blocks (1/4 spacing) -> difficulty rises.