sc_rpc_api/system/
helpers.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Substrate system API helpers.
20
21use sc_chain_spec::{ChainType, Properties};
22use serde::{Deserialize, Serialize};
23use std::fmt;
24
25/// Running node's static details.
26#[derive(Clone, Debug)]
27pub struct SystemInfo {
28	/// Implementation name.
29	pub impl_name: String,
30	/// Implementation version.
31	pub impl_version: String,
32	/// Chain name.
33	pub chain_name: String,
34	/// A custom set of properties defined in the chain spec.
35	pub properties: Properties,
36	/// The type of this chain.
37	pub chain_type: ChainType,
38}
39
40/// Health struct returned by the RPC
41#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct Health {
44	/// Number of connected peers
45	pub peers: usize,
46	/// Is the node syncing
47	pub is_syncing: bool,
48	/// Should this node have any peers
49	///
50	/// Might be false for local chains or when running without discovery.
51	pub should_have_peers: bool,
52}
53
54impl fmt::Display for Health {
55	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
56		write!(fmt, "{} peers ({})", self.peers, if self.is_syncing { "syncing" } else { "idle" })
57	}
58}
59
60/// Network Peer information
61#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
62#[serde(rename_all = "camelCase")]
63pub struct PeerInfo<Hash, Number> {
64	/// Peer ID
65	pub peer_id: String,
66	/// Roles
67	pub roles: String,
68	/// Peer best block hash
69	pub best_hash: Hash,
70	/// Peer best block number
71	pub best_number: Number,
72}
73
74/// The role the node is running as
75#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
76pub enum NodeRole {
77	/// The node is a full node
78	Full,
79	/// The node is an authority
80	Authority,
81}
82
83/// The state of the syncing of the node.
84#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
85#[serde(rename_all = "camelCase")]
86pub struct SyncState<Number> {
87	/// Height of the block at which syncing started.
88	pub starting_block: Number,
89	/// Height of the current best block of the node.
90	pub current_block: Number,
91	/// Height of the highest block in the network.
92	pub highest_block: Number,
93}
94
95#[cfg(test)]
96mod tests {
97	use super::*;
98
99	#[test]
100	fn should_serialize_health() {
101		assert_eq!(
102			::serde_json::to_string(&Health {
103				peers: 1,
104				is_syncing: false,
105				should_have_peers: true,
106			})
107			.unwrap(),
108			r#"{"peers":1,"isSyncing":false,"shouldHavePeers":true}"#,
109		);
110	}
111
112	#[test]
113	fn should_serialize_peer_info() {
114		assert_eq!(
115			::serde_json::to_string(&PeerInfo {
116				peer_id: "2".into(),
117				roles: "a".into(),
118				best_hash: 5u32,
119				best_number: 6u32,
120			})
121			.unwrap(),
122			r#"{"peerId":"2","roles":"a","bestHash":5,"bestNumber":6}"#,
123		);
124	}
125
126	#[test]
127	fn should_serialize_sync_state() {
128		assert_eq!(
129			::serde_json::to_string(&SyncState {
130				starting_block: 12u32,
131				current_block: 50u32,
132				highest_block: 128u32,
133			})
134			.unwrap(),
135			r#"{"startingBlock":12,"currentBlock":50,"highestBlock":128}"#,
136		);
137
138		assert_eq!(
139			::serde_json::to_string(&SyncState {
140				starting_block: 12u32,
141				current_block: 50u32,
142				highest_block: 50u32,
143			})
144			.unwrap(),
145			r#"{"startingBlock":12,"currentBlock":50,"highestBlock":50}"#,
146		);
147	}
148}