Make m_tip_block an std::optional

This change avoids ambiguity when no tip is connected and it is
compared to uint256::ZERO.
This commit is contained in:
Sjors Provoost 2024-12-17 10:18:36 +07:00
parent b042c4f053
commit e058544d0e
No known key found for this signature in database
GPG key ID: 57FF9BDBCC301009
4 changed files with 7 additions and 4 deletions

View file

@ -1807,7 +1807,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
{
WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
return !kernel_notifications.m_tip_block.IsNull() || ShutdownRequested(node);
return kernel_notifications.m_tip_block || ShutdownRequested(node);
});
}

View file

@ -973,7 +973,9 @@ public:
{
WAIT_LOCK(notifications().m_tip_block_mutex, lock);
notifications().m_tip_block_cv.wait_for(lock, timeout, [&]() EXCLUSIVE_LOCKS_REQUIRED(notifications().m_tip_block_mutex) {
return (notifications().m_tip_block != current_tip && notifications().m_tip_block != uint256::ZERO) || chainman().m_interrupt;
// We need to wait for m_tip_block to be set AND for the value
// to differ from the current_tip value.
return (notifications().m_tip_block && notifications().m_tip_block != current_tip) || chainman().m_interrupt;
});
}
// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.

View file

@ -59,7 +59,7 @@ public:
//! The block for which the last blockTip notification was received.
//! It's first set when the tip is connected during node initialization.
//! Might be unset during an early shutdown.
uint256 m_tip_block GUARDED_BY(m_tip_block_mutex){uint256::ZERO};
std::optional<uint256> m_tip_block GUARDED_BY(m_tip_block_mutex);
private:
const std::function<bool()>& m_shutdown_request;

View file

@ -72,7 +72,8 @@ BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)
ChainstateManager& chainman = *Assert(m_node.chainman);
const auto get_notify_tip{[&]() {
LOCK(m_node.notifications->m_tip_block_mutex);
return m_node.notifications->m_tip_block;
BOOST_REQUIRE(m_node.notifications->m_tip_block);
return *m_node.notifications->m_tip_block;
}};
uint256 curr_tip = get_notify_tip();