![]() |
Bitcoin Core 32.99.0
P2P Digital Currency
|
Creates block templates, submits solved blocks, and provides tip-waiting helpers for mining code. More...
#include <block_template_manager.h>
Public Member Functions | |
| BlockTemplateManager (CTxMemPool &mempool, ChainstateManager &chainman, KernelNotifications ¬ifications, 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 ¤t_tip, MillisecondsDouble &timeout, bool &interrupt) |
Wait for the tip to differ from current_tip or timeout. More... | |
| std::optional< interfaces::BlockRef > | WaitTipChanged (const uint256 ¤t_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 |
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.
|
explicit |
Definition at line 35 of file block_template_manager.cpp.
|
inline |
Definition at line 47 of file block_template_manager.h.
| 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().
| last_tip | tip at the start of the cooldown window. |
| interrupt_mining | set to true to interrupt the cooldown. |
Definition at line 212 of file block_template_manager.cpp.
| 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.
| std::optional< BlockRef > node::BlockTemplateManager::GetTip | ( | ) |
Locks cs_main.
Definition at line 114 of file block_template_manager.cpp.
| void node::BlockTemplateManager::InterruptWait | ( | bool & | interrupt_wait | ) |
Interrupt a blocking wait.
Definition at line 122 of file block_template_manager.cpp.
| 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.
Definition at line 74 of file block_template_manager.cpp.
| 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.
| 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.
Definition at line 252 of file block_template_manager.cpp.
| 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.
|
private |
Definition at line 38 of file block_template_manager.h.
|
private |
Definition at line 36 of file block_template_manager.h.
|
private |
Definition at line 35 of file block_template_manager.h.
|
private |
Definition at line 37 of file block_template_manager.h.