Skip to content

Commit 2f23c1e

Browse files
committed
Make range sync chain Id sequential
1 parent a1b7d61 commit 2f23c1e

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

beacon_node/network/src/sync/range_sync/chain.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rand::seq::SliceRandom;
1515
use rand::Rng;
1616
use slog::{crit, debug, o, warn};
1717
use std::collections::{btree_map::Entry, BTreeMap, HashSet};
18-
use std::hash::{Hash, Hasher};
1918
use strum::IntoStaticStr;
2019
use types::{Epoch, EthSpec, Hash256, Slot};
2120

@@ -56,7 +55,7 @@ pub enum RemoveChain {
5655
pub struct KeepChain;
5756

5857
/// A chain identifier
59-
pub type ChainId = u64;
58+
pub type ChainId = Id;
6059
pub type BatchId = Epoch;
6160

6261
#[derive(Debug, Copy, Clone, IntoStaticStr)]
@@ -127,14 +126,9 @@ pub enum ChainSyncingState {
127126
}
128127

129128
impl<T: BeaconChainTypes> SyncingChain<T> {
130-
pub fn id(target_root: &Hash256, target_slot: &Slot) -> u64 {
131-
let mut hasher = std::collections::hash_map::DefaultHasher::new();
132-
(target_root, target_slot).hash(&mut hasher);
133-
hasher.finish()
134-
}
135-
136129
#[allow(clippy::too_many_arguments)]
137130
pub fn new(
131+
id: Id,
138132
start_epoch: Epoch,
139133
target_head_slot: Slot,
140134
target_head_root: Hash256,
@@ -145,8 +139,6 @@ impl<T: BeaconChainTypes> SyncingChain<T> {
145139
let mut peers = FnvHashMap::default();
146140
peers.insert(peer_id, Default::default());
147141

148-
let id = SyncingChain::<T>::id(&target_head_root, &target_head_slot);
149-
150142
SyncingChain {
151143
id,
152144
chain_type,
@@ -165,6 +157,11 @@ impl<T: BeaconChainTypes> SyncingChain<T> {
165157
}
166158
}
167159

160+
/// Returns true if this chain has the same target
161+
pub fn has_same_target(&self, target_head_slot: Slot, target_head_root: Hash256) -> bool {
162+
self.target_head_slot == target_head_slot && self.target_head_root == target_head_root
163+
}
164+
168165
/// Check if the chain has peers from which to process batches.
169166
pub fn available_peers(&self) -> usize {
170167
self.peers.len()
@@ -1258,7 +1255,7 @@ impl<T: BeaconChainTypes> slog::KV for SyncingChain<T> {
12581255
serializer: &mut dyn slog::Serializer,
12591256
) -> slog::Result {
12601257
use slog::Value;
1261-
serializer.emit_u64("id", self.id)?;
1258+
serializer.emit_u32("id", self.id)?;
12621259
Value::serialize(&self.start_epoch, record, "from", serializer)?;
12631260
Value::serialize(
12641261
&self.target_head_slot.epoch(T::EthSpec::slots_per_epoch()),

beacon_node/network/src/sync/range_sync/chain_collection.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::metrics;
99
use crate::sync::network_context::SyncNetworkContext;
1010
use beacon_chain::{BeaconChain, BeaconChainTypes};
1111
use fnv::FnvHashMap;
12+
use lighthouse_network::service::api_types::Id;
1213
use lighthouse_network::PeerId;
1314
use lighthouse_network::SyncInfo;
1415
use slog::{crit, debug, error};
@@ -29,9 +30,9 @@ const MIN_FINALIZED_CHAIN_PROCESSED_EPOCHS: u64 = 10;
2930
#[derive(Clone)]
3031
pub enum RangeSyncState {
3132
/// A finalized chain is being synced.
32-
Finalized(u64),
33+
Finalized(Id),
3334
/// 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]>),
3536
/// There are no head or finalized chains and no long range sync is in progress.
3637
Idle,
3738
}
@@ -74,7 +75,7 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
7475
if syncing_id == id {
7576
// the finalized chain that was syncing was removed
7677
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
7879
.head_chains
7980
.iter()
8081
.filter(|(_id, chain)| chain.is_syncing())
@@ -355,7 +356,7 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
355356
.collect::<Vec<_>>();
356357
preferred_ids.sort_unstable();
357358

358-
let mut syncing_chains = SmallVec::<[u64; PARALLEL_HEAD_CHAINS]>::new();
359+
let mut syncing_chains = SmallVec::<[Id; PARALLEL_HEAD_CHAINS]>::new();
359360
for (_, _, id) in preferred_ids {
360361
let chain = self.head_chains.get_mut(&id).expect("known chain");
361362
if syncing_chains.len() < PARALLEL_HEAD_CHAINS {
@@ -465,15 +466,17 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
465466
sync_type: RangeSyncType,
466467
network: &mut SyncNetworkContext<T>,
467468
) {
468-
let id = SyncingChain::<T>::id(&target_head_root, &target_head_slot);
469469
let collection = if let RangeSyncType::Finalized = sync_type {
470470
&mut self.finalized_chains
471471
} else {
472472
&mut self.head_chains
473473
};
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)) => {
477480
debug!(self.log, "Adding peer to known chain"; "peer_id" => %peer, "sync_type" => ?sync_type, &chain);
478481
debug_assert_eq!(chain.target_head_root, target_head_root);
479482
debug_assert_eq!(chain.target_head_slot, target_head_slot);
@@ -483,23 +486,25 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
483486
} else {
484487
error!(self.log, "Chain removed after adding peer"; "chain" => id, "reason" => ?remove_reason);
485488
}
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);
488492
}
489493
}
490-
Entry::Vacant(entry) => {
494+
None => {
491495
let peer_rpr = peer.to_string();
496+
let id = network.next_id();
492497
let new_chain = SyncingChain::new(
498+
id,
493499
start_epoch,
494500
target_head_slot,
495501
target_head_root,
496502
peer,
497503
sync_type.into(),
498504
&self.log,
499505
);
500-
debug_assert_eq!(new_chain.get_id(), id);
501506
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);
503508
metrics::inc_counter_vec(&metrics::SYNCING_CHAINS_ADDED, &[sync_type.as_str()]);
504509
self.update_metrics();
505510
}

0 commit comments

Comments
 (0)