Skip to content

Commit f74e745

Browse files
fix: backward block downloader (#78)
1 parent a10b440 commit f74e745

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

execution/p2p/bbd.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,18 @@ func (bbd *BackwardBlockDownloader) downloadHeaderChainBackwards(
342342
}
343343
chainLen++
344344
lastHeader = header
345-
h, err := headerReader.HeaderByHash(ctx, header.ParentHash)
345+
346+
// First check if the header itself exists locally - if it does, it's the connection point.
347+
h, err := headerReader.HeaderByHash(ctx, header.Hash())
348+
if err != nil {
349+
return nil, err
350+
}
351+
if h != nil {
352+
connectionPoint = header
353+
break
354+
}
355+
356+
h, err = headerReader.HeaderByHash(ctx, header.ParentHash)
346357
if err != nil {
347358
return nil, err
348359
}

polygon/sync/sync.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,14 @@ func (s *Sync) applyNewMilestoneOnTip(ctx context.Context, event EventNewMilesto
284284
return s.handleMilestoneTipMismatch(ctx, ccb, milestone)
285285
}
286286

287-
return ccb.PruneRoot(milestone.EndBlock().Uint64())
287+
// Prune to (endBlock - 1) to keep one more block in CCB for backward downloads.
288+
// This makes pruning less aggressive and helps backward downloads find connection points.
289+
endBlock := milestone.EndBlock().Uint64()
290+
pruneTo := endBlock
291+
if endBlock > 0 {
292+
pruneTo = endBlock - 1
293+
}
294+
return ccb.PruneRoot(pruneTo)
288295
}
289296

290297
func (s *Sync) applyNewBlockChainOnTip(ctx context.Context, blockChain []*types.Block, ccb *CanonicalChainBuilder, source EventSource, peerId *p2p.PeerId) error {

0 commit comments

Comments
 (0)