mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-11 16:17:54 +02:00
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:
@@ -953,7 +953,7 @@ public:
|
||||
return WaitTipChanged(chainman(), notifications(), current_tip, timeout);
|
||||
}
|
||||
|
||||
std::unique_ptr<BlockTemplate> createNewBlock(const BlockCreateOptions& options) override
|
||||
std::unique_ptr<BlockTemplate> createNewBlock(const BlockCreateOptions& options, bool cooldown) override
|
||||
{
|
||||
// Reject too-small values instead of clamping so callers don't silently
|
||||
// end up mining with different options than requested. This matches the
|
||||
@@ -966,7 +966,24 @@ public:
|
||||
}
|
||||
|
||||
// Ensure m_tip_block is set so consumers of BlockTemplate can rely on that.
|
||||
if (!waitTipChanged(uint256::ZERO, MillisecondsDouble::max())) return {};
|
||||
std::optional<BlockRef> maybe_tip{waitTipChanged(uint256::ZERO, MillisecondsDouble::max())};
|
||||
|
||||
if (!maybe_tip) return {};
|
||||
|
||||
if (cooldown) {
|
||||
// Do not return a template during IBD, because it can have long
|
||||
// pauses and sometimes takes a while to get started. Although this
|
||||
// is useful in general, it's gated behind the cooldown argument,
|
||||
// because on regtest and single miner signets this would wait
|
||||
// forever if no block was mined in the past day.
|
||||
while (chainman().IsInitialBlockDownload()) {
|
||||
maybe_tip = waitTipChanged(maybe_tip->hash, MillisecondsDouble{1000});
|
||||
if (!maybe_tip) return {};
|
||||
}
|
||||
|
||||
// Also wait during the final catch-up moments after IBD.
|
||||
if (!CooldownIfHeadersAhead(chainman(), notifications(), *maybe_tip)) return {};
|
||||
}
|
||||
|
||||
BlockAssembler::Options assemble_options{options};
|
||||
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
|
||||
|
||||
Reference in New Issue
Block a user