Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wait couple rounds for no pings to send an event #3315

Merged
merged 2 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
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