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

Commit

Permalink
Refactoring to unify sync target handling, removal of `NetworkStatus:…
Browse files Browse the repository at this point in the history
…:best_seen_block`
  • Loading branch information
nazar-pc committed Sep 6, 2022
1 parent bb8abd4 commit e1fb1e4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
2 changes: 0 additions & 2 deletions client/network/common/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ where
pub struct NetworkStatus<B: BlockT> {
/// Current global sync state.
pub sync_state: SyncState<NumberFor<B>>,
/// Target sync block number.
pub best_seen_block: Option<NumberFor<B>>,
/// Number of peers participating in syncing.
pub num_sync_peers: u32,
/// Total number of connected peers
Expand Down
15 changes: 11 additions & 4 deletions client/network/common/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,17 @@ pub enum SyncState<BlockNumber> {
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)
impl<BlockNumber> SyncState<BlockNumber>
where
BlockNumber: Clone,
{
/// Are we actively catching up with the chain? If so, returns `Some(target)`.
pub fn major_sync_target(&self) -> Option<BlockNumber> {
match self {
SyncState::Idle => None,
SyncState::Downloading { target } => Some(target.clone()),
SyncState::Importing { target } => Some(target.clone()),
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use sc_network_common::{
warp::{EncodedProof, WarpProofRequest},
BadPeer, ChainSync, OnBlockData, OnBlockJustification, OnStateData, OpaqueBlockRequest,
OpaqueBlockResponse, OpaqueStateRequest, OpaqueStateResponse, PollBlockAnnounceValidation,
SyncState, SyncStatus,
SyncStatus,
},
};
use sp_arithmetic::traits::SaturatedConversion;
Expand Down Expand Up @@ -501,11 +501,7 @@ where

/// Target sync block number.
pub fn best_seen_block(&self) -> Option<NumberFor<B>> {
match self.chain_sync.status().state {
SyncState::Idle => None,
SyncState::Downloading { target } => Some(target),
SyncState::Importing { .. } => None,
}
self.chain_sync.status().state.major_sync_target()
}

/// Number of peers participating in syncing.
Expand Down
4 changes: 2 additions & 2 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ where
let status = self.sync_state();
NetworkStatus {
sync_state: status.state,
best_seen_block: self.best_seen_block(),
num_sync_peers: self.num_sync_peers(),
num_connected_peers: self.num_connected_peers(),
num_active_peers: self.num_active_peers(),
Expand Down Expand Up @@ -1944,7 +1943,8 @@ where
.user_protocol_mut()
.sync_state()
.state
.is_major_syncing();
.major_sync_target()
.is_some();

this.tx_handler_controller.set_gossip_enabled(!is_major_syncing);

Expand Down
4 changes: 2 additions & 2 deletions client/network/sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ where
trace!(target: "sync", "Too many blocks in the queue.");
return Box::new(std::iter::empty())
}
let is_major_syncing = self.status().state.is_major_syncing();
let is_major_syncing = self.status().state.major_sync_target().is_some();
let attrs = self.required_block_attributes();
let blocks = &mut self.blocks;
let fork_targets = &mut self.fork_targets;
Expand Down Expand Up @@ -1776,7 +1776,7 @@ where
);
}

let origin = if !gap && !self.status().state.is_major_syncing() {
let origin = if !gap && self.status().state.major_sync_target().is_none() {
BlockOrigin::NetworkBroadcast
} else {
BlockOrigin::NetworkInitialSync
Expand Down
2 changes: 1 addition & 1 deletion client/service/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl MetricsService {

if let Some(metrics) = self.metrics.as_ref() {
let best_seen_block: Option<u64> =
net_status.best_seen_block.map(|num: NumberFor<T>| {
net_status.sync_state.major_sync_target().map(|num: NumberFor<T>| {
UniqueSaturatedInto::<u64>::unique_saturated_into(num)
});

Expand Down

0 comments on commit e1fb1e4

Please sign in to comment.