Skip to content

Commit fab1e02

Browse files
author
MarcoFalke
committed
refactor: Pass verification_progress into block tip notifications
It is cheap to calculate and the caller does not have to take a lock to calculate it. Also turn pointers that can never be null into references.
1 parent fa76b37 commit fab1e02

9 files changed

+25
-17
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int main(int argc, char* argv[])
7474
class KernelNotifications : public kernel::Notifications
7575
{
7676
public:
77-
kernel::InterruptResult blockTip(SynchronizationState, CBlockIndex&) override
77+
kernel::InterruptResult blockTip(SynchronizationState, CBlockIndex&, double) override
7878
{
7979
std::cout << "Block tip changed" << std::endl;
8080
return {};

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,10 +1787,10 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17871787
#if HAVE_SYSTEM
17881788
const std::string block_notify = args.GetArg("-blocknotify", "");
17891789
if (!block_notify.empty()) {
1790-
uiInterface.NotifyBlockTip_connect([block_notify](SynchronizationState sync_state, const CBlockIndex* pBlockIndex) {
1791-
if (sync_state != SynchronizationState::POST_INIT || !pBlockIndex) return;
1790+
uiInterface.NotifyBlockTip_connect([block_notify](SynchronizationState sync_state, const CBlockIndex& block, double /* verification_progress */) {
1791+
if (sync_state != SynchronizationState::POST_INIT) return;
17921792
std::string command = block_notify;
1793-
ReplaceAll(command, "%s", pBlockIndex->GetBlockHash().GetHex());
1793+
ReplaceAll(command, "%s", block.GetBlockHash().GetHex());
17941794
std::thread t(runCommand, command);
17951795
t.detach(); // thread runs free
17961796
});

src/kernel/notifications_interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Notifications
3737
public:
3838
virtual ~Notifications() = default;
3939

40-
[[nodiscard]] virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) { return {}; }
40+
[[nodiscard]] virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex& index, double verification_progress) { return {}; }
4141
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
4242
virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {}
4343
virtual void warningSet(Warning id, const bilingual_str& message) {}

src/node/interface_ui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { re
5555
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
5656
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
5757
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
58-
void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); }
58+
void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex& block, double verification_progress) { return g_ui_signals.NotifyBlockTip(s, block, verification_progress); }
5959
void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, int64_t height, int64_t timestamp, bool presync) { return g_ui_signals.NotifyHeaderTip(s, height, timestamp, presync); }
6060
void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); }
6161

src/node/interface_ui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class CClientUIInterface
103103
ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible);
104104

105105
/** New block has been accepted */
106-
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex*);
106+
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex& block, double verification_progress);
107107

108108
/** Best header has changed */
109109
ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, SynchronizationState, int64_t height, int64_t timestamp, bool presync);

src/node/interfaces.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,8 @@ class NodeImpl : public Node
409409
}
410410
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
411411
{
412-
return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn, this](SynchronizationState sync_state, const CBlockIndex* block) {
413-
LOCK(chainman().GetMutex());
414-
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
415-
chainman().GuessVerificationProgress(block));
412+
return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex& block, double verification_progress) {
413+
fn(sync_state, BlockTip{block.nHeight, block.GetBlockTime(), block.GetBlockHash()}, verification_progress);
416414
}));
417415
}
418416
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override

src/node/kernel_notifications.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void AlertNotify(const std::string& strMessage)
4848

4949
namespace node {
5050

51-
kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
51+
kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index, double verification_progress)
5252
{
5353
{
5454
LOCK(m_tip_block_mutex);
@@ -57,7 +57,7 @@ kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state
5757
m_tip_block_cv.notify_all();
5858
}
5959

60-
uiInterface.NotifyBlockTip(state, &index);
60+
uiInterface.NotifyBlockTip(state, index, verification_progress);
6161
if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
6262
if (!m_shutdown_request()) {
6363
LogError("Failed to send shutdown signal after reaching stop height\n");

src/node/kernel_notifications.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class KernelNotifications : public kernel::Notifications
3535
KernelNotifications(const std::function<bool()>& shutdown_request, std::atomic<int>& exit_status, node::Warnings& warnings)
3636
: m_shutdown_request(shutdown_request), m_exit_status{exit_status}, m_warnings{warnings} {}
3737

38-
[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex);
38+
[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index, double verification_progress) override EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex);
3939

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

src/validation.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,7 +3544,11 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
35443544
m_chainman.m_options.signals->UpdatedBlockTip(pindexNewTip, pindexFork, still_in_ibd);
35453545
}
35463546

3547-
if (kernel::IsInterrupted(m_chainman.GetNotifications().blockTip(GetSynchronizationState(still_in_ibd, m_chainman.m_blockman.m_blockfiles_indexed), *pindexNewTip))) {
3547+
if (kernel::IsInterrupted(m_chainman.GetNotifications().blockTip(
3548+
/*state=*/GetSynchronizationState(still_in_ibd, m_chainman.m_blockman.m_blockfiles_indexed),
3549+
/*index=*/*pindexNewTip,
3550+
/*verification_progress=*/m_chainman.GuessVerificationProgress(pindexNewTip))))
3551+
{
35483552
// Just breaking and returning success for now. This could
35493553
// be changed to bubble up the kernel::Interrupted value to
35503554
// the caller so the caller could distinguish between
@@ -3777,7 +3781,10 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pinde
37773781
// parameter indicating the source of the tip change so hooks can
37783782
// distinguish user-initiated invalidateblock changes from other
37793783
// changes.
3780-
(void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(m_chainman.IsInitialBlockDownload(), m_chainman.m_blockman.m_blockfiles_indexed), *to_mark_failed->pprev);
3784+
(void)m_chainman.GetNotifications().blockTip(
3785+
/*state=*/GetSynchronizationState(m_chainman.IsInitialBlockDownload(), m_chainman.m_blockman.m_blockfiles_indexed),
3786+
/*index=*/*to_mark_failed->pprev,
3787+
/*verification_progress=*/WITH_LOCK(m_chainman.GetMutex(), return m_chainman.GuessVerificationProgress(to_mark_failed->pprev)));
37813788

37823789
// Fire ActiveTipChange now for the current chain tip to make sure clients are notified.
37833790
// ActivateBestChain may call this as well, but not necessarily.
@@ -4675,7 +4682,10 @@ bool Chainstate::LoadChainTip()
46754682
// Ensure KernelNotifications m_tip_block is set even if no new block arrives.
46764683
if (this->GetRole() != ChainstateRole::BACKGROUND) {
46774684
// Ignoring return value for now.
4678-
(void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed), *pindex);
4685+
(void)m_chainman.GetNotifications().blockTip(
4686+
/*state=*/GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed),
4687+
/*index=*/*pindex,
4688+
/*verification_progress=*/m_chainman.GuessVerificationProgress(tip));
46794689
}
46804690

46814691
return true;

0 commit comments

Comments
 (0)