@@ -646,9 +646,7 @@ func (cs *chainSync) doSync(req *network.BlockRequestMessage) *workerError {
646646
647647 if req .Direction == network .Descending {
648648 // reverse blocks before pre-validating and placing in ready queue
649- for i , j := 0 , len (resp .BlockData )- 1 ; i < j ; i , j = i + 1 , j - 1 {
650- resp .BlockData [i ], resp .BlockData [j ] = resp .BlockData [j ], resp .BlockData [i ]
651- }
649+ reverseBlockData (resp .BlockData )
652650 }
653651
654652 // perform some pre-validation of response, error if failure
@@ -897,10 +895,18 @@ func workerToRequests(w *worker) ([]*network.BlockRequestMessage, error) {
897895 } else {
898896 // in tip-syncing mode, we know the hash of the block on the fork we wish to sync
899897 start , _ = variadic .NewUint64OrHash (w .startHash )
898+
899+ // if we're doing descending requests and not at the last (highest starting) request,
900+ // then use number as start block
901+ if w .direction == network .Descending && i != numRequests - 1 {
902+ start = variadic .MustNewUint64OrHash (startNumber )
903+ }
900904 }
901905
902906 var end * common.Hash
903- if ! w .targetHash .IsEmpty () {
907+ if ! w .targetHash .IsEmpty () && i == numRequests - 1 {
908+ // if we're on our last request (which should contain the target hash),
909+ // then add it
904910 end = & w .targetHash
905911 }
906912
@@ -911,7 +917,21 @@ func workerToRequests(w *worker) ([]*network.BlockRequestMessage, error) {
911917 Direction : w .direction ,
912918 Max : & max ,
913919 }
914- startNumber += maxResponseSize
920+
921+ switch w .direction {
922+ case network .Ascending :
923+ startNumber += maxResponseSize
924+ case network .Descending :
925+ startNumber -= maxResponseSize
926+ }
927+ }
928+
929+ // if our direction is descending, we want to send out the request with the lowest
930+ // startNumber first
931+ if w .direction == network .Descending {
932+ for i , j := 0 , len (reqs )- 1 ; i < j ; i , j = i + 1 , j - 1 {
933+ reqs [i ], reqs [j ] = reqs [j ], reqs [i ]
934+ }
915935 }
916936
917937 return reqs , nil
0 commit comments