From db3c25cfae1cc70171cc9bd921a2bf9be2c9da7e Mon Sep 17 00:00:00 2001 From: Hao Xu Date: Fri, 4 Jul 2025 23:19:55 +0800 Subject: [PATCH] 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 Co-authored-by: optout Co-authored-by: furszy --- src/index/base.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/index/base.cpp b/src/index/base.cpp index fba2f4f6c10..2cb3a2a0d45 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -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));