Repository navigation
Conversation
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.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36357. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
|
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. |
|
🐙 This pull request conflicts with the target branch and needs rebase. |
Based on #36356. Together with it, this completes the "Remove remaining global states" TODO in #33758.
getmininginforeportscurrentblockweightandcurrentblocktxfrom two process-wide statics,BlockAssembler::m_last_block_weightandm_last_block_num_txs, which everyBlockAssembler::CreateNewBlock()call overwrites.Like the
getblocktemplatestatics, 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 withBlockAssembler, sogetmininginfocan report the fee estimator's template instead of the last mining template.This PR:
BlockAssemblerinstance, still set at the same point inCreateNewBlock();BlockTemplateManager::CreateNewTemplate()record them in a member guarded bycs_main, whichgetmininginforeads.CreateNewTemplate()now holdscs_mainacross the build, asCreateNewBlock()already did internally, so concurrent builds cannot record their stats out of order;BlockAssemblerbuild is ignored.In short: the last global mining state moves out of
BlockAssembler, andgetmininginforeports the last template built for mining rather than whatever the node assembled last.