mining: use steady clock for waitNext timeout

bitcoin/bitcoin: PR #36870

1 suggestion

Optional improvements

Link

[suggestion] · No regression test covers timeout expiry with frozen node time

Existing tests do not combine frozen NodeClock with a positive waitNext timeout, so they do not protect this deadline fix against reverting to NodeClock.

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

Details and suggested fix

The testnet4 unit test freezes node time but supplies zero timeouts; the functional timeout checks use real node time. Add a call in testnet4_miner_tests before advancing its initial tip+3min clock: waitNext({.timeout = MillisecondsDouble{50}, .fee_threshold = MAX_MONEY}), and assert that it returns nullptr. With an unchanged tip and no interruption, the 20-minute condition remains false and MAX_MONEY skips template construction, leaving deadline expiry as the return path. This adds roughly 50ms of real waiting and distinguishes the new steady-clock deadline from the old frozen-node-clock deadline. Reverting the clock change would leave the call pending, so detection would rely on the test runner's timeout rather than an assertion failure. Keep the existing 20-minute boundary assertions.


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

Elapsed waiting time and protocol timestamps have different clock requirements. Earlier clock cleanup explicitly distinguished duration measurement from system-clock use. The immediate predecessor discussion specifically identified the waitNext deadline and fee tick as elapsed-time scheduling, while retaining NodeClock for comparison with the tip timestamp.

At the pinned base, WaitAndCreateNewBlock uses one NodeClock-derived variable for the deadline, tick, and test-network timestamp comparison. At the head, deadline/tick accounting uses steady_clock, while the timestamp comparison separately reads NodeClock. WaitTipChanged already uses steady_clock. This distinction concerns local waiting semantics, not a change to block timestamp or difficulty rules.

Source 1 · Source 2

The one-second fee tick balances faster delivery of more valuable mining work against expensive template construction. The original discussion favored fast Stratum v2 template updates, but recognized that assembling a template every second could be costly on slow hardware. Waiting on the condition variable was intended to prevent continuous rebuilding and continuous lock contention.

Both pinned versions wait between fee checks and construct a fresh template to measure whether its total fees exceed the original template by fee_threshold. The changed clock measures this existing scheduling interval; it does not change the fee threshold or assembly strategy. The historical hope that cluster mempool would enable cheaper checks should not be mistaken for the implementation present here, which still builds templates.

Source 1 · Source 2 · Source 3 · Source 4

Returning the next template directly, rather than merely notifying a client that it should request one, was motivated by avoiding another round trip and allowing Bitcoin Core to produce work at opportune times. The broader discussion emphasized tail latency caused by lock contention and other node work after a block arrives.

BlockTemplate::waitNext delegates to WaitAndCreateNewBlock and returns the generated template itself. Its wait combines immediate tip notifications with periodic fee checks, preserving the interface's ability to deliver work rather than just a change notification. These were interface-design goals, not proof that all proposed latency optimizations were implemented.

Source 1 · Source 2 · Source 3

Attaching waitNext to the existing BlockTemplate gives the server an authoritative reference to the work previously sent. The proposal explicitly sought simpler client loops and avoidance of trusting clients to supply block hashes or fee information.

The pinned implementation compares the notified tip with the stored template's hashPrevBlock and derives the baseline fees from that template's vTxFees. Timeout accounting operates around these server-derived conditions; the client selects waiting options without supplying the baseline template's claimed fees.

Source 1 · Source 2

The test-network 20-minute exception preserves CPU mining for non-standard transaction tests and provides a way to advance the chain after substantial hash power leaves. BIP94 retained that functionality while addressing block-storm and timestamp-manipulation attacks, acknowledging a tradeoff between accessibility, mainnet similarity, and resistance to disruption.

waitNext was explicitly designed to return another template when the test-network difficulty changes even without a new tip or sufficient fee increase. The retained NodeClock comparison relates node time to the previous block's timestamp. Pinned template construction also uses NodeClock for header time, and GetNextWorkRequired applies the timestamp-based exception outside difficulty-adjustment boundaries; a local steady-clock deadline cannot substitute for that protocol timestamp relationship.

Source 1 · Source 2

Ralph's inferences

Tip changes and fee increases represent different mining incentives: a tip change makes continuing the old work compete with the accepted chain tip, whereas a fee increase makes replacement work more profitable. This explains prioritizing new-tip delivery regardless of fees while making same-tip replacement conditional on a revenue threshold.

Both pinned versions return a newly generated template unconditionally after a tip change, but otherwise require the configured fee increase. The clock separation changes how long these conditions are awaited, not their economic priority.

Source 1 · Source 2