Add new RPC method to get the chain type (#5576)

* Add new RPC method to get the chain type

This adds a new RPC method to get the chain type of the running chain.
The chain type needs to be specified in the chain spec. This should make
it easier for tools/UI to display extra information without needing to
rely on parsing the chain name.

* Update client/rpc-api/src/system/mod.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Primitive crate

* Feedback

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
Bastian Köcher
2020-04-08 20:41:51 +02:00
committed by GitHub
parent 1d2cbfbdf9
commit 7cdfaff12b
19 changed files with 135 additions and 26 deletions
+1
View File
@@ -16,6 +16,7 @@ sp-core = { version = "2.0.0-alpha.5", path = "../../primitives/core" }
serde = { version = "1.0.101", features = ["derive"] }
serde_json = "1.0.41"
sp-runtime = { version = "2.0.0-alpha.5", path = "../../primitives/runtime" }
sp-chain-spec = { version = "2.0.0-alpha.5", path = "../../primitives/chain-spec" }
sc-telemetry = { version = "2.0.0-alpha.5", path = "../telemetry" }
[package.metadata.docs.rs]
+15 -5
View File
@@ -25,8 +25,7 @@ use serde::{Serialize, Deserialize};
use sp_core::storage::{StorageKey, StorageData, ChildInfo, Storage, StorageChild};
use sp_runtime::BuildStorage;
use serde_json as json;
use crate::RuntimeGenesis;
use crate::extension::GetExtension;
use crate::{RuntimeGenesis, ChainType, extension::GetExtension, Properties};
use sc_network::config::MultiaddrWithPeerId;
use sc_telemetry::TelemetryEndpoints;
@@ -137,6 +136,8 @@ enum Genesis<G> {
struct ClientSpec<E> {
name: String,
id: String,
#[serde(default)]
chain_type: ChainType,
boot_nodes: Vec<MultiaddrWithPeerId>,
telemetry_endpoints: Option<TelemetryEndpoints>,
protocol_id: Option<String>,
@@ -149,9 +150,6 @@ struct ClientSpec<E> {
genesis: serde::de::IgnoredAny,
}
/// Arbitrary properties defined in chain spec as a JSON object
pub type Properties = json::map::Map<String, json::Value>;
/// A type denoting empty extensions.
///
/// We use `Option` here since `()` is not flattenable by serde.
@@ -219,6 +217,7 @@ impl<G, E> ChainSpec<G, E> {
pub fn from_genesis<F: Fn() -> G + 'static + Send + Sync>(
name: &str,
id: &str,
chain_type: ChainType,
constructor: F,
boot_nodes: Vec<MultiaddrWithPeerId>,
telemetry_endpoints: Option<TelemetryEndpoints>,
@@ -229,6 +228,7 @@ impl<G, E> ChainSpec<G, E> {
let client_spec = ClientSpec {
name: name.to_owned(),
id: id.to_owned(),
chain_type,
boot_nodes,
telemetry_endpoints,
protocol_id: protocol_id.map(str::to_owned),
@@ -243,6 +243,11 @@ impl<G, E> ChainSpec<G, E> {
genesis: GenesisSource::Factory(Arc::new(constructor)),
}
}
/// Type of the chain.
fn chain_type(&self) -> ChainType {
self.client_spec.chain_type.clone()
}
}
impl<G, E: serde::de::DeserializeOwned> ChainSpec<G, E> {
@@ -332,6 +337,10 @@ where
ChainSpec::id(self)
}
fn chain_type(&self) -> ChainType {
ChainSpec::chain_type(self)
}
fn telemetry_endpoints(&self) -> &Option<TelemetryEndpoints> {
ChainSpec::telemetry_endpoints(self)
}
@@ -392,6 +401,7 @@ mod tests {
).unwrap();
assert_eq!(spec1.as_json(false), spec2.as_json(false));
assert_eq!(spec2.chain_type(), ChainType::Live)
}
#[derive(Debug, Serialize, Deserialize)]
+5 -3
View File
@@ -107,13 +107,13 @@
//! pub type MyChainSpec<G> = GenericChainSpec<G, Extension>;
//! ```
mod chain_spec;
mod extension;
pub use chain_spec::{ChainSpec as GenericChainSpec, Properties, NoExtension};
pub use chain_spec::{ChainSpec as GenericChainSpec, NoExtension};
pub use extension::{Group, Fork, Forks, Extension, GetExtension, get_extension};
pub use sc_chain_spec_derive::{ChainSpecExtension, ChainSpecGroup};
pub use sp_chain_spec::{Properties, ChainType};
use serde::{Serialize, de::DeserializeOwned};
use sp_runtime::BuildStorage;
@@ -124,12 +124,14 @@ use sc_telemetry::TelemetryEndpoints;
pub trait RuntimeGenesis: Serialize + DeserializeOwned + BuildStorage {}
impl<T: Serialize + DeserializeOwned + BuildStorage> RuntimeGenesis for T {}
/// Common interface to `GenericChainSpec`
/// Common interface of a chain specification.
pub trait ChainSpec: BuildStorage + Send {
/// Spec name.
fn name(&self) -> &str;
/// Spec id.
fn id(&self) -> &str;
/// Type of the chain.
fn chain_type(&self) -> ChainType;
/// A list of bootnode addresses.
fn boot_nodes(&self) -> &[MultiaddrWithPeerId];
/// Telemetry endpoints (if any)
+1
View File
@@ -21,6 +21,7 @@ parking_lot = "0.10.0"
sp-core = { version = "2.0.0-alpha.5", path = "../../primitives/core" }
sp-version = { version = "2.0.0-alpha.5", path = "../../primitives/version" }
sp-runtime = { path = "../../primitives/runtime" , version = "2.0.0-alpha.5"}
sp-chain-spec = { path = "../../primitives/chain-spec" , version = "2.0.0-alpha.5"}
serde = { version = "1.0.101", features = ["derive"] }
serde_json = "1.0.41"
sp-transaction-pool = { version = "2.0.0-alpha.5", path = "../../primitives/transaction-pool" }
@@ -18,10 +18,7 @@
use std::fmt;
use serde::{Serialize, Deserialize};
use serde_json::{Value, map::Map};
/// Node properties
pub type Properties = Map<String, Value>;
use sp_chain_spec::{Properties, ChainType};
/// Running node's static details.
#[derive(Clone, Debug)]
@@ -34,6 +31,8 @@ pub struct SystemInfo {
pub chain_name: String,
/// A custom set of properties defined in the chain spec.
pub properties: Properties,
/// The type of this chain.
pub chain_type: ChainType,
}
/// Health struct returned by the RPC
+7 -3
View File
@@ -25,7 +25,7 @@ use futures::{future::BoxFuture, compat::Compat};
use self::error::Result as SystemResult;
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo, NodeRole};
pub use self::helpers::{SystemInfo, Health, PeerInfo, NodeRole};
pub use self::gen_client::Client as SystemClient;
/// Substrate system RPC API
@@ -39,13 +39,17 @@ pub trait SystemApi<Hash, Number> {
#[rpc(name = "system_version")]
fn system_version(&self) -> SystemResult<String>;
/// Get the chain's type. Given as a string identifier.
/// Get the chain's name. Given as a string identifier.
#[rpc(name = "system_chain")]
fn system_chain(&self) -> SystemResult<String>;
/// Get the chain's type.
#[rpc(name = "system_chainType")]
fn system_type(&self) -> SystemResult<sp_chain_spec::ChainType>;
/// Get a custom set of properties as a JSON object, defined in the chain spec.
#[rpc(name = "system_properties")]
fn system_properties(&self) -> SystemResult<Properties>;
fn system_properties(&self) -> SystemResult<sp_chain_spec::Properties>;
/// Return health status of the node.
///
+1
View File
@@ -27,6 +27,7 @@ sp-runtime = { version = "2.0.0-alpha.5", path = "../../primitives/runtime" }
sp-utils = { version = "2.0.0-alpha.5", path = "../../primitives/utils" }
sp-rpc = { version = "2.0.0-alpha.5", path = "../../primitives/rpc" }
sp-state-machine = { version = "0.8.0-alpha.5", path = "../../primitives/state-machine" }
sp-chain-spec = { version = "2.0.0-alpha.5", path = "../../primitives/chain-spec" }
sc-executor = { version = "0.8.0-alpha.5", path = "../executor" }
sc-block-builder = { version = "0.8.0-alpha.5", path = "../../client/block-builder" }
sc-keystore = { version = "2.0.0-alpha.5", path = "../keystore" }
+6 -2
View File
@@ -28,7 +28,7 @@ use sp_runtime::traits::{self, Header as HeaderT};
use self::error::Result;
pub use sc_rpc_api::system::*;
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo, NodeRole};
pub use self::helpers::{SystemInfo, Health, PeerInfo, NodeRole};
pub use self::gen_client::Client as SystemClient;
/// System API implementation
@@ -82,7 +82,11 @@ impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for Sy
Ok(self.info.chain_name.clone())
}
fn system_properties(&self) -> Result<Properties> {
fn system_type(&self) -> Result<sp_chain_spec::ChainType> {
Ok(self.info.chain_type.clone())
}
fn system_properties(&self) -> Result<sp_chain_spec::Properties> {
Ok(self.info.properties.clone())
}
+13 -4
View File
@@ -105,6 +105,7 @@ fn api<T: Into<Option<Status>>>(sync: T) -> System<Block> {
impl_version: "0.2.0".into(),
chain_name: "testchain".into(),
properties: Default::default(),
chain_type: Default::default(),
}, tx)
}
@@ -117,7 +118,7 @@ fn wait_receiver<T>(rx: Receiver<T>) -> T {
fn system_name_works() {
assert_eq!(
api(None).system_name().unwrap(),
"testclient".to_owned()
"testclient".to_owned(),
);
}
@@ -125,7 +126,7 @@ fn system_name_works() {
fn system_version_works() {
assert_eq!(
api(None).system_version().unwrap(),
"0.2.0".to_owned()
"0.2.0".to_owned(),
);
}
@@ -133,7 +134,7 @@ fn system_version_works() {
fn system_chain_works() {
assert_eq!(
api(None).system_chain().unwrap(),
"testchain".to_owned()
"testchain".to_owned(),
);
}
@@ -141,7 +142,15 @@ fn system_chain_works() {
fn system_properties_works() {
assert_eq!(
api(None).system_properties().unwrap(),
serde_json::map::Map::new()
serde_json::map::Map::new(),
);
}
#[test]
fn system_type_works() {
assert_eq!(
api(None).system_type().unwrap(),
Default::default(),
);
}
+1
View File
@@ -1016,6 +1016,7 @@ ServiceBuilder<
impl_name: config.impl_name.into(),
impl_version: config.impl_version.into(),
properties: chain_spec.properties().clone(),
chain_type: chain_spec.chain_type().clone(),
};
let subscriptions = sc_rpc::Subscriptions::new(Arc::new(tasks_builder.spawn_handle()));
+1 -1
View File
@@ -62,7 +62,7 @@ pub use self::builder::{
pub use config::{Configuration, Role, PruningMode, DatabaseConfig};
pub use sc_chain_spec::{
ChainSpec, GenericChainSpec, Properties, RuntimeGenesis, Extension as ChainSpecExtension,
NoExtension,
NoExtension, ChainType,
};
pub use sp_transaction_pool::{TransactionPool, InPoolTransaction, error::IntoPoolError};
pub use sc_transaction_pool::txpool::Options as TransactionPoolOptions;