diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 08594fe5af3..cf21787d7d1 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -966,10 +966,7 @@ public: std::optional getTip() override { - LOCK(::cs_main); - CBlockIndex* tip{chainman().ActiveChain().Tip()}; - if (!tip) return {}; - return BlockRef{tip->GetBlockHash(), tip->nHeight}; + return GetTip(chainman()); } std::optional waitTipChanged(uint256 current_tip, MillisecondsDouble timeout) override diff --git a/src/node/miner.cpp b/src/node/miner.cpp index 213b46cd0bf..426e065ff21 100644 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -539,4 +539,12 @@ std::unique_ptr WaitAndCreateNewBlock(ChainstateManager& chainma return nullptr; } + +std::optional GetTip(ChainstateManager& chainman) +{ + LOCK(::cs_main); + CBlockIndex* tip{chainman.ActiveChain().Tip()}; + if (!tip) return {}; + return BlockRef{tip->GetBlockHash(), tip->nHeight}; +} } // namespace node diff --git a/src/node/miner.h b/src/node/miner.h index 5dcdca437a3..d0ae8312fb5 100644 --- a/src/node/miner.h +++ b/src/node/miner.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_NODE_MINER_H #define BITCOIN_NODE_MINER_H +#include #include #include #include @@ -31,6 +32,8 @@ class ChainstateManager; namespace Consensus { struct Params; }; +using interfaces::BlockRef; + namespace node { class KernelNotifications; @@ -245,6 +248,9 @@ std::unique_ptr WaitAndCreateNewBlock(ChainstateManager& chainma const std::unique_ptr& block_template, const BlockWaitOptions& options, const BlockAssembler::Options& assemble_options); + +/* Locks cs_main and returns the block hash and block height of the active chain if it exists; otherwise, returns nullopt.*/ +std::optional GetTip(ChainstateManager& chainman); } // namespace node #endif // BITCOIN_NODE_MINER_H