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.
This commit is contained in:
9 files changed
+25
-17
No files matched your search
@@ -74,7 +74,7 @@ int main(int argc, char* argv[])
|
|||||||
class KernelNotifications : public kernel::Notifications
|
class KernelNotifications : public kernel::Notifications
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
kernel::InterruptResult blockTip(SynchronizationState, CBlockIndex&) override
|
kernel::InterruptResult blockTip(SynchronizationState, CBlockIndex&, double) override
|
||||||
{
|
{
|
||||||
std::cout << "Block tip changed" << std::endl;
|
std::cout << "Block tip changed" << std::endl;
|
||||||
return {};
|
return {};
|
||||||
|
|||||||
+3
-3
@@ -1787,10 +1787,10 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
#if HAVE_SYSTEM
|
#if HAVE_SYSTEM
|
||||||
const std::string block_notify = args.GetArg("-blocknotify", "");
|
const std::string block_notify = args.GetArg("-blocknotify", "");
|
||||||
if (!block_notify.empty()) {
|
if (!block_notify.empty()) {
|
||||||
uiInterface.NotifyBlockTip_connect([block_notify](SynchronizationState sync_state, const CBlockIndex* pBlockIndex) {
|
uiInterface.NotifyBlockTip_connect([block_notify](SynchronizationState sync_state, const CBlockIndex& block, double /* verification_progress */) {
|
||||||
if (sync_state != SynchronizationState::POST_INIT || !pBlockIndex) return;
|
if (sync_state != SynchronizationState::POST_INIT) return;
|
||||||
std::string command = block_notify;
|
std::string command = block_notify;
|
||||||
ReplaceAll(command, "%s", pBlockIndex->GetBlockHash().GetHex());
|
ReplaceAll(command, "%s", block.GetBlockHash().GetHex());
|
||||||
std::thread t(runCommand, command);
|
std::thread t(runCommand, command);
|
||||||
t.detach(); // thread runs free
|
t.detach(); // thread runs free
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class Notifications
|
|||||||
public:
|
public:
|
||||||
virtual ~Notifications() = default;
|
virtual ~Notifications() = default;
|
||||||
|
|
||||||
[[nodiscard]] virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) { return {}; }
|
[[nodiscard]] virtual InterruptResult blockTip(SynchronizationState state, CBlockIndex& index, double verification_progress) { return {}; }
|
||||||
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
|
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
|
||||||
virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {}
|
virtual void progress(const bilingual_str& title, int progress_percent, bool resume_possible) {}
|
||||||
virtual void warningSet(Warning id, const bilingual_str& message) {}
|
virtual void warningSet(Warning id, const bilingual_str& message) {}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ void CClientUIInterface::NotifyNumConnectionsChanged(int newNumConnections) { re
|
|||||||
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
|
void CClientUIInterface::NotifyNetworkActiveChanged(bool networkActive) { return g_ui_signals.NotifyNetworkActiveChanged(networkActive); }
|
||||||
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
|
void CClientUIInterface::NotifyAlertChanged() { return g_ui_signals.NotifyAlertChanged(); }
|
||||||
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
|
void CClientUIInterface::ShowProgress(const std::string& title, int nProgress, bool resume_possible) { return g_ui_signals.ShowProgress(title, nProgress, resume_possible); }
|
||||||
void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex* i) { return g_ui_signals.NotifyBlockTip(s, i); }
|
void CClientUIInterface::NotifyBlockTip(SynchronizationState s, const CBlockIndex& block, double verification_progress) { return g_ui_signals.NotifyBlockTip(s, block, verification_progress); }
|
||||||
void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, int64_t height, int64_t timestamp, bool presync) { return g_ui_signals.NotifyHeaderTip(s, height, timestamp, presync); }
|
void CClientUIInterface::NotifyHeaderTip(SynchronizationState s, int64_t height, int64_t timestamp, bool presync) { return g_ui_signals.NotifyHeaderTip(s, height, timestamp, presync); }
|
||||||
void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); }
|
void CClientUIInterface::BannedListChanged() { return g_ui_signals.BannedListChanged(); }
|
||||||
|
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ public:
|
|||||||
ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible);
|
ADD_SIGNALS_DECL_WRAPPER(ShowProgress, void, const std::string& title, int nProgress, bool resume_possible);
|
||||||
|
|
||||||
/** New block has been accepted */
|
/** New block has been accepted */
|
||||||
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex*);
|
ADD_SIGNALS_DECL_WRAPPER(NotifyBlockTip, void, SynchronizationState, const CBlockIndex& block, double verification_progress);
|
||||||
|
|
||||||
/** Best header has changed */
|
/** Best header has changed */
|
||||||
ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, SynchronizationState, int64_t height, int64_t timestamp, bool presync);
|
ADD_SIGNALS_DECL_WRAPPER(NotifyHeaderTip, void, SynchronizationState, int64_t height, int64_t timestamp, bool presync);
|
||||||
|
|||||||
@@ -409,10 +409,8 @@ public:
|
|||||||
}
|
}
|
||||||
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
|
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
|
||||||
{
|
{
|
||||||
return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn, this](SynchronizationState sync_state, const CBlockIndex* block) {
|
return MakeSignalHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex& block, double verification_progress) {
|
||||||
LOCK(chainman().GetMutex());
|
fn(sync_state, BlockTip{block.nHeight, block.GetBlockTime(), block.GetBlockHash()}, verification_progress);
|
||||||
fn(sync_state, BlockTip{block->nHeight, block->GetBlockTime(), block->GetBlockHash()},
|
|
||||||
chainman().GuessVerificationProgress(block));
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
|
std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) override
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ static void AlertNotify(const std::string& strMessage)
|
|||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
|
|
||||||
kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index)
|
kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state, CBlockIndex& index, double verification_progress)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
LOCK(m_tip_block_mutex);
|
LOCK(m_tip_block_mutex);
|
||||||
@@ -57,7 +57,7 @@ kernel::InterruptResult KernelNotifications::blockTip(SynchronizationState state
|
|||||||
m_tip_block_cv.notify_all();
|
m_tip_block_cv.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
uiInterface.NotifyBlockTip(state, &index);
|
uiInterface.NotifyBlockTip(state, index, verification_progress);
|
||||||
if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
|
if (m_stop_at_height && index.nHeight >= m_stop_at_height) {
|
||||||
if (!m_shutdown_request()) {
|
if (!m_shutdown_request()) {
|
||||||
LogError("Failed to send shutdown signal after reaching stop height\n");
|
LogError("Failed to send shutdown signal after reaching stop height\n");
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public:
|
|||||||
KernelNotifications(const std::function<bool()>& shutdown_request, std::atomic<int>& exit_status, node::Warnings& warnings)
|
KernelNotifications(const std::function<bool()>& shutdown_request, std::atomic<int>& exit_status, node::Warnings& warnings)
|
||||||
: m_shutdown_request(shutdown_request), m_exit_status{exit_status}, m_warnings{warnings} {}
|
: m_shutdown_request(shutdown_request), m_exit_status{exit_status}, m_warnings{warnings} {}
|
||||||
|
|
||||||
[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index) override EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex);
|
[[nodiscard]] kernel::InterruptResult blockTip(SynchronizationState state, CBlockIndex& index, double verification_progress) override EXCLUSIVE_LOCKS_REQUIRED(!m_tip_block_mutex);
|
||||||
|
|
||||||
void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override;
|
void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override;
|
||||||
|
|
||||||
|
|||||||
+13
-3
@@ -3544,7 +3544,11 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
|
|||||||
m_chainman.m_options.signals->UpdatedBlockTip(pindexNewTip, pindexFork, still_in_ibd);
|
m_chainman.m_options.signals->UpdatedBlockTip(pindexNewTip, pindexFork, still_in_ibd);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kernel::IsInterrupted(m_chainman.GetNotifications().blockTip(GetSynchronizationState(still_in_ibd, m_chainman.m_blockman.m_blockfiles_indexed), *pindexNewTip))) {
|
if (kernel::IsInterrupted(m_chainman.GetNotifications().blockTip(
|
||||||
|
/*state=*/GetSynchronizationState(still_in_ibd, m_chainman.m_blockman.m_blockfiles_indexed),
|
||||||
|
/*index=*/*pindexNewTip,
|
||||||
|
/*verification_progress=*/m_chainman.GuessVerificationProgress(pindexNewTip))))
|
||||||
|
{
|
||||||
// Just breaking and returning success for now. This could
|
// Just breaking and returning success for now. This could
|
||||||
// be changed to bubble up the kernel::Interrupted value to
|
// be changed to bubble up the kernel::Interrupted value to
|
||||||
// the caller so the caller could distinguish between
|
// the caller so the caller could distinguish between
|
||||||
@@ -3777,7 +3781,10 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* pinde
|
|||||||
// parameter indicating the source of the tip change so hooks can
|
// parameter indicating the source of the tip change so hooks can
|
||||||
// distinguish user-initiated invalidateblock changes from other
|
// distinguish user-initiated invalidateblock changes from other
|
||||||
// changes.
|
// changes.
|
||||||
(void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(m_chainman.IsInitialBlockDownload(), m_chainman.m_blockman.m_blockfiles_indexed), *to_mark_failed->pprev);
|
(void)m_chainman.GetNotifications().blockTip(
|
||||||
|
/*state=*/GetSynchronizationState(m_chainman.IsInitialBlockDownload(), m_chainman.m_blockman.m_blockfiles_indexed),
|
||||||
|
/*index=*/*to_mark_failed->pprev,
|
||||||
|
/*verification_progress=*/WITH_LOCK(m_chainman.GetMutex(), return m_chainman.GuessVerificationProgress(to_mark_failed->pprev)));
|
||||||
|
|
||||||
// Fire ActiveTipChange now for the current chain tip to make sure clients are notified.
|
// Fire ActiveTipChange now for the current chain tip to make sure clients are notified.
|
||||||
// ActivateBestChain may call this as well, but not necessarily.
|
// ActivateBestChain may call this as well, but not necessarily.
|
||||||
@@ -4675,7 +4682,10 @@ bool Chainstate::LoadChainTip()
|
|||||||
// Ensure KernelNotifications m_tip_block is set even if no new block arrives.
|
// Ensure KernelNotifications m_tip_block is set even if no new block arrives.
|
||||||
if (this->GetRole() != ChainstateRole::BACKGROUND) {
|
if (this->GetRole() != ChainstateRole::BACKGROUND) {
|
||||||
// Ignoring return value for now.
|
// Ignoring return value for now.
|
||||||
(void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed), *pindex);
|
(void)m_chainman.GetNotifications().blockTip(
|
||||||
|
/*state=*/GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed),
|
||||||
|
/*index=*/*pindex,
|
||||||
|
/*verification_progress=*/m_chainman.GuessVerificationProgress(tip));
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in new issue
Block a user