From fe89fcb27493e94b6fb136800270032fd859d751 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Fri, 26 Aug 2022 01:59:40 +0300 Subject: [PATCH] Actually fix major sync detection --- client/network/common/src/sync.rs | 2 ++ client/network/src/service.rs | 13 +++++++------ client/network/sync/src/lib.rs | 20 ++++++++++---------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/client/network/common/src/sync.rs b/client/network/common/src/sync.rs index 2ee8f8c51814b..a7c22ce76e73a 100644 --- a/client/network/common/src/sync.rs +++ b/client/network/common/src/sync.rs @@ -67,6 +67,8 @@ pub struct SyncStatus { pub state: SyncState, /// Target sync block number. pub best_seen_block: Option>, + /// Are we actively catching up with the chain? + pub is_major_syncing: bool, /// Number of peers participating in syncing. pub num_peers: u32, /// Number of blocks queued for import diff --git a/client/network/src/service.rs b/client/network/src/service.rs index 162b9c6a30491..1656d38179a9b 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -71,7 +71,7 @@ use sc_network_common::{ NotificationSender as NotificationSenderT, NotificationSenderError, NotificationSenderReady as NotificationSenderReadyT, Signature, SigningError, }, - sync::{SyncState, SyncStatus}, + sync::SyncStatus, }; use sc_peerset::PeersetHandle; use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; @@ -1938,11 +1938,12 @@ where *this.external_addresses.lock() = external_addresses; } - let is_major_syncing = - match this.network_service.behaviour_mut().user_protocol_mut().sync_state().state { - SyncState::Idle => false, - SyncState::Downloading => true, - }; + let is_major_syncing = this + .network_service + .behaviour_mut() + .user_protocol_mut() + .sync_state() + .is_major_syncing; this.tx_handler_controller.set_gossip_enabled(!is_major_syncing); diff --git a/client/network/sync/src/lib.rs b/client/network/sync/src/lib.rs index aae5f4de353fe..960b32061230e 100644 --- a/client/network/sync/src/lib.rs +++ b/client/network/sync/src/lib.rs @@ -389,8 +389,10 @@ where /// Returns the current sync status. fn status(&self) -> SyncStatus { - let best_seen = self.best_seen(); - let sync_state = if let Some(n) = best_seen { + let median_seen = self.median_seen(); + let best_seen_block = + median_seen.and_then(|median| (median > self.best_queued_number).then_some(median)); + let sync_state = if let Some(n) = median_seen { // A chain is classified as downloading if the provided best block is // more than `MAJOR_SYNC_BLOCKS` behind the best block. let best_block = self.client.info().best_number; @@ -414,9 +416,12 @@ where _ => None, }; + let is_major_syncing = matches!(sync_state, SyncState::Downloading); + SyncStatus { state: sync_state, - best_seen_block: best_seen, + best_seen_block, + is_major_syncing, num_peers: self.peers.len() as u32, queued_blocks: self.queue_blocks.len() as u32, state_sync: self.state_sync.as_ref().map(|s| s.progress()), @@ -1720,7 +1725,7 @@ where } /// Returns the best seen block number if we don't have that block yet, `None` otherwise. - fn best_seen(&self) -> Option> { + fn median_seen(&self) -> Option> { let mut best_seens = self.peers.values().map(|p| p.best_number).collect::>(); if best_seens.is_empty() { @@ -1729,12 +1734,7 @@ where let middle = best_seens.len() / 2; // Not the "perfect median" when we have an even number of peers. - let median = *best_seens.select_nth_unstable(middle).1; - if median > self.best_queued_number { - Some(median) - } else { - None - } + Some(*best_seens.select_nth_unstable(middle).1) } }