Skip to content

Commit fa3d62c

Browse files
author
MarcoFalke
committed
Move FindForkInGlobalIndex from BlockManager to CChainState
The helper was moved in commit b026e31, 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.
1 parent c09b41d commit fa3d62c

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

src/index/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ bool BaseIndex::Init()
6565
if (locator.IsNull()) {
6666
m_best_block_index = nullptr;
6767
} else {
68-
m_best_block_index = m_chainstate->m_blockman.FindForkInGlobalIndex(active_chain, locator);
68+
m_best_block_index = m_chainstate->FindForkInGlobalIndex(locator);
6969
}
7070
m_synced = m_best_block_index.load() == active_chain.Tip();
7171
if (!m_synced) {

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,7 +3083,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
30833083
LOCK(cs_main);
30843084

30853085
// Find the last block the caller has in the main chain
3086-
const CBlockIndex* pindex = m_chainman.m_blockman.FindForkInGlobalIndex(m_chainman.ActiveChain(), locator);
3086+
const CBlockIndex* pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
30873087

30883088
// Send the rest of the chain
30893089
if (pindex)
@@ -3203,7 +3203,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
32033203
else
32043204
{
32053205
// Find the last block the caller has in the main chain
3206-
pindex = m_chainman.m_blockman.FindForkInGlobalIndex(m_chainman.ActiveChain(), locator);
3206+
pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
32073207
if (pindex)
32083208
pindex = m_chainman.ActiveChain().Next(pindex);
32093209
}

src/node/interfaces.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ class ChainImpl : public Chain
494494
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
495495
{
496496
LOCK(cs_main);
497-
const CChain& active = Assert(m_node.chainman)->ActiveChain();
498-
if (CBlockIndex* fork = m_node.chainman->m_blockman.FindForkInGlobalIndex(active, locator)) {
497+
const CChainState& active = Assert(m_node.chainman)->ActiveChainstate();
498+
if (CBlockIndex* fork = active.FindForkInGlobalIndex(locator)) {
499499
return fork->nHeight;
500500
}
501501
return std::nullopt;

src/validation.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,24 @@ CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const
155155
return it == m_block_index.end() ? nullptr : it->second;
156156
}
157157

158-
CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
158+
CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
159159
{
160160
AssertLockHeld(cs_main);
161161

162162
// Find the latest block common to locator and chain - we expect that
163163
// locator.vHave is sorted descending by height.
164164
for (const uint256& hash : locator.vHave) {
165-
CBlockIndex* pindex = LookupBlockIndex(hash);
165+
CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
166166
if (pindex) {
167-
if (chain.Contains(pindex))
167+
if (m_chain.Contains(pindex)) {
168168
return pindex;
169-
if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
170-
return chain.Tip();
169+
}
170+
if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) {
171+
return m_chain.Tip();
171172
}
172173
}
173174
}
174-
return chain.Genesis();
175+
return m_chain.Genesis();
175176
}
176177

177178
bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,

src/validation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,6 @@ class BlockManager
466466

467467
CBlockIndex* LookupBlockIndex(const uint256& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
468468

469-
/** Find the last common block between the parameter chain and a locator. */
470-
CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
471-
472469
//! Returns last CBlockIndex* that is a checkpoint
473470
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
474471

@@ -756,6 +753,9 @@ class CChainState
756753
/** Check whether we are doing an initial block download (synchronizing from disk or network) */
757754
bool IsInitialBlockDownload() const;
758755

756+
/** Find the last common block of this chain and a locator. */
757+
CBlockIndex* FindForkInGlobalIndex(const CBlockLocator& locator) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
758+
759759
/**
760760
* Make various assertions about the state of the block index.
761761
*

0 commit comments

Comments
 (0)