Skip to content

Commit 5315278

Browse files
committed
Have createNewBlock() wait for a tip
- return null on shutdown instead of the last tip - ignore timeout value node initialization This allows consumers of BlockTemplate to safely assume that a tip is connected, instead of having to account for startup and early shutdown scenarios.
1 parent 64a2795 commit 5315278

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/interfaces/mining.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ class Mining
101101
virtual std::optional<BlockRef> waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;
102102

103103
/**
104-
* Construct a new block template
104+
* Construct a new block template.
105+
*
106+
* During node initialization, this will wait until the tip is connected.
105107
*
106108
* @param[in] options options for creating the block
107-
* @returns a block template
109+
* @retval BlockTemplate a block template.
110+
* @retval std::nullptr if the node is shut down.
108111
*/
109112
virtual std::unique_ptr<BlockTemplate> createNewBlock(const node::BlockCreateOptions& options = {}) = 0;
110113

src/node/interfaces.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,9 @@ class MinerImpl : public Mining
11021102

11031103
std::unique_ptr<BlockTemplate> createNewBlock(const BlockCreateOptions& options) override
11041104
{
1105+
// Ensure m_tip_block is set so consumers of BlockTemplate can rely on that.
1106+
if (!waitTipChanged(uint256::ZERO, MillisecondsDouble::max())) return {};
1107+
11051108
BlockAssembler::Options assemble_options{options};
11061109
ApplyArgsManOptions(*Assert(m_node.args), assemble_options);
11071110
return std::make_unique<BlockTemplateImpl>(assemble_options, BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(), m_node);

0 commit comments

Comments
 (0)