Change CChain::FindFork() to take ref

The internal null-guard in FindFork() was removed in favor of adding any missing guards at call sites.
This commit is contained in:
optout
2026-02-03 11:18:01 +01:00
parent 20b58e281a
commit c5eb283bca
8 changed files with 14 additions and 14 deletions

View File

@@ -47,10 +47,9 @@ CBlockLocator GetLocator(const CBlockIndex* index)
return CBlockLocator{LocatorEntries(index)};
}
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {
if (pindex == nullptr) {
return nullptr;
}
const CBlockIndex* CChain::FindFork(const CBlockIndex& index) const
{
const auto* pindex{&index};
if (pindex->nHeight > Height())
pindex = pindex->GetAncestor(Height());
while (pindex && !Contains(*pindex))

View File

@@ -440,7 +440,7 @@ public:
void SetTip(CBlockIndex& block);
/** Find the last common block between this chain and a block index entry. */
const CBlockIndex* FindFork(const CBlockIndex* pindex) const;
const CBlockIndex* FindFork(const CBlockIndex& index) const;
/** Find the earliest block with timestamp equal or greater than the given time and height equal or greater than the given height. */
CBlockIndex* FindEarliestAtLeast(int64_t nTime, int height) const;

View File

@@ -166,7 +166,7 @@ static const CBlockIndex* NextSyncBlock(const CBlockIndex* const pindex_prev, CC
// Since block is not in the chain, return the next block in the chain AFTER the last common ancestor.
// Caller will be responsible for rewinding back to the common ancestor.
const auto* fork{chain.FindFork(pindex_prev)};
const auto* fork{chain.FindFork(*pindex_prev)};
// Common ancestor must exist (genesis).
return chain.Next(*Assert(fork));
}

View File

@@ -2374,7 +2374,7 @@ bool StartIndexBackgroundSync(NodeContext& node)
LogWarning("Failed to find block manager entry for best block %s from %s, falling back to genesis for index sync",
summary.best_block_hash.ToString(), summary.name);
} else if (!index_chain.Contains(*pindex)) {
pindex = index_chain.FindFork(pindex);
pindex = index_chain.FindFork(*pindex);
}
}
if (!pindex) {

View File

@@ -1649,11 +1649,12 @@ static RPCMethod getchaintips()
/* Construct the output array. */
UniValue res(UniValue::VARR);
for (const CBlockIndex* block : setTips) {
CHECK_NONFATAL(block);
UniValue obj(UniValue::VOBJ);
obj.pushKV("height", block->nHeight);
obj.pushKV("hash", block->phashBlock->GetHex());
const int branchLen = block->nHeight - active_chain.FindFork(block)->nHeight;
const int branchLen = block->nHeight - active_chain.FindFork(*block)->nHeight;
obj.pushKV("branchlen", branchLen);
std::string status;

View File

@@ -96,13 +96,13 @@ BOOST_AUTO_TEST_CASE(findfork_tests)
const auto check_same{[](const CChain& chain, const auto& blocks) {
for (const auto& block : blocks) {
BOOST_CHECK_EQUAL(chain.FindFork(&block), &block);
BOOST_CHECK_EQUAL(chain.FindFork(block), &block);
}
}};
const auto check_fork_point{[](const CChain& chain, const auto& blocks, const CBlockIndex& fork_point) {
for (const auto& block : blocks) {
BOOST_CHECK_EQUAL(chain.FindFork(&block), &fork_point);
BOOST_CHECK_EQUAL(chain.FindFork(block), &fork_point);
}
}};
@@ -139,7 +139,7 @@ BOOST_AUTO_TEST_CASE(findfork_tests)
// Invalid test case. Mixing chains is not supported
CBlockIndex block_on_unrelated_chain;
BOOST_CHECK_EQUAL(chain_long.FindFork(&block_on_unrelated_chain), nullptr);
BOOST_CHECK_EQUAL(chain_long.FindFork(block_on_unrelated_chain), nullptr);
}
BOOST_AUTO_TEST_CASE(chain_test)

View File

@@ -102,7 +102,7 @@ FUZZ_TARGET(block_index_tree, .init = initialize_block_index_tree)
assert(best_tip); // Should at least return current tip
if (best_tip == chain.Tip()) break; // Nothing to do
// Rewind chain to forking point
const CBlockIndex* fork = chain.FindFork(best_tip);
const CBlockIndex* fork = chain.FindFork(*best_tip);
// If we can't go back to the fork point due to pruned data, abort this run. In reality, a pruned node would also currently just crash in this scenario.
// This is very unlikely to happen due to the minimum pruning threshold of 550MiB.
CBlockIndex* it = chain.Tip();

View File

@@ -3183,7 +3183,7 @@ bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex*
if (m_mempool) AssertLockHeld(m_mempool->cs);
const CBlockIndex* pindexOldTip = m_chain.Tip();
const CBlockIndex* pindexFork = m_chain.FindFork(pindexMostWork);
const CBlockIndex* pindexFork = pindexMostWork ? m_chain.FindFork(*pindexMostWork) : nullptr;
// Disconnect active blocks which are no longer in the best chain.
bool fBlocksDisconnected = false;
@@ -3397,7 +3397,7 @@ bool Chainstate::ActivateBestChain(BlockValidationState& state, std::shared_ptr<
} while (!m_chain.Tip() || (starting_tip && CBlockIndexWorkComparator()(m_chain.Tip(), starting_tip)));
if (!blocks_connected) return true;
const CBlockIndex* pindexFork = m_chain.FindFork(starting_tip);
const CBlockIndex* pindexFork = starting_tip ? m_chain.FindFork(*starting_tip) : nullptr;
bool still_in_ibd = m_chainman.IsInitialBlockDownload();
if (was_in_ibd && !still_in_ibd) {