2424//! The `BasicQueue` and `BasicVerifier` traits allow serial queues to be
2525//! instantiated simply.
2626
27- use crate :: block_import:: { ImportBlock , BlockImport , JustificationImport , ImportResult , BlockOrigin } ;
27+ use crate :: block_import:: {
28+ BlockImport , BlockOrigin , ImportBlock , ImportedAux , ImportResult , JustificationImport ,
29+ } ;
2830use crossbeam_channel:: { self as channel, Receiver , Sender } ;
2931
3032use std:: sync:: Arc ;
@@ -307,31 +309,37 @@ impl<B: BlockT> BlockImporter<B> {
307309
308310 match result {
309311 Ok ( BlockImportResult :: ImportedKnown ( number) ) => link. block_imported ( & hash, number) ,
310- Ok ( BlockImportResult :: ImportedUnknown ( number) ) => {
311- link. block_imported ( & hash, number)
312- }
313- Ok ( BlockImportResult :: ImportedUnjustified ( number) ) => {
312+ Ok ( BlockImportResult :: ImportedUnknown ( number, aux) ) => {
314313 link. block_imported ( & hash, number) ;
315- link. request_justification ( & hash, number) ;
314+
315+ if aux. clear_justification_requests {
316+ trace ! ( target: "sync" , "Block imported clears all pending justification requests {}: {:?}" , number, hash) ;
317+ link. clear_justification_requests ( ) ;
318+ }
319+
320+ if aux. needs_justification {
321+ trace ! ( target: "sync" , "Block imported but requires justification {}: {:?}" , number, hash) ;
322+ link. request_justification ( & hash, number) ;
323+ }
316324 } ,
317325 Err ( BlockImportError :: IncompleteHeader ( who) ) => {
318326 if let Some ( peer) = who {
319327 link. note_useless_and_restart_sync ( peer, "Sent block with incomplete header to import" ) ;
320328 }
321- }
329+ } ,
322330 Err ( BlockImportError :: VerificationFailed ( who, e) ) => {
323331 if let Some ( peer) = who {
324332 link. note_useless_and_restart_sync ( peer, & format ! ( "Verification failed: {}" , e) ) ;
325333 }
326- }
334+ } ,
327335 Err ( BlockImportError :: BadBlock ( who) ) => {
328336 if let Some ( peer) = who {
329337 link. note_useless_and_restart_sync ( peer, "Sent us a bad block" ) ;
330338 }
331- }
339+ } ,
332340 Err ( BlockImportError :: UnknownParent ) | Err ( BlockImportError :: Error ) => {
333- link. restart ( )
334- }
341+ link. restart ( ) ;
342+ } ,
335343 } ;
336344 }
337345 if let Some ( link) = self . link . as_ref ( ) {
@@ -448,13 +456,15 @@ impl<B: BlockT, V: 'static + Verifier<B>> BlockImportWorker<B, V> {
448456/// algorithm.
449457pub trait Link < B : BlockT > : Send {
450458 /// Block imported.
451- fn block_imported ( & self , _hash : & B :: Hash , _number : NumberFor < B > ) { }
459+ fn block_imported ( & self , _hash : & B :: Hash , _number : NumberFor < B > ) { }
452460 /// Batch of blocks imported, with or without error.
453461 fn blocks_processed ( & self , _processed_blocks : Vec < B :: Hash > , _has_error : bool ) { }
454462 /// Justification import result.
455- fn justification_imported ( & self , _who : Origin , _hash : & B :: Hash , _number : NumberFor < B > , _success : bool ) { }
463+ fn justification_imported ( & self , _who : Origin , _hash : & B :: Hash , _number : NumberFor < B > , _success : bool ) { }
464+ /// Clear all pending justification requests.
465+ fn clear_justification_requests ( & self ) { }
456466 /// Request a justification for the given block.
457- fn request_justification ( & self , _hash : & B :: Hash , _number : NumberFor < B > ) { }
467+ fn request_justification ( & self , _hash : & B :: Hash , _number : NumberFor < B > ) { }
458468 /// Disconnect from peer.
459469 fn useless_peer ( & self , _who : Origin , _reason : & str ) { }
460470 /// Disconnect from peer and restart sync.
@@ -469,9 +479,7 @@ pub enum BlockImportResult<N: ::std::fmt::Debug + PartialEq> {
469479 /// Imported known block.
470480 ImportedKnown ( N ) ,
471481 /// Imported unknown block.
472- ImportedUnknown ( N ) ,
473- /// Imported unjustified block that requires one.
474- ImportedUnjustified ( N ) ,
482+ ImportedUnknown ( N , ImportedAux ) ,
475483}
476484
477485/// Block import error.
@@ -520,17 +528,7 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
520528 trace ! ( target: "sync" , "Block already in chain {}: {:?}" , number, hash) ;
521529 Ok ( BlockImportResult :: ImportedKnown ( number) )
522530 } ,
523- Ok ( ImportResult :: AlreadyQueued ) => {
524- trace ! ( target: "sync" , "Block already queued {}: {:?}" , number, hash) ;
525- Ok ( BlockImportResult :: ImportedKnown ( number) )
526- } ,
527- Ok ( ImportResult :: Queued ) => {
528- Ok ( BlockImportResult :: ImportedUnknown ( number) )
529- } ,
530- Ok ( ImportResult :: NeedsJustification ) => {
531- trace ! ( target: "sync" , "Block queued but requires justification {}: {:?}" , number, hash) ;
532- Ok ( BlockImportResult :: ImportedUnjustified ( number) )
533- } ,
531+ Ok ( ImportResult :: Imported ( aux) ) => Ok ( BlockImportResult :: ImportedUnknown ( number, aux) ) ,
534532 Ok ( ImportResult :: UnknownParent ) => {
535533 debug ! ( target: "sync" , "Block with unknown parent {}: {:?}, parent: {:?}" , number, hash, parent) ;
536534 Err ( BlockImportError :: UnknownParent )
@@ -547,7 +545,7 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
547545 } ;
548546
549547 match import_error ( import_handle. check_block ( hash, parent) ) ? {
550- BlockImportResult :: ImportedUnknown ( _ ) => ( ) ,
548+ BlockImportResult :: ImportedUnknown { .. } => ( ) ,
551549 r @ _ => return Ok ( r) , // Any other successfull result means that the block is already imported.
552550 }
553551
@@ -629,7 +627,7 @@ mod tests {
629627 assert_eq ! ( link_port. recv( ) , Ok ( LinkMsg :: BlockImported ) ) ;
630628
631629 // Send an unknown
632- let results = vec ! [ ( Ok ( BlockImportResult :: ImportedUnknown ( Default :: default ( ) ) ) , Default :: default ( ) ) ] ;
630+ let results = vec ! [ ( Ok ( BlockImportResult :: ImportedUnknown ( Default :: default ( ) , Default :: default ( ) ) ) , Default :: default ( ) ) ] ;
633631 let _ = result_sender. send ( BlockImportWorkerMsg :: Imported ( results) ) . ok ( ) . unwrap ( ) ;
634632 assert_eq ! ( link_port. recv( ) , Ok ( LinkMsg :: BlockImported ) ) ;
635633
0 commit comments