rustfmt chain-spec-builder and fix typos
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
parent
6d06fcf9a0
commit
2ad43a56ba
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ghost-staging-chain-spec-builder"
|
name = "ghost-staging-chain-spec-builder"
|
||||||
description = "Utility for building chain-specification files for Ghost and Casper runtimes on `sp-genesis-builder`"
|
description = "Utility for building chain-specification files for Ghost and Casper runtimes on `sp-genesis-builder`"
|
||||||
version = "1.6.1"
|
version = "1.6.2"
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
|
@ -1,20 +1,18 @@
|
|||||||
use chain_spec_builder::{
|
use chain_spec_builder::{
|
||||||
generate_chain_spec_for_runtime, ChainSpecBuilder, ChainSpecBuilderCmd,
|
generate_chain_spec_for_runtime, ChainSpecBuilder, ChainSpecBuilderCmd, ConvertToRawCmd,
|
||||||
ConvertToRawCmd, DisplayPresetCmd, ListPresetsCmd, UpdateCodeCmd,
|
DisplayPresetCmd, ListPresetsCmd, UpdateCodeCmd, VerifyCmd,
|
||||||
VerifyCmd,
|
|
||||||
};
|
};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use sc_chain_spec::{
|
|
||||||
update_code_in_json_chain_spec, GenericChainSpec,
|
|
||||||
GenesisConfigBuilderRuntimeCaller,
|
|
||||||
};
|
|
||||||
use ghost_staging_chain_spec_builder as chain_spec_builder;
|
use ghost_staging_chain_spec_builder as chain_spec_builder;
|
||||||
|
use sc_chain_spec::{
|
||||||
|
update_code_in_json_chain_spec, GenericChainSpec, GenesisConfigBuilderRuntimeCaller,
|
||||||
|
};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match inner_main() {
|
match inner_main() {
|
||||||
Err(e) => eprintln!("{}", format!("{e}")),
|
Err(e) => eprintln!("{}", format!("{e}")),
|
||||||
_ => {},
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,16 +26,16 @@ fn inner_main() -> Result<(), String> {
|
|||||||
ChainSpecBuilderCmd::Create(cmd) => {
|
ChainSpecBuilderCmd::Create(cmd) => {
|
||||||
let chain_spec_json = generate_chain_spec_for_runtime(&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())?;
|
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
|
||||||
},
|
}
|
||||||
ChainSpecBuilderCmd::UpdateCode(UpdateCodeCmd {
|
ChainSpecBuilderCmd::UpdateCode(UpdateCodeCmd {
|
||||||
ref input_chain_spec,
|
ref input_chain_spec,
|
||||||
ref runtime_wasm_path,
|
ref runtime_wasm_path,
|
||||||
}) => {
|
}) => {
|
||||||
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
|
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
|
||||||
|
|
||||||
let mut chain_spec_json = serde_json::from_str::<serde_json::Value>(
|
let mut chain_spec_json =
|
||||||
&chain_spec.as_json(false)?
|
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(false)?)
|
||||||
).map_err(|e| format!("Conversion to json failed: {e}"))?;
|
.map_err(|e| format!("Conversion to json failed: {e}"))?;
|
||||||
update_code_in_json_chain_spec(
|
update_code_in_json_chain_spec(
|
||||||
&mut chain_spec_json,
|
&mut chain_spec_json,
|
||||||
&fs::read(runtime_wasm_path.as_path())
|
&fs::read(runtime_wasm_path.as_path())
|
||||||
@ -47,27 +45,31 @@ fn inner_main() -> Result<(), String> {
|
|||||||
let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json)
|
let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json)
|
||||||
.map_err(|e| format!("to pretty failed: {e}"))?;
|
.map_err(|e| format!("to pretty failed: {e}"))?;
|
||||||
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
|
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
|
||||||
},
|
}
|
||||||
ChainSpecBuilderCmd::ConvertToRaw(ConvertToRawCmd { ref input_chain_spec }) => {
|
ChainSpecBuilderCmd::ConvertToRaw(ConvertToRawCmd {
|
||||||
|
ref input_chain_spec,
|
||||||
|
}) => {
|
||||||
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
|
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
|
||||||
|
|
||||||
let chain_spec_json = serde_json::from_str::<serde_json::Value>(
|
let chain_spec_json =
|
||||||
&chain_spec.as_json(false)?
|
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(false)?)
|
||||||
).map_err(|e| format!("Conversion to json failed: {e}"))?;
|
.map_err(|e| format!("Conversion to json failed: {e}"))?;
|
||||||
|
|
||||||
let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json)
|
let chain_spec_json = serde_json::to_string_pretty(&chain_spec_json)
|
||||||
.map_err(|e| format!("to pretty failed: {e}"))?;
|
.map_err(|e| format!("to pretty failed: {e}"))?;
|
||||||
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
|
fs::write(chain_spec_path, chain_spec_json).map_err(|err| err.to_string())?;
|
||||||
},
|
}
|
||||||
ChainSpecBuilderCmd::Verify(VerifyCmd { ref input_chain_spec }) => {
|
ChainSpecBuilderCmd::Verify(VerifyCmd {
|
||||||
|
ref input_chain_spec,
|
||||||
|
}) => {
|
||||||
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
|
let chain_spec = GenericChainSpec::<()>::from_json_file(input_chain_spec.clone())?;
|
||||||
let _ = serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(true)?)
|
let _ = serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(true)?)
|
||||||
.map_err(|e| format!("Conversion to json failed: {e}"))?;
|
.map_err(|e| format!("Conversion to json failed: {e}"))?;
|
||||||
},
|
}
|
||||||
ChainSpecBuilderCmd::ListPresets(ListPresetsCmd { runtime_wasm_path }) => {
|
ChainSpecBuilderCmd::ListPresets(ListPresetsCmd { runtime_wasm_path }) => {
|
||||||
let code = fs::read(runtime_wasm_path.as_path())
|
let code = fs::read(runtime_wasm_path.as_path())
|
||||||
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
|
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
|
||||||
let caller: GenesisConfigBuilderRuntimeCaller =
|
let caller: GenesisConfigBuilderRuntimeCaller =
|
||||||
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
|
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
|
||||||
let presets = caller
|
let presets = caller
|
||||||
.preset_names()
|
.preset_names()
|
||||||
@ -77,26 +79,26 @@ fn inner_main() -> Result<(), String> {
|
|||||||
.map(|preset| {
|
.map(|preset| {
|
||||||
String::from(
|
String::from(
|
||||||
TryInto::<&str>::try_into(&preset)
|
TryInto::<&str>::try_into(&preset)
|
||||||
.unwrap_or_else(|_| "cannot display preset id")
|
.unwrap_or_else(|_| "cannot display preset id")
|
||||||
.to_string(),
|
.to_string(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
println!("{}", serde_json::json!({"presets": presets}).to_string());
|
println!("{}", serde_json::json!({"presets": presets}).to_string());
|
||||||
},
|
}
|
||||||
ChainSpecBuilderCmd::DisplayPreset(DisplayPresetCmd {
|
ChainSpecBuilderCmd::DisplayPreset(DisplayPresetCmd {
|
||||||
runtime_wasm_path,
|
runtime_wasm_path,
|
||||||
preset_name,
|
preset_name,
|
||||||
}) => {
|
}) => {
|
||||||
let code = fs::read(runtime_wasm_path.as_path())
|
let code = fs::read(runtime_wasm_path.as_path())
|
||||||
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
|
.map_err(|e| format!("wasm blob shall be readable {e}"))?;
|
||||||
let caller: GenesisConfigBuilderRuntimeCaller =
|
let caller: GenesisConfigBuilderRuntimeCaller =
|
||||||
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
|
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
|
||||||
let presets = caller
|
let presets = caller
|
||||||
.get_named_preset(preset_name.as_ref())
|
.get_named_preset(preset_name.as_ref())
|
||||||
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
|
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
|
||||||
println!("{presets}");
|
println!("{presets}");
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
use std::{fs, path::PathBuf};
|
use std::{fs, path::PathBuf};
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
|
use sc_chain_spec::{ChainType, GenericChainSpec, GenesisConfigBuilderRuntimeCaller};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use sc_chain_spec::{
|
|
||||||
ChainType, GenericChainSpec, GenesisConfigBuilderRuntimeCaller,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[derive(Debug, Parser)]
|
||||||
#[command(rename_all = "kebab-case", version, about)]
|
#[command(rename_all = "kebab-case", version, about)]
|
||||||
@ -94,7 +92,7 @@ struct NamedPresetCmd {
|
|||||||
/// The code field of the chain spec will be updated with the runtime provided
|
/// The code field of the chain spec will be updated with the runtime provided
|
||||||
/// in the command line. The operation supports both plain and raw formats.
|
/// in the command line. The operation supports both plain and raw formats.
|
||||||
///
|
///
|
||||||
/// This command does not update chain-spec file in-place. The result of this
|
/// This command does not update chain-spec file in-place. The result of this
|
||||||
/// command will be stored in a file given as `-c/--chain-spec-path` command
|
/// command will be stored in a file given as `-c/--chain-spec-path` command
|
||||||
/// line argument.
|
/// line argument.
|
||||||
#[derive(Parser, Debug, Clone)]
|
#[derive(Parser, Debug, Clone)]
|
||||||
@ -107,7 +105,7 @@ pub struct UpdateCodeCmd {
|
|||||||
pub runtime_wasm_path: PathBuf,
|
pub runtime_wasm_path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts the given chain spec into raw format.
|
/// Converts the given chain spec into raw format.
|
||||||
#[derive(Parser, Debug, Clone)]
|
#[derive(Parser, Debug, Clone)]
|
||||||
pub struct ConvertToRawCmd {
|
pub struct ConvertToRawCmd {
|
||||||
/// Chain spec to be converted.
|
/// Chain spec to be converted.
|
||||||
@ -137,7 +135,7 @@ pub struct DisplayPresetCmd {
|
|||||||
/// Verifies the provided input chain spec.
|
/// Verifies the provided input chain spec.
|
||||||
///
|
///
|
||||||
/// Silently checks if given input chain spec can be converted to raw. It allows
|
/// Silently checks if given input chain spec can be converted to raw. It allows
|
||||||
/// to check if all `RuntimeGenesisConfig` fields are properly initialized and
|
/// to check if all `RuntimeGenesisConfig` fields are properly initialized and
|
||||||
/// if the json does not contain invalid fields.
|
/// if the json does not contain invalid fields.
|
||||||
#[derive(Parser, Debug, Clone)]
|
#[derive(Parser, Debug, Clone)]
|
||||||
pub struct VerifyCmd {
|
pub struct VerifyCmd {
|
||||||
@ -156,22 +154,23 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result<String, String
|
|||||||
.with_chain_type(ChainType::Live);
|
.with_chain_type(ChainType::Live);
|
||||||
|
|
||||||
let builder = match cmd.action {
|
let builder = match cmd.action {
|
||||||
GenesisBuildAction::NamedPreset(NamedPresetCmd { ref preset_name }) =>
|
GenesisBuildAction::NamedPreset(NamedPresetCmd { ref preset_name }) => {
|
||||||
builder.with_genesis_config_preset_name(&preset_name),
|
builder.with_genesis_config_preset_name(&preset_name)
|
||||||
|
}
|
||||||
GenesisBuildAction::Patch(PatchCmd { ref patch_path }) => {
|
GenesisBuildAction::Patch(PatchCmd { ref patch_path }) => {
|
||||||
let patch = fs::read(patch_path.as_path())
|
let patch = fs::read(patch_path.as_path())
|
||||||
.map_err(|e| format!("patch file {patch_path:?} shall be readable: {e}"))?;
|
.map_err(|e| format!("patch file {patch_path:?} shall be readable: {e}"))?;
|
||||||
builder.with_genesis_config_patch(serde_json::from_slice::<Value>(&patch[..]).map_err(
|
builder.with_genesis_config_patch(serde_json::from_slice::<Value>(&patch[..]).map_err(
|
||||||
|e| format!("patch file {patch_path:?} shall contain a valid json: {e}"),
|
|e| format!("patch file {patch_path:?} shall contain a valid json: {e}"),
|
||||||
)?)
|
)?)
|
||||||
},
|
}
|
||||||
GenesisBuildAction::Full(FullCmd { ref config_path }) => {
|
GenesisBuildAction::Full(FullCmd { ref config_path }) => {
|
||||||
let config = fs::read(config_path.as_path())
|
let config = fs::read(config_path.as_path())
|
||||||
.map_err(|e| format!("config file {config_path:?} shall be readable: {e}"))?;
|
.map_err(|e| format!("config file {config_path:?} shall be readable: {e}"))?;
|
||||||
builder.with_genesis_config(serde_json::from_slice::<Value>(&config[..]).map_err(
|
builder.with_genesis_config(serde_json::from_slice::<Value>(&config[..]).map_err(
|
||||||
|e| format!("config file {config_path:?} shall contain a valid json: {e}"),
|
|e| format!("config file {config_path:?} shall contain a valid json: {e}"),
|
||||||
)?)
|
)?)
|
||||||
},
|
}
|
||||||
GenesisBuildAction::Default(DefaultCmd {}) => {
|
GenesisBuildAction::Default(DefaultCmd {}) => {
|
||||||
let caller: GenesisConfigBuilderRuntimeCaller =
|
let caller: GenesisConfigBuilderRuntimeCaller =
|
||||||
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
|
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
|
||||||
@ -179,7 +178,7 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result<String, String
|
|||||||
.get_default_config()
|
.get_default_config()
|
||||||
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
|
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
|
||||||
builder.with_genesis_config(default_config)
|
builder.with_genesis_config(default_config)
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let chain_spec = builder.build();
|
let chain_spec = builder.build();
|
||||||
@ -190,7 +189,7 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result<String, String
|
|||||||
chain_spec.as_json(true)?;
|
chain_spec.as_json(true)?;
|
||||||
println!("Genesis config verification: OK");
|
println!("Genesis config verification: OK");
|
||||||
chain_spec.as_json(false)
|
chain_spec.as_json(false)
|
||||||
},
|
}
|
||||||
(false, false) => chain_spec.as_json(false),
|
(false, false) => chain_spec.as_json(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user