Skip to content

Commit

Permalink
fix: wait couple rounds for no pings to send an event (#3315)
Browse files Browse the repository at this point in the history
Description
---
Wait couple rounds of `no ping` before sending the event.

How Has This Been Tested?
---
npm test -- --name "Full block sync with small reorg"
npm test -- --name "Pruned mode reorg simple"
npm test -- --name "Pruned mode reorg past horizon"
  • Loading branch information
Cifko committed Sep 8, 2021
1 parent d66b65a commit 2dcc0ea
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
Expand Up @@ -53,6 +53,7 @@ impl Display for PeerChainMetadata {
#[derive(Debug)]
pub enum ChainMetadataEvent {
PeerChainMetadataReceived(Vec<PeerChainMetadata>),
NetworkSilence,
}

#[derive(Clone)]
Expand Down
20 changes: 19 additions & 1 deletion base_layer/core/src/base_node/chain_metadata_service/service.rs
Expand Up @@ -43,12 +43,15 @@ use tari_comms::{
use tari_p2p::services::liveness::{LivenessEvent, LivenessHandle, Metadata, MetadataKey};
use tokio::sync::broadcast;

const NUM_ROUNDS_NETWORK_SILENCE: u16 = 3;

pub(super) struct ChainMetadataService {
liveness: LivenessHandle,
base_node: LocalNodeCommsInterface,
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 +72,7 @@ impl ChainMetadataService {
peer_chain_metadata: Vec::new(),
connectivity,
event_publisher,
number_of_rounds_no_pings: 0,
}
}

Expand Down Expand Up @@ -159,6 +163,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 +174,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 +184,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 >= NUM_ROUNDS_NETWORK_SILENCE {
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 +202,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
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
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
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 2dcc0ea

Please sign in to comment.