From 99eea043fd8a0efa9ff9eadc4bd35dcb57eff9cd Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 22 May 2024 17:39:34 +0530 Subject: [PATCH 1/7] Adds ability to specify chain type in chain-spec-builder --- substrate/bin/utils/chain-spec-builder/src/lib.rs | 10 ++++++++-- substrate/client/chain-spec/src/lib.rs | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index 7982da76227ae..6f27a5276fc84 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -117,7 +117,7 @@ //! [sp-genesis-builder-list]: ../sp_genesis_builder/trait.GenesisBuilder.html#method.preset_names //! [sp-genesis-builder-get-preset]: ../sp_genesis_builder/trait.GenesisBuilder.html#method.get_preset -use std::{fs, path::PathBuf}; +use std::{fs, path::PathBuf, str::FromStr}; use clap::{Parser, Subcommand}; use sc_chain_spec::{GenericChainSpec, GenesisConfigBuilderRuntimeCaller}; @@ -154,6 +154,9 @@ pub struct CreateCmd { /// The chain id. #[arg(long, short = 'i', default_value = "custom")] chain_id: String, + /// The chain type. + #[arg(long, short = 't', default_value = "Live")] + chain_type: String, /// The path to runtime wasm blob. #[arg(long, short)] runtime_wasm_path: PathBuf, @@ -256,10 +259,13 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result::builder(&code[..], Default::default()) .with_name(&cmd.chain_name[..]) .with_id(&cmd.chain_id[..]) - .with_chain_type(sc_chain_spec::ChainType::Live); + .with_chain_type(chain_type); let builder = match cmd.action { GenesisBuildAction::NamedPreset(NamedPresetCmd { ref preset_name }) => diff --git a/substrate/client/chain-spec/src/lib.rs b/substrate/client/chain-spec/src/lib.rs index abe01dafd9248..e78e20b896ba0 100644 --- a/substrate/client/chain-spec/src/lib.rs +++ b/substrate/client/chain-spec/src/lib.rs @@ -327,6 +327,8 @@ mod genesis_block; mod genesis_config_builder; pub mod json_patch; +use std::str::FromStr; + pub use self::{ chain_spec::{ update_code_in_json_chain_spec, ChainSpec as GenericChainSpec, ChainSpecBuilder, @@ -370,6 +372,19 @@ impl Default for ChainType { } } +impl FromStr for ChainType { + type Err = (); + + fn from_str(s: &str) -> Result { + Ok(match s { + "Development" => ChainType::Development, + "Local" => ChainType::Local, + "Live" => ChainType::Live, + _ => ChainType::Custom(s.to_string()), + }) + } +} + /// Arbitrary properties defined in chain spec as a JSON object pub type Properties = serde_json::map::Map; From 19a1d85ce83dd0cd2de2fed5df2a1f4ce6e75834 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Wed, 22 May 2024 18:09:27 +0530 Subject: [PATCH 2/7] Minor fix --- substrate/bin/utils/chain-spec-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index 6f27a5276fc84..9cef144d5d5c0 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -260,7 +260,7 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result::builder(&code[..], Default::default()) .with_name(&cmd.chain_name[..]) From 20b2383f51a4e7456ec6dd773336b53d0bb5a6f5 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 27 May 2024 10:37:07 +0530 Subject: [PATCH 3/7] Uses ValueEnum --- Cargo.lock | 1 + .../bin/utils/chain-spec-builder/src/lib.rs | 13 ++++++------- substrate/client/chain-spec/Cargo.toml | 1 + substrate/client/chain-spec/src/lib.rs | 18 ++---------------- 4 files changed, 10 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2a4b9b138bf85..583a93fc9b6d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16318,6 +16318,7 @@ name = "sc-chain-spec" version = "28.0.0" dependencies = [ "array-bytes", + "clap 4.5.3", "docify", "log", "memmap2 0.9.3", diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index 9cef144d5d5c0..485769640db67 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -117,10 +117,10 @@ //! [sp-genesis-builder-list]: ../sp_genesis_builder/trait.GenesisBuilder.html#method.preset_names //! [sp-genesis-builder-get-preset]: ../sp_genesis_builder/trait.GenesisBuilder.html#method.get_preset -use std::{fs, path::PathBuf, str::FromStr}; +use std::{fs, path::PathBuf}; use clap::{Parser, Subcommand}; -use sc_chain_spec::{GenericChainSpec, GenesisConfigBuilderRuntimeCaller}; +use sc_chain_spec::{ChainType, GenericChainSpec, GenesisConfigBuilderRuntimeCaller}; use serde_json::Value; /// A utility to easily create a chain spec definition. @@ -155,8 +155,8 @@ pub struct CreateCmd { #[arg(long, short = 'i', default_value = "custom")] chain_id: String, /// The chain type. - #[arg(long, short = 't', default_value = "Live")] - chain_type: String, + #[arg(value_enum, short = 't', default_value = "Live")] + chain_type: ChainType, /// The path to runtime wasm blob. #[arg(long, short)] runtime_wasm_path: PathBuf, @@ -259,13 +259,12 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result::builder(&code[..], Default::default()) .with_name(&cmd.chain_name[..]) .with_id(&cmd.chain_id[..]) - .with_chain_type(chain_type); + .with_chain_type(chain_type.clone()); let builder = match cmd.action { GenesisBuildAction::NamedPreset(NamedPresetCmd { ref preset_name }) => diff --git a/substrate/client/chain-spec/Cargo.toml b/substrate/client/chain-spec/Cargo.toml index 84ef89783adc0..af1e676847e1a 100644 --- a/substrate/client/chain-spec/Cargo.toml +++ b/substrate/client/chain-spec/Cargo.toml @@ -16,6 +16,7 @@ workspace = true targets = ["x86_64-unknown-linux-gnu"] [dependencies] +clap = { version = "4.5.3", features = ["derive"] } codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive"] } memmap2 = "0.9.3" serde = { features = ["derive"], workspace = true, default-features = true } diff --git a/substrate/client/chain-spec/src/lib.rs b/substrate/client/chain-spec/src/lib.rs index e78e20b896ba0..f940314833c9d 100644 --- a/substrate/client/chain-spec/src/lib.rs +++ b/substrate/client/chain-spec/src/lib.rs @@ -327,8 +327,6 @@ mod genesis_block; mod genesis_config_builder; pub mod json_patch; -use std::str::FromStr; - pub use self::{ chain_spec::{ update_code_in_json_chain_spec, ChainSpec as GenericChainSpec, ChainSpecBuilder, @@ -354,7 +352,7 @@ use sp_runtime::BuildStorage; /// /// This can be used by tools to determine the type of a chain for displaying /// additional information or enabling additional features. -#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +#[derive(clap::ValueEnum, serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] pub enum ChainType { /// A development chain that runs mainly on one node. Development, @@ -363,6 +361,7 @@ pub enum ChainType { /// A live chain. Live, /// Some custom chain type. + #[clap(skip)] Custom(String), } @@ -372,19 +371,6 @@ impl Default for ChainType { } } -impl FromStr for ChainType { - type Err = (); - - fn from_str(s: &str) -> Result { - Ok(match s { - "Development" => ChainType::Development, - "Local" => ChainType::Local, - "Live" => ChainType::Live, - _ => ChainType::Custom(s.to_string()), - }) - } -} - /// Arbitrary properties defined in chain spec as a JSON object pub type Properties = serde_json::map::Map; From 49f3f37745a3b97d94e49f2620a8665f5f433b26 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 27 May 2024 11:06:08 +0530 Subject: [PATCH 4/7] Minor fix --- substrate/bin/utils/chain-spec-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index 485769640db67..72870f0d2abe2 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -155,7 +155,7 @@ pub struct CreateCmd { #[arg(long, short = 'i', default_value = "custom")] chain_id: String, /// The chain type. - #[arg(value_enum, short = 't', default_value = "Live")] + #[arg(value_enum, short = 't', default_value = "live")] chain_type: ChainType, /// The path to runtime wasm blob. #[arg(long, short)] From 81dc050a4f04ae76c61a707cda0d5fa564cff783 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Mon, 27 May 2024 11:28:33 +0530 Subject: [PATCH 5/7] Adds PrDoc --- prdoc/pr_4542.prdoc | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 prdoc/pr_4542.prdoc diff --git a/prdoc/pr_4542.prdoc b/prdoc/pr_4542.prdoc new file mode 100644 index 0000000000000..faaf9dc2c2885 --- /dev/null +++ b/prdoc/pr_4542.prdoc @@ -0,0 +1,13 @@ +title: "Adds ability to specify chain type in chain-spec-builder" + +doc: + - audience: Node Operator + description: | + Currently, `chain-spec-builder` only creates a spec with Live chain type. This PR adds the + ability to specify it while keeping the same default. + +crates: + - name: staging-chain-spec-builder + bump: patch + - name: sc-chain-spec + bump: patch From 6cecffa1150dba5ef180d4ac382ae5fde7b8ebde Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 30 May 2024 15:43:01 +0530 Subject: [PATCH 6/7] Makes clap optional --- substrate/bin/utils/chain-spec-builder/Cargo.toml | 2 +- substrate/client/chain-spec/Cargo.toml | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/Cargo.toml b/substrate/bin/utils/chain-spec-builder/Cargo.toml index 5c8a3ab4e89a5..161da5e1d57ac 100644 --- a/substrate/bin/utils/chain-spec-builder/Cargo.toml +++ b/substrate/bin/utils/chain-spec-builder/Cargo.toml @@ -25,6 +25,6 @@ crate-type = ["rlib"] [dependencies] clap = { version = "4.5.3", features = ["derive"] } log = { workspace = true, default-features = true } -sc-chain-spec = { path = "../../../client/chain-spec" } +sc-chain-spec = { path = "../../../client/chain-spec", features = ["clap"] } serde_json = { workspace = true, default-features = true } sp-tracing = { path = "../../../primitives/tracing" } diff --git a/substrate/client/chain-spec/Cargo.toml b/substrate/client/chain-spec/Cargo.toml index af1e676847e1a..f0c2b0642e4f3 100644 --- a/substrate/client/chain-spec/Cargo.toml +++ b/substrate/client/chain-spec/Cargo.toml @@ -16,7 +16,7 @@ workspace = true targets = ["x86_64-unknown-linux-gnu"] [dependencies] -clap = { version = "4.5.3", features = ["derive"] } +clap = { version = "4.5.3", features = ["derive"], optional = true } codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive"] } memmap2 = "0.9.3" serde = { features = ["derive"], workspace = true, default-features = true } @@ -43,3 +43,8 @@ substrate-test-runtime = { path = "../../test-utils/runtime" } sp-keyring = { path = "../../primitives/keyring" } sp-application-crypto = { default-features = false, path = "../../primitives/application-crypto", features = ["serde"] } sp-consensus-babe = { default-features = false, path = "../../primitives/consensus/babe", features = ["serde"] } + +[features] +clap = [ + "dep:clap" +] From 8c15449638ba0cbd1c575d6f6adc2ecec97102b9 Mon Sep 17 00:00:00 2001 From: Nikhil Gupta <17176722+gupnik@users.noreply.github.com> Date: Thu, 30 May 2024 17:06:29 +0530 Subject: [PATCH 7/7] Minor fix --- substrate/client/chain-spec/Cargo.toml | 5 ----- substrate/client/chain-spec/src/lib.rs | 5 +++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/substrate/client/chain-spec/Cargo.toml b/substrate/client/chain-spec/Cargo.toml index cc3664c7a2d86..5b411b642a0e3 100644 --- a/substrate/client/chain-spec/Cargo.toml +++ b/substrate/client/chain-spec/Cargo.toml @@ -44,8 +44,3 @@ sp-keyring = { path = "../../primitives/keyring" } sp-application-crypto = { default-features = false, path = "../../primitives/application-crypto", features = ["serde"] } sp-consensus-babe = { default-features = false, path = "../../primitives/consensus/babe", features = ["serde"] } regex = "1.6.0" - -[features] -clap = [ - "dep:clap" -] diff --git a/substrate/client/chain-spec/src/lib.rs b/substrate/client/chain-spec/src/lib.rs index 5bfca458f6f51..653c3c618b772 100644 --- a/substrate/client/chain-spec/src/lib.rs +++ b/substrate/client/chain-spec/src/lib.rs @@ -351,7 +351,8 @@ use sp_runtime::BuildStorage; /// /// This can be used by tools to determine the type of a chain for displaying /// additional information or enabling additional features. -#[derive(clap::ValueEnum, serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] +#[cfg_attr(feature = "clap", derive(clap::ValueEnum))] pub enum ChainType { /// A development chain that runs mainly on one node. Development, @@ -360,7 +361,7 @@ pub enum ChainType { /// A live chain. Live, /// Some custom chain type. - #[clap(skip)] + #[cfg_attr(feature = "clap", clap(skip))] Custom(String), }