index: add explicit early exit in NextSyncBlock() when the input is the chain tip

When pindex_prev is the chain tip, return earlier and explicitly rather than
mixing it with the reorg case. For more detail, please see PR:
https://github.com/bitcoin/bitcoin/pull/32875

Co-authored-by: l0rinc <pap.lorinc@gmail.com>
Co-authored-by: optout <optout@nostrplebs.com>
Co-authored-by: furszy <matiasfurszyfer@protonmail.com>
This commit is contained in:
Hao Xu
2025-07-04 23:19:55 +08:00
parent 52e8c1ce32
commit db3c25cfae

View File

@@ -155,11 +155,15 @@ static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain&
return chain.Genesis();
}
const CBlockIndex* pindex = chain.Next(pindex_prev);
if (pindex) {
if (const auto* pindex{chain.Next(pindex_prev)}) {
return pindex;
}
// If there is no next block, we might be synced
if (pindex_prev == chain.Tip()) {
return nullptr;
}
// 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.
return chain.Next(chain.FindFork(pindex_prev));