Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
Companion for paritytech/cumulus#1325
Browse files Browse the repository at this point in the history
  • Loading branch information
jiguantong committed Jul 6, 2022
1 parent 1f28087 commit 85eea75
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 108 deletions.
60 changes: 11 additions & 49 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,10 @@

// --- std ---
use std::path::PathBuf;
// --- crates.io ---
use clap::Parser;
// --- darwinia-network ---
use crate::chain_spec;

/// Sub-commands supported by the collator.
#[derive(Debug, clap::Subcommand)]
pub enum Subcommand {
/// Export the genesis state of the parachain.
#[clap(name = "export-genesis-state")]
ExportGenesisState(ExportGenesisStateCommand),

/// Export the genesis wasm of the parachain.
#[clap(name = "export-genesis-wasm")]
ExportGenesisWasm(ExportGenesisWasmCommand),

/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),

Expand All @@ -49,11 +37,17 @@ pub enum Subcommand {
/// Import blocks.
ImportBlocks(sc_cli::ImportBlocksCmd),

/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),

/// Remove the whole chain.
PurgeChain(cumulus_client_cli::PurgeChainCmd),

/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),
/// Export the genesis state of the parachain.
ExportGenesisState(cumulus_client_cli::ExportGenesisStateCommand),

/// Export the genesis wasm of the parachain.
ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),

/// Key management CLI utilities
#[clap(subcommand)]
Expand All @@ -70,39 +64,7 @@ pub enum Subcommand {
TryRuntime(try_runtime_cli::TryRuntimeCmd),
}

/// Command for exporting the genesis state of the parachain
#[derive(Debug, Parser)]
pub struct ExportGenesisStateCommand {
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,

/// Write output in binary. Default is to write in hex.
#[clap(long)]
pub raw: bool,

/// The name of the chain for that the genesis state should be exported.
#[clap(long)]
pub chain: Option<String>,
}

/// Command for exporting the genesis wasm file.
#[derive(Debug, Parser)]
pub struct ExportGenesisWasmCommand {
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,

/// Write output in binary. Default is to write in hex.
#[clap(short, long, action)]
pub raw: bool,

/// The name of the chain for that the genesis wasm file should be exported.
#[clap(long, action)]
pub chain: Option<String>,
}

#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
#[clap(
propagate_version = true,
args_conflicts_with_subcommands = true,
Expand Down Expand Up @@ -147,9 +109,9 @@ impl RelayChainCli {
para_config: &sc_service::Configuration,
relay_chain_args: impl Iterator<Item = &'a String>,
) -> Self {
let extension = chain_spec::Extensions::try_get(&*para_config.chain_spec);
let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
let chain_id = extension.map(|e| e.relay_chain.clone());
let base_path = para_config.base_path.as_ref().map(|x| x.path().join("polkadot"));
Self { base_path, chain_id, base: polkadot_cli::RunCmd::parse_from(relay_chain_args) }
Self { base_path, chain_id, base: clap::Parser::parse_from(relay_chain_args) }
}
}
79 changes: 20 additions & 59 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

// --- std ---
use std::{env, io::Write, net::SocketAddr, path::PathBuf};
use std::{env, net::SocketAddr, path::PathBuf};
// --- crates.io ---
use codec::Encode;
use log::info;
// --- paritytech ---
use cumulus_client_service::genesis::generate_genesis_block;
use cumulus_client_cli::generate_genesis_block;
use cumulus_primitives_core::ParaId;
use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE};
use sc_cli::{
Expand Down Expand Up @@ -65,7 +65,7 @@ impl SubstrateCli for Cli {
2018
}

fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
load_spec(id)
}

Expand Down Expand Up @@ -173,15 +173,6 @@ fn set_default_ss58_version(chain_spec: &Box<dyn ChainSpec>) {
sp_core::crypto::set_default_ss58_version(ss58_version);
}

fn extract_genesis_wasm(chain_spec: &Box<dyn sc_service::ChainSpec>) -> Result<Vec<u8>> {
let mut storage = chain_spec.build_storage()?;

storage
.top
.remove(sp_core::storage::well_known_keys::CODE)
.ok_or_else(|| "Could not find wasm file in genesis state!".into())
}

/// Parse command line arguments into service configuration.
pub fn run() -> Result<()> {
/// Creates partial components for the runtimes that are supported by the benchmarks.
Expand Down Expand Up @@ -277,7 +268,7 @@ pub fn run() -> Result<()> {
let id = ParaId::from(para_id);
let parachain_account = AccountIdConversion::<AccountId>::into_account_truncating(&id);
let state_version = Cli::native_runtime_version(&config.chain_spec).state_version();
let block: Block = generate_genesis_block(&config.chain_spec, state_version)
let block: Block = generate_genesis_block(&*config.chain_spec, state_version)
.map_err(|e| format!("{:?}", e))?;
let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
let tokio_handle = config.tokio_handle.clone();
Expand Down Expand Up @@ -349,6 +340,9 @@ pub fn run() -> Result<()> {
Ok(cmd.run(components.client, components.import_queue))
})
},
Some(Subcommand::Revert(cmd)) => construct_async_run!(|components, cli, cmd, config| {
Ok(cmd.run(components.client, components.backend, None))
}),
Some(Subcommand::PurgeChain(cmd)) => {
let runner = cli.create_runner(cmd)?;

Expand All @@ -370,53 +364,20 @@ pub fn run() -> Result<()> {
cmd.run(config, polkadot_config)
})
},
Some(Subcommand::Revert(cmd)) => construct_async_run!(|components, cli, cmd, config| {
Ok(cmd.run(components.client, components.backend, None))
}),
Some(Subcommand::ExportGenesisState(params)) => {
let mut builder = sc_cli::LoggerBuilder::new("");

builder.with_profiling(sc_tracing::TracingReceiver::Log, "");

let _ = builder.init();
let spec = load_spec(&params.chain.clone().unwrap_or_default())?;
let state_version = Cli::native_runtime_version(&spec).state_version();
let block: Block = generate_genesis_block(&spec, state_version)?;
let raw_header = block.header().encode();
let output_buf = if params.raw {
raw_header
} else {
format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
};

if let Some(output) = &params.output {
std::fs::write(output, output_buf)?;
} else {
std::io::stdout().write_all(&output_buf)?;
}

Ok(())
Some(Subcommand::ExportGenesisState(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|_config| {
let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;
let state_version = Cli::native_runtime_version(&spec).state_version();
cmd.run::<Block>(&*spec, state_version)
})
},
Some(Subcommand::ExportGenesisWasm(params)) => {
let mut builder = sc_cli::LoggerBuilder::new("");
builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
let _ = builder.init();

let raw_wasm_blob =
extract_genesis_wasm(&cli.load_spec(&params.chain.clone().unwrap_or_default())?)?;
let output_buf = if params.raw {
raw_wasm_blob
} else {
format!("0x{:?}", HexDisplay::from(&raw_wasm_blob)).into_bytes()
};

if let Some(output) = &params.output {
std::fs::write(output, output_buf)?;
} else {
std::io::stdout().write_all(&output_buf)?;
}

Ok(())
Some(Subcommand::ExportGenesisWasm(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|_config| {
let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;
cmd.run(&*spec)
})
},
Some(Subcommand::Key(cmd)) => Ok(cmd.run(&cli)?),
Some(Subcommand::Benchmark(cmd)) => {
Expand Down

0 comments on commit 85eea75

Please sign in to comment.