mining: use steady clock for waitNext timeout

bitcoin/bitcoin: PR #36870

1 minor finding · 2 suggestions

Findings

Link

[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).

Details and suggested fix

GetNextWorkRequired() applies the 20-minute exception only at non-adjustment heights. At adjustment heights, the retarget calculation does not read the candidate's timestamp. waitNext() refreshes without checking height, so its refreshed template carries the retarget result there, which need not be minimum difficulty. At non-adjustment heights, a minimum-difficulty template can already have nTime > previous_time + 1200; rolling it further forward does not change the required bits. Conversely, a client interpreting this note as a timestamp cap could clamp that template back to previous_time + 1200. Since submitSolution() preserves nBits, this produces bad-diffbits when the normal required bits differ from the minimum.

Document the timestamp range matching the template's nBits at non-adjustment heights instead of imposing a universal cap. Warn against moving non-minimum bits strictly past the boundary, allow further forward rolling of an already-late minimum-difficulty template, and qualify the waitNext() difficulty guarantee to exclude adjustment heights.


Optional improvements

Link

[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).

Details and suggested fix

The testnet4 test freezes FakeNodeClock three minutes after genesis, preventing the 20-minute refresh, but every waitNext() call uses .timeout = MillisecondsDouble{0}. The old implementation also exits with that input because deadline == now; the IPC timeout checks instead run with advancing node time. Add a short positive-timeout call before advancing the frozen clock, leave fee_threshold at its default, and assert that it returns nullptr. Run the call in a worker with a generous bounded wait. If that wait expires, call interruptWait(), join without holding cs_main, and fail the test so a regression cannot hang the suite.


Link

[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 at 15186686acd2 (commit) (commit).

Details and suggested fix

15186686acd25d70e8f06ad35463be018421f048 (mining: use steady clock for waitNext timeout) also replaces now > tip_time + 20min with NodeClock::now() >= tip_time + 20min + 1s. With sub-second node time, the old comparison can request a refresh before UpdateTime() has a whole-second timestamp strictly past the boundary. The new comparison prevents that early refresh. The follow-up, cac1ef8cdb166255997b0e119b825b85df8baa06 (mining: document strict 20 minute testnet rule in waitNext), only adds the API note.

Explain the timestamp-rounding correction in the first commit's message so reviewers can assess it separately from the timeout fix. Alternatively, keep NodeClock::now() > tip_time + 20min in the first commit and move the >= ... + 1s change and its comment into the second. That split keeps the clock types consistent, preserves the existing whole-second boundary assertions, and leaves the final tree unchanged.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

The original waitNext refresh change was motivated by creating a minimum-difficulty template after 20 minutes on testnet. Its interface documentation stated that guarantee without an adjustment-height qualification. The accompanying test exercised the block after genesis, checked the strict boundary at 20 minutes plus one second, and explicitly noted that it could not test an actual difficulty change because difficulty was already 1.

This establishes the refresh feature's intended ordinary use, but not an explicit decision about retarget heights. The new timestamp guidance elaborates an existing unqualified description; that description alone is insufficient evidence that waitNext was intended to override the adjustment-height consensus rules.

Source 1

Ralph's inferences

The minimum-difficulty outcome necessarily excludes difficulty-adjustment heights as an application of the 20-minute exception. In both pinned versions, GetNextWorkRequired considers that exception only when the candidate height is not an adjustment boundary. Boundary blocks instead use CalculateNextWorkRequired. Testnet4's code explains that preserving real difficulty in the first block of each period is what allows subsequent retargets to use that block as their difficulty baseline.

This is a protocol constraint independent of the timeout clock. At an adjustment height, exceeding the previous block's timestamp by 20 minutes does not itself mandate minimum difficulty. The retarget calculation determines the required bits, even if its result happens to be minimum difficulty.

A refresh notification and a guaranteed minimum-difficulty result have different scopes. Both the base and head waitNext implementations trigger template creation after the time threshold without checking adjustment height; neither thereby changes the consensus difficulty calculation.

The historically supportable qualification is that waitNext refreshes after the threshold, while a minimum-difficulty result follows only where the ordinary testnet exception applies. The timestamp-rolling warning describes retaining non-minimum bits while crossing that exception's strict greater-than-20-minute boundary at a non-adjustment height. It is not a universal prohibition on timestamps beyond that boundary: at adjustment heights, crossing it alone neither requires minimum difficulty nor causes bad-diffbits; a template already carrying the required minimum bits also does not become invalid merely by crossing it. This qualifies the guidance's scope without establishing an explicit historical author decision about the API wording.