@@ -152,7 +152,7 @@ impl Extension {
152
152
client. block_header ( & parent_hash. into ( ) ) . expect ( "Parent header of the snapshot header must exist" ) ;
153
153
return State :: SnapshotBody {
154
154
block : hash,
155
- prev_root : parent. state_root ( ) ,
155
+ prev_root : parent. transactions_root ( ) ,
156
156
}
157
157
}
158
158
@@ -408,8 +408,8 @@ impl NetworkExtension<Event> for Extension {
408
408
State :: SnapshotHeader ( _, num) => {
409
409
for id in & peer_ids {
410
410
self . send_header_request ( id, RequestMessage :: Headers {
411
- start_number : num,
412
- max_count : 1 ,
411
+ start_number : num - 1 ,
412
+ max_count : 2 ,
413
413
} ) ;
414
414
}
415
415
}
@@ -518,89 +518,41 @@ pub enum Event {
518
518
519
519
impl Extension {
520
520
fn new_headers ( & mut self , imported : Vec < BlockHash > , enacted : Vec < BlockHash > , retracted : Vec < BlockHash > ) {
521
- if let Some ( next_state) = match self . state {
522
- State :: SnapshotHeader ( hash, ..) => {
523
- if imported. contains ( & hash) {
524
- let header = self . client . block_header ( & BlockId :: Hash ( hash) ) . expect ( "Imported header must exist" ) ;
525
- Some ( State :: SnapshotChunk {
526
- block : hash,
527
- restore : SnapshotRestore :: new ( header. state_root ( ) ) ,
528
- } )
529
- } else {
530
- None
521
+ if let State :: Full = self . state {
522
+ let peer_ids: Vec < _ > = self . header_downloaders . keys ( ) . cloned ( ) . collect ( ) ;
523
+ for id in peer_ids {
524
+ if let Some ( peer) = self . header_downloaders . get_mut ( & id) {
525
+ peer. mark_as_imported ( imported. clone ( ) ) ;
531
526
}
532
527
}
533
- State :: SnapshotBody {
534
- ..
535
- } => None ,
536
- State :: SnapshotChunk {
537
- ..
538
- } => None ,
539
- State :: Full => {
540
- let peer_ids: Vec < _ > = self . header_downloaders . keys ( ) . cloned ( ) . collect ( ) ;
541
- for id in peer_ids {
542
- if let Some ( peer) = self . header_downloaders . get_mut ( & id) {
543
- peer. mark_as_imported ( imported. clone ( ) ) ;
544
- }
545
- }
546
- let mut headers_to_download: Vec < _ > = enacted
547
- . into_iter ( )
548
- . map ( |hash| self . client . block_header ( & BlockId :: Hash ( hash) ) . expect ( "Enacted header must exist" ) )
549
- . collect ( ) ;
550
- headers_to_download. sort_unstable_by_key ( EncodedHeader :: number) ;
551
- #[ allow( clippy:: redundant_closure) ]
552
- // False alarm. https://github.com/rust-lang/rust-clippy/issues/1439
553
- headers_to_download. dedup_by_key ( |h| h. hash ( ) ) ;
554
-
555
- let headers: Vec < _ > = headers_to_download
556
- . into_iter ( )
557
- . filter ( |header| self . client . block_body ( & BlockId :: Hash ( header. hash ( ) ) ) . is_none ( ) )
558
- . collect ( ) ; // FIXME: No need to collect here if self is not borrowed.
559
- for header in headers {
560
- let parent = self
561
- . client
562
- . block_header ( & BlockId :: Hash ( header. parent_hash ( ) ) )
563
- . expect ( "Enacted header must have parent" ) ;
564
- let is_empty = header. transactions_root ( ) == parent. transactions_root ( ) ;
565
- self . body_downloader . add_target ( & header. decode ( ) , is_empty) ;
566
- }
567
- self . body_downloader . remove_target ( & retracted) ;
568
- None
528
+ let mut headers_to_download: Vec < _ > = enacted
529
+ . into_iter ( )
530
+ . map ( |hash| self . client . block_header ( & BlockId :: Hash ( hash) ) . expect ( "Enacted header must exist" ) )
531
+ . collect ( ) ;
532
+ headers_to_download. sort_unstable_by_key ( EncodedHeader :: number) ;
533
+ #[ allow( clippy:: redundant_closure) ]
534
+ // False alarm. https://github.com/rust-lang/rust-clippy/issues/1439
535
+ headers_to_download. dedup_by_key ( |h| h. hash ( ) ) ;
536
+
537
+ let headers: Vec < _ > = headers_to_download
538
+ . into_iter ( )
539
+ . filter ( |header| self . client . block_body ( & BlockId :: Hash ( header. hash ( ) ) ) . is_none ( ) )
540
+ . collect ( ) ; // FIXME: No need to collect here if self is not borrowed.
541
+ for header in headers {
542
+ let parent = self
543
+ . client
544
+ . block_header ( & BlockId :: Hash ( header. parent_hash ( ) ) )
545
+ . expect ( "Enacted header must have parent" ) ;
546
+ let is_empty = header. transactions_root ( ) == parent. transactions_root ( ) ;
547
+ self . body_downloader . add_target ( & header. decode ( ) , is_empty) ;
569
548
}
570
- } {
571
- cdebug ! ( SYNC , "Transitioning state to {:?}" , next_state) ;
572
- self . state = next_state;
549
+ self . body_downloader . remove_target ( & retracted) ;
573
550
}
574
551
}
575
552
576
553
fn new_blocks ( & mut self , imported : Vec < BlockHash > , invalid : Vec < BlockHash > ) {
577
- if let Some ( next_state) = match self . state {
578
- State :: SnapshotHeader ( hash, ..) => {
579
- if imported. contains ( & hash) {
580
- let header = self . client . block_header ( & BlockId :: Hash ( hash) ) . expect ( "Imported header must exist" ) ;
581
- Some ( State :: SnapshotChunk {
582
- block : hash,
583
- restore : SnapshotRestore :: new ( header. state_root ( ) ) ,
584
- } )
585
- } else {
586
- None
587
- }
588
- }
589
- State :: SnapshotBody {
590
- ..
591
- } => unimplemented ! ( ) ,
592
- State :: SnapshotChunk {
593
- ..
594
- } => None ,
595
- State :: Full => {
596
- self . body_downloader . remove_target ( & imported) ;
597
- self . body_downloader . remove_target ( & invalid) ;
598
- None
599
- }
600
- } {
601
- cdebug ! ( SYNC , "Transitioning state to {:?}" , next_state) ;
602
- self . state = next_state;
603
- }
554
+ self . body_downloader . remove_target ( & imported) ;
555
+ self . body_downloader . remove_target ( & invalid) ;
604
556
605
557
let chain_info = self . client . chain_info ( ) ;
606
558
@@ -858,20 +810,20 @@ impl Extension {
858
810
ctrace ! ( SYNC , "Received header response from({}) with length({})" , from, headers. len( ) ) ;
859
811
match self . state {
860
812
State :: SnapshotHeader ( hash, _) => match headers {
861
- [ header] if header. hash ( ) == hash => {
813
+ [ parent , header] if header. hash ( ) == hash => {
862
814
match self . client . import_bootstrap_header ( & header) {
863
- Err ( BlockImportError :: Import ( ImportError :: AlreadyInChain ) ) => {
864
- self . state = State :: SnapshotChunk {
815
+ Ok ( _ ) | Err ( BlockImportError :: Import ( ImportError :: AlreadyInChain ) ) => {
816
+ self . state = State :: SnapshotBody {
865
817
block : hash,
866
- restore : SnapshotRestore :: new ( * header . state_root ( ) ) ,
818
+ prev_root : * parent . transactions_root ( ) ,
867
819
} ;
820
+ cdebug ! ( SYNC , "Transitioning state to {:?}" , self . state) ;
868
821
}
869
822
Err ( BlockImportError :: Import ( ImportError :: AlreadyQueued ) ) => { }
870
823
// FIXME: handle import errors
871
824
Err ( err) => {
872
825
cwarn ! ( SYNC , "Cannot import header({}): {:?}" , header. hash( ) , err) ;
873
826
}
874
- _ => { }
875
827
}
876
828
}
877
829
_ => cdebug ! (
0 commit comments