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

Commit

Permalink
Sync: Improve major sync detection (paritytech#11547)
Browse files Browse the repository at this point in the history
* Sync: Improve major sync detection

When we still have a full import queue, we should still report that we are in major sync mode,
otherwise validators may will already start producing blocks.

* Use median

* Review comments
  • Loading branch information
bkchr authored and DaviRain-Su committed Aug 23, 2022
1 parent 5ac01f9 commit 5258c44
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions client/network/sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,28 @@ where
.map(|p| PeerInfo { best_hash: p.best_hash, best_number: p.best_number })
}

/// Returns the best seen block.
fn best_seen(&self) -> Option<NumberFor<B>> {
let mut best_seens = self.peers.values().map(|p| p.best_number).collect::<Vec<_>>();

if best_seens.is_empty() {
None
} else {
let middle = best_seens.len() / 2;

// Not the "perfect median" when we have an even number of peers.
Some(*best_seens.select_nth_unstable(middle).1)
}
}

/// Returns the current sync status.
pub fn status(&self) -> Status<B> {
let best_seen = self.peers.values().map(|p| p.best_number).max();
let best_seen = self.best_seen();
let sync_state = if let Some(n) = best_seen {
// A chain is classified as downloading if the provided best block is
// more than `MAJOR_SYNC_BLOCKS` behind the best queued block.
if n > self.best_queued_number && n - self.best_queued_number > MAJOR_SYNC_BLOCKS.into()
{
// more than `MAJOR_SYNC_BLOCKS` behind the best block.
let best_block = self.client.info().best_number;
if n > best_block && n - best_block > MAJOR_SYNC_BLOCKS.into() {
SyncState::Downloading
} else {
SyncState::Idle
Expand Down

0 comments on commit 5258c44

Please sign in to comment.