@@ -120,8 +120,9 @@ pub struct ChainSync<B: BlockT> {
120120 /// A set of hashes of blocks that are being downloaded or have been
121121 /// downloaded and are queued for import.
122122 queue_blocks : HashSet < B :: Hash > ,
123- /// The best block number that we are currently importing.
124- best_importing_number : NumberFor < B > ,
123+ /// The best block number that was successfully imported into the chain.
124+ /// This can not decrease.
125+ best_imported_number : NumberFor < B > ,
125126 /// Finality proof handler.
126127 request_builder : Option < BoxFinalityProofRequestBuilder < B > > ,
127128 /// Fork sync targets.
@@ -301,12 +302,12 @@ impl<B: BlockT> ChainSync<B> {
301302 blocks : BlockCollection :: new ( ) ,
302303 best_queued_hash : info. chain . best_hash ,
303304 best_queued_number : info. chain . best_number ,
305+ best_imported_number : info. chain . best_number ,
304306 extra_finality_proofs : ExtraRequests :: new ( ) ,
305307 extra_justifications : ExtraRequests :: new ( ) ,
306308 role,
307309 required_block_attributes,
308310 queue_blocks : Default :: default ( ) ,
309- best_importing_number : Zero :: zero ( ) ,
310311 request_builder,
311312 fork_targets : Default :: default ( ) ,
312313 is_idle : false ,
@@ -756,14 +757,8 @@ impl<B: BlockT> ChainSync<B> {
756757 self . on_block_queued ( h, n)
757758 }
758759
759- let new_best_importing_number = new_blocks. last ( )
760- . and_then ( |b| b. header . as_ref ( ) . map ( |h| * h. number ( ) ) )
761- . unwrap_or_else ( || Zero :: zero ( ) ) ;
762-
763760 self . queue_blocks . extend ( new_blocks. iter ( ) . map ( |b| b. hash ) ) ;
764761
765- self . best_importing_number = std:: cmp:: max ( new_best_importing_number, self . best_importing_number ) ;
766-
767762 Ok ( OnBlockData :: Import ( origin, new_blocks) )
768763 }
769764
@@ -897,6 +892,10 @@ impl<B: BlockT> ChainSync<B> {
897892 trace ! ( target: "sync" , "Block imported but requires finality proof {}: {:?}" , number, hash) ;
898893 self . request_finality_proof ( & hash, number) ;
899894 }
895+
896+ if number > self . best_imported_number {
897+ self . best_imported_number = number;
898+ }
900899 } ,
901900 Err ( BlockImportError :: IncompleteHeader ( who) ) => {
902901 if let Some ( peer) = who {
@@ -936,9 +935,6 @@ impl<B: BlockT> ChainSync<B> {
936935 for hash in hashes {
937936 self . queue_blocks . remove ( & hash) ;
938937 }
939- if has_error {
940- self . best_importing_number = Zero :: zero ( )
941- }
942938
943939 self . is_idle = false ;
944940 output. into_iter ( )
@@ -1130,11 +1126,10 @@ impl<B: BlockT> ChainSync<B> {
11301126 fn restart < ' a > ( & ' a mut self ) -> impl Iterator < Item = Result < ( PeerId , BlockRequest < B > ) , BadPeer > > + ' a
11311127 {
11321128 self . queue_blocks . clear ( ) ;
1133- self . best_importing_number = Zero :: zero ( ) ;
11341129 self . blocks . clear ( ) ;
11351130 let info = self . client . info ( ) ;
11361131 self . best_queued_hash = info. chain . best_hash ;
1137- self . best_queued_number = info. chain . best_number ;
1132+ self . best_queued_number = std :: cmp :: max ( info. chain . best_number , self . best_imported_number ) ;
11381133 self . is_idle = false ;
11391134 debug ! ( target: "sync" , "Restarted with {} ({})" , self . best_queued_number, self . best_queued_hash) ;
11401135 let old_peers = std:: mem:: replace ( & mut self . peers , HashMap :: new ( ) ) ;
0 commit comments