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

pull w0xlt wants to merge 5 commits into bitcoin:master from w0xlt:block-template-manager-gbt-steady-clock changing 5 files +167 −40
  1. w0xlt commented at 2:46 AM on November 20, 2026: 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.

  2. rpc: gather the getblocktemplate cache state into a struct
    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.
    a93d9361ea
  3. refactor: move getblocktemplate cache into BlockTemplateManager
    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
    f04c8df9f5
  4. test: cover the BlockTemplateManager getblocktemplate cache
    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 <66912335+enochaz@users.noreply.github.com>
    00a62a2a91
  5. test: check the rebuilt getblocktemplate transactions after prioritisetransaction
    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 <66912335+enochaz@users.noreply.github.com>
    329b9055d5
  6. mining: measure the getblocktemplate template age with a steady clock
    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 <66912335+enochaz@users.noreply.github.com>
    8576b18a71
  7. DrahtBot added the label Mining on Nov 20, 2026
  8. DrahtBot commented at 2:47 AM on November 20, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36921 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36921.svg"></sub> (test: fix mining_prioritisetransaction check that always passes by w0xlt)
    • #35671 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35671.svg"></sub> (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors)
    • #33922 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33922.svg"></sub> (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.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  9. w0xlt marked this as a draft on Nov 20, 2026
  10. DrahtBot added the label CI failed on Nov 20, 2026
  11. fanquake closed this on Nov 20, 2026

  12. fanquake reopened this on Nov 20, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-20 15:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me