mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 22:47:56 +00:00
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:
@@ -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)]
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user