difftreelog
refactor(node) use export-genesis state from cumulus
in: master
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
2 files changed
node/cli/src/cli.rsdiffbeforeafterboth24#[derive(Debug, Parser)]24#[derive(Debug, Parser)]25pub enum Subcommand {25pub enum Subcommand {26 /// Export the genesis state of the parachain.26 /// Export the genesis state of the parachain.27 #[clap(name = "export-genesis-state")]28 ExportGenesisState(ExportGenesisStateCommand),27 ExportGenesisState(cumulus_client_cli::ExportGenesisStateCommand),292830 /// Export the genesis wasm of the parachain.29 /// Export the genesis wasm of the parachain.31 #[clap(name = "export-genesis-wasm")]32 ExportGenesisWasm(ExportGenesisWasmCommand),30 ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),333134 /// Build a chain specification.32 /// Build a chain specification.35 BuildSpec(sc_cli::BuildSpecCmd),33 BuildSpec(sc_cli::BuildSpecCmd),60 TryRuntime(try_runtime_cli::TryRuntimeCmd),58 TryRuntime(try_runtime_cli::TryRuntimeCmd),61}59}6263/// Command for exporting the genesis state of the parachain64#[derive(Debug, Parser)]65pub struct ExportGenesisStateCommand {66 /// Output file name or stdout if unspecified.67 #[clap(parse(from_os_str))]68 pub output: Option<PathBuf>,6970 /// Id of the parachain this state is for.71 ///72 /// Default: 10073 #[clap(long, conflicts_with = "chain")]74 pub parachain_id: Option<u32>,7576 /// Write output in binary. Default is to write in hex.77 #[clap(short, long)]78 pub raw: bool,7980 /// The name of the chain for that the genesis state should be exported.81 #[clap(long, conflicts_with = "parachain-id")]82 pub chain: Option<String>,83}8485/// Command for exporting the genesis wasm file.86#[derive(Debug, Parser)]87pub struct ExportGenesisWasmCommand {88 /// Output file name or stdout if unspecified.89 #[clap(parse(from_os_str))]90 pub output: Option<PathBuf>,9192 /// Write output in binary. Default is to write in hex.93 #[clap(short, long)]94 pub raw: bool,9596 /// The name of the chain for that the genesis wasm file should be exported.97 #[clap(long)]98 pub chain: Option<String>,99}10060101#[derive(Debug, Parser)]61#[derive(Debug, Parser)]102#[clap(args_conflicts_with_subcommands = true, subcommand_negates_reqs = true)]62#[clap(args_conflicts_with_subcommands = true, subcommand_negates_reqs = true)]node/cli/src/command.rsdiffbeforeafterboth505051use codec::Encode;51use codec::Encode;52use cumulus_primitives_core::ParaId;52use cumulus_primitives_core::ParaId;53use cumulus_client_service::genesis::generate_genesis_block;53use cumulus_client_cli::generate_genesis_block;54use std::{future::Future, pin::Pin};54use std::{future::Future, pin::Pin};55use log::info;55use log::info;56use sc_cli::{56use sc_cli::{62};62};63use sp_core::hexdisplay::HexDisplay;63use sp_core::hexdisplay::HexDisplay;64use sp_runtime::traits::{AccountIdConversion, Block as BlockT};64use sp_runtime::traits::{AccountIdConversion, Block as BlockT};65use std::{io::Write, net::SocketAddr, time::Duration};65use std::{net::SocketAddr, time::Duration};666667use up_common::types::opaque::Block;67use up_common::types::opaque::Block;6868191 }191 }192}192}193194#[allow(clippy::borrowed_box)]195fn extract_genesis_wasm(chain_spec: &Box<dyn sc_service::ChainSpec>) -> Result<Vec<u8>> {196 let mut storage = chain_spec.build_storage()?;197198 storage199 .top200 .remove(sp_core::storage::well_known_keys::CODE)201 .ok_or_else(|| "Could not find wasm file in genesis state!".into())202}203193204macro_rules! async_run_with_runtime {194macro_rules! async_run_with_runtime {205 (195 (248 }}238 }}249}239}240241macro_rules! sync_run_with_runtime {242 (243 $runtime_api:path, $executor:path,244 $runner:ident, $components:ident, $cli:ident, $cmd:ident, $config:ident,245 $( $code:tt )*246 ) => {247 $runner.sync_run(|$config| {248 $( $code )*249 })250 };251}252253macro_rules! construct_sync_run {254 (|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{255 let runner = $cli.create_runner($cmd)?;256257 match runner.config().chain_spec.runtime_id() {258 #[cfg(feature = "unique-runtime")]259 RuntimeId::Unique => sync_run_with_runtime!(260 unique_runtime::RuntimeApi, UniqueRuntimeExecutor,261 runner, $components, $cli, $cmd, $config, $( $code )*262 ),263264 #[cfg(feature = "quartz-runtime")]265 RuntimeId::Quartz => sync_run_with_runtime!(266 quartz_runtime::RuntimeApi, QuartzRuntimeExecutor,267 runner, $components, $cli, $cmd, $config, $( $code )*268 ),269270 RuntimeId::Opal => sync_run_with_runtime!(271 opal_runtime::RuntimeApi, OpalRuntimeExecutor,272 runner, $components, $cli, $cmd, $config, $( $code )*273 ),274275 RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into())276 }277 }}278}250279251macro_rules! start_node_using_chain_runtime {280macro_rules! start_node_using_chain_runtime {252 ($start_node_fn:ident($config:expr $(, $($args:expr),+)?) $($code:tt)*) => {281 ($start_node_fn:ident($config:expr $(, $($args:expr),+)?) $($code:tt)*) => {329 Some(Subcommand::Revert(cmd)) => construct_async_run!(|components, cli, cmd, config| {358 Some(Subcommand::Revert(cmd)) => construct_async_run!(|components, cli, cmd, config| {330 Ok(cmd.run(components.client, components.backend, None))359 Ok(cmd.run(components.client, components.backend, None))331 }),360 }),332 Some(Subcommand::ExportGenesisState(params)) => {361 Some(Subcommand::ExportGenesisState(cmd)) => {333 let mut builder = sc_cli::LoggerBuilder::new("");362 construct_sync_run!(|components, cli, cmd, _config| {334 builder.with_profiling(sc_tracing::TracingReceiver::Log, "");335 let _ = builder.init();363 let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;336337 let spec = load_spec(¶ms.chain.clone().unwrap_or_default())?;338 let state_version = Cli::native_runtime_version(&spec).state_version();364 let state_version = Cli::native_runtime_version(&spec).state_version();339 let block: Block = generate_genesis_block(&spec, state_version)?;365 cmd.run::<Block>(&*spec, state_version)340 let raw_header = block.header().encode();366 })341 let output_buf = if params.raw {342 raw_header343 } else {344 format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()345 };346347 if let Some(output) = ¶ms.output {348 std::fs::write(output, output_buf)?;349 } else {350 std::io::stdout().write_all(&output_buf)?;351 }352353 Ok(())354 }367 }355 Some(Subcommand::ExportGenesisWasm(params)) => {368 Some(Subcommand::ExportGenesisWasm(cmd)) => {356 let mut builder = sc_cli::LoggerBuilder::new("");369 construct_sync_run!(|components, cli, cmd, _config| {357 builder.with_profiling(sc_tracing::TracingReceiver::Log, "");358 let _ = builder.init();359360 let raw_wasm_blob =370 let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;361 extract_genesis_wasm(&cli.load_spec(¶ms.chain.clone().unwrap_or_default())?)?;371 cmd.run(&*spec)362 let output_buf = if params.raw {372 })363 raw_wasm_blob364 } else {365 format!("0x{:?}", HexDisplay::from(&raw_wasm_blob)).into_bytes()366 };367368 if let Some(output) = ¶ms.output {369 std::fs::write(output, output_buf)?;370 } else {371 std::io::stdout().write_all(&output_buf)?;372 }373374 Ok(())375 }373 }376 Some(Subcommand::Benchmark(cmd)) => {374 Some(Subcommand::Benchmark(cmd)) => {377 use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE};375 use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE};500498501 let state_version =499 let state_version =502 RelayChainCli::native_runtime_version(&config.chain_spec).state_version();500 RelayChainCli::native_runtime_version(&config.chain_spec).state_version();503 let block: Block = generate_genesis_block(&config.chain_spec, state_version)501 let block: Block = generate_genesis_block(&*config.chain_spec, state_version)504 .map_err(|e| format!("{:?}", e))?;502 .map_err(|e| format!("{:?}", e))?;505 let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));503 let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));506 let genesis_hash = format!("0x{:?}", HexDisplay::from(&block.header().hash().0));504 let genesis_hash = format!("0x{:?}", HexDisplay::from(&block.header().hash().0));