Skip to content

Commit

Permalink
refactor(node): use export-genesis state from cumulus
Browse files Browse the repository at this point in the history
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

Signed-off-by: Yaroslav Bolyukin <iam@lach.pw>
  • Loading branch information
CertainLach authored and Daniel Shiposha committed Aug 16, 2022
1 parent 26734e9 commit e198017
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 97 deletions.
44 changes: 2 additions & 42 deletions node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ const NODE_NAME_ENV: &str = "UNIQUE_NODE_NAME";
#[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),
Expand Down Expand Up @@ -60,44 +58,6 @@ 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(parse(from_os_str))]
pub output: Option<PathBuf>,

/// Id of the parachain this state is for.
///
/// Default: 100
#[clap(long, conflicts_with = "chain")]
pub parachain_id: Option<u32>,

/// 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<String>,
}

/// 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<PathBuf>,

/// 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<String>,
}

#[derive(Debug, Parser)]
#[clap(args_conflicts_with_subcommands = true, subcommand_negates_reqs = true)]
pub struct Cli {
Expand Down
108 changes: 53 additions & 55 deletions node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use crate::service::{OpalRuntimeExecutor, DefaultRuntimeExecutor};

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::{
Expand All @@ -62,7 +62,7 @@ use sc_service::{
};
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;

Expand Down Expand Up @@ -191,16 +191,6 @@ impl SubstrateCli for RelayChainCli {
}
}

#[allow(clippy::borrowed_box)]
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())
}

macro_rules! async_run_with_runtime {
(
$runtime_api:path, $executor:path,
Expand Down Expand Up @@ -248,6 +238,45 @@ macro_rules! construct_async_run {
}}
}

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() {
Expand Down Expand Up @@ -329,49 +358,18 @@ pub fn run() -> Result<()> {
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)) => {
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::<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)) => {
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};
Expand Down Expand Up @@ -500,7 +498,7 @@ pub fn run() -> Result<()> {

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));
Expand Down

0 comments on commit e198017

Please sign in to comment.