Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
3 changes: 2 additions & 1 deletion src/kernel/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(1, params.nLwmaStartDifficulty);
if (startTarget > powLimit) startTarget = powLimit;
return startTarget.GetCompact();
}

arith_uint256 sumTarget(0);
Expand Down
4 changes: 3 additions & 1 deletion src/test/pow_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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.
Expand Down
Loading