chain-spec-builder: cleanup (#2174)

This PR removes:
-  `New`, `Generate`, `Edit` commands,
- `kitchensink` dependency
from the `chain-spec-builder` util.

New `convert-to-raw`, `update-code` commands were added.

Additionally renames the `runtime` command (which was added in #1256) to
`create`.

---------

Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: command-bot <>
This commit is contained in:
Michal Kucharczyk
2023-11-21 11:51:23 +01:00
committed by GitHub
parent 126f64a91e
commit 2fd8c51ebc
4 changed files with 116 additions and 320 deletions
@@ -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" }
@@ -17,14 +17,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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::<Vec<_>>();
let nominator_seeds = (0..nominators).map(|_| rand_str()).collect::<Vec<_>>();
let endowed_seeds = (0..endowed).map(|_| rand_str()).collect::<Vec<_>>();
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::<sr25519::Public>(&seed).to_ss58check()
})
.collect();
let endowed_accounts = endowed_seeds
.into_iter()
.map(|seed| {
chain_spec::get_account_id_from_seed::<sr25519::Public>(&seed).to_ss58check()
})
.collect();
let sudo_account =
chain_spec::get_account_id_from_seed::<sr25519::Public>(&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::<serde_json::Value>(&chain_spec.as_json(convert_to_raw)?)
serde_json::from_str::<serde_json::Value>(&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}"))?[..],
);
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::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::<serde_json::Value>(&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::<serde_json::Value>(&chain_spec.as_json(true)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;
},
};
Ok(())
}
+86 -211
View File
@@ -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<String>,
/// Active nominators (SS58 format), each backing a random subset of the aforementioned
/// authorities.
#[arg(long, short, default_value = "0")]
pub nominator_accounts: Vec<String>,
/// Endowed account address (SS58 format).
#[arg(long, short)]
pub endowed_accounts: Vec<String>,
/// 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<PathBuf>,
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<PathBuf>,
}
/// 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<PathBuf>,
/// 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 ConvertToRawCmd {
/// Chain spec to be converted.
pub input_chain_spec: PathBuf,
}
/// 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 edited
#[arg(long, short)]
/// Chain spec to be verified.
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<PathBuf>,
}
/// Generate the chain spec using the given seeds and accounts.
pub fn generate_chain_spec(
authority_seeds: Vec<String>,
nominator_accounts: Vec<String>,
endowed_accounts: Vec<String>,
sudo_account: String,
) -> Result<String, String> {
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::<Result<Vec<_>, String>>()?;
let endowed_accounts = endowed_accounts
.into_iter()
.map(parse_account)
.collect::<Result<Vec<_>, 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::<Vec<_>>();
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<String, String> {
/// Processes `CreateCmd` and returns JSON version of `ChainSpec`.
pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result<String, String> {
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);