mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 14:53:43 +01:00
interfaces: refactor: move waitTipChanged implementation to miner
- This commit creates a function `WaitTipChanged` that waits for the connected tip to change until timeout elapsed. - This function is now used by `waitTipChanged` Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
@@ -547,4 +547,33 @@ std::optional<BlockRef> GetTip(ChainstateManager& chainman)
|
||||
if (!tip) return {};
|
||||
return BlockRef{tip->GetBlockHash(), tip->nHeight};
|
||||
}
|
||||
|
||||
std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout)
|
||||
{
|
||||
Assume(timeout >= 0ms); // No internal callers should use a negative timeout
|
||||
if (timeout < 0ms) timeout = 0ms;
|
||||
if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
|
||||
auto deadline{std::chrono::steady_clock::now() + timeout};
|
||||
{
|
||||
WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
|
||||
// For callers convenience, wait longer than the provided timeout
|
||||
// during startup for the tip to be non-null. That way this function
|
||||
// always returns valid tip information when possible and only
|
||||
// returns null when shutting down, not when timing out.
|
||||
kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
|
||||
return kernel_notifications.TipBlock() || chainman.m_interrupt;
|
||||
});
|
||||
if (chainman.m_interrupt) return {};
|
||||
// At this point TipBlock is set, so continue to wait until it is
|
||||
// different then `current_tip` provided by caller.
|
||||
kernel_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
|
||||
return Assume(kernel_notifications.TipBlock()) != current_tip || chainman.m_interrupt;
|
||||
});
|
||||
}
|
||||
if (chainman.m_interrupt) return {};
|
||||
|
||||
// Must release m_tip_block_mutex before getTip() locks cs_main, to
|
||||
// avoid deadlocks.
|
||||
return GetTip(chainman);
|
||||
}
|
||||
} // namespace node
|
||||
|
||||
Reference in New Issue
Block a user