blockchain: add BestChainHeaderForkHeight to return the fork point between the best chain and the best header chain
What changed, and why it matters
This commit adds a new read-only helper function that reports where the chain of fully-downloaded blocks and the chain of known block headers last shared a common block. It does not change any existing behavior, fix a bug, or alter security logic. It simply exposes information already maintained internally so other code can use it for block downloads after reorganizations.
No security action required. Review as normal code addition if consuming this API in future commits.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces BestChainHeaderForkHeight() in blockchain/chain.go. It acquires chainLock for reading, calls FindFork between b.bestChain and b.bestHeader.Tip(), and returns the fork height (or 0 if none). This is a pure accessor with no state mutation, no validation change, and no consensus logic change. The function is intended to support choosing a starting height for block downloads when the best header chain has diverged from the best chain due to a reorg.
Changed components
blockchain/chain.goInspect captured patch +15 / −0
diff --git a/blockchain/chain.go b/blockchain/chain.go
index 960e280..fb624ef 100644
--- a/blockchain/chain.go
+++ b/blockchain/chain.go
@@ -1353,6 +1353,21 @@ func (b *BlockChain) BestHeader() (chainhash.Hash, int32) {
return best.hash, best.height
}
+// BestChainHeaderForkHeight returns the height of the fork point between the
+// best chain and the best header chain. This can be used to determine the
+// starting height for block downloads when the best header chain has diverged
+// from the best chain due to a reorg.
+func (b *BlockChain) BestChainHeaderForkHeight() int32 {
+ b.chainLock.RLock()
+ defer b.chainLock.RUnlock()
+
+ fork := b.bestChain.FindFork(b.bestHeader.Tip())
+ if fork == nil {
+ return 0
+ }
+ return fork.height
+}
+
// TipStatus is the status of a chain tip.
type TipStatus byte
Why this scored 13/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.