Bitcoin Core 32.99.0
P2P Digital Currency
Public Member Functions | Private Attributes | List of all members
node::BlockTemplateManager Class Reference

Creates block templates, submits solved blocks, and provides tip-waiting helpers for mining code. More...

#include <block_template_manager.h>

Collaboration diagram for node::BlockTemplateManager:
[legend]

Public Member Functions

 BlockTemplateManager (CTxMemPool &mempool, ChainstateManager &chainman, KernelNotifications &notifications, BlockCreateOptions block_create_args={})
 
const BlockCreateOptions & BlockCreateArgs () const
 
std::unique_ptr< CBlockTemplate > CreateNewTemplate (const BlockCreateOptions &options)
 Create a fresh block template, applying init-time defaults to any unset options. More...
 
bool SubmitBlock (const std::shared_ptr< const CBlock > &block, std::string &reason, std::string &debug)
 Submit a block via ProcessNewBlock and capture validation state. More...
 
std::optional< interfaces::BlockRef > GetTip ()
 Locks cs_main. More...
 
std::optional< interfaces::BlockRef > WaitTipChanged (const uint256 &current_tip, MillisecondsDouble &timeout, bool &interrupt)
 Wait for the tip to differ from current_tip or timeout. More...
 
std::optional< interfaces::BlockRef > WaitTipChanged (const uint256 &current_tip, MillisecondsDouble timeout=MillisecondsDouble::max())
 Convenience overload for in-process callers (e.g. More...
 
bool CooldownIfHeadersAhead (const interfaces::BlockRef &last_tip, bool &interrupt_mining)
 Wait while the best known header extends the current chain tip AND at least one block is being added to the tip every 3 seconds. More...
 
void InterruptWait (bool &interrupt_wait)
 Interrupt a blocking wait. More...
 
std::unique_ptr< CBlockTemplate > WaitAndCreateNewBlock (const std::unique_ptr< CBlockTemplate > &block_template, const BlockWaitOptions &wait_options, const BlockCreateOptions &create_options, bool &interrupt_wait)
 Return a new block template when fees rise to a certain threshold or after a new tip; return nullptr if timeout is reached. More...
 

Private Attributes

CTxMemPool & m_mempool
 
ChainstateManager & m_chainman
 
KernelNotifications & m_notifications
 
const BlockCreateOptions m_block_create_args
 

Detailed Description

Creates block templates, submits solved blocks, and provides tip-waiting helpers for mining code.

Owns the init-time block creation args.

Definition at line 32 of file block_template_manager.h.

Constructor & Destructor Documentation

◆ BlockTemplateManager()

node::BlockTemplateManager::BlockTemplateManager ( CTxMemPool &  mempool,
ChainstateManager &  chainman,
KernelNotifications &  notifications,
BlockCreateOptions  block_create_args = {} 
)
explicit

Definition at line 35 of file block_template_manager.cpp.

Member Function Documentation

◆ BlockCreateArgs()

const BlockCreateOptions & node::BlockTemplateManager::BlockCreateArgs ( ) const
inline
Returns
the block creation args set during node init.

Definition at line 47 of file block_template_manager.h.

◆ CooldownIfHeadersAhead()

bool node::BlockTemplateManager::CooldownIfHeadersAhead ( const interfaces::BlockRef &  last_tip,
bool &  interrupt_mining 
)

Wait while the best known header extends the current chain tip AND at least one block is being added to the tip every 3 seconds.

If the tip is sufficiently far behind, allow up to 20 seconds for the next tip update.

It's not safe to keep waiting, because a malicious miner could announce a header and delay revealing the block, causing all other miners using this software to stall. At the same time, we need to balance between the default waiting time being brief, but not ending the cooldown prematurely when a random block is slow to download (or process).

The cooldown only applies to createNewBlock(), which is typically called once per connected client. Subsequent templates are provided by waitNext().

Parameters
last_tiptip at the start of the cooldown window.
interrupt_miningset to true to interrupt the cooldown.
Returns
false if interrupted.

Definition at line 212 of file block_template_manager.cpp.

Here is the call graph for this function:

◆ CreateNewTemplate()

std::unique_ptr< CBlockTemplate > node::BlockTemplateManager::CreateNewTemplate ( const BlockCreateOptions &  options)

Create a fresh block template, applying init-time defaults to any unset options.

Definition at line 42 of file block_template_manager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTip()

std::optional< BlockRef > node::BlockTemplateManager::GetTip ( )

Locks cs_main.

Returns
the active chain tip, or nullopt if none exists.

Definition at line 114 of file block_template_manager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ InterruptWait()

void node::BlockTemplateManager::InterruptWait ( bool &  interrupt_wait)

Interrupt a blocking wait.

Definition at line 122 of file block_template_manager.cpp.

◆ SubmitBlock()

bool node::BlockTemplateManager::SubmitBlock ( const std::shared_ptr< const CBlock > &  block,
std::string &  reason,
std::string &  debug 
)

Submit a block via ProcessNewBlock and capture validation state.

Returns
whether the block was accepted as a new valid block.

Definition at line 74 of file block_template_manager.cpp.

Here is the call graph for this function:

◆ WaitAndCreateNewBlock()

std::unique_ptr< CBlockTemplate > node::BlockTemplateManager::WaitAndCreateNewBlock ( const std::unique_ptr< CBlockTemplate > &  block_template,
const BlockWaitOptions &  wait_options,
const BlockCreateOptions &  create_options,
bool &  interrupt_wait 
)

Return a new block template when fees rise to a certain threshold or after a new tip; return nullptr if timeout is reached.

We determine if fees increased compared to the previous template by generating a fresh template. There may be more efficient ways to determine how much (approximate) fees for the next block increased, perhaps more so after Cluster Mempool.

We'll also create a new template if the tip changed during this iteration.

Definition at line 129 of file block_template_manager.cpp.

Here is the call graph for this function:

◆ WaitTipChanged() [1/2]

std::optional< BlockRef > node::BlockTemplateManager::WaitTipChanged ( const uint256 &  current_tip,
MillisecondsDouble &  timeout,
bool &  interrupt 
)

Wait for the tip to differ from current_tip or timeout.

Waits indefinitely during startup for a non-null tip.

Returns
the current tip, or nullopt if the node is shutting down or interrupt is set (not when the timeout is reached).

Definition at line 252 of file block_template_manager.cpp.

Here is the caller graph for this function:

◆ WaitTipChanged() [2/2]

std::optional< BlockRef > node::BlockTemplateManager::WaitTipChanged ( const uint256 &  current_tip,
MillisecondsDouble  timeout = MillisecondsDouble::max() 
)

Convenience overload for in-process callers (e.g.

RPC) that have no interrupt handle. Takes the timeout by value so the caller's variable is left unchanged; the wait still ends on chain shutdown.

Definition at line 246 of file block_template_manager.cpp.

Member Data Documentation

◆ m_block_create_args

const BlockCreateOptions node::BlockTemplateManager::m_block_create_args
private

Definition at line 38 of file block_template_manager.h.

◆ m_chainman

ChainstateManager& node::BlockTemplateManager::m_chainman
private

Definition at line 36 of file block_template_manager.h.

◆ m_mempool

CTxMemPool& node::BlockTemplateManager::m_mempool
private

Definition at line 35 of file block_template_manager.h.

◆ m_notifications

KernelNotifications& node::BlockTemplateManager::m_notifications
private

Definition at line 37 of file block_template_manager.h.


The documentation for this class was generated from the following files: