@@ -9,6 +9,7 @@ use crate::metrics;
9
9
use crate :: sync:: network_context:: SyncNetworkContext ;
10
10
use beacon_chain:: { BeaconChain , BeaconChainTypes } ;
11
11
use fnv:: FnvHashMap ;
12
+ use lighthouse_network:: service:: api_types:: Id ;
12
13
use lighthouse_network:: PeerId ;
13
14
use lighthouse_network:: SyncInfo ;
14
15
use slog:: { crit, debug, error} ;
@@ -29,9 +30,9 @@ const MIN_FINALIZED_CHAIN_PROCESSED_EPOCHS: u64 = 10;
29
30
#[ derive( Clone ) ]
30
31
pub enum RangeSyncState {
31
32
/// A finalized chain is being synced.
32
- Finalized ( u64 ) ,
33
+ Finalized ( Id ) ,
33
34
/// There are no finalized chains and we are syncing one more head chains.
34
- Head ( SmallVec < [ u64 ; PARALLEL_HEAD_CHAINS ] > ) ,
35
+ Head ( SmallVec < [ Id ; PARALLEL_HEAD_CHAINS ] > ) ,
35
36
/// There are no head or finalized chains and no long range sync is in progress.
36
37
Idle ,
37
38
}
@@ -74,7 +75,7 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
74
75
if syncing_id == id {
75
76
// the finalized chain that was syncing was removed
76
77
debug_assert ! ( was_syncing && sync_type == RangeSyncType :: Finalized ) ;
77
- let syncing_head_ids: SmallVec < [ u64 ; PARALLEL_HEAD_CHAINS ] > = self
78
+ let syncing_head_ids: SmallVec < [ Id ; PARALLEL_HEAD_CHAINS ] > = self
78
79
. head_chains
79
80
. iter ( )
80
81
. filter ( |( _id, chain) | chain. is_syncing ( ) )
@@ -355,7 +356,7 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
355
356
. collect :: < Vec < _ > > ( ) ;
356
357
preferred_ids. sort_unstable ( ) ;
357
358
358
- let mut syncing_chains = SmallVec :: < [ u64 ; PARALLEL_HEAD_CHAINS ] > :: new ( ) ;
359
+ let mut syncing_chains = SmallVec :: < [ Id ; PARALLEL_HEAD_CHAINS ] > :: new ( ) ;
359
360
for ( _, _, id) in preferred_ids {
360
361
let chain = self . head_chains . get_mut ( & id) . expect ( "known chain" ) ;
361
362
if syncing_chains. len ( ) < PARALLEL_HEAD_CHAINS {
@@ -465,15 +466,17 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
465
466
sync_type : RangeSyncType ,
466
467
network : & mut SyncNetworkContext < T > ,
467
468
) {
468
- let id = SyncingChain :: < T > :: id ( & target_head_root, & target_head_slot) ;
469
469
let collection = if let RangeSyncType :: Finalized = sync_type {
470
470
& mut self . finalized_chains
471
471
} else {
472
472
& mut self . head_chains
473
473
} ;
474
- match collection. entry ( id) {
475
- Entry :: Occupied ( mut entry) => {
476
- let chain = entry. get_mut ( ) ;
474
+
475
+ match collection
476
+ . iter_mut ( )
477
+ . find ( |( _, chain) | chain. has_same_target ( target_head_slot, target_head_root) )
478
+ {
479
+ Some ( ( & id, chain) ) => {
477
480
debug ! ( self . log, "Adding peer to known chain" ; "peer_id" => %peer, "sync_type" => ?sync_type, & chain) ;
478
481
debug_assert_eq ! ( chain. target_head_root, target_head_root) ;
479
482
debug_assert_eq ! ( chain. target_head_slot, target_head_slot) ;
@@ -483,23 +486,25 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
483
486
} else {
484
487
error ! ( self . log, "Chain removed after adding peer" ; "chain" => id, "reason" => ?remove_reason) ;
485
488
}
486
- let chain = entry. remove ( ) ;
487
- self . on_chain_removed ( & id, chain. is_syncing ( ) , sync_type) ;
489
+ let is_syncing = chain. is_syncing ( ) ;
490
+ collection. remove ( & id) ;
491
+ self . on_chain_removed ( & id, is_syncing, sync_type) ;
488
492
}
489
493
}
490
- Entry :: Vacant ( entry ) => {
494
+ None => {
491
495
let peer_rpr = peer. to_string ( ) ;
496
+ let id = network. next_id ( ) ;
492
497
let new_chain = SyncingChain :: new (
498
+ id,
493
499
start_epoch,
494
500
target_head_slot,
495
501
target_head_root,
496
502
peer,
497
503
sync_type. into ( ) ,
498
504
& self . log ,
499
505
) ;
500
- debug_assert_eq ! ( new_chain. get_id( ) , id) ;
501
506
debug ! ( self . log, "New chain added to sync" ; "peer_id" => peer_rpr, "sync_type" => ?sync_type, & new_chain) ;
502
- entry . insert ( new_chain) ;
507
+ collection . insert ( id , new_chain) ;
503
508
metrics:: inc_counter_vec ( & metrics:: SYNCING_CHAINS_ADDED , & [ sync_type. as_str ( ) ] ) ;
504
509
self . update_metrics ( ) ;
505
510
}
0 commit comments