Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Uses ValueEnum
  • Loading branch information
gupnik committed May 27, 2024
commit 20b2383f51a4e7456ec6dd773336b53d0bb5a6f5
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions substrate/bin/utils/chain-spec-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@
//! [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, str::FromStr};
use std::{fs, path::PathBuf};

use clap::{Parser, Subcommand};
use sc_chain_spec::{GenericChainSpec, GenesisConfigBuilderRuntimeCaller};
use sc_chain_spec::{ChainType, GenericChainSpec, GenesisConfigBuilderRuntimeCaller};
use serde_json::Value;

/// A utility to easily create a chain spec definition.
Expand Down Expand Up @@ -155,8 +155,8 @@ pub struct CreateCmd {
#[arg(long, short = 'i', default_value = "custom")]
chain_id: String,
/// The chain type.
#[arg(long, short = 't', default_value = "Live")]
chain_type: String,
#[arg(value_enum, short = 't', default_value = "Live")]
chain_type: ChainType,
/// The path to runtime wasm blob.
#[arg(long, short)]
runtime_wasm_path: PathBuf,
Expand Down Expand Up @@ -259,13 +259,12 @@ 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 chain_type = sc_chain_spec::ChainType::from_str(&cmd.chain_type[..])
.map_err(|_| "chain type should be valid".to_string())?;
let chain_type = &cmd.chain_type;

let builder = GenericChainSpec::<()>::builder(&code[..], Default::default())
.with_name(&cmd.chain_name[..])
.with_id(&cmd.chain_id[..])
.with_chain_type(chain_type);
.with_chain_type(chain_type.clone());

let builder = match cmd.action {
GenesisBuildAction::NamedPreset(NamedPresetCmd { ref preset_name }) =>
Expand Down
1 change: 1 addition & 0 deletions substrate/client/chain-spec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
clap = { version = "4.5.3", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive"] }
memmap2 = "0.9.3"
serde = { features = ["derive"], workspace = true, default-features = true }
Expand Down
18 changes: 2 additions & 16 deletions substrate/client/chain-spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ mod genesis_block;
mod genesis_config_builder;
pub mod json_patch;

use std::str::FromStr;

pub use self::{
chain_spec::{
update_code_in_json_chain_spec, ChainSpec as GenericChainSpec, ChainSpecBuilder,
Expand All @@ -354,7 +352,7 @@ use sp_runtime::BuildStorage;
///
/// This can be used by tools to determine the type of a chain for displaying
/// additional information or enabling additional features.
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
#[derive(clap::ValueEnum, serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)]
pub enum ChainType {
/// A development chain that runs mainly on one node.
Development,
Expand All @@ -363,6 +361,7 @@ pub enum ChainType {
/// A live chain.
Live,
/// Some custom chain type.
#[clap(skip)]
Custom(String),
}

Expand All @@ -372,19 +371,6 @@ impl Default for ChainType {
}
}

impl FromStr for ChainType {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"Development" => ChainType::Development,
"Local" => ChainType::Local,
"Live" => ChainType::Live,
_ => ChainType::Custom(s.to_string()),
})
}
}

/// Arbitrary properties defined in chain spec as a JSON object
pub type Properties = serde_json::map::Map<String, serde_json::Value>;

Expand Down