forked from ghostchain/ghost-node
		
	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,23 +45,27 @@ 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}"))?;
 | 
				
			||||||
@ -83,7 +85,7 @@ fn inner_main() -> Result<(), 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,
 | 
				
			||||||
@ -96,7 +98,7 @@ fn inner_main() -> Result<(), String> {
 | 
				
			|||||||
                .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)]
 | 
				
			||||||
@ -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