use crate::util::RwLock;
use std::sync::Arc;
use std::time::SystemTime;
use crate::core::core::hash::Hash;
use crate::core::ser::ProtocolVersion;
use chrono::prelude::*;
use crate::chain::SyncStatus;
use crate::p2p;
use crate::p2p::Capabilities;
use grin_core::pow::Difficulty;
#[derive(Clone)]
pub struct ServerStateInfo {
pub stratum_stats: Arc<RwLock<StratumStats>>,
}
impl Default for ServerStateInfo {
fn default() -> ServerStateInfo {
ServerStateInfo {
stratum_stats: Arc::new(RwLock::new(StratumStats::default())),
}
}
}
#[derive(Debug, Clone)]
pub struct ServerStats {
pub peer_count: u32,
pub chain_stats: ChainStats,
pub header_stats: ChainStats,
pub sync_status: SyncStatus,
pub stratum_stats: StratumStats,
pub peer_stats: Vec<PeerStats>,
pub diff_stats: DiffStats,
pub tx_stats: Option<TxStats>,
pub disk_usage_gb: String,
}
#[derive(Clone, Serialize, Debug)]
pub struct ChainStats {
pub height: u64,
pub last_block_h: Hash,
pub total_difficulty: Difficulty,
pub latest_timestamp: DateTime<Utc>,
}
#[derive(Clone, Serialize, Debug)]
pub struct TxStats {
pub tx_pool_size: usize,
pub tx_pool_kernels: usize,
pub stem_pool_size: usize,
pub stem_pool_kernels: usize,
}
#[derive(Clone, Serialize, Debug)]
pub struct WorkerStats {
pub id: String,
pub is_connected: bool,
pub last_seen: SystemTime,
pub initial_block_height: u64,
pub pow_difficulty: u64,
pub num_accepted: u64,
pub num_rejected: u64,
pub num_stale: u64,
pub num_blocks_found: u64,
}
#[derive(Clone, Serialize, Debug)]
pub struct StratumStats {
pub is_enabled: bool,
pub is_running: bool,
pub num_workers: usize,
pub block_height: u64,
pub network_difficulty: u64,
pub edge_bits: u16,
pub blocks_found: u16,
pub network_hashrate: f64,
pub minimum_share_difficulty: u64,
pub worker_stats: Vec<WorkerStats>,
}
#[derive(Debug, Clone)]
pub struct DiffStats {
pub height: u64,
pub last_blocks: Vec<DiffBlock>,
pub average_block_time: u64,
pub average_difficulty: u64,
pub window_size: u64,
}
#[derive(Clone, Debug)]
pub struct DiffBlock {
pub block_height: i64,
pub block_hash: Hash,
pub difficulty: u64,
pub time: u64,
pub duration: u64,
pub secondary_scaling: u32,
pub is_secondary: bool,
}
#[derive(Clone, Debug)]
pub struct PeerStats {
pub state: String,
pub addr: String,
pub version: ProtocolVersion,
pub user_agent: String,
pub total_difficulty: u64,
pub height: u64,
pub direction: String,
pub last_seen: DateTime<Utc>,
pub sent_bytes_per_sec: u64,
pub received_bytes_per_sec: u64,
pub capabilities: Capabilities,
}
impl PartialEq for PeerStats {
fn eq(&self, other: &PeerStats) -> bool {
*self.addr == other.addr
}
}
impl PartialEq for WorkerStats {
fn eq(&self, other: &WorkerStats) -> bool {
*self.id == other.id
}
}
impl PartialEq for DiffBlock {
fn eq(&self, other: &DiffBlock) -> bool {
self.block_height == other.block_height
}
}
impl PeerStats {
pub fn from_peer(peer: &p2p::Peer) -> PeerStats {
let state = if peer.is_banned() {
"Banned"
} else if peer.is_connected() {
"Connected"
} else {
"Disconnected"
};
let addr = peer.info.addr.to_string();
let direction = match peer.info.direction {
p2p::types::Direction::Inbound => "Inbound",
p2p::types::Direction::Outbound => "Outbound",
};
PeerStats {
state: state.to_string(),
addr: addr,
version: peer.info.version,
user_agent: peer.info.user_agent.clone(),
total_difficulty: peer.info.total_difficulty().to_num(),
height: peer.info.height(),
direction: direction.to_string(),
last_seen: peer.info.last_seen(),
sent_bytes_per_sec: peer.tracker().sent_bytes.read().bytes_per_min() / 60,
received_bytes_per_sec: peer.tracker().received_bytes.read().bytes_per_min() / 60,
capabilities: peer.info.capabilities,
}
}
}
impl Default for WorkerStats {
fn default() -> WorkerStats {
WorkerStats {
id: String::from("unknown"),
is_connected: false,
last_seen: SystemTime::now(),
initial_block_height: 0,
pow_difficulty: 0,
num_accepted: 0,
num_rejected: 0,
num_stale: 0,
num_blocks_found: 0,
}
}
}
impl Default for StratumStats {
fn default() -> StratumStats {
StratumStats {
is_enabled: false,
is_running: false,
num_workers: 0,
block_height: 0,
network_difficulty: 0,
edge_bits: 32,
blocks_found: 0,
network_hashrate: 0.0,
minimum_share_difficulty: 1,
worker_stats: Vec::new(),
}
}
}