Skip to content

Commit 81cea5d

Browse files
committed
Ensure m_tip_block is never ZERO
To avoid future code changes from reintroducing the ambiguity fixed by the previous commit, mark m_tip_block private and Assume that it's not set to uint256::ZERO.
1 parent e058544 commit 81cea5d

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
18071807
{
18081808
WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
18091809
kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
1810-
return kernel_notifications.m_tip_block || ShutdownRequested(node);
1810+
return kernel_notifications.TipBlock() || ShutdownRequested(node);
18111811
});
18121812
}
18131813

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ class MinerImpl : public Mining
975975
notifications().m_tip_block_cv.wait_for(lock, timeout, [&]() EXCLUSIVE_LOCKS_REQUIRED(notifications().m_tip_block_mutex) {
976976
// We need to wait for m_tip_block to be set AND for the value
977977
// to differ from the current_tip value.
978-
return (notifications().m_tip_block && notifications().m_tip_block != current_tip) || chainman().m_interrupt;
978+
return (notifications().TipBlock() && notifications().TipBlock() != current_tip) || chainman().m_interrupt;
979979
});
980980
}
981981
// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.

src/node/kernel_notifications.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state
5252
{
5353
{
5454
LOCK(m_tip_block_mutex);
55+
Assume(index.GetBlockHash() != uint256::ZERO);
5556
m_tip_block = index.GetBlockHash();
5657
m_tip_block_cv.notify_all();
5758
}
@@ -99,6 +100,13 @@ void KernelNotifications::fatalError(const bilingual_str& message)
99100
m_exit_status, message, &m_warnings);
100101
}
101102

103+
std::optional<uint256> KernelNotifications::TipBlock()
104+
{
105+
AssertLockHeld(m_tip_block_mutex);
106+
return m_tip_block;
107+
};
108+
109+
102110
void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications)
103111
{
104112
if (auto value{args.GetIntArg("-stopatheight")}) notifications.m_stop_at_height = *value;

src/node/kernel_notifications.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ class KernelNotifications : public kernel::Notifications
5959
//! The block for which the last blockTip notification was received.
6060
//! It's first set when the tip is connected during node initialization.
6161
//! Might be unset during an early shutdown.
62-
std::optional<uint256> m_tip_block GUARDED_BY(m_tip_block_mutex);
62+
std::optional<uint256> TipBlock() EXCLUSIVE_LOCKS_REQUIRED(m_tip_block_mutex);
6363

6464
private:
6565
const std::function<bool()>& m_shutdown_request;
6666
std::atomic<int>& m_exit_status;
6767
node::Warnings& m_warnings;
68+
69+
std::optional<uint256> m_tip_block GUARDED_BY(m_tip_block_mutex);
6870
};
6971

7072
void ReadNotificationArgs(const ArgsManager& args, KernelNotifications& notifications);

src/test/validation_chainstate_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ BOOST_FIXTURE_TEST_CASE(chainstate_update_tip, TestChain100Setup)
7272
ChainstateManager& chainman = *Assert(m_node.chainman);
7373
const auto get_notify_tip{[&]() {
7474
LOCK(m_node.notifications->m_tip_block_mutex);
75-
BOOST_REQUIRE(m_node.notifications->m_tip_block);
76-
return *m_node.notifications->m_tip_block;
75+
BOOST_REQUIRE(m_node.notifications->TipBlock());
76+
return *m_node.notifications->TipBlock();
7777
}};
7878
uint256 curr_tip = get_notify_tip();
7979

0 commit comments

Comments
 (0)