#35675 mining: add block template manager

full analysis

https://github.com/bitcoin/bitcoin/pull/35675 · ismaelsadeeq · +891/-449 in 28 files, 15 commits · labels: Mining

Goal

  • Centralize mining coordination and template lifecycle handling in a dedicated manager
  • Avoid unnecessary block data copying during getblocktemplate RPC calls
  • Unblock downstream improvements to template memory and fee tracking

This PR introduces `node::BlockTemplateManager` to manage block template creation, submission, and tip-waiting helpers in one place. It moves init-time mining options from `NodeContext` into the manager, routes RPC and test callers directly to the manager, and eliminates `interfaces::Mining` from `NodeContext`. It also optimizes `getblocktemplate` by avoiding redundant block and transaction copies, and adds a dedicated fuzz test for the manager.

Problem: Mining options and template creation helpers were spread across `NodeContext`, `miner.cpp`, the `Mining` interface, and RPC code. This caused unnecessary `CBlock` copies in `getblocktemplate` and complicated downstream work on template fee tracking and memory management.

Category: Mining (#1 of 13)

P2 · unblocks #35581

  • P2 because it cleans up the architectural boundary between mining coordination and node internals
  • Directly unblocks template memory tracking and fee inflow tracking in downstream work

Centralizes block template management and eliminates redundant block copies in getblocktemplate. It provides substantial structural improvement to mining interfaces and directly unblocks fee-inflow tracking in #35581 and template footprint tracking in #33922.

Membership: Introduces BlockTemplateManager, restructures template generation, submission, and wait routines in src/node/miner.cpp, and updates getblocktemplate.

Factors: security/stability 1, bug 0, performance 1, user value 1, leverage 2

Reviewability: Ready

  • Ready to review now with clean CI and an active author

The PR has passing CI, clean mergeability, and broad review coverage on its latest revision.

Author status: Active. Responded promptly to review rounds and force-pushed updates through 2026-09-13.

Open concerns:

  • marcofleon noted the added fuzz target is ineffective at exercising BlockTemplateManager specifically and suggested dropping the commit to handle in a separate PR.

Resolved concerns:

  • Sjors and w0xlt noticed a rebase had accidentally reverted changes from merged PR #34672, which the author resolved.
  • w0xlt pointed out that `wallet/test/fuzz/fees.cpp` needed to reset and recreate the template manager when resetting the mempool to avoid dangling pointers.
  • pablomartin4btc highlighted manager construction timing relative to chainstate activation.

Agreement: Mild

  • Strong consensus across multiple reviewers after verifying rebase and lifecycle fixes
  • Supported after copy-avoidance suggestions were incorporated (Sjors)
  • Verified SubmitBlock rebase fix and test lifecycle ordering (w0xlt, pablomartin4btc)
  • Nonblocking suggestion that the new fuzz harness could be deepened in a follow-up (marcofleon)

Mild: four reviewers strongly support the refactor, while marcofleon questions the new fuzz test's effectiveness and suggested dropping it (open, nonblocking).

Four reviewers have re-ACKed the current branch. All earlier functional objections regarding rebasing regressions and lifetime bugs were resolved. The only remaining remark is from marcofleon questioning the utility of the fuzz target added in the final commit, which they explicitly noted was not a blocker for the refactor.

  • Sjors re-ACKed on 2026-09-14: 're-ACK e8b283c35a656f808250e4b32d3494f49a000b08'
  • pablomartin4btc re-ACKed on 2026-09-14: 're-ACK e8b283c35a656f808250e4b32d3494f49a000b08'
  • w0xlt re-ACKed on 2026-09-14: 'reACK e8b283c35a656f808250e4b32d3494f49a000b08'
  • enirox001 re-ACKed on 2026-09-16: 'reACK https://github.com/bitcoin/bitcoin/pull/35675/changes/e8b283c35a656f808250e4b32d3494f49a000b08'
  • marcofleon on 2026-09-16: 'light code review ACK 3d7d190b6995b3c0adb6528c2d9eac81a8f839d9... I don't think this is a blocker for the refactor. I would just drop the last commit and PR it separately.'

Objections:

ReviewerKindHarmStatusBlockingAuthor repliedQuote
Sjorscorrectnessrebase reverted changes from merged PR #34672resolvedyesyes2026-07-23: 'I think your rebase after https://github.com/bitcoin/bitcoin/pull/34672 reverted some of the changes from that PR.'
Settled: 2026-07-24: Sjors commented 'utACK a87d81548be53ed0af747adf5b8fd22e0d6b8289'
w0xltcorrectnessrebase reverted changes from merged PR #34672resolvedyesyes2026-07-23: 'LGTM, but the current code is really reverting https://github.com/bitcoin/bitcoin/pull/34672.'
Settled: 2026-07-29: w0xlt commented 'ACK dad787680358adeba552802c152343a7d9ddfbd0'
pablomartin4btccorrectnesscalling CreateBlockTemplateManager before chainstate is activated causes crash if CreateNewBlock is calledresolvednoyes2026-07-16: 'CreateBlockTemplateManager() is being called before chainstate is activated... CreateNewBlock() asserts pindexPrev != nullptr and would crash if called at this point.'
Settled: 2026-07-16: author added comments and clarified constructor requirements in cd83dfd9a6
w0xltcorrectnessreplacing node.mempool leaves BlockTemplateManager with a dangling reference in wallet fuzz targetresolvednoyes2026-07-28: 'It looks like src/wallet/test/fuzz/fees.cpp needs the same reset/recreate handling as the other fuzz targets.'
Settled: 2026-07-29: w0xlt commented 'ACK dad787680358adeba552802c152343a7d9ddfbd0'
marcofleonapproachfuzz harness loops compete for input bytes making it ineffective at testing the manageropennono2026-09-16: 'For the fuzz test, I don't think this is really a BlockTemplateManager test... I don't think this is a blocker for the refactor. I would just drop the last commit and PR it separately.'

Support:

  • Sjors: Avoids copying template data in getblocktemplate and enables clean basing of #33922
  • pablomartin4btc: Clean refactor consolidating block creation, submission, and tip-waiting into BlockTemplateManager and removing node.mining
  • enirox001: Centralized block template manager clarifies mining options and improves consistency
  • w0xlt: Confirmed correct refactor and lifetime management across fuzz targets
  • marcofleon: Light code review ACK for the refactor commits up to 3d7d190b69

Participants: Sjors (objection), pablomartin4btc (objection), enirox001 (support), w0xlt (objection), marcofleon (objection)

State derived from the lists: nonblocking objection open (marcofleon)

Review verdicts (DrahtBot): 4 (+1)

Dependencies

Enables:

  • #35581 node: add block template manager and track waitNext fee inflow
  • #33922 mining: add getMemoryLoad() and track template non-mempool memory footprint

Base for: #33922, #35581

Files

453 lines under test/bench/ci.

  • src/test/fuzz/block_template_manager.cpp +329/-0
  • src/node/block_template_manager.cpp +288/-0
  • src/node/miner.cpp +0/-243
  • src/node/block_template_manager.h +106/-0
  • src/rpc/mining.cpp +36/-38
  • src/node/miner.h +0/-58
  • src/node/interfaces.cpp +14/-26
  • src/rpc/blockchain.cpp +15/-16
  • src/test/miner_tests.cpp +20/-6
  • src/test/util/setup_common.cpp +15/-8
  • src/test/validation_block_tests.cpp +10/-9
  • src/test/util/mining.cpp +9/-8
  • src/init.cpp +8/-8
  • src/node/context.h +3/-11
  • src/test/peerman_tests.cpp +5/-4
  • src/rpc/server_util.cpp +4/-4
  • src/test/fuzz/rpc.cpp +4/-4
  • src/rpc/server_util.h +2/-4
  • src/test/util/setup_common.h +4/-0
  • src/test/fuzz/connect_block.cpp +3/-0
  • src/test/fuzz/utxo_snapshot.cpp +3/-0
  • src/test/util/validation.cpp +3/-0
  • src/test/validation_chainstatemanager_tests.cpp +3/-0
  • src/wallet/test/fuzz/fees.cpp +3/-0
  • ci/test/03_test_script.sh +1/-1
  • src/node/context.cpp +1/-1
  • src/CMakeLists.txt +1/-0
  • src/test/fuzz/CMakeLists.txt +1/-0

Card

This PR introduces node::BlockTemplateManager to centralize block template creation, block submission, and mining wait helpers in one place, removing scattered options from NodeContext and avoiding unnecessary block copies in getblocktemplate. It directly unblocks fee-inflow tracking in #35581 and memory footprint tracking in #33922. Four reviewers strongly support the change with re-ACKs on the latest commit. One reviewer raised a nonblocking concern regarding the efficacy of the newly added fuzz test and suggested handling it separately.

Data

dossier JSON · extract JSON · model openrouter/google/gemini-3.8-flash, generated 2026-09-17T21:38, confidence high, input hash 3561518962f14e99