Skip to content

Commit b026e31

Browse files
committed
validation: Move FindForkInGlobalIndex to BlockManager
[META] This commit should be followed up by removing the comments and assertions meant only to show that the change is correct. FindForkInGlobalIndex only acts on BlockManager. Note to reviewers: Since FindForkInGlobalIndex is always called with ::ChainActive() as its first parameter, it is possible to move FindForkInGlobalIndex to CChainState and remove this const CChain& parameter to instead use m_chain. However, it seems like the original intention was for FindForkInGlobalIndex to work with _any_ chain, not just the current active chain. Let me know if this should be changed.
1 parent 3664a15 commit b026e31

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

src/index/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ bool BaseIndex::Init()
6262
if (locator.IsNull()) {
6363
m_best_block_index = nullptr;
6464
} else {
65-
m_best_block_index = FindForkInGlobalIndex(::ChainActive(), locator);
65+
m_best_block_index = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator);
6666
}
6767
m_synced = m_best_block_index.load() == ::ChainActive().Tip();
6868
return true;

src/net_processing.cpp

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

29702970
// Find the last block the caller has in the main chain
2971-
const CBlockIndex* pindex = FindForkInGlobalIndex(::ChainActive(), locator);
2971+
const CBlockIndex* pindex = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator);
29722972

29732973
// Send the rest of the chain
29742974
if (pindex)
@@ -3088,7 +3088,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
30883088
else
30893089
{
30903090
// Find the last block the caller has in the main chain
3091-
pindex = FindForkInGlobalIndex(::ChainActive(), locator);
3091+
pindex = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator);
30923092
if (pindex)
30933093
pindex = ::ChainActive().Next(pindex);
30943094
}

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ class ChainImpl : public Chain
447447
{
448448
LOCK(cs_main);
449449
const CChain& active = Assert(m_node.chainman)->ActiveChain();
450-
if (CBlockIndex* fork = FindForkInGlobalIndex(active, locator)) {
450+
if (CBlockIndex* fork = g_chainman.m_blockman.FindForkInGlobalIndex(active, locator)) {
451451
return fork->nHeight;
452452
}
453453
return nullopt;

src/validation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,15 @@ CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash)
175175
return it == m_block_index.end() ? nullptr : it->second;
176176
}
177177

178-
CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
178+
CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
179179
{
180180
AssertLockHeld(cs_main);
181181

182+
assert(std::addressof(g_chainman.m_blockman) == std::addressof(*this));
182183
// Find the latest block common to locator and chain - we expect that
183184
// locator.vHave is sorted descending by height.
184185
for (const uint256& hash : locator.vHave) {
185-
CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
186+
CBlockIndex* pindex = LookupBlockIndex(hash);
186187
if (pindex) {
187188
if (chain.Contains(pindex))
188189
return pindex;

src/validation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,6 @@ class CVerifyDB {
311311
bool VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
312312
};
313313

314-
/** Find the last common block between the parameter chain and a locator. */
315-
CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
316-
317314
enum DisconnectResult
318315
{
319316
DISCONNECT_OK, // All good.
@@ -433,6 +430,9 @@ class BlockManager
433430

434431
CBlockIndex* LookupBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
435432

433+
/** Find the last common block between the parameter chain and a locator. */
434+
CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
435+
436436
~BlockManager() {
437437
Unload();
438438
}

0 commit comments

Comments
 (0)