From fa3d62cf7b3501a056b34c5458c14d2fe6a55bd7 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Tue, 23 Nov 2021 18:07:00 +0100 Subject: Move FindForkInGlobalIndex from BlockManager to CChainState The helper was moved in commit b026e318c39f59a06e29f1b25c7f577e01b25ccb, which also mentioned that it could be moved to CChainState. So do that, as the functionality is not block-storage related. This also allows to drop one function argument. --- src/index/base.cpp | 2 +- src/net_processing.cpp | 4 ++-- src/node/interfaces.cpp | 4 ++-- src/validation.cpp | 13 +++++++------ src/validation.h | 6 +++--- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index 8525dcbfa0..419b6fa982 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -65,7 +65,7 @@ bool BaseIndex::Init() if (locator.IsNull()) { m_best_block_index = nullptr; } else { - m_best_block_index = m_chainstate->m_blockman.FindForkInGlobalIndex(active_chain, locator); + m_best_block_index = m_chainstate->FindForkInGlobalIndex(locator); } m_synced = m_best_block_index.load() == active_chain.Tip(); if (!m_synced) { diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 2a30afdb5b..d4f0e94056 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3083,7 +3083,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, LOCK(cs_main); // Find the last block the caller has in the main chain - const CBlockIndex* pindex = m_chainman.m_blockman.FindForkInGlobalIndex(m_chainman.ActiveChain(), locator); + const CBlockIndex* pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator); // Send the rest of the chain if (pindex) @@ -3203,7 +3203,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, else { // Find the last block the caller has in the main chain - pindex = m_chainman.m_blockman.FindForkInGlobalIndex(m_chainman.ActiveChain(), locator); + pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator); if (pindex) pindex = m_chainman.ActiveChain().Next(pindex); } diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 075842ef73..8109dce2c0 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -494,8 +494,8 @@ public: std::optional findLocatorFork(const CBlockLocator& locator) override { LOCK(cs_main); - const CChain& active = Assert(m_node.chainman)->ActiveChain(); - if (CBlockIndex* fork = m_node.chainman->m_blockman.FindForkInGlobalIndex(active, locator)) { + const CChainState& active = Assert(m_node.chainman)->ActiveChainstate(); + if (CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) { return fork->nHeight; } return std::nullopt; diff --git a/src/validation.cpp b/src/validation.cpp index 1aac71fb0f..c2d86d89c5 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -155,23 +155,24 @@ CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const return it == m_block_index.end() ? nullptr : it->second; } -CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator) +CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const { AssertLockHeld(cs_main); // Find the latest block common to locator and chain - we expect that // locator.vHave is sorted descending by height. for (const uint256& hash : locator.vHave) { - CBlockIndex* pindex = LookupBlockIndex(hash); + CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)}; if (pindex) { - if (chain.Contains(pindex)) + if (m_chain.Contains(pindex)) { return pindex; - if (pindex->GetAncestor(chain.Height()) == chain.Tip()) { - return chain.Tip(); + } + if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) { + return m_chain.Tip(); } } } - return chain.Genesis(); + return m_chain.Genesis(); } bool CheckInputScripts(const CTransaction& tx, TxValidationState& state, diff --git a/src/validation.h b/src/validation.h index 534df9bc87..27b8f4e906 100644 --- a/src/validation.h +++ b/src/validation.h @@ -466,9 +466,6 @@ public: CBlockIndex* LookupBlockIndex(const uint256& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main); - /** Find the last common block between the parameter chain and a locator. */ - CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator) EXCLUSIVE_LOCKS_REQUIRED(cs_main); - //! Returns last CBlockIndex* that is a checkpoint CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main); @@ -756,6 +753,9 @@ public: /** Check whether we are doing an initial block download (synchronizing from disk or network) */ bool IsInitialBlockDownload() const; + /** Find the last common block of this chain and a locator. */ + CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main); + /** * Make various assertions about the state of the block index. * -- cgit v1.2.3