mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 10:17:57 +00:00
Sync: Improve major sync detection (#11547)
* 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
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user