Skip to content

mining: use steady clock for waitNext timeout - #36870

Open
ViniciusCestarii wants to merge 2 commits into
bitcoin:masterfrom
ViniciusCestarii:waitnext-steady-clock
Open

ViniciusCestarii wants to merge 2 commits into
bitcoin:masterfrom
ViniciusCestarii:waitnext-steady-clock

Conversation

@ViniciusCestarii

@ViniciusCestarii ViniciusCestarii commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Follow up suggested in #36791 (comment).

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

DrahtBot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

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.

@nervana21

Copy link
Copy Markdown
Contributor

Concept ACK

1 similar comment
@w0xlt

w0xlt commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@nervana21

Copy link
Copy Markdown
Contributor

tACK a02c31c

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

@enochaz enochaz left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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? So steady_clock seems good here as it matches WaitTipChanged.

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

I think this will conflict trivially with my #36940 on the deadline line.

@Sjors Sjors left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review a02c31c

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. Never roll more
 *       than 20 minutes past the previous block's time on these networks,
 *       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:

Comment thread src/node/block_template_manager.cpp Outdated
// Alternate waiting for a new tip and checking if fees have risen.
// The latter check is expensive so we only run it once per second.
auto now{NodeClock::now()};
auto now{std::chrono::steady_clock::now()};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a02c31c 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, changed to SteadyClock in cac1ef8

Comment thread src/node/block_template_manager.cpp Resolved

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK a02c31c

ViniciusCestarii and others added 2 commits November 24, 2026 09:24
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.

Co-authored-by: Sjors Provoost <[email protected]>
Co-authored-by: Russell Yanofsky <[email protected]>
@ViniciusCestarii

ViniciusCestarii commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews! Force-pushed cac1ef8 addressing @Sjors #36870 (review) fixing the testnet3 and testnet4 adjustment rule, use SteadyClock and document timestamp rolling. Also addressed @enochaz #36870 (review) to add PR description to the commit body.

Also added Co-Authors

@w0xlt w0xlt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reACK cac1ef8

@DrahtBot
DrahtBot requested a review from enochaz November 25, 2026 00:32
@nervana21

Copy link
Copy Markdown
Contributor

reACK cac1ef8

@fanquake
fanquake requested review from ryanofsky and removed request for enochaz November 25, 2026 08:59
@Sjors

Sjors commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

ACK cac1ef8

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

Sjors commented Nov 25, 2026

Copy link
Copy Markdown
Member

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

@ViniciusCestarii

Copy link
Copy Markdown
Contributor Author

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 enochaz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK cac1ef8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants