From fa927055dd43dda945396574273a210186beec9f Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Wed, 17 Jul 2024 08:58:47 +0200 Subject: [PATCH] refactor: Make m_last_notified_header private --- src/validation.cpp | 24 ++++++++++++------------ src/validation.h | 8 +++++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index 72ff24d5848..c49ec404cab 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3405,24 +3405,24 @@ static SynchronizationState GetSynchronizationState(bool init, bool blockfiles_i return SynchronizationState::INIT_DOWNLOAD; } -static bool NotifyHeaderTip(ChainstateManager& chainman) LOCKS_EXCLUDED(cs_main) +bool ChainstateManager::NotifyHeaderTip() { bool fNotify = false; bool fInitialBlockDownload = false; CBlockIndex* pindexHeader = nullptr; { - LOCK(cs_main); - pindexHeader = chainman.m_best_header; + LOCK(GetMutex()); + pindexHeader = m_best_header; - if (pindexHeader != chainman.m_last_notified_header) { + if (pindexHeader != m_last_notified_header) { fNotify = true; - fInitialBlockDownload = chainman.IsInitialBlockDownload(); - chainman.m_last_notified_header = pindexHeader; + fInitialBlockDownload = IsInitialBlockDownload(); + m_last_notified_header = pindexHeader; } } - // Send block tip changed notifications without cs_main + // Send block tip changed notifications without the lock held if (fNotify) { - chainman.GetNotifications().headerTip(GetSynchronizationState(fInitialBlockDownload, chainman.m_blockman.m_blockfiles_indexed), pindexHeader->nHeight, pindexHeader->nTime, false); + GetNotifications().headerTip(GetSynchronizationState(fInitialBlockDownload, m_blockman.m_blockfiles_indexed), pindexHeader->nHeight, pindexHeader->nTime, false); } return fNotify; } @@ -4378,7 +4378,7 @@ bool ChainstateManager::ProcessNewBlockHeaders(const std::vector& } } } - if (NotifyHeaderTip(*this)) { + if (NotifyHeaderTip()) { if (IsInitialBlockDownload() && ppindex && *ppindex) { const CBlockIndex& last_accepted{**ppindex}; int64_t blocks_left{(NodeClock::now() - last_accepted.Time()) / GetConsensus().PowTargetSpacing()}; @@ -4549,7 +4549,7 @@ bool ChainstateManager::ProcessNewBlock(const std::shared_ptr& blo } } - NotifyHeaderTip(*this); + NotifyHeaderTip(); BlockValidationState state; // Only used to report errors, not invalidity - ignore it if (!ActiveChainstate().ActivateBestChain(state, block)) { @@ -5126,7 +5126,7 @@ void ChainstateManager::LoadExternalBlockFile( } } - NotifyHeaderTip(*this); + NotifyHeaderTip(); if (!blocks_with_unknown_parent) continue; @@ -5152,7 +5152,7 @@ void ChainstateManager::LoadExternalBlockFile( } range.first++; blocks_with_unknown_parent->erase(it); - NotifyHeaderTip(*this); + NotifyHeaderTip(); } } } catch (const std::exception& e) { diff --git a/src/validation.h b/src/validation.h index ea4ec842423..08e672c6209 100644 --- a/src/validation.h +++ b/src/validation.h @@ -906,6 +906,11 @@ private: CBlockIndex* m_best_invalid GUARDED_BY(::cs_main){nullptr}; + /** The last header for which a headerTip notification was issued. */ + CBlockIndex* m_last_notified_header GUARDED_BY(GetMutex()){nullptr}; + + bool NotifyHeaderTip() LOCKS_EXCLUDED(GetMutex()); + //! Internal helper for ActivateSnapshot(). //! //! De-serialization of a snapshot that is created with @@ -1063,9 +1068,6 @@ public: /** Best header we've seen so far (used for getheaders queries' starting points). */ CBlockIndex* m_best_header GUARDED_BY(::cs_main){nullptr}; - /** The last header for which a headerTip notification was issued. */ - CBlockIndex* m_last_notified_header GUARDED_BY(::cs_main){nullptr}; - //! The total number of bytes available for us to use across all in-memory //! coins caches. This will be split somehow across chainstates. int64_t m_total_coinstip_cache{0};