1use crate::util::RwLock;
19use std::sync::Arc;
20use std::time::SystemTime;
21
22use crate::core::core::hash::Hash;
23use crate::core::ser::ProtocolVersion;
24
25use chrono::prelude::*;
26
27use crate::chain::SyncStatus;
28use crate::p2p;
29use crate::p2p::Capabilities;
30use gringron_core::pow::Difficulty;
31
32#[derive(Clone)]
35pub struct ServerStateInfo {
36 pub stratum_stats: Arc<RwLock<StratumStats>>,
38}
39
40impl Default for ServerStateInfo {
41 fn default() -> ServerStateInfo {
42 ServerStateInfo {
43 stratum_stats: Arc::new(RwLock::new(StratumStats::default())),
44 }
45 }
46}
47#[derive(Debug, Clone)]
50pub struct ServerStats {
51 pub peer_count: u32,
53 pub chain_stats: ChainStats,
55 pub header_stats: ChainStats,
57 pub sync_status: SyncStatus,
59 pub stratum_stats: StratumStats,
61 pub peer_stats: Vec<PeerStats>,
63 pub diff_stats: DiffStats,
65 pub tx_stats: Option<TxStats>,
67 pub disk_usage_gb: String,
69}
70
71#[derive(Clone, Serialize, Debug)]
73pub struct ChainStats {
74 pub height: u64,
76 pub last_block_h: Hash,
78 pub total_difficulty: Difficulty,
80 pub latest_timestamp: DateTime<Utc>,
82}
83#[derive(Clone, Serialize, Debug)]
85pub struct TxStats {
86 pub tx_pool_size: usize,
88 pub tx_pool_kernels: usize,
90 pub stem_pool_size: usize,
92 pub stem_pool_kernels: usize,
94}
95#[derive(Clone, Serialize, Debug)]
97pub struct WorkerStats {
98 pub id: String,
100 pub is_connected: bool,
102 pub last_seen: SystemTime,
104 pub initial_block_height: u64,
106 pub pow_difficulty: u64,
108 pub num_accepted: u64,
110 pub num_rejected: u64,
112 pub num_stale: u64,
114 pub num_blocks_found: u64,
116}
117
118#[derive(Clone, Serialize, Debug)]
120pub struct StratumStats {
121 pub is_enabled: bool,
123 pub is_running: bool,
125 pub num_workers: usize,
127 pub block_height: u64,
129 pub network_difficulty: u64,
131 pub edge_bits: u16,
133 pub blocks_found: u16,
135 pub network_hashrate: f64,
137 pub minimum_share_difficulty: u64,
139 pub worker_stats: Vec<WorkerStats>,
141}
142
143#[derive(Debug, Clone)]
145pub struct DiffStats {
146 pub height: u64,
148 pub last_blocks: Vec<DiffBlock>,
150 pub average_block_time: u64,
152 pub average_difficulty: u64,
154 pub window_size: u64,
156}
157
158#[derive(Clone, Debug)]
160pub struct DiffBlock {
161 pub block_height: i64,
163 pub block_hash: Hash,
165 pub difficulty: u64,
167 pub time: u64,
169 pub duration: u64,
171 pub secondary_scaling: u32,
173 pub is_secondary: bool,
175}
176
177#[derive(Clone, Debug)]
179pub struct PeerStats {
180 pub state: String,
182 pub addr: String,
184 pub version: ProtocolVersion,
186 pub user_agent: String,
188 pub total_difficulty: u64,
190 pub height: u64,
192 pub direction: String,
194 pub last_seen: DateTime<Utc>,
196 pub sent_bytes_per_sec: u64,
198 pub received_bytes_per_sec: u64,
200 pub capabilities: Capabilities,
202}
203
204impl PartialEq for PeerStats {
205 fn eq(&self, other: &PeerStats) -> bool {
206 *self.addr == other.addr
207 }
208}
209
210impl PartialEq for WorkerStats {
211 fn eq(&self, other: &WorkerStats) -> bool {
212 *self.id == other.id
213 }
214}
215
216impl PartialEq for DiffBlock {
217 fn eq(&self, other: &DiffBlock) -> bool {
218 self.block_height == other.block_height
219 }
220}
221
222impl PeerStats {
223 pub fn from_peer(peer: &p2p::Peer) -> PeerStats {
225 let state = if peer.is_banned() {
227 "Banned"
228 } else if peer.is_connected() {
229 "Connected"
230 } else {
231 "Disconnected"
232 };
233 let addr = peer.info.addr.to_string();
234 let direction = match peer.info.direction {
235 p2p::types::Direction::Inbound => "Inbound",
236 p2p::types::Direction::Outbound => "Outbound",
237 };
238 PeerStats {
239 state: state.to_string(),
240 addr: addr,
241 version: peer.info.version,
242 user_agent: peer.info.user_agent.clone(),
243 total_difficulty: peer.info.total_difficulty().to_num(),
244 height: peer.info.height(),
245 direction: direction.to_string(),
246 last_seen: peer.info.last_seen(),
247 sent_bytes_per_sec: peer.tracker().sent_bytes.read().bytes_per_min() / 60,
248 received_bytes_per_sec: peer.tracker().received_bytes.read().bytes_per_min() / 60,
249 capabilities: peer.info.capabilities,
250 }
251 }
252}
253
254impl Default for WorkerStats {
255 fn default() -> WorkerStats {
256 WorkerStats {
257 id: String::from("unknown"),
258 is_connected: false,
259 last_seen: SystemTime::now(),
260 initial_block_height: 0,
261 pow_difficulty: 0,
262 num_accepted: 0,
263 num_rejected: 0,
264 num_stale: 0,
265 num_blocks_found: 0,
266 }
267 }
268}
269
270impl Default for StratumStats {
271 fn default() -> StratumStats {
272 StratumStats {
273 is_enabled: false,
274 is_running: false,
275 num_workers: 0,
276 block_height: 0,
277 network_difficulty: 0,
278 edge_bits: 32,
279 blocks_found: 0,
280 network_hashrate: 0.0,
281 minimum_share_difficulty: 1,
282 worker_stats: Vec::new(),
283 }
284 }
285}