use console::style;
use log::info;
use sc_client_api::ClientInfo;
use sc_network::NetworkStatus;
use sc_network_sync::{SyncState, SyncStatus, WarpSyncPhase, WarpSyncProgress};
use sp_runtime::traits::{Block as BlockT, CheckedDiv, NumberFor, Saturating, Zero};
use std::{fmt, time::Instant};
pub struct InformantDisplay<B: BlockT> {
last_number: Option<NumberFor<B>>,
last_update: Instant,
last_total_bytes_inbound: u64,
last_total_bytes_outbound: u64,
}
impl<B: BlockT> InformantDisplay<B> {
pub fn new() -> InformantDisplay<B> {
InformantDisplay {
last_number: None,
last_update: Instant::now(),
last_total_bytes_inbound: 0,
last_total_bytes_outbound: 0,
}
}
pub fn display(
&mut self,
info: &ClientInfo<B>,
net_status: NetworkStatus,
sync_status: SyncStatus<B>,
num_connected_peers: usize,
) {
let best_number = info.chain.best_number;
let best_hash = info.chain.best_hash;
let finalized_number = info.chain.finalized_number;
let speed = speed::<B>(best_number, self.last_number, self.last_update);
let total_bytes_inbound = net_status.total_bytes_inbound;
let total_bytes_outbound = net_status.total_bytes_outbound;
let now = Instant::now();
let elapsed = (now - self.last_update).as_secs();
self.last_update = now;
self.last_number = Some(best_number);
let diff_bytes_inbound = total_bytes_inbound - self.last_total_bytes_inbound;
let diff_bytes_outbound = total_bytes_outbound - self.last_total_bytes_outbound;
let (avg_bytes_per_sec_inbound, avg_bytes_per_sec_outbound) = if elapsed > 0 {
self.last_total_bytes_inbound = total_bytes_inbound;
self.last_total_bytes_outbound = total_bytes_outbound;
(diff_bytes_inbound / elapsed, diff_bytes_outbound / elapsed)
} else {
(diff_bytes_inbound, diff_bytes_outbound)
};
let (level, status, target) =
match (sync_status.state, sync_status.state_sync, sync_status.warp_sync) {
(
sync_status,
_,
Some(WarpSyncProgress { phase: WarpSyncPhase::DownloadingBlocks(n), .. }),
) if !sync_status.is_major_syncing() => ("⏩", "Block history".into(), format!(", #{}", n)),
(_, _, Some(warp))
if !matches!(warp.phase, WarpSyncPhase::DownloadingBlocks(_)) =>
(
"⏩",
"Warping".into(),
format!(
", {}, {:.2} Mib",
warp.phase,
(warp.total_bytes as f32) / (1024f32 * 1024f32)
),
),
(_, Some(state), _) => (
"⚙️ ",
"State sync".into(),
format!(
", {}, {}%, {:.2} Mib",
state.phase,
state.percentage,
(state.size as f32) / (1024f32 * 1024f32)
),
),
(SyncState::Idle, _, _) => ("💤", "Idle".into(), "".into()),
(SyncState::Downloading { target }, _, _) =>
("⚙️ ", format!("Syncing{}", speed), format!(", target=#{target}")),
(SyncState::Importing { target }, _, _) =>
("⚙️ ", format!("Preparing{}", speed), format!(", target=#{target}")),
};
info!(
target: "substrate",
"{} {}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}",
level,
style(&status).white().bold(),
target,
style(num_connected_peers).white().bold(),
style(best_number).white().bold(),
best_hash,
style(finalized_number).white().bold(),
info.chain.finalized_hash,
style(TransferRateFormat(avg_bytes_per_sec_inbound)).green(),
style(TransferRateFormat(avg_bytes_per_sec_outbound)).red(),
)
}
}
fn speed<B: BlockT>(
best_number: NumberFor<B>,
last_number: Option<NumberFor<B>>,
last_update: Instant,
) -> String {
let elapsed_ms = {
let elapsed = last_update.elapsed();
let since_last_millis = elapsed.as_secs() * 1000;
let since_last_subsec_millis = elapsed.subsec_millis() as u64;
since_last_millis + since_last_subsec_millis
};
let diff = match last_number {
None => return String::new(),
Some(n) => best_number.saturating_sub(n),
};
if let Ok(diff) = TryInto::<u128>::try_into(diff) {
let speed = diff
.saturating_mul(10_000)
.checked_div(u128::from(elapsed_ms))
.map_or(0.0, |s| s as f64) /
10.0;
format!(" {:4.1} bps", speed)
} else {
let one_thousand = NumberFor::<B>::from(1_000u32);
let elapsed =
NumberFor::<B>::from(<u32 as TryFrom<_>>::try_from(elapsed_ms).unwrap_or(u32::MAX));
let speed = diff
.saturating_mul(one_thousand)
.checked_div(&elapsed)
.unwrap_or_else(Zero::zero);
format!(" {} bps", speed)
}
}
struct TransferRateFormat(u64);
impl fmt::Display for TransferRateFormat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.0 == 0 {
return write!(f, "0")
}
if self.0 < 100 {
return write!(f, "{} B/s", self.0)
}
if self.0 < 1024 * 1024 {
return write!(f, "{:.1}kiB/s", self.0 as f64 / 1024.0)
}
write!(f, "{:.1}MiB/s", self.0 as f64 / (1024.0 * 1024.0))
}
}