Skip to content

Commit fa7f52a

Browse files
author
MarcoFalke
committed
refactor: Use wait_for predicate to check for interrupt
Also use uint256::ZERO where appropriate for self-documenting code.
1 parent 5ca28ef commit fa7f52a

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/interfaces/mining.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class Mining
6161
virtual std::optional<BlockRef> getTip() = 0;
6262

6363
/**
64-
* Waits for the tip to change
64+
* Waits for the connected tip to change. If the tip was not connected on
65+
* startup, this will wait.
6566
*
6667
* @param[in] current_tip block hash of the current chain tip. Function waits
6768
* for the chain tip to differ from this.

src/node/interfaces.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -940,19 +940,12 @@ class MinerImpl : public Mining
940940

941941
BlockRef waitTipChanged(uint256 current_tip, MillisecondsDouble timeout) override
942942
{
943-
// Interrupt check interval
944-
const MillisecondsDouble tick{1000};
945-
auto now{std::chrono::steady_clock::now()};
946-
auto deadline = now + timeout;
947-
// std::chrono does not check against overflow
948-
if (deadline < now) deadline = std::chrono::steady_clock::time_point::max();
943+
if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
949944
{
950945
WAIT_LOCK(notifications().m_tip_block_mutex, lock);
951-
while ((notifications().m_tip_block == uint256() || notifications().m_tip_block == current_tip) && !chainman().m_interrupt) {
952-
now = std::chrono::steady_clock::now();
953-
if (now >= deadline) break;
954-
notifications().m_tip_block_cv.wait_until(lock, std::min(deadline, now + tick));
955-
}
946+
notifications().m_tip_block_cv.wait_for(lock, timeout, [&]() EXCLUSIVE_LOCKS_REQUIRED(notifications().m_tip_block_mutex) {
947+
return (notifications().m_tip_block != current_tip && notifications().m_tip_block != uint256::ZERO) || chainman().m_interrupt;
948+
});
956949
}
957950
// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
958951
LOCK(::cs_main);

src/node/kernel_notifications.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ class KernelNotifications : public kernel::Notifications
5757
Mutex m_tip_block_mutex;
5858
std::condition_variable m_tip_block_cv GUARDED_BY(m_tip_block_mutex);
5959
//! The block for which the last blockTip notification was received for.
60-
uint256 m_tip_block GUARDED_BY(m_tip_block_mutex);
60+
//! The initial ZERO means that no block has been connected yet, which may
61+
//! be true even long after startup, until shutdown.
62+
uint256 m_tip_block GUARDED_BY(m_tip_block_mutex){uint256::ZERO};
6163

6264
private:
6365
const std::function<bool()>& m_shutdown_request;

0 commit comments

Comments
 (0)