rustfmt chain-spec-builder and fix typos

Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
Uncle Stretch 2025-07-29 14:26:12 +03:00
parent 6d06fcf9a0
commit 2ad43a56ba
Signed by: str3tch
GPG Key ID: 84F3190747EE79AA
3 changed files with 42 additions and 41 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "ghost-staging-chain-spec-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"
authors.workspace = true
edition.workspace = true

View File

@ -1,20 +1,18 @@
use chain_spec_builder::{
generate_chain_spec_for_runtime, ChainSpecBuilder, ChainSpecBuilderCmd,
ConvertToRawCmd, DisplayPresetCmd, ListPresetsCmd, UpdateCodeCmd,
VerifyCmd,
generate_chain_spec_for_runtime, ChainSpecBuilder, ChainSpecBuilderCmd, ConvertToRawCmd,
DisplayPresetCmd, ListPresetsCmd, UpdateCodeCmd, VerifyCmd,
};
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 sc_chain_spec::{
update_code_in_json_chain_spec, GenericChainSpec, GenesisConfigBuilderRuntimeCaller,
};
use std::fs;
fn main() {
match inner_main() {
Err(e) => eprintln!("{}", format!("{e}")),
_ => {},
_ => {}
}
}
@ -28,16 +26,16 @@ fn inner_main() -> Result<(), String> {
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::UpdateCode(UpdateCodeCmd {
ref input_chain_spec,
ref runtime_wasm_path,
}) => {
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(false)?
).map_err(|e| format!("Conversion to json failed: {e}"))?;
let mut chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(false)?)
.map_err(|e| format!("Conversion to json failed: {e}"))?;
update_code_in_json_chain_spec(
&mut chain_spec_json,
&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)
.map_err(|e| format!("to pretty failed: {e}"))?;
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_json = serde_json::from_str::<serde_json::Value>(
&chain_spec.as_json(false)?
).map_err(|e| format!("Conversion to json failed: {e}"))?;
let chain_spec_json =
serde_json::from_str::<serde_json::Value>(&chain_spec.as_json(false)?)
.map_err(|e| format!("Conversion to json 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 }) => {
}
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}"))?;
},
}
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 =
let caller: GenesisConfigBuilderRuntimeCaller =
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
let presets = caller
.preset_names()
@ -77,26 +79,26 @@ fn inner_main() -> Result<(), String> {
.map(|preset| {
String::from(
TryInto::<&str>::try_into(&preset)
.unwrap_or_else(|_| "cannot display preset id")
.to_string(),
.unwrap_or_else(|_| "cannot display preset id")
.to_string(),
)
})
.collect();
println!("{}", serde_json::json!({"presets": presets}).to_string());
},
ChainSpecBuilderCmd::DisplayPreset(DisplayPresetCmd {
runtime_wasm_path,
}
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 =
let caller: GenesisConfigBuilderRuntimeCaller =
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
let presets = caller
.get_named_preset(preset_name.as_ref())
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
println!("{presets}");
},
}
};
Ok(())

View File

@ -1,10 +1,8 @@
use std::{fs, path::PathBuf};
use clap::{Parser, Subcommand};
use sc_chain_spec::{ChainType, GenericChainSpec, GenesisConfigBuilderRuntimeCaller};
use serde_json::Value;
use sc_chain_spec::{
ChainType, GenericChainSpec, GenesisConfigBuilderRuntimeCaller,
};
#[derive(Debug, Parser)]
#[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
/// 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
/// line argument.
#[derive(Parser, Debug, Clone)]
@ -107,7 +105,7 @@ pub struct UpdateCodeCmd {
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)]
pub struct ConvertToRawCmd {
/// Chain spec to be converted.
@ -137,7 +135,7 @@ pub struct DisplayPresetCmd {
/// 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` fields are properly initialized and
/// to check if all `RuntimeGenesisConfig` fields are properly initialized and
/// if the json does not contain invalid fields.
#[derive(Parser, Debug, Clone)]
pub struct VerifyCmd {
@ -156,22 +154,23 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result<String, String
.with_chain_type(ChainType::Live);
let builder = match cmd.action {
GenesisBuildAction::NamedPreset(NamedPresetCmd { ref preset_name }) =>
builder.with_genesis_config_preset_name(&preset_name),
GenesisBuildAction::NamedPreset(NamedPresetCmd { ref preset_name }) => {
builder.with_genesis_config_preset_name(&preset_name)
}
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}"))?;
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}"),
)?)
},
}
GenesisBuildAction::Full(FullCmd { ref config_path }) => {
let config = fs::read(config_path.as_path())
.map_err(|e| format!("config file {config_path:?} shall be readable: {e}"))?;
builder.with_genesis_config(serde_json::from_slice::<Value>(&config[..]).map_err(
|e| format!("config file {config_path:?} shall contain a valid json: {e}"),
)?)
},
}
GenesisBuildAction::Default(DefaultCmd {}) => {
let caller: GenesisConfigBuilderRuntimeCaller =
GenesisConfigBuilderRuntimeCaller::new(&code[..]);
@ -179,7 +178,7 @@ pub fn generate_chain_spec_for_runtime(cmd: &CreateCmd) -> Result<String, String
.get_default_config()
.map_err(|e| format!("getting default config from runtime should work: {e}"))?;
builder.with_genesis_config(default_config)
},
}
};
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)?;
println!("Genesis config verification: OK");
chain_spec.as_json(false)
},
}
(false, false) => chain_spec.as_json(false),
}
}