Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Add new RPC method to get the chain type #5576

Merged
merged 5 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ members = [
"primitives/consensus/pow",
"primitives/consensus/vrf",
"primitives/core",
"primitives/chain-spec",
"primitives/debug-derive",
"primitives/storage",
"primitives/externalities",
Expand Down
8 changes: 5 additions & 3 deletions bin/node-template/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use node_template_runtime::{
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig,
SudoConfig, SystemConfig, WASM_BINARY, Signature
};
use sp_consensus_aura::sr25519::{AuthorityId as AuraId};
use sp_finality_grandpa::{AuthorityId as GrandpaId};
use sc_service;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_finality_grandpa::AuthorityId as GrandpaId;
use sp_runtime::traits::{Verify, IdentifyAccount};
use sc_service::ChainType;

// Note this is the URL for the telemetry server
//const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
Expand Down Expand Up @@ -42,6 +42,7 @@ pub fn development_config() -> ChainSpec {
ChainSpec::from_genesis(
"Development",
"dev",
ChainType::Development,
|| testnet_genesis(
vec![
authority_keys_from_seed("Alice"),
Expand All @@ -67,6 +68,7 @@ pub fn local_testnet_config() -> ChainSpec {
ChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
ChainType::Local,
|| testnet_genesis(
vec![
authority_keys_from_seed("Alice"),
Expand Down
7 changes: 6 additions & 1 deletion bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use node_runtime::{
};
use node_runtime::Block;
use node_runtime::constants::currency::*;
use sc_service;
use sc_service::ChainType;
use hex_literal::hex;
use sc_telemetry::TelemetryEndpoints;
use grandpa_primitives::{AuthorityId as GrandpaId};
Expand Down Expand Up @@ -158,6 +158,7 @@ pub fn staging_testnet_config() -> ChainSpec {
ChainSpec::from_genesis(
"Staging Testnet",
"staging_testnet",
ChainType::Live,
staging_testnet_config_genesis,
boot_nodes,
Some(TelemetryEndpoints::new(vec![(STAGING_TELEMETRY_URL.to_string(), 0)])
Expand Down Expand Up @@ -338,6 +339,7 @@ pub fn development_config() -> ChainSpec {
ChainSpec::from_genesis(
"Development",
"dev",
ChainType::Development,
development_config_genesis,
vec![],
None,
Expand All @@ -364,6 +366,7 @@ pub fn local_testnet_config() -> ChainSpec {
ChainSpec::from_genesis(
"Local Testnet",
"local_testnet",
ChainType::Local,
local_testnet_genesis,
vec![],
None,
Expand Down Expand Up @@ -396,6 +399,7 @@ pub(crate) mod tests {
ChainSpec::from_genesis(
"Integration Test",
"test",
ChainType::Development,
local_testnet_genesis_instant_single,
vec![],
None,
Expand All @@ -410,6 +414,7 @@ pub(crate) mod tests {
ChainSpec::from_genesis(
"Integration Test",
"test",
ChainType::Development,
local_testnet_genesis,
vec![],
None,
Expand Down
1 change: 1 addition & 0 deletions bin/utils/chain-spec-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = "https://github.com/paritytech/substrate/"
[dependencies]
ansi_term = "0.12.1"
sc-keystore = { version = "2.0.0-alpha.5", path = "../../../client/keystore" }
sc-chain-spec = { version = "2.0.0-alpha.5", path = "../../../client/chain-spec" }
node-cli = { version = "2.0.0-alpha.5", path = "../../node/cli" }
sp-core = { version = "2.0.0-alpha.5", path = "../../../primitives/core" }
rand = "0.7.2"
Expand Down
1 change: 1 addition & 0 deletions bin/utils/chain-spec-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ fn generate_chain_spec(
let chain_spec = chain_spec::ChainSpec::from_genesis(
"Custom",
"custom",
sc_chain_spec::ChainType::Live,
move || genesis_constructor(&authority_seeds, &endowed_accounts, &sudo_account),
vec![],
None,
Expand Down
1 change: 1 addition & 0 deletions client/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
20 changes: 15 additions & 5 deletions client/chain-spec/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>,
Expand All @@ -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.
Expand Down Expand Up @@ -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>,
Expand All @@ -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),
Expand All @@ -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> {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)]
Expand Down
8 changes: 5 additions & 3 deletions client/chain-spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions client/rpc-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
7 changes: 3 additions & 4 deletions client/rpc-api/src/system/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions client/rpc-api/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
///
Expand Down
1 change: 1 addition & 0 deletions client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
8 changes: 6 additions & 2 deletions client/rpc/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}

Expand Down
Loading