diff --git a/Cargo.lock b/Cargo.lock
index b2d655e6f41ce..697f232f9719a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -18315,18 +18315,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 0b373b8e9247f..f587989e0039b 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.108"
-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..986293179a915 100644
--- a/substrate/bin/utils/chain-spec-builder/bin/main.rs
+++ b/substrate/bin/utils/chain-spec-builder/bin/main.rs
@@ -17,14 +17,11 @@
// 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, ConvertToRawCmd,
+ UpdateCodeCmd, 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;
@@ -32,110 +29,48 @@ 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();
- 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 {
+ 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}"))?[..],
- );
- }
-
- serde_json::to_string_pretty(&chain_spec_json)
- .map_err(|e| format!("to pretty failed: {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::Verify(VerifyCmd { ref input_chain_spec, ref runtime_wasm_path }) => {
- write_chain_spec = false;
+ ChainSpecBuilderCmd::ConvertToRaw(ConvertToRawCmd { ref input_chain_spec }) => {
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
- let mut chain_spec_json =
+
+ let 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}"))
- },
- }?;
- if write_chain_spec {
- fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())
- } else {
- Ok(())
- }
+ 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)?)
+ .map_err(|e| format!("Conversion to json failed: {e}"))?;
+ },
+ };
+ Ok(())
}
diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs
index 6f21b68c3684a..60ec0f1a656b4 100644
--- a/substrate/bin/utils/chain-spec-builder/src/lib.rs
+++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs
@@ -21,31 +21,69 @@
//! 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:
+//! ```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.
+//!
+//! 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.
+//!
+//! 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.
+//! ```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`].
//!
//! [`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::{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 +97,25 @@ pub struct ChainSpecBuilder {
#[derive(Debug, Subcommand)]
#[command(rename_all = "kebab-case")]
pub enum ChainSpecBuilderCmd {
- New(NewCmd),
- Generate(GenerateCmd),
- Runtime(RuntimeCmd),
- Edit(EditCmd),
+ Create(CreateCmd),
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,
+ UpdateCode(UpdateCodeCmd),
+ ConvertToRaw(ConvertToRawCmd),
}
/// 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
@@ -144,7 +137,6 @@ enum GenesisBuildAction {
#[derive(Parser, Debug, Clone)]
struct PatchCmd {
/// The path to the runtime genesis config patch.
- #[arg(long, short)]
patch_path: PathBuf,
}
@@ -152,7 +144,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,
}
@@ -163,161 +154,45 @@ 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,
}
-/// 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,
+ /// The path to new runtime wasm blob to be stored into chain-spec.
+ pub runtime_wasm_path: PathBuf,
}
-/// Verifies provided input chain spec. If the runtime is provided verification is performed against
-/// new runtime.
+/// Converts the given chain spec into the raw format.
#[derive(Parser, Debug, Clone)]
-pub struct VerifyCmd {
- /// Chain spec to be edited
- #[arg(long, short)]
+pub struct ConvertToRawCmd {
+ /// Chain spec to be converted.
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);
+/// 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.
+ pub input_chain_spec: PathBuf,
}
-/// 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);