mining: add cooldown argument to createNewBlock()

At startup, if the needs to catch up, connected mining clients will
receive a flood of new templates as new blocks are connected.

Fix this by adding a cooldown argument to createNewBlock(). When set
to true, block template creation is briefly paused while the best
header chain is ahead of the tip.

This wait only happens when the best header extends the current tip,
to ignore competing branches.

Additionally, cooldown waits for isInitialBlockDownload() to latch to
false, which happens when there is less than a day of blocks left to sync.

When cooldown is false createNewBlock() returns immediately. The argument
is optional, because many tests are negatively impacted by this
mechanism, and single miner signets could end up stuck if no block
was mined for a day.

The getblocktemplate RPC also opts out, because it would add a delay
to each call.

Fixes #33994
This commit is contained in:
Sjors Provoost
2026-02-04 12:50:31 +01:00
parent cb3473a680
commit a11297a904
11 changed files with 149 additions and 35 deletions

View File

@@ -6282,6 +6282,19 @@ void ChainstateManager::RecalculateBestHeader()
}
}
std::optional<int> ChainstateManager::BlocksAheadOfTip() const
{
LOCK(::cs_main);
const CBlockIndex* best_header{m_best_header};
const CBlockIndex* tip{ActiveChain().Tip()};
// Only consider headers that extend the active tip; ignore competing branches.
if (best_header && tip && best_header->nChainWork > tip->nChainWork &&
best_header->GetAncestor(tip->nHeight) == tip) {
return best_header->nHeight - tip->nHeight;
}
return std::nullopt;
}
bool ChainstateManager::ValidatedSnapshotCleanup(Chainstate& validated_cs, Chainstate& unvalidated_cs)
{
AssertLockHeld(::cs_main);