Subscribe to core topics after sync (#1613)

## Issue Addressed

N/A

## Proposed Changes

Prevent subscribing to core gossipsub topics until after we have achieved a full sync. This prevents us censoring gossipsub channels, getting penalised in gossipsub 1.1 scoring and saves us computation time in attempting to validate gossipsub messages which we will be unable to do with a non-sync'd chain.
This commit is contained in:
Age Manning
2020-09-23 03:26:33 +00:00
parent 80ecafaae4
commit 80e52a0263
9 changed files with 68 additions and 30 deletions

View File

@@ -113,7 +113,7 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
}
/// Updates the global sync state and logs any changes.
pub fn update_sync_state(&mut self) {
pub fn update_sync_state(&mut self, network: &mut SyncNetworkContext<T::EthSpec>) {
// if there is no range sync occurring, the state is either synced or not based on
// connected peers.
@@ -130,8 +130,11 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
let mut peer_state = self.network_globals.sync_state.write();
if new_state != *peer_state {
info!(self.log, "Sync state updated"; "old_state" => format!("{}",peer_state), "new_state" => format!("{}",new_state));
if new_state == SyncState::Synced {
network.subscribe_core_topics();
}
*peer_state = new_state;
}
*peer_state = new_state;
} else {
// The state is based on a range sync state, update it
let mut node_sync_state = self.network_globals.sync_state.write();
@@ -148,12 +151,12 @@ impl<T: BeaconChainTypes> ChainCollection<T> {
///
/// We could be awaiting a head sync. If we are in the head syncing state, without any head
/// chains, then update the state to idle.
pub fn fully_synced_peer_found(&mut self) {
pub fn fully_synced_peer_found(&mut self, network: &mut SyncNetworkContext<T::EthSpec>) {
if let RangeSyncState::Head { .. } = self.state {
if self.head_chains.is_empty() {
// Update the global network state to either synced or stalled.
self.state = RangeSyncState::Idle;
self.update_sync_state();
self.update_sync_state(network);
}
}
}

View File

@@ -98,8 +98,8 @@ impl<T: BeaconChainTypes> RangeSync<T> {
/// On re-status, a peer that has no head to download indicates that this state can be set to
/// idle as there are in fact no head chains to download. This function notifies the chain
/// collection that the state can safely be set to idle.
pub fn fully_synced_peer_found(&mut self) {
self.chains.fully_synced_peer_found()
pub fn fully_synced_peer_found(&mut self, network: &mut SyncNetworkContext<T::EthSpec>) {
self.chains.fully_synced_peer_found(network)
}
/// A useful peer has been added. The SyncManager has identified this peer as needing either
@@ -168,7 +168,7 @@ impl<T: BeaconChainTypes> RangeSync<T> {
// check if the new peer's addition will favour a new syncing chain.
self.chains.update(network);
// update the global sync state if necessary
self.chains.update_sync_state();
self.chains.update_sync_state(network);
} else {
// there is no finalized chain that matches this peer's last finalized target
// create a new finalized chain
@@ -183,7 +183,7 @@ impl<T: BeaconChainTypes> RangeSync<T> {
);
self.chains.update(network);
// update the global sync state
self.chains.update_sync_state();
self.chains.update_sync_state(network);
}
}
RangeSyncType::Head => {
@@ -229,7 +229,7 @@ impl<T: BeaconChainTypes> RangeSync<T> {
);
}
self.chains.update(network);
self.chains.update_sync_state();
self.chains.update_sync_state(network);
}
}
}
@@ -292,7 +292,7 @@ impl<T: BeaconChainTypes> RangeSync<T> {
// head chain.
self.chains.set_head_sync();
// Update the global variables
self.chains.update_sync_state();
self.chains.update_sync_state(network);
// if there are no more finalized chains, re-status all known peers awaiting a head
// sync
@@ -329,7 +329,7 @@ impl<T: BeaconChainTypes> RangeSync<T> {
// update the state of the collection
self.chains.update(network);
// update the global state and log any change
self.chains.update_sync_state();
self.chains.update_sync_state(network);
}
Some((_, ProcessingResult::KeepChain)) => {}
None => {
@@ -358,7 +358,7 @@ impl<T: BeaconChainTypes> RangeSync<T> {
// update the state of the collection
self.chains.update(network);
// update the global state and inform the user
self.chains.update_sync_state();
self.chains.update_sync_state(network);
}
/// When a peer gets removed, both the head and finalized chains need to be searched to check which pool the peer is in. The chain may also have a batch or batches awaiting