mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 06:21:11 +00:00
Actually fix major sync detection (#12114)
* Actually fix major sync detection * Introduce `SyncState::Importing` state * Add target to SyncState enum variants and add `is_major_syncing` method on it * Remove unnecessary duplicated `best_seen_block` from `SyncState` struct * Revert "Remove unnecessary duplicated `best_seen_block` from `SyncState` struct" This reverts commit bb8abd458c939881c049f69d59f3acba47c97c5c. * Add missing `websocket` feature to `libp2p` Co-authored-by: parity-processbot <>
This commit is contained in:
@@ -93,42 +93,37 @@ impl<B: BlockT> InformantDisplay<B> {
|
|||||||
(diff_bytes_inbound, diff_bytes_outbound)
|
(diff_bytes_inbound, diff_bytes_outbound)
|
||||||
};
|
};
|
||||||
|
|
||||||
let (level, status, target) = match (
|
let (level, status, target) =
|
||||||
net_status.sync_state,
|
match (net_status.sync_state, net_status.state_sync, net_status.warp_sync) {
|
||||||
net_status.best_seen_block,
|
(
|
||||||
net_status.state_sync,
|
_,
|
||||||
net_status.warp_sync,
|
_,
|
||||||
) {
|
Some(WarpSyncProgress { phase: WarpSyncPhase::DownloadingBlocks(n), .. }),
|
||||||
(
|
) => ("⏩", "Block history".into(), format!(", #{}", n)),
|
||||||
_,
|
(_, _, Some(warp)) => (
|
||||||
_,
|
"⏩",
|
||||||
_,
|
"Warping".into(),
|
||||||
Some(WarpSyncProgress { phase: WarpSyncPhase::DownloadingBlocks(n), .. }),
|
format!(
|
||||||
) => ("⏩", "Block history".into(), format!(", #{}", n)),
|
", {}, {:.2} Mib",
|
||||||
(_, _, _, Some(warp)) => (
|
warp.phase,
|
||||||
"⏩",
|
(warp.total_bytes as f32) / (1024f32 * 1024f32)
|
||||||
"Warping".into(),
|
),
|
||||||
format!(
|
|
||||||
", {}, {:.2} Mib",
|
|
||||||
warp.phase,
|
|
||||||
(warp.total_bytes as f32) / (1024f32 * 1024f32)
|
|
||||||
),
|
),
|
||||||
),
|
(_, Some(state), _) => (
|
||||||
(_, _, Some(state), _) => (
|
"⚙️ ",
|
||||||
"⚙️ ",
|
"Downloading state".into(),
|
||||||
"Downloading state".into(),
|
format!(
|
||||||
format!(
|
", {}%, {:.2} Mib",
|
||||||
", {}%, {:.2} Mib",
|
state.percentage,
|
||||||
state.percentage,
|
(state.size as f32) / (1024f32 * 1024f32)
|
||||||
(state.size as f32) / (1024f32 * 1024f32)
|
),
|
||||||
),
|
),
|
||||||
),
|
(SyncState::Idle, _, _) => ("💤", "Idle".into(), "".into()),
|
||||||
(SyncState::Idle, _, _, _) => ("💤", "Idle".into(), "".into()),
|
(SyncState::Downloading { target }, _, _) =>
|
||||||
(SyncState::Downloading, None, _, _) =>
|
("⚙️ ", format!("Syncing{}", speed), format!(", target=#{target}")),
|
||||||
("⚙️ ", format!("Preparing{}", speed), "".into()),
|
(SyncState::Importing { target }, _, _) =>
|
||||||
(SyncState::Downloading, Some(n), None, _) =>
|
("⚙️ ", format!("Preparing{}", speed), format!(", target=#{target}")),
|
||||||
("⚙️ ", format!("Syncing{}", speed), format!(", target=#{}", n)),
|
};
|
||||||
};
|
|
||||||
|
|
||||||
if self.format.enable_color {
|
if self.format.enable_color {
|
||||||
info!(
|
info!(
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ fnv = "1.0.6"
|
|||||||
futures = "0.3.21"
|
futures = "0.3.21"
|
||||||
futures-timer = "3.0.2"
|
futures-timer = "3.0.2"
|
||||||
ip_network = "0.4.1"
|
ip_network = "0.4.1"
|
||||||
libp2p = { version = "0.49.0", features = ["async-std", "dns", "identify", "kad", "mdns-async-io", "mplex", "noise", "ping", "tcp", "yamux"] }
|
libp2p = { version = "0.49.0", features = ["async-std", "dns", "identify", "kad", "mdns-async-io", "mplex", "noise", "ping", "tcp", "yamux", "websocket"] }
|
||||||
linked_hash_set = "0.1.3"
|
linked_hash_set = "0.1.3"
|
||||||
linked-hash-map = "0.5.4"
|
linked-hash-map = "0.5.4"
|
||||||
log = "0.4.17"
|
log = "0.4.17"
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ where
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct NetworkStatus<B: BlockT> {
|
pub struct NetworkStatus<B: BlockT> {
|
||||||
/// Current global sync state.
|
/// Current global sync state.
|
||||||
pub sync_state: SyncState,
|
pub sync_state: SyncState<NumberFor<B>>,
|
||||||
/// Target sync block number.
|
/// Target sync block number.
|
||||||
pub best_seen_block: Option<NumberFor<B>>,
|
pub best_seen_block: Option<NumberFor<B>>,
|
||||||
/// Number of peers participating in syncing.
|
/// Number of peers participating in syncing.
|
||||||
|
|||||||
@@ -44,11 +44,20 @@ pub struct PeerInfo<Block: BlockT> {
|
|||||||
|
|
||||||
/// Reported sync state.
|
/// Reported sync state.
|
||||||
#[derive(Clone, Eq, PartialEq, Debug)]
|
#[derive(Clone, Eq, PartialEq, Debug)]
|
||||||
pub enum SyncState {
|
pub enum SyncState<BlockNumber> {
|
||||||
/// Initial sync is complete, keep-up sync is active.
|
/// Initial sync is complete, keep-up sync is active.
|
||||||
Idle,
|
Idle,
|
||||||
/// Actively catching up with the chain.
|
/// Actively catching up with the chain.
|
||||||
Downloading,
|
Downloading { target: BlockNumber },
|
||||||
|
/// All blocks are downloaded and are being imported.
|
||||||
|
Importing { target: BlockNumber },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<BlockNumber> SyncState<BlockNumber> {
|
||||||
|
/// Are we actively catching up with the chain?
|
||||||
|
pub fn is_major_syncing(&self) -> bool {
|
||||||
|
!matches!(self, SyncState::Idle)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reported state download progress.
|
/// Reported state download progress.
|
||||||
@@ -64,7 +73,7 @@ pub struct StateDownloadProgress {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SyncStatus<Block: BlockT> {
|
pub struct SyncStatus<Block: BlockT> {
|
||||||
/// Current global sync state.
|
/// Current global sync state.
|
||||||
pub state: SyncState,
|
pub state: SyncState<NumberFor<Block>>,
|
||||||
/// Target sync block number.
|
/// Target sync block number.
|
||||||
pub best_seen_block: Option<NumberFor<Block>>,
|
pub best_seen_block: Option<NumberFor<Block>>,
|
||||||
/// Number of peers participating in syncing.
|
/// Number of peers participating in syncing.
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ use sc_network_common::{
|
|||||||
NotificationSender as NotificationSenderT, NotificationSenderError,
|
NotificationSender as NotificationSenderT, NotificationSenderError,
|
||||||
NotificationSenderReady as NotificationSenderReadyT, Signature, SigningError,
|
NotificationSenderReady as NotificationSenderReadyT, Signature, SigningError,
|
||||||
},
|
},
|
||||||
sync::{SyncState, SyncStatus},
|
sync::SyncStatus,
|
||||||
ExHashT,
|
ExHashT,
|
||||||
};
|
};
|
||||||
use sc_peerset::PeersetHandle;
|
use sc_peerset::PeersetHandle;
|
||||||
@@ -1997,11 +1997,13 @@ where
|
|||||||
*this.external_addresses.lock() = external_addresses;
|
*this.external_addresses.lock() = external_addresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
let is_major_syncing =
|
let is_major_syncing = this
|
||||||
match this.network_service.behaviour_mut().user_protocol_mut().sync_state().state {
|
.network_service
|
||||||
SyncState::Idle => false,
|
.behaviour_mut()
|
||||||
SyncState::Downloading => true,
|
.user_protocol_mut()
|
||||||
};
|
.sync_state()
|
||||||
|
.state
|
||||||
|
.is_major_syncing();
|
||||||
|
|
||||||
this.is_major_syncing.store(is_major_syncing, Ordering::Relaxed);
|
this.is_major_syncing.store(is_major_syncing, Ordering::Relaxed);
|
||||||
|
|
||||||
|
|||||||
@@ -410,13 +410,21 @@ where
|
|||||||
|
|
||||||
/// Returns the current sync status.
|
/// Returns the current sync status.
|
||||||
fn status(&self) -> SyncStatus<B> {
|
fn status(&self) -> SyncStatus<B> {
|
||||||
let best_seen = self.best_seen();
|
let median_seen = self.median_seen();
|
||||||
let sync_state = if let Some(n) = best_seen {
|
let best_seen_block =
|
||||||
|
median_seen.and_then(|median| (median > self.best_queued_number).then_some(median));
|
||||||
|
let sync_state = if let Some(target) = median_seen {
|
||||||
// A chain is classified as downloading if the provided best block is
|
// A chain is classified as downloading if the provided best block is
|
||||||
// more than `MAJOR_SYNC_BLOCKS` behind the best block.
|
// more than `MAJOR_SYNC_BLOCKS` behind the best block or as importing
|
||||||
|
// if the same can be said about queued blocks.
|
||||||
let best_block = self.client.info().best_number;
|
let best_block = self.client.info().best_number;
|
||||||
if n > best_block && n - best_block > MAJOR_SYNC_BLOCKS.into() {
|
if target > best_block && target - best_block > MAJOR_SYNC_BLOCKS.into() {
|
||||||
SyncState::Downloading
|
// If target is not queued, we're downloading, otherwise importing.
|
||||||
|
if target > self.best_queued_number {
|
||||||
|
SyncState::Downloading { target }
|
||||||
|
} else {
|
||||||
|
SyncState::Importing { target }
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
SyncState::Idle
|
SyncState::Idle
|
||||||
}
|
}
|
||||||
@@ -437,7 +445,7 @@ where
|
|||||||
|
|
||||||
SyncStatus {
|
SyncStatus {
|
||||||
state: sync_state,
|
state: sync_state,
|
||||||
best_seen_block: best_seen,
|
best_seen_block,
|
||||||
num_peers: self.peers.len() as u32,
|
num_peers: self.peers.len() as u32,
|
||||||
queued_blocks: self.queue_blocks.len() as u32,
|
queued_blocks: self.queue_blocks.len() as u32,
|
||||||
state_sync: self.state_sync.as_ref().map(|s| s.progress()),
|
state_sync: self.state_sync.as_ref().map(|s| s.progress()),
|
||||||
@@ -693,7 +701,7 @@ where
|
|||||||
trace!(target: "sync", "Too many blocks in the queue.");
|
trace!(target: "sync", "Too many blocks in the queue.");
|
||||||
return Box::new(std::iter::empty())
|
return Box::new(std::iter::empty())
|
||||||
}
|
}
|
||||||
let major_sync = self.status().state == SyncState::Downloading;
|
let is_major_syncing = self.status().state.is_major_syncing();
|
||||||
let attrs = self.required_block_attributes();
|
let attrs = self.required_block_attributes();
|
||||||
let blocks = &mut self.blocks;
|
let blocks = &mut self.blocks;
|
||||||
let fork_targets = &mut self.fork_targets;
|
let fork_targets = &mut self.fork_targets;
|
||||||
@@ -703,7 +711,7 @@ where
|
|||||||
let client = &self.client;
|
let client = &self.client;
|
||||||
let queue = &self.queue_blocks;
|
let queue = &self.queue_blocks;
|
||||||
let allowed_requests = self.allowed_requests.take();
|
let allowed_requests = self.allowed_requests.take();
|
||||||
let max_parallel = if major_sync { 1 } else { self.max_parallel_downloads };
|
let max_parallel = if is_major_syncing { 1 } else { self.max_parallel_downloads };
|
||||||
let gap_sync = &mut self.gap_sync;
|
let gap_sync = &mut self.gap_sync;
|
||||||
let iter = self.peers.iter_mut().filter_map(move |(&id, peer)| {
|
let iter = self.peers.iter_mut().filter_map(move |(&id, peer)| {
|
||||||
if !peer.state.is_available() || !allowed_requests.contains(&id) {
|
if !peer.state.is_available() || !allowed_requests.contains(&id) {
|
||||||
@@ -1797,8 +1805,8 @@ where
|
|||||||
Ok((sync, Box::new(ChainSyncInterfaceHandle::new(tx))))
|
Ok((sync, Box::new(ChainSyncInterfaceHandle::new(tx))))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the best seen block number if we don't have that block yet, `None` otherwise.
|
/// Returns the median seen block number.
|
||||||
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<_>>();
|
let mut best_seens = self.peers.values().map(|p| p.best_number).collect::<Vec<_>>();
|
||||||
|
|
||||||
if best_seens.is_empty() {
|
if best_seens.is_empty() {
|
||||||
@@ -1807,12 +1815,7 @@ where
|
|||||||
let middle = best_seens.len() / 2;
|
let middle = best_seens.len() / 2;
|
||||||
|
|
||||||
// Not the "perfect median" when we have an even number of peers.
|
// Not the "perfect median" when we have an even number of peers.
|
||||||
let median = *best_seens.select_nth_unstable(middle).1;
|
Some(*best_seens.select_nth_unstable(middle).1)
|
||||||
if median > self.best_queued_number {
|
|
||||||
Some(median)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1854,7 +1857,7 @@ where
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let origin = if !gap && self.status().state != SyncState::Downloading {
|
let origin = if !gap && !self.status().state.is_major_syncing() {
|
||||||
BlockOrigin::NetworkBroadcast
|
BlockOrigin::NetworkBroadcast
|
||||||
} else {
|
} else {
|
||||||
BlockOrigin::NetworkInitialSync
|
BlockOrigin::NetworkInitialSync
|
||||||
|
|||||||
Reference in New Issue
Block a user