Skip to content

Commit 5bbbc0d

Browse files
committed
Merge bitcoin/bitcoin#31325: Make m_tip_block std::optional
81cea5d Ensure m_tip_block is never ZERO (Sjors Provoost) e058544 Make m_tip_block an std::optional (Sjors Provoost) Pull request description: Suggested in bitcoin/bitcoin#31297 (comment) ACKs for top commit: fjahr: re-ACK 81cea5d tdb3: code review re ACK 81cea5d l0rinc: ACK 81cea5d Tree-SHA512: 31a75ba29e3d567bab32e4e7925a419d9d7a4d2d85ed1c1012116d8d22adc14d31d5b4ce5f6c499c994188dcd26a01cced05be74f94c892fc90ae17a6783a472
2 parents c1252b1 + 81cea5d commit 5bbbc0d

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
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.IsNull() || ShutdownRequested(node);
1810+
return kernel_notifications.TipBlock() || ShutdownRequested(node);
18111811
});
18121812
}
18131813

src/node/interfaces.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,9 @@ class MinerImpl : public Mining
971971
{
972972
WAIT_LOCK(notifications().m_tip_block_mutex, lock);
973973
notifications().m_tip_block_cv.wait_for(lock, timeout, [&]() EXCLUSIVE_LOCKS_REQUIRED(notifications().m_tip_block_mutex) {
974-
return (notifications().m_tip_block != current_tip && notifications().m_tip_block != uint256::ZERO) || chainman().m_interrupt;
974+
// We need to wait for m_tip_block to be set AND for the value
975+
// to differ from the current_tip value.
976+
return (notifications().TipBlock() && notifications().TipBlock() != current_tip) || chainman().m_interrupt;
975977
});
976978
}
977979
// 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-
uint256 m_tip_block GUARDED_BY(m_tip_block_mutex){uint256::ZERO};
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +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-
return m_node.notifications->m_tip_block;
75+
BOOST_REQUIRE(m_node.notifications->TipBlock());
76+
return *m_node.notifications->TipBlock();
7677
}};
7778
uint256 curr_tip = get_notify_tip();
7879

0 commit comments

Comments
 (0)