From eddb86ab9418c45a091cb076e93cb504928c2a7b Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 6 Nov 2023 13:16:38 +0100 Subject: [PATCH 1/8] chain-spec-builder: clean-up --- Cargo.lock | 7 - .../bin/utils/chain-spec-builder/Cargo.toml | 7 - .../bin/utils/chain-spec-builder/bin/main.rs | 91 +----- .../bin/utils/chain-spec-builder/src/lib.rs | 272 +++++------------- 4 files changed, 88 insertions(+), 289 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b4496368ee82..1ff1056a4ed49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18007,18 +18007,11 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" name = "staging-chain-spec-builder" version = "2.0.0" dependencies = [ - "ansi_term", "clap 4.4.6", - "kitchensink-runtime", "log", - "rand 0.8.5", "sc-chain-spec", - "sc-keystore", "serde_json", - "sp-core", - "sp-keystore", "sp-tracing 10.0.0", - "staging-node-cli", ] [[package]] diff --git a/substrate/bin/utils/chain-spec-builder/Cargo.toml b/substrate/bin/utils/chain-spec-builder/Cargo.toml index 5c7e21abeab94..4decd6ef3e61d 100644 --- a/substrate/bin/utils/chain-spec-builder/Cargo.toml +++ b/substrate/bin/utils/chain-spec-builder/Cargo.toml @@ -20,15 +20,8 @@ name = "chain-spec-builder" crate-type = ["rlib"] [dependencies] -ansi_term = "0.12.1" clap = { version = "4.4.6", features = ["derive"] } -rand = "0.8" -kitchensink-runtime = { version = "3.0.0-dev", path = "../../node/runtime" } log = "0.4.17" -node-cli = { package = "staging-node-cli", path = "../../node/cli" } sc-chain-spec = { path = "../../../client/chain-spec" } -sc-keystore = { path = "../../../client/keystore" } serde_json = "1.0.100" -sp-core = { path = "../../../primitives/core" } -sp-keystore = { path = "../../../primitives/keystore" } sp-tracing = { version = "10.0.0", path = "../../../primitives/tracing" } diff --git a/substrate/bin/utils/chain-spec-builder/bin/main.rs b/substrate/bin/utils/chain-spec-builder/bin/main.rs index 83892afd6ace5..e1adb4283de8d 100644 --- a/substrate/bin/utils/chain-spec-builder/bin/main.rs +++ b/substrate/bin/utils/chain-spec-builder/bin/main.rs @@ -17,14 +17,10 @@ // along with this program. If not, see . use chain_spec_builder::{ - generate_authority_keys_and_store, generate_chain_spec, generate_chain_spec_for_runtime, - print_seeds, ChainSpecBuilder, ChainSpecBuilderCmd, EditCmd, GenerateCmd, NewCmd, VerifyCmd, + generate_chain_spec_for_runtime, ChainSpecBuilder, ChainSpecBuilderCmd, EditCmd, VerifyCmd, }; use clap::Parser; -use node_cli::chain_spec; -use rand::{distributions::Alphanumeric, rngs::OsRng, Rng}; use sc_chain_spec::{update_code_in_json_chain_spec, GenericChainSpec}; -use sp_core::{crypto::Ss58Codec, sr25519}; use staging_chain_spec_builder as chain_spec_builder; use std::fs; @@ -42,58 +38,12 @@ fn main() -> Result<(), String> { } let chain_spec_path = builder.chain_spec_path.to_path_buf(); - let mut write_chain_spec = true; - let chain_spec_json = match builder.command { - ChainSpecBuilderCmd::Generate(GenerateCmd { - authorities, - nominators, - endowed, - keystore_path, - }) => { - let authorities = authorities.max(1); - let rand_str = || -> String { - OsRng.sample_iter(&Alphanumeric).take(32).map(char::from).collect() - }; - - let authority_seeds = (0..authorities).map(|_| rand_str()).collect::>(); - let nominator_seeds = (0..nominators).map(|_| rand_str()).collect::>(); - let endowed_seeds = (0..endowed).map(|_| rand_str()).collect::>(); - let sudo_seed = rand_str(); - - print_seeds(&authority_seeds, &nominator_seeds, &endowed_seeds, &sudo_seed); - - if let Some(keystore_path) = keystore_path { - generate_authority_keys_and_store(&authority_seeds, &keystore_path)?; - } - - let nominator_accounts = nominator_seeds - .into_iter() - .map(|seed| { - chain_spec::get_account_id_from_seed::(&seed).to_ss58check() - }) - .collect(); - - let endowed_accounts = endowed_seeds - .into_iter() - .map(|seed| { - chain_spec::get_account_id_from_seed::(&seed).to_ss58check() - }) - .collect(); - - let sudo_account = - chain_spec::get_account_id_from_seed::(&sudo_seed).to_ss58check(); - - generate_chain_spec(authority_seeds, nominator_accounts, endowed_accounts, sudo_account) + match builder.command { + ChainSpecBuilderCmd::Create(cmd) => { + let chain_spec_json = generate_chain_spec_for_runtime(&cmd)?; + fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?; }, - ChainSpecBuilderCmd::New(NewCmd { - authority_seeds, - nominator_accounts, - endowed_accounts, - sudo_account, - }) => - generate_chain_spec(authority_seeds, nominator_accounts, endowed_accounts, sudo_account), - ChainSpecBuilderCmd::Runtime(cmd) => generate_chain_spec_for_runtime(&cmd), ChainSpecBuilderCmd::Edit(EditCmd { ref input_chain_spec, ref runtime_wasm_path, @@ -112,30 +62,15 @@ fn main() -> Result<(), String> { ); } - serde_json::to_string_pretty(&chain_spec_json) - .map_err(|e| format!("to pretty failed: {e}")) + let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json) + .map_err(|e| format!("to pretty failed: {e}"))?; + fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?; }, - ChainSpecBuilderCmd::Verify(VerifyCmd { ref input_chain_spec, ref runtime_wasm_path }) => { - write_chain_spec = false; + ChainSpecBuilderCmd::Verify(VerifyCmd { ref input_chain_spec }) => { let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?; - let mut chain_spec_json = - serde_json::from_str::(&chain_spec.as_json(true)?) - .map_err(|e| format!("Conversion to json failed: {e}"))?; - if let Some(path) = runtime_wasm_path { - update_code_in_json_chain_spec( - &mut chain_spec_json, - &fs::read(path.as_path()) - .map_err(|e| format!("Wasm blob file could not be read: {e}"))?[..], - ); - }; - serde_json::to_string_pretty(&chain_spec_json) - .map_err(|e| format!("to pretty failed: {e}")) + let _ = serde_json::from_str::(&chain_spec.as_json(true)?) + .map_err(|e| format!("Conversion to json failed: {e}"))?; }, - }?; - - if write_chain_spec { - fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string()) - } else { - Ok(()) - } + }; + Ok(()) } diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index 50a8dd0899623..4ee41f23135e0 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -21,31 +21,76 @@ //! A chain-spec is short for `chain-configuration`. See the [`sc-chain-spec`] for more information. //! //! Note that this binary is analogous to the `build-spec` subcommand, contained in typical -//! substrate-based nodes. This particular binary is capable of building a more sophisticated chain -//! specification that can be used with the substrate-node, ie. [`node-cli`]. +//! substrate-based nodes. This particular binary is capable of interacting with +//! [`sp-genesis-builder`] implementation of any provided runtime allowing to build chain-spec JSON +//! files. //! -//! See [`ChainSpecBuilder`] for a list of available commands. +//! See [`ChainSpecBuilderCmd`] for a list of available commands. +//! +//! ## Typical use-cases. +//! - **Get default config from runtime.** +//! +//! Query the default genesis config from the provided `runtime.wasm` and use it in the chain +//! spec. Tool can also store runtime's default genesis config in given file (`-d`): +//! ```text +//! chain-spec-builder create -r runtime.wasm default -d /dev/stdout +//! ``` +//! +//! _Note:_ `GenesisBuilder::create_default_config` runtime function is called. +//! +//! +//! - **Generate raw storage chain spec using genesis config patch.** +//! +//! Patch the runtime's default genesis config with provided `patch.json` and generate raw +//! storage (`-s`) version of chain spec: +//! ```text +//! chain-spec-builder create -s -r runtime.wasm patch -p patch.json +//! ``` +//! +//! _Note:_ `GenesisBuilder::build_config` runtime function is called. +//! +//! - **Generate raw storage chain spec using full genesis config.** +//! +//! Build the chain spec using provided full genesis config json file. No defaults will be used: +//! ```text +//! chain-spec-builder create -s -r runtime.wasm full -c full-genesis-config.json +//! ``` +//! +//! _Note_: `GenesisBuilder::build_config` runtime function is called. +//! +//! - **Generate human readable chain spec using provided genesis config patch.** +//! +//! ```text +//! chain-spec-builder create -r runtime.wasm patch -p patch.json +//! ``` +//! +//! _Note_: No runtime API is called. +//! +//! - **Generate human readable chain spec using provided full genesis config.** +//! +//! ```text +//! chain-spec-builder create -r runtime.wasm full -c full-genesis-config.json +//! ``` +//! _Note_: No runtime API is called. +//! +//! The `chain-spec-builder` provides also some extra utilities: +//! - `verify`: allows to verify if *human readable* chain spec is valid (precisely: all required +//! fields in genesis config are initialized), +//! - `edit`, allows to: +//! - update the code in the given chain spec, +//! - convert given chain spec to the raw chain spec, //! //! [`sc-chain-spec`]: ../sc_chain_spec/index.html //! [`node-cli`]: ../node_cli/index.html +//! [`sp-genesis-builder`]: ../sp_genesis_builder/index.html -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::{fs, path::PathBuf}; -use ansi_term::Style; use clap::{Parser, Subcommand}; -use sc_chain_spec::GenesisConfigBuilderRuntimeCaller; - -use node_cli::chain_spec::{self, AccountId}; -use sc_keystore::LocalKeystore; +use sc_chain_spec::{GenericChainSpec, GenesisConfigBuilderRuntimeCaller}; use serde_json::Value; -use sp_core::crypto::{ByteArray, Ss58Codec}; -use sp_keystore::KeystorePtr; -/// A utility to easily create a testnet chain spec definition with a given set -/// of authorities and endowed accounts and/or generate random accounts. +/// A utility to easily create a chain spec definition. #[derive(Debug, Parser)] #[command(rename_all = "kebab-case")] pub struct ChainSpecBuilder { @@ -59,70 +104,24 @@ pub struct ChainSpecBuilder { #[derive(Debug, Subcommand)] #[command(rename_all = "kebab-case")] pub enum ChainSpecBuilderCmd { - New(NewCmd), - Generate(GenerateCmd), - Runtime(RuntimeCmd), + Create(CreateCmd), Edit(EditCmd), Verify(VerifyCmd), } -/// Create a new chain spec with the given authorities, endowed and sudo -/// accounts. Only works for kitchen-sink runtime -#[derive(Parser, Debug)] -#[command(rename_all = "kebab-case")] -pub struct NewCmd { - /// Authority key seed. - #[arg(long, short, required = true)] - pub authority_seeds: Vec, - /// Active nominators (SS58 format), each backing a random subset of the aforementioned - /// authorities. - #[arg(long, short, default_value = "0")] - pub nominator_accounts: Vec, - /// Endowed account address (SS58 format). - #[arg(long, short)] - pub endowed_accounts: Vec, - /// Sudo account address (SS58 format). - #[arg(long, short)] - pub sudo_account: String, -} - -/// Create a new chain spec with the given number of authorities and endowed -/// accounts. Random keys will be generated as required. -#[derive(Parser, Debug)] -pub struct GenerateCmd { - /// The number of authorities. - #[arg(long, short)] - pub authorities: usize, - /// The number of nominators backing the aforementioned authorities. - /// - /// Will nominate a random subset of `authorities`. - #[arg(long, short, default_value_t = 0)] - pub nominators: usize, - /// The number of endowed accounts. - #[arg(long, short, default_value_t = 0)] - pub endowed: usize, - /// Path to use when saving generated keystores for each authority. - /// - /// At this path, a new folder will be created for each authority's - /// keystore named `auth-$i` where `i` is the authority index, i.e. - /// `auth-0`, `auth-1`, etc. - #[arg(long, short)] - pub keystore_path: Option, -} - /// Create a new chain spec by interacting with the provided runtime wasm blob. #[derive(Parser, Debug)] -pub struct RuntimeCmd { - /// The name of chain +pub struct CreateCmd { + /// The name of chain. #[arg(long, short = 'n', default_value = "Custom")] chain_name: String, - /// The chain id + /// The chain id. #[arg(long, short = 'i', default_value = "custom")] chain_id: String, - /// The path to runtime wasm blob + /// The path to runtime wasm blob. #[arg(long, short)] runtime_wasm_path: PathBuf, - /// Export chainspec as raw storage + /// Export chainspec as raw storage. #[arg(long, short = 's')] raw_storage: bool, /// Verify the genesis config. This silently generates the raw storage from genesis config. Any @@ -171,153 +170,32 @@ struct DefaultCmd { /// can be updated with the runtime provided in the command line. #[derive(Parser, Debug, Clone)] pub struct EditCmd { - /// Chain spec to be edited + /// Chain spec to be edited. #[arg(long, short)] pub input_chain_spec: PathBuf, - /// The path to new runtime wasm blob to be stored into chain-spec + /// The path to new runtime wasm blob to be stored into chain-spec. #[arg(long, short = 'r')] pub runtime_wasm_path: Option, - /// Convert genesis spec to raw format + /// Convert genesis spec to raw format. #[arg(long, short = 's')] pub convert_to_raw: bool, } -/// Verifies provided input chain spec. If the runtime is provided verification is performed against +/// Verifies provided input chain spec. /// new runtime. #[derive(Parser, Debug, Clone)] pub struct VerifyCmd { - /// Chain spec to be edited + /// Chain spec to be edited. #[arg(long, short)] pub input_chain_spec: PathBuf, - /// The path to new runtime wasm blob to be stored into chain-spec - #[arg(long, short = 'r')] - pub runtime_wasm_path: Option, -} - -/// Generate the chain spec using the given seeds and accounts. -pub fn generate_chain_spec( - authority_seeds: Vec, - nominator_accounts: Vec, - endowed_accounts: Vec, - sudo_account: String, -) -> Result { - let parse_account = |address: String| { - AccountId::from_string(&address) - .map_err(|err| format!("Failed to parse account address: {:?}", err)) - }; - - let nominator_accounts = nominator_accounts - .into_iter() - .map(parse_account) - .collect::, String>>()?; - - let endowed_accounts = endowed_accounts - .into_iter() - .map(parse_account) - .collect::, String>>()?; - - let sudo_account = parse_account(sudo_account)?; - - let authorities = authority_seeds - .iter() - .map(AsRef::as_ref) - .map(chain_spec::authority_keys_from_seed) - .collect::>(); - - chain_spec::ChainSpec::builder(kitchensink_runtime::wasm_binary_unwrap(), Default::default()) - .with_name("Custom") - .with_id("custom") - .with_chain_type(sc_chain_spec::ChainType::Live) - .with_genesis_config_patch(chain_spec::testnet_genesis( - authorities, - nominator_accounts, - sudo_account, - Some(endowed_accounts), - )) - .build() - .as_json(false) -} - -/// Generate the authority keys and store them in the given `keystore_path`. -pub fn generate_authority_keys_and_store( - seeds: &[String], - keystore_path: &Path, -) -> Result<(), String> { - for (n, seed) in seeds.iter().enumerate() { - let keystore: KeystorePtr = - LocalKeystore::open(keystore_path.join(format!("auth-{}", n)), None) - .map_err(|err| err.to_string())? - .into(); - - let (_, _, grandpa, babe, im_online, authority_discovery, mixnet) = - chain_spec::authority_keys_from_seed(seed); - - let insert_key = |key_type, public| { - keystore - .insert(key_type, &format!("//{}", seed), public) - .map_err(|_| format!("Failed to insert key: {}", grandpa)) - }; - - insert_key(sp_core::crypto::key_types::BABE, babe.as_slice())?; - - insert_key(sp_core::crypto::key_types::GRANDPA, grandpa.as_slice())?; - - insert_key(sp_core::crypto::key_types::IM_ONLINE, im_online.as_slice())?; - - insert_key( - sp_core::crypto::key_types::AUTHORITY_DISCOVERY, - authority_discovery.as_slice(), - )?; - - insert_key(sp_core::crypto::key_types::MIXNET, mixnet.as_slice())?; - } - - Ok(()) -} - -/// Print the given seeds -pub fn print_seeds( - authority_seeds: &[String], - nominator_seeds: &[String], - endowed_seeds: &[String], - sudo_seed: &str, -) { - let header = Style::new().bold().underline(); - let entry = Style::new().bold(); - - println!("{}", header.paint("Authority seeds")); - - for (n, seed) in authority_seeds.iter().enumerate() { - println!("{} //{}", entry.paint(format!("auth-{}:", n)), seed); - } - - println!("{}", header.paint("Nominator seeds")); - - for (n, seed) in nominator_seeds.iter().enumerate() { - println!("{} //{}", entry.paint(format!("nom-{}:", n)), seed); - } - - println!(); - - if !endowed_seeds.is_empty() { - println!("{}", header.paint("Endowed seeds")); - for (n, seed) in endowed_seeds.iter().enumerate() { - println!("{} //{}", entry.paint(format!("endowed-{}:", n)), seed); - } - - println!(); - } - - println!("{}", header.paint("Sudo seed")); - println!("//{}", sudo_seed); } -/// Processes `RuntimeCmd` and returns JSON version of `ChainSpec` -pub fn generate_chain_spec_for_runtime(cmd: &RuntimeCmd) -> Result { +/// Processes `CreateCmd` and returns JSON version of `ChainSpec`. +pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result { let code = fs::read(cmd.runtime_wasm_path.as_path()) .map_err(|e| format!("wasm blob shall be readable {e}"))?; - let builder = chain_spec::ChainSpec::builder(&code[..], Default::default()) + let builder = GenericChainSpec::<()>::builder(&code[..], Default::default()) .with_name(&cmd.chain_name[..]) .with_id(&cmd.chain_id[..]) .with_chain_type(sc_chain_spec::ChainType::Live); From a4f84652c036d0780b4863c305676bf9297385a3 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 6 Nov 2023 13:44:45 +0100 Subject: [PATCH 2/8] fix --- substrate/bin/utils/chain-spec-builder/bin/main.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/bin/main.rs b/substrate/bin/utils/chain-spec-builder/bin/main.rs index e1adb4283de8d..b9391afd16287 100644 --- a/substrate/bin/utils/chain-spec-builder/bin/main.rs +++ b/substrate/bin/utils/chain-spec-builder/bin/main.rs @@ -28,15 +28,6 @@ fn main() -> Result<(), String> { sp_tracing::try_init_simple(); let builder = ChainSpecBuilder::parse(); - #[cfg(build_type = "debug")] - if matches!(builder.command, ChainSpecBuilderCmd::Generate(_) | ChainSpecBuilderCmd::New(_)) { - println!( - "The chain spec builder builds a chain specification that includes a Substrate runtime \ - compiled as WASM. To ensure proper functioning of the included runtime compile (or run) \ - the chain spec builder binary in `--release` mode.\n", - ); - } - let chain_spec_path = builder.chain_spec_path.to_path_buf(); match builder.command { From ec7e55895562e4c66eb22ad028f2798ead63c815 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 20 Nov 2023 21:49:12 +0100 Subject: [PATCH 3/8] Update substrate/bin/utils/chain-spec-builder/src/lib.rs Co-authored-by: Sebastian Kunert --- substrate/bin/utils/chain-spec-builder/src/lib.rs | 1 - 1 file changed, 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 4ee41f23135e0..a4eaf890fd6dd 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -182,7 +182,6 @@ pub struct EditCmd { } /// Verifies provided input chain spec. -/// new runtime. #[derive(Parser, Debug, Clone)] pub struct VerifyCmd { /// Chain spec to be edited. From e707feab0630f8f5d1fdcec9fec3e648a4e60329 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 20 Nov 2023 21:51:34 +0100 Subject: [PATCH 4/8] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- substrate/bin/utils/chain-spec-builder/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index a4eaf890fd6dd..fdd909d5cdfce 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -32,7 +32,7 @@ //! //! Query the default genesis config from the provided `runtime.wasm` and use it in the chain //! spec. Tool can also store runtime's default genesis config in given file (`-d`): -//! ```text +//! ```text //! chain-spec-builder create -r runtime.wasm default -d /dev/stdout //! ``` //! @@ -43,7 +43,7 @@ //! //! Patch the runtime's default genesis config with provided `patch.json` and generate raw //! storage (`-s`) version of chain spec: -//! ```text +//! ```text //! chain-spec-builder create -s -r runtime.wasm patch -p patch.json //! ``` //! @@ -60,7 +60,7 @@ //! //! - **Generate human readable chain spec using provided genesis config patch.** //! -//! ```text +//! ```text //! chain-spec-builder create -r runtime.wasm patch -p patch.json //! ``` //! @@ -71,7 +71,6 @@ //! ```text //! chain-spec-builder create -r runtime.wasm full -c full-genesis-config.json //! ``` -//! _Note_: No runtime API is called. //! //! The `chain-spec-builder` provides also some extra utilities: //! - `verify`: allows to verify if *human readable* chain spec is valid (precisely: all required From b4e80054f9cf9cbcc72caefaadea87fa37aa6961 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Mon, 20 Nov 2023 22:38:25 +0100 Subject: [PATCH 5/8] review suggestions --- .../bin/utils/chain-spec-builder/src/lib.rs | 72 +++++++++---------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index cbab57a534ecd..cf0aec878f237 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -28,60 +28,58 @@ //! See [`ChainSpecBuilderCmd`] for a list of available commands. //! //! ## Typical use-cases. -//! - **Get default config from runtime.** +//! #### Get default config from runtime. //! -//! Query the default genesis config from the provided `runtime.wasm` and use it in the chain +//! Query the default genesis config from the provided `runtime.wasm` and use it in the chain //! spec. Tool can also store runtime's default genesis config in given file (`-d`): //! ```text -//! chain-spec-builder create -r runtime.wasm default -d /dev/stdout -//! ``` +//! chain-spec-builder create -r runtime.wasm default /dev/stdout +//! ``` //! -//! _Note:_ `GenesisBuilder::create_default_config` runtime function is called. +//! _Note:_ [`GenesisBuilder::create_default_config`][sp-genesis-builder-create] runtime function is called. //! //! -//! - **Generate raw storage chain spec using genesis config patch.** +//! #### Generate raw storage chain spec using genesis config patch. //! -//! Patch the runtime's default genesis config with provided `patch.json` and generate raw +//! Patch the runtime's default genesis config with provided `patch.json` and generate raw //! storage (`-s`) version of chain spec: -//! ```text -//! chain-spec-builder create -s -r runtime.wasm patch -p patch.json -//! ``` -//! -//! _Note:_ `GenesisBuilder::build_config` runtime function is called. -//! -//! - **Generate raw storage chain spec using full genesis config.** +//! ```text +//! chain-spec-builder create -s -r runtime.wasm patch patch.json +//! ``` //! -//! Build the chain spec using provided full genesis config json file. No defaults will be used: -//! ```text -//! chain-spec-builder create -s -r runtime.wasm full -c full-genesis-config.json -//! ``` +//! _Note:_ [`GenesisBuilder::build_config`][sp-genesis-builder-build] runtime function is called. //! -//! _Note_: `GenesisBuilder::build_config` runtime function is called. +//! #### Generate raw storage chain spec using full genesis config. //! -//! - **Generate human readable chain spec using provided genesis config patch.** -//! -//! ```text -//! chain-spec-builder create -r runtime.wasm patch -p patch.json -//! ``` +//! Build the chain spec using provided full genesis config json file. No defaults will be used: +//! ```text +//! chain-spec-builder create -s -r runtime.wasm full full-genesis-config.json +//! ``` //! -//! _Note_: No runtime API is called. +//! _Note_: [`GenesisBuilder::build_config`][sp-genesis-builder-build] runtime function is called. //! -//! - **Generate human readable chain spec using provided full genesis config.** +//! #### Generate human readable chain spec using provided genesis config patch. +//! ```text +//! chain-spec-builder create -r runtime.wasm patch patch.json +//! ``` //! -//! ```text -//! chain-spec-builder create -r runtime.wasm full -c full-genesis-config.json -//! ``` +//! #### Generate human readable chain spec using provided full genesis config. +//! ```text +//! chain-spec-builder create -r runtime.wasm full full-genesis-config.json +//! ``` //! -//! The `chain-spec-builder` provides also some extra utilities: -//! - `verify`: allows to verify if *human readable* chain spec is valid (precisely: all required +//! #### The `chain-spec-builder` provides also some extra utilities: +//! - `verify`: allows to verify if *human readable* chain spec is valid (precisely: all required //! fields in genesis config are initialized), -//! - `edit`, allows to: -//! - update the code in the given chain spec, -//! - convert given chain spec to the raw chain spec, +//! - `edit`, allows to: +//! - update the code in the given chain spec, +//! - convert given chain spec to the raw chain spec, //! //! [`sc-chain-spec`]: ../sc_chain_spec/index.html //! [`node-cli`]: ../node_cli/index.html //! [`sp-genesis-builder`]: ../sp_genesis_builder/index.html +//! [sp-genesis-builder-create]: ../sp_genesis_builder/trait.GenesisBuilder.html#method.create_default_config +//! [sp-genesis-builder-build]: ../sp_genesis_builder/trait.GenesisBuilder.html#method.build_config use std::{fs, path::PathBuf}; @@ -142,7 +140,6 @@ enum GenesisBuildAction { #[derive(Parser, Debug, Clone)] struct PatchCmd { /// The path to the runtime genesis config patch. - #[arg(long, short)] patch_path: PathBuf, } @@ -150,7 +147,6 @@ struct PatchCmd { #[derive(Parser, Debug, Clone)] struct FullCmd { /// The path to the full runtime genesis config json file. - #[arg(long, short)] config_path: PathBuf, } @@ -161,7 +157,6 @@ struct FullCmd { struct DefaultCmd { /// If provided stores the default genesis config json file at given path (in addition to /// chain-spec). - #[arg(long, short)] default_config_path: Option, } @@ -183,8 +178,7 @@ pub struct EditCmd { /// Verifies provided input chain spec. #[derive(Parser, Debug, Clone)] pub struct VerifyCmd { - /// Chain spec to be edited. - #[arg(long, short)] + /// Chain spec to be verified. pub input_chain_spec: PathBuf, } From c5026ac1e7595a48108200ae566c285993bbd98f Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Mon, 20 Nov 2023 21:44:54 +0000 Subject: [PATCH 6/8] ".git/.scripts/commands/fmt/fmt.sh" --- substrate/bin/utils/chain-spec-builder/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index cf0aec878f237..22b3d7c2f8d3f 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -35,7 +35,7 @@ //! ```text //! chain-spec-builder create -r runtime.wasm default /dev/stdout //! ``` -//! +//! //! _Note:_ [`GenesisBuilder::create_default_config`][sp-genesis-builder-create] runtime function is called. //! //! @@ -46,7 +46,7 @@ //! ```text //! chain-spec-builder create -s -r runtime.wasm patch patch.json //! ``` -//! +//! //! _Note:_ [`GenesisBuilder::build_config`][sp-genesis-builder-build] runtime function is called. //! //! #### Generate raw storage chain spec using full genesis config. @@ -55,25 +55,25 @@ //! ```text //! chain-spec-builder create -s -r runtime.wasm full full-genesis-config.json //! ``` -//! +//! //! _Note_: [`GenesisBuilder::build_config`][sp-genesis-builder-build] runtime function is called. //! //! #### Generate human readable chain spec using provided genesis config patch. //! ```text //! chain-spec-builder create -r runtime.wasm patch patch.json //! ``` -//! +//! //! #### Generate human readable chain spec using provided full genesis config. //! ```text //! chain-spec-builder create -r runtime.wasm full full-genesis-config.json //! ``` -//! +//! //! #### The `chain-spec-builder` provides also some extra utilities: //! - `verify`: allows to verify if *human readable* chain spec is valid (precisely: all required //! fields in genesis config are initialized), //! - `edit`, allows to: -//! - update the code in the given chain spec, -//! - convert given chain spec to the raw chain spec, +//! - update the code in the given chain spec, +//! - convert given chain spec to the raw chain spec, //! //! [`sc-chain-spec`]: ../sc_chain_spec/index.html //! [`node-cli`]: ../node_cli/index.html From ed84b6bab6f51f9bc56855e6a359c14514a1ed15 Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Tue, 21 Nov 2023 11:06:11 +0100 Subject: [PATCH 7/8] edit improved --- .../bin/utils/chain-spec-builder/bin/main.rs | 31 +++++---- .../bin/utils/chain-spec-builder/src/lib.rs | 63 ++++++++++--------- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/bin/main.rs b/substrate/bin/utils/chain-spec-builder/bin/main.rs index b9391afd16287..986293179a915 100644 --- a/substrate/bin/utils/chain-spec-builder/bin/main.rs +++ b/substrate/bin/utils/chain-spec-builder/bin/main.rs @@ -17,7 +17,8 @@ // along with this program. If not, see . use chain_spec_builder::{ - generate_chain_spec_for_runtime, ChainSpecBuilder, ChainSpecBuilderCmd, EditCmd, VerifyCmd, + generate_chain_spec_for_runtime, ChainSpecBuilder, ChainSpecBuilderCmd, ConvertToRawCmd, + UpdateCodeCmd, VerifyCmd, }; use clap::Parser; use sc_chain_spec::{update_code_in_json_chain_spec, GenericChainSpec}; @@ -35,28 +36,36 @@ fn main() -> Result<(), String> { let chain_spec_json = generate_chain_spec_for_runtime(&cmd)?; fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?; }, - ChainSpecBuilderCmd::Edit(EditCmd { + ChainSpecBuilderCmd::UpdateCode(UpdateCodeCmd { ref input_chain_spec, ref runtime_wasm_path, - convert_to_raw, }) => { let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?; let mut chain_spec_json = - serde_json::from_str::(&chain_spec.as_json(convert_to_raw)?) + serde_json::from_str::(&chain_spec.as_json(false)?) .map_err(|e| format!("Conversion to json failed: {e}"))?; - if let Some(path) = runtime_wasm_path { - update_code_in_json_chain_spec( - &mut chain_spec_json, - &fs::read(path.as_path()) - .map_err(|e| format!("Wasm blob file could not be read: {e}"))?[..], - ); - } + update_code_in_json_chain_spec( + &mut chain_spec_json, + &fs::read(runtime_wasm_path.as_path()) + .map_err(|e| format!("Wasm blob file could not be read: {e}"))?[..], + ); let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json) .map_err(|e| format!("to pretty failed: {e}"))?; fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?; }, + ChainSpecBuilderCmd::ConvertToRaw(ConvertToRawCmd { ref input_chain_spec }) => { + let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?; + + let chain_spec_json = + serde_json::from_str::(&chain_spec.as_json(true)?) + .map_err(|e| format!("Conversion to json failed: {e}"))?; + + let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json) + .map_err(|e| format!("Conversion to pretty failed: {e}"))?; + fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?; + }, ChainSpecBuilderCmd::Verify(VerifyCmd { ref input_chain_spec }) => { let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?; let _ = serde_json::from_str::(&chain_spec.as_json(true)?) diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index 22b3d7c2f8d3f..15f954bba5158 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -28,52 +28,48 @@ //! See [`ChainSpecBuilderCmd`] for a list of available commands. //! //! ## Typical use-cases. -//! #### Get default config from runtime. +//! ##### Get default config from runtime. //! //! Query the default genesis config from the provided `runtime.wasm` and use it in the chain -//! spec. Tool can also store runtime's default genesis config in given file (`-d`): +//! spec. Tool can also store runtime's default genesis config in given file: //! ```text //! chain-spec-builder create -r runtime.wasm default /dev/stdout //! ``` -//! +//! //! _Note:_ [`GenesisBuilder::create_default_config`][sp-genesis-builder-create] runtime function is called. //! //! -//! #### Generate raw storage chain spec using genesis config patch. +//! ##### Generate raw storage chain spec using genesis config patch. //! //! Patch the runtime's default genesis config with provided `patch.json` and generate raw //! storage (`-s`) version of chain spec: //! ```text //! chain-spec-builder create -s -r runtime.wasm patch patch.json //! ``` -//! +//! //! _Note:_ [`GenesisBuilder::build_config`][sp-genesis-builder-build] runtime function is called. //! -//! #### Generate raw storage chain spec using full genesis config. +//! ##### Generate raw storage chain spec using full genesis config. //! //! Build the chain spec using provided full genesis config json file. No defaults will be used: //! ```text //! chain-spec-builder create -s -r runtime.wasm full full-genesis-config.json //! ``` -//! +//! //! _Note_: [`GenesisBuilder::build_config`][sp-genesis-builder-build] runtime function is called. //! -//! #### Generate human readable chain spec using provided genesis config patch. +//! ##### Generate human readable chain spec using provided genesis config patch. //! ```text //! chain-spec-builder create -r runtime.wasm patch patch.json //! ``` -//! -//! #### Generate human readable chain spec using provided full genesis config. +//! +//! ##### Generate human readable chain spec using provided full genesis config. //! ```text //! chain-spec-builder create -r runtime.wasm full full-genesis-config.json //! ``` -//! -//! #### The `chain-spec-builder` provides also some extra utilities: -//! - `verify`: allows to verify if *human readable* chain spec is valid (precisely: all required -//! fields in genesis config are initialized), -//! - `edit`, allows to: -//! - update the code in the given chain spec, -//! - convert given chain spec to the raw chain spec, +//! +//! ##### Extra tools. +//! The `chain-spec-builder` provides also some extra utilities: [`VerifyCmd`], [`ConvertToRawCmd`], [`UpdateCodeCmd`]. //! //! [`sc-chain-spec`]: ../sc_chain_spec/index.html //! [`node-cli`]: ../node_cli/index.html @@ -102,8 +98,9 @@ pub struct ChainSpecBuilder { #[command(rename_all = "kebab-case")] pub enum ChainSpecBuilderCmd { Create(CreateCmd), - Edit(EditCmd), Verify(VerifyCmd), + UpdateCode(UpdateCodeCmd), + ConvertToRaw(ConvertToRawCmd), } /// Create a new chain spec by interacting with the provided runtime wasm blob. @@ -160,22 +157,30 @@ struct DefaultCmd { default_config_path: Option, } -/// Edits provided input chain spec. Input can be converted into raw storage chain-spec. The code -/// can be updated with the runtime provided in the command line. +/// Updates the code in the provided input chain spec. +/// +/// The code field of the chain spec will be updated with the runtime provided in the +/// command line. This operation supports both plain and raw formats. #[derive(Parser, Debug, Clone)] -pub struct EditCmd { - /// Chain spec to be edited. - #[arg(long, short)] +pub struct UpdateCodeCmd { + /// Chain spec to be updated. pub input_chain_spec: PathBuf, /// The path to new runtime wasm blob to be stored into chain-spec. - #[arg(long, short = 'r')] - pub runtime_wasm_path: Option, - /// Convert genesis spec to raw format. - #[arg(long, short = 's')] - pub convert_to_raw: bool, + pub runtime_wasm_path: PathBuf, +} + +/// Converts the given chain spec into the raw format. +#[derive(Parser, Debug, Clone)] +pub struct ConvertToRawCmd { + /// Chain spec to be converted. + pub input_chain_spec: PathBuf, } -/// Verifies provided input chain spec. +/// Verifies the provided input chain spec. +/// +/// Silently checks if given input chain spec can be converted to raw. It allows to check if all +/// RuntimeGenesisConfig fiels are properly initialized and if the json does not contain invalid +/// fields. #[derive(Parser, Debug, Clone)] pub struct VerifyCmd { /// Chain spec to be verified. From 91c9894462fd3ac9b10b4ce14295b90b56ad6551 Mon Sep 17 00:00:00 2001 From: command-bot <> Date: Tue, 21 Nov 2023 10:07:29 +0000 Subject: [PATCH 8/8] ".git/.scripts/commands/fmt/fmt.sh" --- substrate/bin/utils/chain-spec-builder/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index 15f954bba5158..60ec0f1a656b4 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -35,7 +35,7 @@ //! ```text //! chain-spec-builder create -r runtime.wasm default /dev/stdout //! ``` -//! +//! //! _Note:_ [`GenesisBuilder::create_default_config`][sp-genesis-builder-create] runtime function is called. //! //! @@ -46,7 +46,7 @@ //! ```text //! chain-spec-builder create -s -r runtime.wasm patch patch.json //! ``` -//! +//! //! _Note:_ [`GenesisBuilder::build_config`][sp-genesis-builder-build] runtime function is called. //! //! ##### Generate raw storage chain spec using full genesis config. @@ -55,19 +55,19 @@ //! ```text //! chain-spec-builder create -s -r runtime.wasm full full-genesis-config.json //! ``` -//! +//! //! _Note_: [`GenesisBuilder::build_config`][sp-genesis-builder-build] runtime function is called. //! //! ##### Generate human readable chain spec using provided genesis config patch. //! ```text //! chain-spec-builder create -r runtime.wasm patch patch.json //! ``` -//! +//! //! ##### Generate human readable chain spec using provided full genesis config. //! ```text //! chain-spec-builder create -r runtime.wasm full full-genesis-config.json //! ``` -//! +//! //! ##### Extra tools. //! The `chain-spec-builder` provides also some extra utilities: [`VerifyCmd`], [`ConvertToRawCmd`], [`UpdateCodeCmd`]. //!