From e1980179e647db8b299cca32cdc9e2b3bf5e51b2 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 21 Jul 2022 07:52:51 +0000 Subject: [PATCH] refactor(node): use export-genesis state from cumulus We had our implementation for some reason, however it is now broken, and I see no reason to keep it, as upstream implements exact same options --- --- a/node/cli/src/cli.rs +++ b/node/cli/src/cli.rs @@ -24,12 +24,10 @@ #[derive(Debug, Parser)] pub enum Subcommand { /// Export the genesis state of the parachain. - #[clap(name = "export-genesis-state")] - ExportGenesisState(ExportGenesisStateCommand), + ExportGenesisState(cumulus_client_cli::ExportGenesisStateCommand), /// Export the genesis wasm of the parachain. - #[clap(name = "export-genesis-wasm")] - ExportGenesisWasm(ExportGenesisWasmCommand), + ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand), /// Build a chain specification. BuildSpec(sc_cli::BuildSpecCmd), @@ -58,44 +56,6 @@ /// Try runtime 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(parse(from_os_str))] - pub output: Option, - - /// Id of the parachain this state is for. - /// - /// Default: 100 - #[clap(long, conflicts_with = "chain")] - pub parachain_id: Option, - - /// Write output in binary. Default is to write in hex. - #[clap(short, long)] - pub raw: bool, - - /// The name of the chain for that the genesis state should be exported. - #[clap(long, conflicts_with = "parachain-id")] - pub chain: Option, -} - -/// Command for exporting the genesis wasm file. -#[derive(Debug, Parser)] -pub struct ExportGenesisWasmCommand { - /// Output file name or stdout if unspecified. - #[clap(parse(from_os_str))] - pub output: Option, - - /// Write output in binary. Default is to write in hex. - #[clap(short, long)] - pub raw: bool, - - /// The name of the chain for that the genesis wasm file should be exported. - #[clap(long)] - pub chain: Option, } #[derive(Debug, Parser)] --- a/node/cli/src/command.rs +++ b/node/cli/src/command.rs @@ -50,7 +50,7 @@ use codec::Encode; use cumulus_primitives_core::ParaId; -use cumulus_client_service::genesis::generate_genesis_block; +use cumulus_client_cli::generate_genesis_block; use std::{future::Future, pin::Pin}; use log::info; use sc_cli::{ @@ -62,7 +62,7 @@ }; use sp_core::hexdisplay::HexDisplay; use sp_runtime::traits::{AccountIdConversion, Block as BlockT}; -use std::{io::Write, net::SocketAddr, time::Duration}; +use std::{net::SocketAddr, time::Duration}; use up_common::types::opaque::Block; @@ -189,16 +189,6 @@ fn native_runtime_version(chain_spec: &Box) -> &'static RuntimeVersion { polkadot_cli::Cli::native_runtime_version(chain_spec) } -} - -#[allow(clippy::borrowed_box)] -fn extract_genesis_wasm(chain_spec: &Box) -> Result> { - 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()) } macro_rules! async_run_with_runtime { @@ -248,6 +238,45 @@ }} } +macro_rules! sync_run_with_runtime { + ( + $runtime_api:path, $executor:path, + $runner:ident, $components:ident, $cli:ident, $cmd:ident, $config:ident, + $( $code:tt )* + ) => { + $runner.sync_run(|$config| { + $( $code )* + }) + }; +} + +macro_rules! construct_sync_run { + (|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{ + let runner = $cli.create_runner($cmd)?; + + match runner.config().chain_spec.runtime_id() { + #[cfg(feature = "unique-runtime")] + RuntimeId::Unique => sync_run_with_runtime!( + unique_runtime::RuntimeApi, UniqueRuntimeExecutor, + runner, $components, $cli, $cmd, $config, $( $code )* + ), + + #[cfg(feature = "quartz-runtime")] + RuntimeId::Quartz => sync_run_with_runtime!( + quartz_runtime::RuntimeApi, QuartzRuntimeExecutor, + runner, $components, $cli, $cmd, $config, $( $code )* + ), + + RuntimeId::Opal => sync_run_with_runtime!( + opal_runtime::RuntimeApi, OpalRuntimeExecutor, + runner, $components, $cli, $cmd, $config, $( $code )* + ), + + RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()) + } + }} +} + macro_rules! start_node_using_chain_runtime { ($start_node_fn:ident($config:expr $(, $($args:expr),+)?) $($code:tt)*) => { match $config.chain_spec.runtime_id() { @@ -329,49 +358,18 @@ 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(¶ms.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) = ¶ms.output { - std::fs::write(output, output_buf)?; - } else { - std::io::stdout().write_all(&output_buf)?; - } - - Ok(()) + Some(Subcommand::ExportGenesisState(cmd)) => { + construct_sync_run!(|components, cli, cmd, _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::(&*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(¶ms.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) = ¶ms.output { - std::fs::write(output, output_buf)?; - } else { - std::io::stdout().write_all(&output_buf)?; - } - - Ok(()) + Some(Subcommand::ExportGenesisWasm(cmd)) => { + construct_sync_run!(|components, cli, cmd, _config| { + let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?; + cmd.run(&*spec) + }) } Some(Subcommand::Benchmark(cmd)) => { use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE}; @@ -500,7 +498,7 @@ let state_version = RelayChainCli::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 genesis_hash = format!("0x{:?}", HexDisplay::from(&block.header().hash().0)); -- gitstuff