@@ -430,7 +430,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
430430 }
431431 height := latest .Number .Uint64 ()
432432
433- origin , err := d .findAncestor (p , height )
433+ origin , err := d .findAncestor (p , latest )
434434 if err != nil {
435435 return err
436436 }
@@ -587,41 +587,85 @@ func (d *Downloader) fetchHeight(p *peerConnection) (*types.Header, error) {
587587 }
588588}
589589
590+ // calculateRequestSpan calculates what headers to request from a peer when trying to determine the
591+ // common ancestor.
592+ // It returns parameters to be used for peer.RequestHeadersByNumber:
593+ // from - starting block number
594+ // count - number of headers to request
595+ // skip - number of headers to skip
596+ // and also returns 'max', the last block which is expected to be returned by the remote peers,
597+ // given the (from,count,skip)
598+ func calculateRequestSpan (remoteHeight , localHeight uint64 ) (int64 , int , int , uint64 ) {
599+ var (
600+ from int
601+ count int
602+ MaxCount = MaxHeaderFetch / 16
603+ )
604+ // requestHead is the highest block that we will ask for. If requestHead is not offset,
605+ // the highest block that we will get is 16 blocks back from head, which means we
606+ // will fetch 14 or 15 blocks unnecessarily in the case the height difference
607+ // between us and the peer is 1-2 blocks, which is most common
608+ requestHead := int (remoteHeight ) - 1
609+ if requestHead < 0 {
610+ requestHead = 0
611+ }
612+ // requestBottom is the lowest block we want included in the query
613+ // Ideally, we want to include just below own head
614+ requestBottom := int (localHeight - 1 )
615+ if requestBottom < 0 {
616+ requestBottom = 0
617+ }
618+ totalSpan := requestHead - requestBottom
619+ span := 1 + totalSpan / MaxCount
620+ if span < 2 {
621+ span = 2
622+ }
623+ if span > 16 {
624+ span = 16
625+ }
626+
627+ count = 1 + totalSpan / span
628+ if count > MaxCount {
629+ count = MaxCount
630+ }
631+ if count < 2 {
632+ count = 2
633+ }
634+ from = requestHead - (count - 1 )* span
635+ if from < 0 {
636+ from = 0
637+ }
638+ max := from + (count - 1 )* span
639+ return int64 (from ), count , span - 1 , uint64 (max )
640+ }
641+
590642// findAncestor tries to locate the common ancestor link of the local chain and
591643// a remote peers blockchain. In the general case when our node was in sync and
592644// on the correct chain, checking the top N links should already get us a match.
593645// In the rare scenario when we ended up on a long reorganisation (i.e. none of
594646// the head links match), we do a binary search to find the common ancestor.
595- func (d * Downloader ) findAncestor (p * peerConnection , height uint64 ) (uint64 , error ) {
647+ func (d * Downloader ) findAncestor (p * peerConnection , remoteHeader * types. Header ) (uint64 , error ) {
596648 // Figure out the valid ancestor range to prevent rewrite attacks
597- floor , ceil := int64 (- 1 ), d .lightchain .CurrentHeader ().Number .Uint64 ()
598-
599- if d .mode == FullSync {
600- ceil = d .blockchain .CurrentBlock ().NumberU64 ()
601- } else if d .mode == FastSync {
602- ceil = d .blockchain .CurrentFastBlock ().NumberU64 ()
649+ var (
650+ floor = int64 (- 1 )
651+ localHeight uint64
652+ remoteHeight = remoteHeader .Number .Uint64 ()
653+ )
654+ switch d .mode {
655+ case FullSync :
656+ localHeight = d .blockchain .CurrentBlock ().NumberU64 ()
657+ case FastSync :
658+ localHeight = d .blockchain .CurrentFastBlock ().NumberU64 ()
659+ default :
660+ localHeight = d .lightchain .CurrentHeader ().Number .Uint64 ()
603661 }
604- if ceil >= MaxForkAncestry {
605- floor = int64 (ceil - MaxForkAncestry )
662+ p .log .Debug ("Looking for common ancestor" , "local" , localHeight , "remote" , remoteHeight )
663+ if localHeight >= MaxForkAncestry {
664+ floor = int64 (localHeight - MaxForkAncestry )
606665 }
607- p .log .Debug ("Looking for common ancestor" , "local" , ceil , "remote" , height )
608666
609- // Request the topmost blocks to short circuit binary ancestor lookup
610- head := ceil
611- if head > height {
612- head = height
613- }
614- from := int64 (head ) - int64 (MaxHeaderFetch )
615- if from < 0 {
616- from = 0
617- }
618- // Span out with 15 block gaps into the future to catch bad head reports
619- limit := 2 * MaxHeaderFetch / 16
620- count := 1 + int ((int64 (ceil )- from )/ 16 )
621- if count > limit {
622- count = limit
623- }
624- go p .peer .RequestHeadersByNumber (uint64 (from ), count , 15 , false )
667+ from , count , skip , max := calculateRequestSpan (remoteHeight , localHeight )
668+ go p .peer .RequestHeadersByNumber (uint64 (from ), count , skip , false )
625669
626670 // Wait for the remote response to the head fetch
627671 number , hash := uint64 (0 ), common.Hash {}
@@ -647,30 +691,26 @@ func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, err
647691 return 0 , errEmptyHeaderSet
648692 }
649693 // Make sure the peer's reply conforms to the request
650- for i := 0 ; i < len (headers ); i ++ {
651- if number := headers [i ].Number .Int64 (); number != from + int64 (i )* 16 {
652- p .log .Warn ("Head headers broke chain ordering" , "index" , i , "requested" , from + int64 (i )* 16 , "received" , number )
694+ for i , header := range headers {
695+ expectNumber := from + int64 (i )* int64 ((skip + 1 ))
696+ if number := header .Number .Int64 (); number != expectNumber {
697+ p .log .Warn ("Head headers broke chain ordering" , "index" , i , "requested" , expectNumber , "received" , number )
653698 return 0 , errInvalidChain
654699 }
655700 }
656701 // Check if a common ancestor was found
657702 finished = true
658703 for i := len (headers ) - 1 ; i >= 0 ; i -- {
659704 // Skip any headers that underflow/overflow our requested set
660- if headers [i ].Number .Int64 () < from || headers [i ].Number .Uint64 () > ceil {
705+ if headers [i ].Number .Int64 () < from || headers [i ].Number .Uint64 () > max {
661706 continue
662707 }
663708 // Otherwise check if we already know the header or not
664709 h := headers [i ].Hash ()
665710 n := headers [i ].Number .Uint64 ()
666- if (d .mode == FullSync && d .blockchain .HasBlock (h , n )) || (d .mode != FullSync && d .lightchain .HasHeader (h , n )) {
711+ if (d .mode == FullSync && d .blockchain .HasBlock (h , n )) ||
712+ (d .mode != FullSync && d .lightchain .HasHeader (h , n )) {
667713 number , hash = n , h
668-
669- // If every header is known, even future ones, the peer straight out lied about its head
670- if number > height && i == limit - 1 {
671- p .log .Warn ("Lied about chain head" , "reported" , height , "found" , number )
672- return 0 , errStallingPeer
673- }
674714 break
675715 }
676716 }
@@ -694,7 +734,7 @@ func (d *Downloader) findAncestor(p *peerConnection, height uint64) (uint64, err
694734 return number , nil
695735 }
696736 // Ancestor not found, we need to binary search over our chain
697- start , end := uint64 (0 ), head
737+ start , end := uint64 (0 ), remoteHeight
698738 if floor > 0 {
699739 start = uint64 (floor )
700740 }
0 commit comments