From 17b892d5577d8260ee9f9efa5bd25c42f075d187 Mon Sep 17 00:00:00 2001 From: tmpolaczyk <44604217+tmpolaczyk@users.noreply.github.com> Date: Wed, 6 Sep 2023 18:54:34 +0200 Subject: [PATCH] Resolve state version from chain spec instead of assuming default (#247) * Resolve state version from chain spec instead of assuming default * Resolve state version from chain spec in templates * Fix container chain export-genesis-state command --- .../templates/frontier/node/src/command.rs | 9 +++- .../templates/simple/node/src/command.rs | 9 +++- node/src/cli.rs | 51 +++++++++++++++++-- node/src/command.rs | 42 +++++---------- 4 files changed, 74 insertions(+), 37 deletions(-) diff --git a/container-chains/templates/frontier/node/src/command.rs b/container-chains/templates/frontier/node/src/command.rs index 5ec73f479..caa86df38 100644 --- a/container-chains/templates/frontier/node/src/command.rs +++ b/container-chains/templates/frontier/node/src/command.rs @@ -32,6 +32,7 @@ use { ChainSpec, CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams, NetworkParams, Result, RuntimeVersion, SharedParams, SubstrateCli, }, + sc_client_api::ExecutorProvider, sc_service::{ config::{BasePath, PrometheusConfig}, DatabaseSource, @@ -238,9 +239,13 @@ pub fn run() -> Result<()> { } Some(Subcommand::ExportGenesisState(cmd)) => { let runner = cli.create_runner(cmd)?; - runner.sync_run(|_config| { + runner.sync_run(|mut config| { let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?; - let state_version = Cli::native_runtime_version(&spec).state_version(); + let partials = new_partial(&mut config, false)?; + let state_version = sc_chain_spec::resolve_state_version_from_wasm( + &spec.build_storage()?, + partials.client.executor(), + )?; cmd.run::(&*spec, state_version) }) } diff --git a/container-chains/templates/simple/node/src/command.rs b/container-chains/templates/simple/node/src/command.rs index 50b276024..9936b7547 100644 --- a/container-chains/templates/simple/node/src/command.rs +++ b/container-chains/templates/simple/node/src/command.rs @@ -30,6 +30,7 @@ use { ChainSpec, CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams, NetworkParams, Result, RuntimeVersion, SharedParams, SubstrateCli, }, + sc_client_api::ExecutorProvider, sc_service::config::{BasePath, PrometheusConfig}, sp_core::hexdisplay::HexDisplay, sp_runtime::traits::{AccountIdConversion, Block as BlockT}, @@ -217,9 +218,13 @@ pub fn run() -> Result<()> { } Some(Subcommand::ExportGenesisState(cmd)) => { let runner = cli.create_runner(cmd)?; - runner.sync_run(|_config| { + 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(); + let partials = new_partial(&config)?; + let state_version = sc_chain_spec::resolve_state_version_from_wasm( + &spec.build_storage()?, + partials.client.executor(), + )?; cmd.run::(&*spec, state_version) }) } diff --git a/node/src/cli.rs b/node/src/cli.rs index a24df14ff..bb7cdf7cc 100644 --- a/node/src/cli.rs +++ b/node/src/cli.rs @@ -16,11 +16,15 @@ use { crate::{chain_spec::RawGenesisConfig, service::Sealing}, + cumulus_client_cli::generate_genesis_block, pallet_registrar_runtime_api::ContainerChainGenesisData, - sc_cli::{CliConfiguration, NodeKeyParams, SharedParams}, + parity_scale_codec::Encode, + sc_cli::{ChainSpec, CliConfiguration, NodeKeyParams, SharedParams}, + sc_client_api::ExecutorProvider, sc_network::config::MultiaddrWithPeerId, - sp_runtime::traits::Get, - std::{collections::BTreeMap, path::PathBuf}, + sp_core::hexdisplay::HexDisplay, + sp_runtime::traits::{Block as BlockT, Get}, + std::{collections::BTreeMap, fs, io, io::Write, path::PathBuf}, tp_container_chain_genesis_data::json::properties_to_map, }; @@ -50,6 +54,7 @@ pub enum Subcommand { PurgeChain(cumulus_client_cli::PurgeChainCmd), /// Export the genesis state of the parachain. + // TODO: use cumulus_client_cli::ExportGenesisStateCommand after 1.0.0 upgrade ExportGenesisState(ExportGenesisStateCommand), /// Export the genesis wasm of the parachain. @@ -119,6 +124,46 @@ pub struct ExportGenesisStateCommand { /// The name of the chain for that the genesis state should be exported. #[arg(long)] pub chain: Option, + + #[allow(missing_docs)] + #[command(flatten)] + pub shared_params: sc_cli::SharedParams, +} + +impl ExportGenesisStateCommand { + /// Run the export-genesis-state command + pub fn run( + &self, + chain_spec: &dyn ChainSpec, + client: &impl ExecutorProvider, + ) -> sc_cli::Result<()> { + let state_version = sc_chain_spec::resolve_state_version_from_wasm( + &chain_spec.build_storage()?, + client.executor(), + )?; + + let block: Block = generate_genesis_block(chain_spec, state_version)?; + let raw_header = block.header().encode(); + let output_buf = if self.raw { + raw_header + } else { + format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes() + }; + + if let Some(output) = &self.output { + fs::write(output, output_buf)?; + } else { + io::stdout().write_all(&output_buf)?; + } + + Ok(()) + } +} + +impl sc_cli::CliConfiguration for ExportGenesisStateCommand { + fn shared_params(&self) -> &sc_cli::SharedParams { + &self.shared_params + } } /// Command for exporting the genesis wasm file. diff --git a/node/src/command.rs b/node/src/command.rs index 353e75dc9..8a58ea801 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -337,36 +337,18 @@ pub fn run() -> Result<()> { cmd.run(config, polkadot_config) }) } - Some(Subcommand::ExportGenesisState(params)) => { - let mut builder = sc_cli::LoggerBuilder::new(""); - builder.with_profiling(sc_tracing::TracingReceiver::Log, ""); - let _ = builder.init(); - - // Cumulus approach here, we directly call the generic load_spec func - let chain_spec = load_spec( - ¶ms.chain.clone().unwrap_or_default(), - params.parachain_id.unwrap_or(1000).into(), - )?; - let state_version = Cli::native_runtime_version(&chain_spec).state_version(); - - let output_buf = { - let block: Block = generate_genesis_block(&*chain_spec, state_version)?; - let raw_header = block.header().encode(); - - 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)) => { + let runner = cli.create_runner(cmd)?; + runner.sync_run(|config| { + let partials = new_partial(&config)?; + // Cumulus approach here, we directly call the generic load_spec func + let chain_spec = load_spec( + &cmd.chain.clone().unwrap_or_default(), + cmd.parachain_id.unwrap_or(1000).into(), + )?; + + cmd.run(&*chain_spec, &*partials.client) + }) } Some(Subcommand::ExportGenesisWasm(params)) => { let mut builder = sc_cli::LoggerBuilder::new("");