Repository navigation
node: add block template manager and track waitNext fee inflow - #35581
ismaelsadeeq wants to merge 27 commits into
Conversation
|
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/35581. 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. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
9bd3893 to
e03f222
Compare
|
Concept ACK |
e03f222 to
28c9934
Compare
28c9934 to
8c73017
Compare
|
It would be nice to have a PR focussed on the BlockTemplateManager:
I should be able to build #33922 / Sjors#120 on top of that. The |
0abdb60 to
111870f
Compare
Add MemPoolChunk and MemPoolChunksUpdate structs to kernel/mempool_entry.h and MakeMemPoolChunks helper to txmempool.cpp for converting TxGraph::Chunk vectors into MemPoolChunk vectors. Each removal path now captures the before/after fee rate diagram chunks and fires MempoolUpdated so subscribers can observe every mempool change. This signal allows subscribers to observe mempool fee rate diagram changes in an asynchronous way without holding the mempool lock. This changes the order of operations in removeForBlock: all block transactions in the mempool are now removed before conflict removals. In blockencodings_tests, SyncWithValidationInterfaceQueue() drains the async MempoolUpdated signal queue before LOCK2(cs_main, pool.cs). The LOCK2 moves after the sync to avoid deadlock.
Replace `m_package_feerates` with `m_template_chunks`, which records each selected chunk's `FeePerWeight` feerate, unadjusted weight, sigops cost, and the witness IDs of the transactions selected in that chunk. This gives block templates enough information to describe both the selection order and the transaction group selected at each step.
Index template chunks by hash and feerate to update snapshots without sorting them after each mempool addition.
Register BlockTemplateManager for validation callbacks and keep tracked snapshots synchronized with mempool and chain-tip changes.
Track template fee inflow and report staleness once the fee improvement reaches the requested threshold. The fee improvement is the lower-bound difference between the current tracked chunk fees and the original template fees.
Use BlockTemplateManager snapshot tracking to decide when waitNext should return a replacement template due to fee inflow. This avoids rebuilding full templates on every wait tick just to compare fees. Tracked snapshots are cleaned up by BlockTemplateImpl destruction, so an original template can remain alive and call waitNext again after a replacement is returned.
111870f to
24a87b7
Compare
|
🐙 This pull request conflicts with the target branch and needs rebase. |
Motivation
The mining interface and rpc helpers (
WaitAndCreateNewBlockand friends) are free functions scattered acrosssrc/node/miner.cpp, and every caller has to passChainstateManagerandKernelNotificationsinto them, which is verbose.SubmitBlockspins up a short-livedCValidationInterfacesubscriber on every call just to read back the block validation state. This PR adds a BlockTemplateManager that holds that state and exposes those helpers as methods, so callers don't have to thread chainman/notifications (or a throwaway subscriber) around.It also removes a redundant pattern in
waitNext. Currently, to decide whether mempool fee inflow rose enough to be worth a new template, waitNext lockscs_mainand assembles a full block template every tick just to sum its fees and compare. After cluster mempool see https://delvingbitcoin.org/t/determining-blocktemplate-fee-increase-using-fee-rate-diagram/2052, we shouldn't have to build a whole block template to answer that question. This PR uses the #34803 mempool notifications to record the previous template state and track the inflow above the lowest included chunk, and only assembles and returns a new template once that inflow is above the threshold.I added a fuzz harness and unit tests for the manager. The fuzz test already caught a bug in the miner where it skips legitimate transactions due to an incorrect block chunk size limit check #35580.
Changes
MempoolUpdatednotification that returns the before and after diagramsBlockTemplateManagerwrapper aroundBlockAssemblerthat holds state andcreates templates, and move the free functions (mining args, SubmitBlock, GetTip,
WaitTipChanged, CooldownIfHeadersAhead, WaitAndCreateNewBlock) onto it as methods
BlockTemplateManagerinsteadof the IPC Mining interface, which also drops the per-access block copies the IPC
accessors (getBlock) forced
There are now two entry points for template creation. Mining clients go through the IPC
Mining interface and get tracked templates, while in-process RPC and tests call BlockTemplateManager directly and get untracked ones. This is deliberate: one-shot RPC templates don't need staleness tracking, and it also avoids process-static RPC caches owning BlockTemplateImpl objects whose destructor reaches back into NodeContext at shutdown.
50% of the code addition here are tests ~1090 lines