mining: use steady clock for waitNext timeout #36870

Open
ViniciusCestarii wants to merge 2 commits from waitnext-steady-clock into master AGit
ViniciusCestarii commented 2026-11-14 13:04:49 +00:00 (Migrated from github.com)

Follow up suggested in https://github.com/bitcoin/bitcoin/pull/36791#discussion_r4568050151.

waitNext measures its timeout and 1-second fee tick with the wall clock, while waitTipChanged uses the steady clock. This means a wall clock jump changes how long waitNext waits, and it never times out while mocktime is frozen.

Use the steady clock for the deadline and tick. NodeClock is kept only for the testnet 20-minute rule, which must compare node time with the tip's timestamp.

Follow up suggested in https://github.com/bitcoin/bitcoin/pull/36791#discussion_r4568050151. `waitNext` measures its timeout and 1-second fee tick with the wall clock, while `waitTipChanged` uses the steady clock. This means a wall clock jump changes how long `waitNext` waits, and it never times out while mocktime is frozen. Use the steady clock for the deadline and tick. `NodeClock` is kept only for the testnet 20-minute rule, which must compare node time with the tip's timestamp.
DrahtBot commented 2026-11-14 13:04:57 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK w0xlt, nervana21, Sjors, enochaz

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36525 (mining: add Block interface, fix template data race by Sjors)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36870 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36870.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36870#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [w0xlt](https://github.com/bitcoin/bitcoin/pull/36870#pullrequestreview-6049692357), [nervana21](https://github.com/bitcoin/bitcoin/pull/36870#issuecomment-7148856430), [Sjors](https://github.com/bitcoin/bitcoin/pull/36870#issuecomment-7166830719), [enochaz](https://github.com/bitcoin/bitcoin/pull/36870#pullrequestreview-6070163142) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36525](https://github.com/bitcoin/bitcoin/pull/36525) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36525.svg"></sub> (mining: add Block interface, fix template data race by Sjors) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
nervana21 commented 2026-11-14 13:09:11 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
First-time contributor

🟠 Minor · The testnet 20-minute note overstates the consensus rule

The new note incorrectly caps timestamp rolling for minimum-difficulty templates and promises minimum difficulty at adjustment heights; no in-tree caller was found that applies this cap.

Location: src/interfaces/mining.h:40 (head).

🔵 Suggestion · Frozen-clock coverage never exercises a positive waitNext() timeout

The existing frozen-clock unit test uses a zero timeout, so it cannot detect the timeout hang that this PR fixes.

Location: src/node/block_template_manager.cpp:119 (head).

🔵 Suggestion · The steady-clock commit leaves the testnet boundary fix unexplained

On sub-second wall clocks, 15186686 also changes the testnet refresh boundary, but its clock-focused message does not explain that separate behavior change.

Location: src/node/block_template_manager.cpp:155 (commit).

Review coverage was incomplete.


