From 5123d3a6123857ef3600514b74bf2b8b8919e28c Mon Sep 17 00:00:00 2001 From: Martin Stefcek Date: Wed, 8 Sep 2021 09:23:13 +0200 Subject: [PATCH] fix: wait couple rounds for no pings to send an event --- .../base_node/chain_metadata_service/handle.rs | 1 + .../chain_metadata_service/service.rs | 18 +++++++++++++++++- .../state_machine_service/states/listening.rs | 8 ++++++++ integration_tests/features/Reorgs.feature | 4 ++-- integration_tests/features/Sync.feature | 2 +- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/base_layer/core/src/base_node/chain_metadata_service/handle.rs b/base_layer/core/src/base_node/chain_metadata_service/handle.rs index c68036fe9f..cb102c9637 100644 --- a/base_layer/core/src/base_node/chain_metadata_service/handle.rs +++ b/base_layer/core/src/base_node/chain_metadata_service/handle.rs @@ -53,6 +53,7 @@ impl Display for PeerChainMetadata { #[derive(Debug)] pub enum ChainMetadataEvent { PeerChainMetadataReceived(Vec), + NetworkSilence, } #[derive(Clone)] diff --git a/base_layer/core/src/base_node/chain_metadata_service/service.rs b/base_layer/core/src/base_node/chain_metadata_service/service.rs index 643173d97d..5f81583e2c 100644 --- a/base_layer/core/src/base_node/chain_metadata_service/service.rs +++ b/base_layer/core/src/base_node/chain_metadata_service/service.rs @@ -49,6 +49,7 @@ pub(super) struct ChainMetadataService { peer_chain_metadata: Vec, connectivity: ConnectivityRequester, event_publisher: broadcast::Sender>, + number_of_rounds_no_pings: u16, } impl ChainMetadataService { @@ -69,6 +70,7 @@ impl ChainMetadataService { peer_chain_metadata: Vec::new(), connectivity, event_publisher, + number_of_rounds_no_pings: 0, } } @@ -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?; }, @@ -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?; }, @@ -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 @@ -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 diff --git a/base_layer/core/src/base_node/state_machine_service/states/listening.rs b/base_layer/core/src/base_node/state_machine_service/states/listening.rs index 92349c269a..096bf2b91c 100644 --- a/base_layer/core/src/base_node/state_machine_service/states/listening.rs +++ b/base_layer/core/src/base_node/state_machine_service/states/listening.rs @@ -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(); diff --git a/integration_tests/features/Reorgs.feature b/integration_tests/features/Reorgs.feature index 7050b6e899..d249f05015 100644 --- a/integration_tests/features/Reorgs.feature +++ b/integration_tests/features/Reorgs.feature @@ -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 @@ -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 diff --git a/integration_tests/features/Sync.feature b/integration_tests/features/Sync.feature index 076813ea45..a306e46d1c 100644 --- a/integration_tests/features/Sync.feature +++ b/integration_tests/features/Sync.feature @@ -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