@@ -571,11 +571,8 @@ where
571
571
let network_head = p2p. get_head_header ( ) . await ?;
572
572
let network_head_height = network_head. height ( ) . value ( ) ;
573
573
574
- // If store is empty, intialize it with network head
575
- if store. head_height ( ) . await . is_err ( ) {
576
- store. insert ( network_head. clone ( ) ) . await ?;
577
- }
578
-
574
+ // Insert HEAD to the store and initialize header-sub
575
+ store. insert ( network_head. clone ( ) ) . await ?;
579
576
p2p. init_header_sub ( network_head) . await ?;
580
577
581
578
Ok ( network_head_height)
@@ -795,7 +792,7 @@ mod tests {
795
792
let store = Arc :: new ( store) ;
796
793
797
794
let mut headers = gen. next_many ( 520 ) ;
798
- let network_head = headers . last ( ) . cloned ( ) . unwrap ( ) ;
795
+ let network_head = gen . next ( ) ; // height 546
799
796
800
797
let syncer = Syncer :: start ( SyncerArgs {
801
798
p2p : Arc :: new ( p2p) ,
@@ -816,9 +813,9 @@ mod tests {
816
813
let head_from_syncer = p2p_mock. expect_init_header_sub ( ) . await ;
817
814
assert_eq ! ( head_from_syncer, network_head) ;
818
815
819
- assert_syncing ( & syncer, & store, & [ 1 ..=25 ] , 545 ) . await ;
816
+ assert_syncing ( & syncer, & store, & [ 1 ..=25 , 546 ..= 546 ] , 546 ) . await ;
820
817
821
- // Syncer requested the first batch ([37 , 545])
818
+ // Syncer requested the first batch ([34 , 545])
822
819
handle_session_batch (
823
820
& mut p2p_mock,
824
821
& headers,
@@ -834,7 +831,7 @@ mod tests {
834
831
] ,
835
832
)
836
833
. await ;
837
- assert_syncing ( & syncer, & store, & [ 1 ..=25 , 34 ..=545 ] , 545 ) . await ;
834
+ assert_syncing ( & syncer, & store, & [ 1 ..=25 , 34 ..=546 ] , 546 ) . await ;
838
835
839
836
// Syncer requested the remaining batch ([26, 33])
840
837
let ( height, amount, respond_to) = p2p_mock. expect_header_request_for_height_cmd ( ) . await ;
@@ -844,7 +841,7 @@ mod tests {
844
841
. send ( Ok ( headers. drain ( ..8 ) . collect ( ) ) )
845
842
. map_err ( |_| "headers [538, 545]" )
846
843
. unwrap ( ) ;
847
- assert_syncing ( & syncer, & store, & [ 1 ..=545 ] , 545 ) . await ;
844
+ assert_syncing ( & syncer, & store, & [ 1 ..=546 ] , 546 ) . await ;
848
845
849
846
// Syncer is fulling synced and awaiting for events
850
847
p2p_mock. expect_no_cmd ( ) . await ;
@@ -872,45 +869,64 @@ mod tests {
872
869
#[ async_test]
873
870
async fn all_peers_disconnected ( ) {
874
871
let mut gen = ExtendedHeaderGenerator :: new ( ) ;
875
- let headers = gen. next_many ( 26 ) ;
876
872
877
- // Start Syncer and report height 25 as HEAD
878
- let ( syncer, store, mut p2p_mock) = initialized_syncer ( headers[ 25 ] . clone ( ) ) . await ;
873
+ let _gap = gen. next_many ( 24 ) ;
874
+ let header25 = gen. next ( ) ;
875
+ let _gap = gen. next_many ( 4 ) ;
876
+ let header30 = gen. next ( ) ;
877
+ let _gap = gen. next_many ( 4 ) ;
878
+ let header35 = gen. next ( ) ;
879
+
880
+ // Start Syncer and report height 30 as HEAD
881
+ let ( syncer, store, mut p2p_mock) = initialized_syncer ( header30) . await ;
879
882
880
- // Wait for the request but do not reply to it
883
+ // Wait for the request but do not reply to it.
881
884
let ( height, amount, _respond_to) = p2p_mock. expect_header_request_for_height_cmd ( ) . await ;
882
885
assert_eq ! ( height, 1 ) ;
883
- assert_eq ! ( amount, 25 ) ;
886
+ assert_eq ! ( amount, 29 ) ;
884
887
885
888
p2p_mock. announce_all_peers_disconnected ( ) ;
889
+ // Syncer is now back to `connecting_event_loop`.
886
890
p2p_mock. expect_no_cmd ( ) . await ;
887
891
892
+ // Accounce a non-trusted peer. Syncer in `connecting_event_loop` can progress only
893
+ // if a trusted peer is connected.
894
+ p2p_mock. announce_peer_connected ( ) ;
895
+ p2p_mock. expect_no_cmd ( ) . await ;
896
+
897
+ // Accounce a trusted peer.
888
898
p2p_mock. announce_trusted_peer_connected ( ) ;
889
899
890
- // Syncer is now back to `connecting_event_loop`, so we expect a request for HEAD
900
+ // Now syncer will send request for HEAD.
891
901
let ( height, amount, respond_to) = p2p_mock. expect_header_request_for_height_cmd ( ) . await ;
892
902
assert_eq ! ( height, 0 ) ;
893
903
assert_eq ! ( amount, 1 ) ;
894
- // Now HEAD is height 26
895
- respond_to. send ( Ok ( vec ! [ headers[ 25 ] . clone( ) ] ) ) . unwrap ( ) ;
896
904
897
- // Syncer initializes HeaderSub with the latest HEAD
898
- let head_from_syncer = p2p_mock . expect_init_header_sub ( ) . await ;
899
- assert_eq ! ( & head_from_syncer , headers . last ( ) . unwrap ( ) ) ;
905
+ // Report an older head. Syncer should not accept it.
906
+ respond_to . send ( Ok ( vec ! [ header25 ] ) ) . unwrap ( ) ;
907
+ assert_syncing ( & syncer , & store , & [ 30 ..= 30 ] , 30 ) . await ;
900
908
901
- // Syncer now moved to `connected_event_loop`
909
+ // Syncer will request HEAD again after some time.
910
+ sleep ( Duration :: from_secs ( 1 ) ) . await ;
902
911
let ( height, amount, respond_to) = p2p_mock. expect_header_request_for_height_cmd ( ) . await ;
903
- assert_eq ! ( height, 1 ) ;
904
- assert_eq ! ( amount, 25 ) ;
905
- respond_to
906
- . send ( Ok ( headers[ 0 ..25 ] . to_vec ( ) ) )
907
- // Mapping to avoid spamming error message on failure
908
- . map_err ( |_| "headers [1, 26]" )
909
- . unwrap ( ) ;
912
+ assert_eq ! ( height, 0 ) ;
913
+ assert_eq ! ( amount, 1 ) ;
910
914
911
- assert_syncing ( & syncer, & store, & [ 1 ..=26 ] , 26 ) . await ;
915
+ // Report newer HEAD than before.
916
+ respond_to. send ( Ok ( vec ! [ header35. clone( ) ] ) ) . unwrap ( ) ;
917
+ assert_syncing ( & syncer, & store, & [ 30 ..=30 , 35 ..=35 ] , 35 ) . await ;
912
918
913
- // Node is fully synced, so nothing else is produced.
919
+ // Syncer initializes HeaderSub with the latest HEAD.
920
+ let head_from_syncer = p2p_mock. expect_init_header_sub ( ) . await ;
921
+ assert_eq ! ( head_from_syncer, header35) ;
922
+
923
+ // Syncer now is in `connected_event_loop` and will try to sync the gap
924
+ // that is closer to HEAD.
925
+ let ( height, amount, _respond_to) = p2p_mock. expect_header_request_for_height_cmd ( ) . await ;
926
+ assert_eq ! ( height, 31 ) ;
927
+ assert_eq ! ( amount, 4 ) ;
928
+
929
+ p2p_mock. announce_all_peers_disconnected ( ) ;
914
930
p2p_mock. expect_no_cmd ( ) . await ;
915
931
}
916
932
@@ -1039,6 +1055,9 @@ mod tests {
1039
1055
let head_from_syncer = handle. expect_init_header_sub ( ) . await ;
1040
1056
assert_eq ! ( head_from_syncer, head) ;
1041
1057
1058
+ let head_height = head. height ( ) . value ( ) ;
1059
+ assert_syncing ( & syncer, & store, & [ head_height..=head_height] , head_height) . await ;
1060
+
1042
1061
( syncer, store, handle)
1043
1062
}
1044
1063
0 commit comments