@@ -99,6 +99,7 @@ type Downloader struct {
9999 mode SyncMode // Synchronisation mode defining the strategy used (per sync cycle)
100100 mux * event.TypeMux // Event multiplexer to announce sync operation events
101101
102+ genesis uint64 // Genesis block number to limit sync to (e.g. light client CHT)
102103 queue * queue // Scheduler for selecting the hashes to download
103104 peers * peerSet // Set of active peers from which download can proceed
104105 stateDB ethdb.Database
@@ -664,7 +665,28 @@ func (d *Downloader) findAncestor(p *peerConnection, remoteHeader *types.Header)
664665 }
665666 p .log .Debug ("Looking for common ancestor" , "local" , localHeight , "remote" , remoteHeight )
666667 if localHeight >= MaxForkAncestry {
668+ // We're above the max reorg threshold, find the earliest fork point
667669 floor = int64 (localHeight - MaxForkAncestry )
670+
671+ // If we're doing a light sync, ensure the floor doesn't go below the CHT, as
672+ // all headers before that point will be missing.
673+ if d .mode == LightSync {
674+ // If we dont know the current CHT position, find it
675+ if d .genesis == 0 {
676+ header := d .lightchain .CurrentHeader ()
677+ for header != nil {
678+ d .genesis = header .Number .Uint64 ()
679+ if floor >= int64 (d .genesis )- 1 {
680+ break
681+ }
682+ header = d .lightchain .GetHeaderByHash (header .ParentHash )
683+ }
684+ }
685+ // We already know the "genesis" block number, cap floor to that
686+ if floor < int64 (d .genesis )- 1 {
687+ floor = int64 (d .genesis ) - 1
688+ }
689+ }
668690 }
669691 from , count , skip , max := calculateRequestSpan (remoteHeight , localHeight )
670692
0 commit comments