File tree Expand file tree Collapse file tree 3 files changed +9
-13
lines changed Expand file tree Collapse file tree 3 files changed +9
-13
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,8 @@ class Mining
61
61
virtual std::optional<BlockRef> getTip () = 0;
62
62
63
63
/* *
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.
65
66
*
66
67
* @param[in] current_tip block hash of the current chain tip. Function waits
67
68
* for the chain tip to differ from this.
Original file line number Diff line number Diff line change @@ -940,19 +940,12 @@ class MinerImpl : public Mining
940
940
941
941
BlockRef waitTipChanged (uint256 current_tip, MillisecondsDouble timeout) override
942
942
{
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
949
944
{
950
945
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
+ });
956
949
}
957
950
// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
958
951
LOCK (::cs_main);
Original file line number Diff line number Diff line change @@ -57,7 +57,9 @@ class KernelNotifications : public kernel::Notifications
57
57
Mutex m_tip_block_mutex;
58
58
std::condition_variable m_tip_block_cv GUARDED_BY (m_tip_block_mutex);
59
59
// ! 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};
61
63
62
64
private:
63
65
const std::function<bool ()>& m_shutdown_request;
You can’t perform that action at this time.
0 commit comments