Skip to content

mining: use a steady clock for the getblocktemplate rebuild interval - #36922

Draft
w0xlt wants to merge 5 commits into
bitcoin:masterfrom
w0xlt:block-template-manager-gbt-steady-clock
Draft

w0xlt wants to merge 5 commits into
bitcoin:masterfrom
w0xlt:block-template-manager-gbt-steady-clock

Conversation

@w0xlt

@w0xlt w0xlt commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Based on #36356 and #36921. Only the last commit is new.

After a mempool change, getblocktemplate only rebuilds its cached template once it's more than 5 seconds old, and it measures that age with the system clock. That works fine in practice, but as pointed out in a review comment, a steady clock is the better fit for measuring an interval, since system time changes don't affect it.

This switches to the steady clock, like #30372 did for RandAddSeedPerfmon. Since setmocktime doesn't move it, mining_prioritisetransaction.py now waits out the 5 seconds, and the unit test uses a fake steady clock.

@DrahtBot

DrahtBot commented Nov 20, 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.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36921 (test: fix mining_prioritisetransaction check that always passes by w0xlt)
  • #35671 (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors)
  • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint 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.

w0xlt and others added 5 commits November 24, 2026 16:23
Replace the function-local statics in getblocktemplate with a
CachedBlockTemplate struct and a GetNewOrCachedBlockTemplate() helper,
both local to rpc/mining.cpp. The cache is still process-wide, but it is
now GUARDED_BY(::cs_main), so its synchronization is checked.

GetNewOrCachedBlockTemplate() keeps the existing rules: the template is
rebuilt when the tip changes, or when the mempool changed and the cached
template is more than 5 seconds old. A failed rebuild leaves the cached
template unset so that the next call tries again.

This prepares for moving the cache into BlockTemplateManager in the next
commit. While the code is still next to the original, it also:

- Drops the static CBlockIndex pointer. pindexPrev is looked up from the
  template's hashPrevBlock, so it cannot outlive the ChainstateManager
  that owns it.
- Reads the active chain tip inside GetNewOrCachedBlockTemplate() and
  compares it with the template's hashPrevBlock, so the cache cannot
  pair a template with a tip it does not build on. The caller no longer
  needs to re-read the tip after longpolling.
- Builds the longpollid from hashPrevBlock, so it always names the block
  the template builds on.
- Replaces the deprecated GetTime() with the equivalent
  Now<NodeSeconds>().
- Names the 5 second rebuild interval GBT_TEMPLATE_REBUILD_INTERVAL.
Move CachedBlockTemplate, GBT_TEMPLATE_REBUILD_INTERVAL and
GetNewOrCachedBlockTemplate() from rpc/mining.cpp into
BlockTemplateManager. The file-scope cache becomes a member that is
still guarded by cs_main, and the interval becomes a public static
member. The longpolling code reads the cached mempool counter through
the new GetCachedTransactionsUpdated().

The cache now lives as long as the node context instead of the process.

Apart from the member accesses and declarations, this is a move. It
can be reviewed with:

git show --color-moved=dimmed-zebra --color-moved-ws=allow-indentation-change
Check that GetNewOrCachedBlockTemplate() reuses the cached template
until the tip changes, or until the mempool changes and the template is
more than 5 seconds old, and that GetCachedTransactionsUpdated() reports
the mempool counter sampled for the last build. Also check that a failed
rebuild leaves the cache without a template, so that the next call
retries without waiting for the 5 seconds to pass.

Check that a replacement manager starts with an empty cache and builds
its own template on the same tip with the same mempool.

Co-authored-by: Enoch Azariah <[email protected]>
…etransaction

The check that prioritisetransaction makes getblocktemplate return a new
block compared the whole template. curtime follows the mock clock, so the
comparison passed even when the cached template was reused. Check that the
deprioritised transaction leaves the template instead.

Co-authored-by: Enoch Azariah <[email protected]>
A mempool change only rebuilds the cached getblocktemplate template once
it is older than the rebuild interval. Measure that age with the steady
clock instead of the system clock, since it is the better fit for an
interval and isn't affected by system time changes.

setmocktime no longer ages the template, so mining_prioritisetransaction.py
now waits for the rebuild interval to pass, and the unit test advances a
fake steady clock instead of mocktime.

Co-authored-by: Enoch Azariah <[email protected]>
@w0xlt
w0xlt force-pushed the block-template-manager-gbt-steady-clock branch from 8576b18 to c229b81 Compare November 25, 2026 00:31
@DrahtBot

Copy link
Copy Markdown
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants