Skip to content

mining: move BlockAssembler statics into BlockTemplateManager - #36357

Draft
w0xlt wants to merge 3 commits into
bitcoin:masterfrom
w0xlt:block-template-manager-last-block-stats
Draft

w0xlt wants to merge 3 commits into
bitcoin:masterfrom
w0xlt:block-template-manager-last-block-stats

Conversation

@w0xlt

@w0xlt w0xlt commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Based on #36356. Together with it, this completes the "Remove remaining global states" TODO in #33758.

getmininginfo reports currentblockweight and currentblocktx from two process-wide statics, BlockAssembler::m_last_block_weight and m_last_block_num_txs, which every BlockAssembler::CreateNewBlock() call overwrites.

Like the getblocktemplate statics, they are shared by every node context in the process and nothing checks their locking. They also record templates that were not built for mining: the mempool fee estimator builds its own templates with BlockAssembler, so getmininginfo can report the fee estimator's template instead of the last mining template.

This PR:

  • makes the two fields per-BlockAssembler instance, still set at the same point in CreateNewBlock();
  • has BlockTemplateManager::CreateNewTemplate() record them in a member guarded by cs_main, which getmininginfo reads. CreateNewTemplate() now holds cs_main across the build, as CreateNewBlock() already did internally, so concurrent builds cannot record their stats out of order;
  • adds a unit test showing that manager builds are recorded, that the latest one wins, and that a direct BlockAssembler build is ignored.

In short: the last global mining state moves out of BlockAssembler, and getmininginfo reports the last template built for mining rather than whatever the node assembled last.

Add a CachedBlockTemplate member to BlockTemplateManager, guarded by
cs_main, along with GetCachedTransactionsUpdated() and
RefreshCachedTemplate().

RefreshCachedTemplate() follows the rules of the function-local statics
in the getblocktemplate RPC, which the next commit replaces: 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. The deprecated
GetTime() is replaced with the equivalent Now<NodeSeconds>().

Unlike the RPC, RefreshCachedTemplate() does not take the tip from the
caller or cache its CBlockIndex. It reads the active chain tip itself
and compares it with the template's hashPrevBlock, so the cache cannot
pair a template with a tip it does not build on.
Replace the function-local statics in getblocktemplate with the template
cached by BlockTemplateManager. The state now lives as long as the node
context instead of the process, and its synchronization is checked
through GUARDED_BY(::cs_main).

The static CBlockIndex pointer is not carried over: pindexPrev is looked
up from the template's hashPrevBlock, so it can no longer outlive the
ChainstateManager that owns it. The longpollid is built from
hashPrevBlock as well, so it always names the block the template builds
on, and the tip no longer needs to be re-read after longpolling.
BlockAssembler::m_last_block_num_txs and m_last_block_weight were
process-wide statics, written by every BlockAssembler::CreateNewBlock()
call and read by getmininginfo as currentblocktx and currentblockweight.

Make them per-instance, and have
BlockTemplateManager::CreateNewTemplate() copy them into a member
guarded by cs_main, which getmininginfo reads.
CreateNewTemplate() now holds cs_main across the build, as
CreateNewBlock() already did internally, so concurrent builds cannot
record their stats out of order.

Behavior change: only templates built through BlockTemplateManager
(getblocktemplate, the generate RPCs and the mining interface) update
these fields. Templates that the mempool fee estimator builds with
BlockAssembler directly no longer do, and neither does a build that
throws after assembling the block (e.g. a TestBlockValidity() failure).

Update the getmininginfo help text to match.
@DrahtBot

DrahtBot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

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

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36357.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK ismaelsadeeq

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 getTotalFees(), protect m_block_template with mutex by Sjors)
  • #36451 (refactor: Use NotNull for CreateNewBlock and pimpls by maflcko)
  • #36356 (mining: move getblocktemplate cache into BlockTemplateManager by w0xlt)
  • #35671 (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors)
  • #35511 (RFC: consensus: Make CAmount a class by hodlinator)
  • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors)
  • #29700 (kernel, refactor: return error status on all fatal errors by ryanofsky)

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
w0xlt marked this pull request as draft September 27, 2026 15:09
@ismaelsadeeq

Copy link
Copy Markdown
Member

Concept ACK

Now that #34075 is merged, we can also move its cache in there, i.e cache the block template based on time and tip changes. This brings us multiple advantages

i) We consolidate the block template caching for the RPC and fees to the block template manager for better code organisation and reuse.
ii) The mempool fee rate estimator's reliance on the chainstate manager is reduced; the only dependence after that will be on the chain tip, which can also be removed by using chain tip change validation signals. After that, the mempool fee rate estimator will be independent of the mempool and chainstate manager, in replacement with block template manager.

@DrahtBot

DrahtBot commented Nov 5, 2026

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