Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Actually fix major sync detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Aug 25, 2022
1 parent 4262c63 commit fe89fcb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 2 additions & 0 deletions client/network/common/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub struct SyncStatus<Block: BlockT> {
pub state: SyncState,
/// Target sync block number.
pub best_seen_block: Option<NumberFor<Block>>,
/// 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
Expand Down
13 changes: 7 additions & 6 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);

Expand Down
20 changes: 10 additions & 10 deletions client/network/sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,10 @@ where

/// Returns the current sync status.
fn status(&self) -> SyncStatus<B> {
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;
Expand All @@ -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()),
Expand Down Expand Up @@ -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<NumberFor<B>> {
fn median_seen(&self) -> Option<NumberFor<B>> {
let mut best_seens = self.peers.values().map(|p| p.best_number).collect::<Vec<_>>();

if best_seens.is_empty() {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit fe89fcb

Please sign in to comment.