use sc_chain_spec::{ChainType, Properties};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug)]
pub struct SystemInfo {
pub impl_name: String,
pub impl_version: String,
pub chain_name: String,
pub properties: Properties,
pub chain_type: ChainType,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Health {
pub peers: usize,
pub is_syncing: bool,
pub should_have_peers: bool,
}
impl fmt::Display for Health {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{} peers ({})", self.peers, if self.is_syncing { "syncing" } else { "idle" })
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PeerInfo<Hash, Number> {
pub peer_id: String,
pub roles: String,
pub best_hash: Hash,
pub best_number: Number,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum NodeRole {
Full,
Authority,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncState<Number> {
pub starting_block: Number,
pub current_block: Number,
pub highest_block: Number,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_serialize_health() {
assert_eq!(
::serde_json::to_string(&Health {
peers: 1,
is_syncing: false,
should_have_peers: true,
})
.unwrap(),
r#"{"peers":1,"isSyncing":false,"shouldHavePeers":true}"#,
);
}
#[test]
fn should_serialize_peer_info() {
assert_eq!(
::serde_json::to_string(&PeerInfo {
peer_id: "2".into(),
roles: "a".into(),
best_hash: 5u32,
best_number: 6u32,
})
.unwrap(),
r#"{"peerId":"2","roles":"a","bestHash":5,"bestNumber":6}"#,
);
}
#[test]
fn should_serialize_sync_state() {
assert_eq!(
::serde_json::to_string(&SyncState {
starting_block: 12u32,
current_block: 50u32,
highest_block: 128u32,
})
.unwrap(),
r#"{"startingBlock":12,"currentBlock":50,"highestBlock":128}"#,
);
assert_eq!(
::serde_json::to_string(&SyncState {
starting_block: 12u32,
current_block: 50u32,
highest_block: 50u32,
})
.unwrap(),
r#"{"startingBlock":12,"currentBlock":50,"highestBlock":50}"#,
);
}
}