Skip to content

Commit

Permalink
node: Track last block that received a blockTip notification
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCharlatan committed Aug 22, 2024
1 parent e2843f2 commit 3c66506
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <node/interface_ui.h>
#include <node/mini_miner.h>
#include <node/miner.h>
#include <node/kernel_notifications.h>
#include <node/transaction.h>
#include <node/types.h>
#include <node/warnings.h>
Expand Down Expand Up @@ -879,14 +880,14 @@ class MinerImpl : public Mining
// std::chrono does not check against overflow
if (deadline < now) deadline = std::chrono::steady_clock::time_point::max();
{
WAIT_LOCK(g_best_block_mutex, lock);
while ((g_best_block == uint256() || g_best_block == current_tip) && !chainman().m_interrupt) {
WAIT_LOCK(notifications().m_tip_block_mutex, lock);
while ((notifications().m_tip_block == uint256() || notifications().m_tip_block == current_tip) && !chainman().m_interrupt) {
now = std::chrono::steady_clock::now();
if (now >= deadline) break;
g_best_block_cv.wait_until(lock, std::min(deadline, now + tick));
notifications().m_tip_block_cv.wait_until(lock, std::min(deadline, now + tick));
}
}
// Must release g_best_block_mutex before locking cs_main, to avoid deadlocks.
// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
LOCK(::cs_main);
return BlockRef{chainman().ActiveChain().Tip()->GetBlockHash(), chainman().ActiveChain().Tip()->nHeight};
}
Expand Down Expand Up @@ -923,6 +924,7 @@ class MinerImpl : public Mining

NodeContext* context() override { return &m_node; }
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
KernelNotifications& notifications() { return *Assert(m_node.notifications); }
NodeContext& m_node;
};
} // namespace
Expand Down
6 changes: 6 additions & 0 deletions src/node/kernel_notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ namespace node {

kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
{
{
LOCK(m_tip_block_mutex);
m_tip_block= index.GetBlockHash();
m_tip_block_cv.notify_all();
}

uiInterface.NotifyBlockTip(state, &index);
if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
if (!m_shutdown()) {
Expand Down
12 changes: 11 additions & 1 deletion src/node/kernel_notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include <kernel/notifications_interface.h>

#include <sync.h>
#include <threadsafety.h>
#include <uint256.h>

#include <atomic>
#include <cstdint>

Expand Down Expand Up @@ -34,7 +38,7 @@ class KernelNotifications : public kernel::Notifications
KernelNotifications(util::SignalInterrupt& shutdown, std::atomic<int>& exit_status, node::Warnings& warnings)
: m_shutdown(shutdown), m_exit_status{exit_status}, m_warnings{warnings} {}

[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override;
[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex);

void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override;

Expand All @@ -52,6 +56,12 @@ class KernelNotifications : public kernel::Notifications
int m_stop_at_height{DEFAULT_STOPATHEIGHT};
//! Useful for tests, can be set to false to avoid shutdown on fatal error.
bool m_shutdown_on_fatal_error{true};

Mutex m_tip_block_mutex;
std::condition_variable m_tip_block_cv;
//! The block for which the last blockTip notification was received for.
uint256 m_tip_block;

private:
util::SignalInterrupt& m_shutdown;
std::atomic<int>& m_exit_status;
Expand Down

0 comments on commit 3c66506

Please sign in to comment.