Skip to content

Commit

Permalink
Have createNewBlock() wait for a tip
Browse files Browse the repository at this point in the history
Additionally it returns null if the node started to shutdown before TipBlock() was set.
  • Loading branch information
Sjors committed Feb 3, 2025
1 parent ad3af40 commit be15f2e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/interfaces/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ class Mining
virtual std::optional<BlockRef> waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;

/**
* Construct a new block template
* Construct a new block template.
*
* During node initialization, this will
* wait until the tip is connected.
*
* @param[in] options options for creating the block
* @returns a block template
* @retval BlockTemplate a block template.
* @retval empty if node is shutting down
*/
virtual std::unique_ptr<BlockTemplate> createNewBlock(const node::BlockCreateOptions& options = {}) = 0;

Expand Down
3 changes: 3 additions & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,9 @@ class MinerImpl : public Mining

std::unique_ptr<BlockTemplate> createNewBlock(const BlockCreateOptions& options) override
{
// Ensure m_tip_block is set so consumers of BlockTemplate can rely on that.
if (!waitTipChanged(uint256::ZERO, MillisecondsDouble::max())) return {};

BlockAssembler::Options assemble_options{options};
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
return std::make_unique<BlockTemplateImpl>(BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(), m_node);
Expand Down

0 comments on commit be15f2e

Please sign in to comment.