@@ -602,13 +602,12 @@ impl<N: Network> Service<N> {
602602 let end_block = end_block?. ok_or ( BlockRangeScannerError :: BlockNotFound ( end_height) ) ?;
603603
604604 // normalize block range
605- let ( start_block, end_block) =
606- match start_block. header ( ) . number ( ) . cmp ( & end_block. header ( ) . number ( ) ) {
607- Ordering :: Greater => ( end_block, start_block) ,
608- _ => ( start_block, end_block) ,
609- } ;
605+ let ( from, to) = match start_block. header ( ) . number ( ) . cmp ( & end_block. header ( ) . number ( ) ) {
606+ Ordering :: Greater => ( start_block, end_block) ,
607+ _ => ( end_block, start_block) ,
608+ } ;
610609
611- self . stream_rewind ( start_block , end_block ) . await ?;
610+ self . stream_rewind ( from , to ) . await ?;
612611
613612 _ = self . subscriber . take ( ) ;
614613
@@ -624,18 +623,16 @@ impl<N: Network> Service<N> {
624623 let blocks_read_per_epoch = self . config . blocks_read_per_epoch ;
625624
626625 // for checking whether reorg occurred
627- let mut to_hash = to . header ( ) . hash ( ) ;
626+ let mut tip_hash = from . header ( ) . hash ( ) ;
628627
629628 let from = from. header ( ) . number ( ) ;
630629 let to = to. header ( ) . number ( ) ;
631- let stream_end = from;
632630
633631 // we're iterating in reverse
634- let mut batch_from = to ;
632+ let mut batch_from = from ;
635633
636- while batch_from >= stream_end {
637- let batch_to =
638- batch_from. saturating_sub ( blocks_read_per_epoch as u64 - 1 ) . max ( stream_end) ;
634+ while batch_from >= to {
635+ let batch_to = batch_from. saturating_sub ( blocks_read_per_epoch as u64 - 1 ) . max ( to) ;
639636
640637 // stream the range regularly, i.e. from smaller block number to greater
641638 self . send_to_subscriber ( BlockRangeMessage :: Data ( batch_to..=batch_from) ) . await ;
@@ -645,27 +642,29 @@ impl<N: Network> Service<N> {
645642 debug ! ( batch_count = batch_count, "Processed rewind batches" ) ;
646643 }
647644
648- // check early if end of stream achieved to avoid subtraction overflow when `stream_end
645+ // check early if end of stream achieved to avoid subtraction overflow when `to
649646 // == 0`
650- if batch_to == stream_end {
647+ if batch_to == to {
651648 break ;
652649 }
653650
654- if self . reorg_detected ( to_hash ) . await ? {
655- info ! ( block_number = %to , hash = %to_hash , "Reorg detected" ) ;
651+ if self . reorg_detected ( tip_hash ) . await ? {
652+ info ! ( block_number = %from , hash = %tip_hash , "Reorg detected" ) ;
656653 self . send_to_subscriber ( BlockRangeMessage :: Status ( ScannerStatus :: ReorgDetected ) )
657654 . await ;
658655 // restart rewind
659- batch_from = to ;
656+ batch_from = from ;
660657 // store the updated end block hash
661- to_hash = self
658+ tip_hash = self
662659 . provider
663- . get_block_by_number ( to . into ( ) )
660+ . get_block_by_number ( from . into ( ) )
664661 . await ?
665662 . expect ( "Chain should have the same height post-reorg" )
666663 . header ( )
667664 . hash ( ) ;
668665 } else {
666+ // SAFETY: `batch_to` is always greater than `to`, so `batch_to - 1` is always
667+ // a valid unsigned integer
669668 batch_from = batch_to - 1 ;
670669 }
671670 }
0 commit comments