Skip to content

Commit

Permalink
Resolve state version from chain spec instead of assuming default (#247)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tmpolaczyk authored Sep 6, 2023
1 parent ceed65a commit 17b892d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 37 deletions.
9 changes: 7 additions & 2 deletions container-chains/templates/frontier/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<Block>(&*spec, state_version)
})
}
Expand Down
9 changes: 7 additions & 2 deletions container-chains/templates/simple/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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::<Block>(&*spec, state_version)
})
}
Expand Down
51 changes: 48 additions & 3 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<String>,

#[allow(missing_docs)]
#[command(flatten)]
pub shared_params: sc_cli::SharedParams,
}

impl ExportGenesisStateCommand {
/// Run the export-genesis-state command
pub fn run<Block: BlockT>(
&self,
chain_spec: &dyn ChainSpec,
client: &impl ExecutorProvider<Block>,
) -> 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.
Expand Down
42 changes: 12 additions & 30 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
&params.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) = &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 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("");
Expand Down

0 comments on commit 17b892d

Please sign in to comment.