This PR implements the first step of #33389.
Main motivation and design rationale are discussed in #33389.
It introduces a BlockTemplateCache
exposed via the node interfaces.
Block template requests from other node components should go through the cache. Each request specifies the maximum age of the template in seconds. If a cached template exists with matching configuration and is still fresh, it is returned; otherwise, a new template with the requested configuration is generated and added to the cache.
The BlockTemplateCache
subscribes to the validation interface BlockConnected
notification and clears the cache when a new block is connected, preventing stale templates from being served.
The cache has a configurable maximum size (default 10).
If test_block_validity
is true in the request block creation options and the found cached template has not been validated yet, it is checked and returned. This is the reason GetBlockTemplate
also locks cs_main
; otherwise, the method can run with only locking m_mutex
.
MakeBlockTemplateCache
returns a shared pointer so it can be registered with the validation interface using RegisterSharedValidationInterface
hence not requiring explicit unregistration.