@@ -825,8 +825,18 @@ impl Chain {
825825 return Ok ( BlockSyncResponse :: None ) ;
826826 }
827827
828- // Find common block between header chain and block chain.
829- let mut oldest_height = header_head. height ;
828+ let next_epoch_id =
829+ self . get_block_header ( & block_head. last_block_hash ) ?. next_epoch_id ( ) . clone ( ) ;
830+
831+ // Don't run State Sync if header head is not more than one epoch ahead.
832+ if block_head. epoch_id != header_head. epoch_id && next_epoch_id != header_head. epoch_id {
833+ if block_head. height < header_head. height . saturating_sub ( block_fetch_horizon) {
834+ // Epochs are different and we are too far from horizon, State Sync is needed
835+ return Ok ( BlockSyncResponse :: StateNeeded ) ;
836+ }
837+ }
838+
839+ // Find hashes of blocks to sync
830840 let mut current = self . get_block_header ( & header_head. last_block_hash ) . map ( |h| h. clone ( ) ) ;
831841 while let Ok ( header) = current {
832842 if header. height ( ) <= block_head. height {
@@ -835,21 +845,9 @@ impl Chain {
835845 }
836846 }
837847
838- oldest_height = header. height ( ) ;
839848 hashes. push ( * header. hash ( ) ) ;
840849 current = self . get_previous_header ( & header) . map ( |h| h. clone ( ) ) ;
841850 }
842- let next_epoch_id =
843- self . get_block_header ( & block_head. last_block_hash ) ?. next_epoch_id ( ) . clone ( ) ;
844-
845- // Don't run State Sync if header head is not more than one epoch ahead.
846- if block_head. epoch_id != header_head. epoch_id && next_epoch_id != header_head. epoch_id {
847- let sync_head = self . sync_head ( ) ?;
848- if oldest_height < sync_head. height . saturating_sub ( block_fetch_horizon) {
849- // Epochs are different and we are too far from horizon, State Sync is needed
850- return Ok ( BlockSyncResponse :: StateNeeded ) ;
851- }
852- }
853851
854852 // Sort hashes by height
855853 hashes. reverse ( ) ;
0 commit comments