From 2b4626c365258062055877a0c6e9dfb82d3d1b6f Mon Sep 17 00:00:00 2001 From: Michal Kucharczyk <1728078+michalkucharczyk@users.noreply.github.com> Date: Thu, 14 Dec 2023 17:22:09 +0100 Subject: [PATCH] chain-spec-builder: presets added --- .../bin/utils/chain-spec-builder/bin/main.rs | 27 +++++- .../bin/utils/chain-spec-builder/src/lib.rs | 95 +++++++++++++++---- 2 files changed, 103 insertions(+), 19 deletions(-) diff --git a/substrate/bin/utils/chain-spec-builder/bin/main.rs b/substrate/bin/utils/chain-spec-builder/bin/main.rs index 986293179a91..c709287ee078 100644 --- a/substrate/bin/utils/chain-spec-builder/bin/main.rs +++ b/substrate/bin/utils/chain-spec-builder/bin/main.rs @@ -18,10 +18,12 @@ use chain_spec_builder::{ generate_chain_spec_for_runtime, ChainSpecBuilder, ChainSpecBuilderCmd, ConvertToRawCmd, - UpdateCodeCmd, VerifyCmd, + DisplayPresetCmd, ListPresetsCmd, UpdateCodeCmd, VerifyCmd, }; use clap::Parser; -use sc_chain_spec::{update_code_in_json_chain_spec, GenericChainSpec}; +use sc_chain_spec::{ + update_code_in_json_chain_spec, GenericChainSpec, GenesisConfigBuilderRuntimeCaller, +}; use staging_chain_spec_builder as chain_spec_builder; use std::fs; @@ -71,6 +73,27 @@ fn main() -> Result<(), String> { let _ = serde_json::from_str::(&chain_spec.as_json(true)?) .map_err(|e| format!("Conversion to json failed: {e}"))?; }, + ChainSpecBuilderCmd::ListPresets(ListPresetsCmd { runtime_wasm_path }) => { + let code = fs::read(runtime_wasm_path.as_path()) + .map_err(|e| format!("wasm blob shall be readable {e}"))?; + let caller: GenesisConfigBuilderRuntimeCaller = + GenesisConfigBuilderRuntimeCaller::new(&code[..]); + let presets = caller + .preset_names() + .map_err(|e| format!("getting default config from runtime should work: {e}"))?; + let presets: Vec = presets.into_iter().map(Into::into).collect(); + println!("Known presets are:\n{presets:#?}"); + }, + ChainSpecBuilderCmd::DisplayPreset(DisplayPresetCmd { runtime_wasm_path, preset_name }) => { + let code = fs::read(runtime_wasm_path.as_path()) + .map_err(|e| format!("wasm blob shall be readable {e}"))?; + let caller: GenesisConfigBuilderRuntimeCaller = + GenesisConfigBuilderRuntimeCaller::new(&code[..]); + let preset = caller + .get_named_preset(preset_name.as_ref()) + .map_err(|e| format!("getting default config from runtime should work: {e}"))?; + println!("{preset}"); + }, }; Ok(()) } diff --git a/substrate/bin/utils/chain-spec-builder/src/lib.rs b/substrate/bin/utils/chain-spec-builder/src/lib.rs index 4cb7f242bfe0..a057dc4d75c1 100644 --- a/substrate/bin/utils/chain-spec-builder/src/lib.rs +++ b/substrate/bin/utils/chain-spec-builder/src/lib.rs @@ -28,17 +28,54 @@ //! See [`ChainSpecBuilderCmd`] for a list of available commands. //! //! ## Typical use-cases. -//! ##### Get default config from runtime. +//! ##### Generate chains-spec using 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: +//! spec. //! ```text -//! chain-spec-builder create -r runtime.wasm default /dev/stdout +//! chain-spec-builder create -r runtime.wasm default //! ``` -//! +//! //! _Note:_ [`GenesisBuilder::create_default_config`][sp-genesis-builder-create] runtime function is called. //! //! +//! ##### Display the runtime's default `GenesisConfig` +//! +//! Displays the content of the runtime's default `GenesisConfig` +//! ```text +//! chain-spec-builder display-preset -r runtime.wasm +//! ``` +//! +//! _Note:_ [`GenesisBuilder::get_preset`][sp-genesis-builder-get-preset] runtime function is called. +//! +//! ##### Display the `GenesisConfig` preset with given name +//! +//! Displays the content of the `GenesisConfig` preset for given name +//! ```text +//! chain-spec-builder display-preset -r runtime.wasm -p "staging" +//! ``` +//! +//! _Note:_ [`GenesisBuilder::get_preset`][sp-genesis-builder-get-preset] runtime function is called. +//! +//! ##### List the names of `GenesisConfig` presets provided by runtime. +//! +//! Displays the names of the presets of `GenesisConfigs` provided by runtime. +//! ```text +//! chain-spec-builder list-presets -r runtime.wasm +//! ``` +//! +//! _Note:_ [`GenesisBuilder::preset_names`][sp-genesis-builder-list] runtime function is called. +//! +//! ##### Generate chain spec using runtime provided genesis config preset. +//! +//! Patch the runtime's default genesis config with the named preset provided by the runtime and generate the plain +//! version of chain spec: +//! ```text +//! chain-spec-builder create -r runtime.wasm named-patch "staging" +//! ``` +//! +//! _Note:_ [`GenesisBuilder::get_preset`][sp-genesis-builder-get-preset] and [`GenesisBuilder::build_state`][sp-genesis-builder-build] runtime functions are called. +//! //! ##### Generate raw storage chain spec using genesis config patch. //! //! Patch the runtime's default genesis config with provided `patch.json` and generate raw @@ -46,7 +83,7 @@ //! ```text //! chain-spec-builder create -s -r runtime.wasm patch patch.json //! ``` -//! +//! //! _Note:_ [`GenesisBuilder::build_state`][sp-genesis-builder-build] runtime function is called. //! //! ##### Generate raw storage chain spec using full genesis config. @@ -55,19 +92,19 @@ //! ```text //! chain-spec-builder create -s -r runtime.wasm full full-genesis-config.json //! ``` -//! +//! //! _Note_: [`GenesisBuilder::build_state`][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`]. //! @@ -76,6 +113,8 @@ //! [`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_state +//! [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}; @@ -101,6 +140,8 @@ pub enum ChainSpecBuilderCmd { Verify(VerifyCmd), UpdateCode(UpdateCodeCmd), ConvertToRaw(ConvertToRawCmd), + ListPresets(ListPresetsCmd), + DisplayPreset(DisplayPresetCmd), } /// Create a new chain spec by interacting with the provided runtime wasm blob. @@ -131,6 +172,7 @@ enum GenesisBuildAction { Patch(PatchCmd), Full(FullCmd), Default(DefaultCmd), + NamedPreset(NamedPresetCmd), } /// Patches the runtime's default genesis config with provided patch. @@ -151,10 +193,12 @@ struct FullCmd { /// default genesis config may not be valid. For some runtimes initial values should be added there /// (e.g. session keys, babe epoch). #[derive(Parser, Debug, Clone)] -struct DefaultCmd { - /// If provided stores the default genesis config json file at given path (in addition to - /// chain-spec). - default_config_path: Option, +struct DefaultCmd {} + +/// Uses named preset provided by runtime to build the chains spec. +#[derive(Parser, Debug, Clone)] +struct NamedPresetCmd { + preset_name: String, } /// Updates the code in the provided input chain spec. @@ -176,6 +220,25 @@ pub struct ConvertToRawCmd { pub input_chain_spec: PathBuf, } +/// Lists available presets +#[derive(Parser, Debug, Clone)] +pub struct ListPresetsCmd { + /// The path to runtime wasm blob. + #[arg(long, short)] + pub runtime_wasm_path: PathBuf, +} + +/// Displays given preset +#[derive(Parser, Debug, Clone)] +pub struct DisplayPresetCmd { + /// The path to runtime wasm blob. + #[arg(long, short)] + pub runtime_wasm_path: PathBuf, + /// Preset to be displayed. If none is given default will be displayed. + #[arg(long, short)] + pub preset_name: Option, +} + /// Verifies the provided input chain spec. /// /// Silently checks if given input chain spec can be converted to raw. It allows to check if all @@ -198,6 +261,8 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result + builder.with_genesis_config_patch_name(preset_name.clone()), GenesisBuildAction::Patch(PatchCmd { ref patch_path }) => { let patch = fs::read(patch_path.as_path()) .map_err(|e| format!("patch file {patch_path:?} shall be readable: {e}"))?; @@ -212,16 +277,12 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result { + GenesisBuildAction::Default(DefaultCmd {}) => { let caller: GenesisConfigBuilderRuntimeCaller = GenesisConfigBuilderRuntimeCaller::new(&code[..]); let default_config = caller .get_default_config() .map_err(|e| format!("getting default config from runtime should work: {e}"))?; - default_config_path.clone().map(|path| { - fs::write(path.as_path(), serde_json::to_string_pretty(&default_config).unwrap()) - .map_err(|err| err.to_string()) - }); builder.with_genesis_config(default_config) }, };