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
+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);