Reviewed: cac1ef8cdb16 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:bf798f720ef764c6d822a1885aa53fec433b7e12a3cb60b208eb5b4636279ec7:3329223fb40bbbe44124b0523f0fa08f23650d06836368f60e45a38d2db02971 --> <!-- ralph-base:dbe9eacd17ae1cc24581081613123a40b44a9f31 --> <!-- ralph-head:cac1ef8cdb166255997b0e119b825b85df8baa06 --> <!-- ralph-case:041e87a97252e43ff0c3ae6df123bf311e8ee82e3e34c615f8880c993b1761d5 --> **🟠 Minor · The testnet 20-minute note overstates the consensus rule** The new note incorrectly caps timestamp rolling for minimum-difficulty templates and promises minimum difficulty at adjustment heights; no in-tree caller was found that applies this cap. Location: [`src/interfaces/mining.h:40`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/cac1ef8cdb166255997b0e119b825b85df8baa06/src/interfaces/mining.h#L40>) (head). **🔵 Suggestion · Frozen-clock coverage never exercises a positive `waitNext()` timeout** The existing frozen-clock unit test uses a zero timeout, so it cannot detect the timeout hang that this PR fixes. Location: [`src/node/block_template_manager.cpp:119`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/cac1ef8cdb166255997b0e119b825b85df8baa06/src/node/block_template_manager.cpp#L119>) (head). **🔵 Suggestion · The steady-clock commit leaves the testnet boundary fix unexplained** On sub-second wall clocks, `15186686` also changes the testnet refresh boundary, but its clock-focused message does not explain that separate behavior change. Location: src/node/block_template_manager.cpp:155 (commit). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`cac1ef8cdb16`](<https://git.fish.foo/bitcoin/bitcoin/commit/cac1ef8cdb166255997b0e119b825b85df8baa06>) · [Review report](<https://review.fish.foo/report/36870-cac1ef8cdb166255997b0e119b825b85df8baa06-3034-3.html>) · [Previous reports](<https://review.fish.foo/traces/36870/>) [permalink](<https://review.fish.foo/report/36870-cac1ef8cdb166255997b0e119b825b85df8baa06-3034-3.html>)
w0xlt commented 2026-11-16 20:29:55 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
nervana21 commented 2026-11-17 23:41:58 +00:00 (Migrated from github.com)

tACK a02c31ca22

Nit. #36862 has been merged, so a rebase and/or a description tweak would ease review.

tACK a02c31ca22e7c76fdd737d36b16140bbb618c140 Nit. #36862 has been merged, so a rebase and/or a description tweak would ease review.
enochaz (Migrated from github.com) reviewed 2026-11-21 19:48:25 +00:00
enochaz (Migrated from github.com) left a comment

tACK a02c31c

I first thought MockableSteadyClock would be better here, to match #36922 (which uses it for the gbt cache age) and so tests could still control the timeout. But deadline and now + tick go into wait_until, which waits in real time, so a mocked clock could drift from the actual wait. steady_clock is right here, and it matches WaitTipChanged.

nit: the rationale from the PR description would be useful in the commit body.

This will conflict trivially with my #36940 on the deadline line.

tACK a02c31c I first thought `MockableSteadyClock` would be better here, to match #36922 (which uses it for the gbt cache age) and so tests could still control the timeout. But deadline and `now + tick` go into `wait_until`, which waits in real time, so a mocked clock could drift from the actual wait. `steady_clock` is right here, and it matches `WaitTipChanged`. nit: the rationale from the PR description would be useful in the commit body. This will conflict trivially with my #36940 on the deadline line.
Sjors (Migrated from github.com) reviewed 2026-11-23 16:08:45 +00:00
Sjors (Migrated from github.com) left a comment

Code review a02c31ca22

The rounding issue in my inline comment also reminds me that we should probably document that timestamp rolling needs extra care on testnet3 and testnet4.

E.g. a miner could get a template a few seconds before the rule kicks in and find a solution before waitNext() returns, which submitSolution() would reject.

/**
 * Block header. Its nTime is a suggested timestamp; submitSolution()
 * takes the final one.
 *
 * @note On testnet3 and testnet4 a block more than 20 minutes after the
 *       previous block must use the minimum difficulty, and nBits are
 *       fixed for this template. Unless nTime is already past that point,
 *       it must never be rolled more than 20 minutes past the previous
 *       block's time, or the block is invalid (bad-diffbits). waitNext()
 *       returns a minimum difficulty template once that point is reached.
 */
virtual CBlockHeader getBlockHeader() = 0;

IIUC testnet5 gets rid of this rule:

Code review a02c31ca22e7c76fdd737d36b16140bbb618c140 The rounding issue in my inline comment also reminds me that we should probably document that timestamp rolling needs extra care on testnet3 and testnet4. E.g. a miner could get a template a few seconds before the rule kicks in and find a solution before waitNext() returns, which `submitSolution()` would reject. ```cpp /** * Block header. Its nTime is a suggested timestamp; submitSolution() * takes the final one. * * @note On testnet3 and testnet4 a block more than 20 minutes after the * previous block must use the minimum difficulty, and nBits are * fixed for this template. Unless nTime is already past that point, * it must never be rolled more than 20 minutes past the previous * block's time, or the block is invalid (bad-diffbits). waitNext() * returns a minimum difficulty template once that point is reached. */ virtual CBlockHeader getBlockHeader() = 0; ``` IIUC testnet5 gets rid of this rule:
Sjors (Migrated from github.com) commented 2026-11-23 15:05:42 +00:00

In a02c31ca22 mining: use steady clock for waitNext timeout: @maflcko, @sedited, @ryanofsky should we prefer SteadyClock over std::chrono::steady_clock in new code? It's nice to have all the clocks defined in src/util/time.h.

In a02c31ca22e7c76fdd737d36b16140bbb618c140 _mining: use steady clock for waitNext timeout_: @maflcko, @sedited, @ryanofsky should we prefer `SteadyClock` over `std::chrono::steady_clock` in new code? It's nice to have all the clocks defined in `src/util/time.h`.
Sjors (Migrated from github.com) commented 2026-11-23 15:57:45 +00:00

The testnet difficulty adjustment rule actually kicks in at 20 minutes + 1 second. This accidentally worked correctly before, but now needs:

// The difficulty rule requires a block timestamp strictly more
// than 20 minutes after the tip.
const NodeClock::time_point tip_time{std::chrono::seconds{m_chainman.ActiveChain().Tip()->GetBlockTime()}};
   if (NodeClock::now() >= tip_time + 20min + 1s) {
The testnet difficulty adjustment rule actually kicks in at 20 minutes + 1 second. This accidentally worked correctly before, but now needs: ```cpp // The difficulty rule requires a block timestamp strictly more // than 20 minutes after the tip. const NodeClock::time_point tip_time{std::chrono::seconds{m_chainman.ActiveChain().Tip()->GetBlockTime()}}; if (NodeClock::now() >= tip_time + 20min + 1s) { ```
w0xlt (Migrated from github.com) reviewed 2026-11-24 00:22:48 +00:00
w0xlt (Migrated from github.com) left a comment
ACK a02c31ca22e7c76fdd737d36b16140bbb618c140
ViniciusCestarii commented 2026-11-24 12:32:48 +00:00 (Migrated from github.com)

Thanks for the reviews! Force-pushed cac1ef8cdb addressing @Sjors https://github.com/bitcoin/bitcoin/pull/36870#pullrequestreview-6023988836 fixing the testnet3 and testnet4 adjustment rule, use SteadyClock and document timestamp rolling. Also addressed @enochaz https://github.com/bitcoin/bitcoin/pull/36870#pullrequestreview-6007764096 to add PR description to the commit body.

Also added Co-Authors

Thanks for the reviews! Force-pushed cac1ef8cdb166255997b0e119b825b85df8baa06 addressing @Sjors https://github.com/bitcoin/bitcoin/pull/36870#pullrequestreview-6023988836 fixing the testnet3 and testnet4 adjustment rule, use `SteadyClock` and document timestamp rolling. Also addressed @enochaz https://github.com/bitcoin/bitcoin/pull/36870#pullrequestreview-6007764096 to add PR description to the commit body. Also added Co-Authors
ViniciusCestarii (Migrated from github.com) reviewed 2026-11-24 12:37:05 +00:00
ViniciusCestarii (Migrated from github.com) commented 2026-11-24 12:37:05 +00:00

Thanks for finding that! Fixed in cac1ef8cdb

Thanks for finding that! Fixed in cac1ef8cdb166255997b0e119b825b85df8baa06
ViniciusCestarii (Migrated from github.com) reviewed 2026-11-24 12:37:39 +00:00
ViniciusCestarii (Migrated from github.com) commented 2026-11-24 12:37:39 +00:00

I agree, changed to SteadyClock in cac1ef8cdb

I agree, changed to SteadyClock in cac1ef8cdb166255997b0e119b825b85df8baa06
w0xlt (Migrated from github.com) reviewed 2026-11-25 00:32:10 +00:00
w0xlt (Migrated from github.com) left a comment

reACK cac1ef8cdb

reACK cac1ef8cdb166255997b0e119b825b85df8baa06
nervana21 commented 2026-11-25 00:46:11 +00:00 (Migrated from github.com)

reACK cac1ef8cdb

reACK cac1ef8cdb166255997b0e119b825b85df8baa06
Sjors commented 2026-11-25 15:25:52 +00:00 (Migrated from github.com)

ACK cac1ef8cdb

Ironically Ralph's review is hard to read by agents because the site blocks bots.

The testnet 20-minute note overstates the consensus rule

This is true, but the instruction to never roll testnet3 and testnet4 blocks more than 20 minutes is fine despite that.

Frozen-clock coverage never exercises a positive waitNext() timeout

This sounds like adding test coverage for tests, I would ignore it.

ACK cac1ef8cdb166255997b0e119b825b85df8baa06 Ironically Ralph's review is hard to read by agents because the site blocks bots. > The testnet 20-minute note overstates the consensus rule This is true, but the instruction to never roll testnet3 and testnet4 blocks more than 20 minutes is fine despite that. > Frozen-clock coverage never exercises a positive waitNext() timeout This sounds like adding test coverage for tests, I would ignore it.
Sjors commented 2026-11-25 15:56:51 +00:00 (Migrated from github.com)

While reviewing this, my agent noticed https://github.com/bitcoin/bitcoin/issues/36983. It's easier to fix it on top of this PR, but that can be a followup.

While reviewing this, my agent noticed https://github.com/bitcoin/bitcoin/issues/36983. It's easier to fix it on top of this PR, but that can be a followup.
ViniciusCestarii commented 2026-11-25 19:18:48 +00:00 (Migrated from github.com)

Thanks for the reviews!

While reviewing this, my agent noticed #36983. It's easier to fix it on top of this PR, but that can be a followup.

Nice! I agree that it is better addressed in a followup. Happy to review if you open it.

Thanks for the reviews! > While reviewing this, my agent noticed #36983. It's easier to fix it on top of this PR, but that can be a followup. Nice! I agree that it is better addressed in a followup. Happy to review if you open it.
enochaz (Migrated from github.com) reviewed 2026-11-26 07:39:14 +00:00
enochaz (Migrated from github.com) left a comment
ACK https://github.com/bitcoin/bitcoin/pull/36870/changes/cac1ef8cdb166255997b0e119b825b85df8baa06
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36870
No description provided.