1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use crate::OutputFormat;
use ansi_term::Colour;
use log::info;
use sc_client_api::ClientInfo;
use sc_network::{NetworkStatus, SyncState};
use sp_runtime::traits::{Block as BlockT, CheckedDiv, NumberFor, Saturating, Zero};
use std::{
convert::{TryFrom, TryInto},
fmt,
};
use wasm_timer::Instant;
pub struct InformantDisplay<B: BlockT> {
last_number: Option<NumberFor<B>>,
last_update: Instant,
last_total_bytes_inbound: u64,
last_total_bytes_outbound: u64,
format: OutputFormat,
}
impl<B: BlockT> InformantDisplay<B> {
pub fn new(format: OutputFormat) -> InformantDisplay<B> {
InformantDisplay {
last_number: None,
last_update: Instant::now(),
last_total_bytes_inbound: 0,
last_total_bytes_outbound: 0,
format,
}
}
pub fn display(&mut self, info: &ClientInfo<B>, net_status: NetworkStatus<B>) {
let best_number = info.chain.best_number;
let best_hash = info.chain.best_hash;
let finalized_number = info.chain.finalized_number;
let num_connected_peers = net_status.num_connected_peers;
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 (
net_status.sync_state,
net_status.best_seen_block,
net_status.state_sync,
net_status.warp_sync,
) {
(_, _, _, Some(warp)) => (
"⏩",
"Warping".into(),
format!(
", {}, ({:.2}) Mib",
warp.phase,
(warp.total_bytes as f32) / (1024f32 * 1024f32)
),
),
(_, _, Some(state), _) => (
"⚙️ ",
"Downloading state".into(),
format!(
", {}%, ({:.2}) Mib",
state.percentage,
(state.size as f32) / (1024f32 * 1024f32)
),
),
(SyncState::Idle, _, _, _) => ("💤", "Idle".into(), "".into()),
(SyncState::Downloading, None, _, _) =>
("⚙️ ", format!("Preparing{}", speed), "".into()),
(SyncState::Downloading, Some(n), None, _) =>
("⚙️ ", format!("Syncing{}", speed), format!(", target=#{}", n)),
};
if self.format.enable_color {
info!(
target: "substrate",
"{} {}{} ({} peers), best: #{} ({}), finalized #{} ({}), {} {}",
level,
Colour::White.bold().paint(&status),
target,
Colour::White.bold().paint(format!("{}", num_connected_peers)),
Colour::White.bold().paint(format!("{}", best_number)),
best_hash,
Colour::White.bold().paint(format!("{}", finalized_number)),
info.chain.finalized_hash,
Colour::Green.paint(format!("⬇ {}", TransferRateFormat(avg_bytes_per_sec_inbound))),
Colour::Red.paint(format!("⬆ {}", TransferRateFormat(avg_bytes_per_sec_outbound))),
)
} else {
info!(
target: "substrate",
"{} {}{} ({} peers), best: #{} ({}), finalized #{} ({}), ⬇ {} ⬆ {}",
level,
status,
target,
num_connected_peers,
best_number,
best_hash,
finalized_number,
info.chain.finalized_hash,
TransferRateFormat(avg_bytes_per_sec_inbound),
TransferRateFormat(avg_bytes_per_sec_outbound),
)
}
}
}
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))
}
}