Skip to content

Commit

Permalink
fix: wait couple rounds for no pings to send an event
Browse files Browse the repository at this point in the history
  • Loading branch information
Cifko committed Sep 8, 2021
1 parent dfc6fd2 commit 5123d3a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Display for PeerChainMetadata {
#[derive(Debug)]
pub enum ChainMetadataEvent {
PeerChainMetadataReceived(Vec<PeerChainMetadata>),
NetworkSilence,
}

#[derive(Clone)]
Expand Down
18 changes: 17 additions & 1 deletion base_layer/core/src/base_node/chain_metadata_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub(super) struct ChainMetadataService {
peer_chain_metadata: Vec<PeerChainMetadata>,
connectivity: ConnectivityRequester,
event_publisher: broadcast::Sender<Arc<ChainMetadataEvent>>,
number_of_rounds_no_pings: u16,
}

impl ChainMetadataService {
Expand All @@ -69,6 +70,7 @@ impl ChainMetadataService {
peer_chain_metadata: Vec::new(),
connectivity,
event_publisher,
number_of_rounds_no_pings: 0,
}
}

Expand Down Expand Up @@ -159,6 +161,7 @@ impl ChainMetadataService {
"Received ping from neighbouring node '{}'.",
event.node_id
);
self.number_of_rounds_no_pings = 0;
self.collect_chain_state_from_ping(&event.node_id, &event.metadata)?;
self.send_chain_metadata_to_event_publisher().await?;
},
Expand All @@ -169,6 +172,7 @@ impl ChainMetadataService {
"Received pong from neighbouring node '{}'.",
event.node_id
);
self.number_of_rounds_no_pings = 0;
self.collect_chain_state_from_pong(&event.node_id, &event.metadata)?;
self.send_chain_metadata_to_event_publisher().await?;
},
Expand All @@ -178,7 +182,14 @@ impl ChainMetadataService {
target: LOG_TARGET,
"New chain metadata round sent to {} peer(s)", num_peers
);
self.send_chain_metadata_to_event_publisher().await?;
// If there were no pings for awhile, we are probably alone.
if *num_peers == 0 {
self.number_of_rounds_no_pings += 1;
if self.number_of_rounds_no_pings >= 10 {
self.send_network_silence().await?;
self.number_of_rounds_no_pings = 0;
}
}
// Ensure that we're waiting for the correct amount of peers to respond
// and have allocated space for their replies

Expand All @@ -189,6 +200,11 @@ impl ChainMetadataService {
Ok(())
}

async fn send_network_silence(&mut self) -> Result<(), ChainMetadataSyncError> {
let _ = self.event_publisher.send(Arc::new(ChainMetadataEvent::NetworkSilence));
Ok(())
}

async fn send_chain_metadata_to_event_publisher(&mut self) -> Result<(), ChainMetadataSyncError> {
// send only fails if there are no subscribers.
let _ = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ impl Listening {
loop {
let metadata_event = shared.metadata_event_stream.recv().await;
match metadata_event.as_ref().map(|v| v.deref()) {
Ok(ChainMetadataEvent::NetworkSilence) => {
debug!("NetworkSilence event received");
if !self.is_synced {
self.is_synced = true;
shared.set_state_info(StateInfo::Listening(ListeningInfo::new(true)));
debug!(target: LOG_TARGET, "Initial sync achieved");
}
},
Ok(ChainMetadataEvent::PeerChainMetadataReceived(peer_metadata_list)) => {
let mut peer_metadata_list = peer_metadata_list.clone();

Expand Down
4 changes: 2 additions & 2 deletions integration_tests/features/Reorgs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Feature: Reorgs
And I mine a block on B at height 4 with an invalid MMR
Then node B is at tip BTip1

@critical @reorg @broken
@critical @reorg
Scenario: Pruned mode reorg simple
Given I have a base node NODE1 connected to all seed nodes
And I have wallet WALLET1 connected to base node NODE1
Expand All @@ -63,7 +63,7 @@ Feature: Reorgs
When I start base node NODE1
Then all nodes are at height 20

@critical @reorg @broken
@critical @reorg
Scenario: Pruned mode reorg past horizon
Given I have a base node NODE1 connected to all seed nodes
And I have wallet WALLET1 connected to base node NODE1
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/features/Sync.feature
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Feature: Block Sync
Then NODE1 should have 11 peers
Then NODE2 should have 11 peers

@critical @reorg @broken
@critical @reorg
Scenario: Full block sync with small reorg
Given I have a base node NODE1
And I have wallet WALLET1 connected to base node NODE1
Expand Down

0 comments on commit 5123d3a

Please sign in to comment.