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
+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)]