Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve state_version from WASM in export-genesis-state subcommand #3164

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sc-chain-spec = { workspace = true }
sc-cli = { workspace = true }
sc-client-api = { workspace = true }
sc-consensus-grandpa = { workspace = true }
sc-executor = { workspace = true }
sc-network = { workspace = true }
sc-service = { workspace = true }
sc-sysinfo = { workspace = true }
Expand Down
22 changes: 1 addition & 21 deletions node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn validate_url(arg: &str) -> Result<Url, String> {
pub enum Subcommand {
/// Export the genesis state of the parachain.
#[clap(name = "export-genesis-state")]
ExportGenesisHead(ExportGenesisHeadCommand),
ExportGenesisHead(cumulus_client_cli::ExportGenesisHeadCommand),

/// Export the genesis wasm of the parachain.
#[clap(name = "export-genesis-wasm")]
Expand Down Expand Up @@ -108,26 +108,6 @@ pub struct BuildSpecCommand {
pub mnemonic: Option<String>,
}

/// Command for exporting the genesis state of the parachain
#[derive(Debug, Parser)]
pub struct ExportGenesisHeadCommand {
/// Output file name or stdout if unspecified.
#[clap(value_parser)]
pub output: Option<PathBuf>,

/// Id of the parachain this state is for.
#[clap(long)]
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)]
pub chain: Option<String>,
}

/// Command for exporting the genesis wasm file.
#[derive(Debug, Parser)]
pub struct ExportGenesisWasmCommand {
Expand Down
52 changes: 23 additions & 29 deletions node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ use moonbeam_service::moonbeam_runtime;
#[cfg(feature = "moonriver-native")]
use moonbeam_service::moonriver_runtime;

use moonbeam_service::{chain_spec, frontier_database_dir, HostFunctions, IdentifyVariant};
use moonbeam_service::{chain_spec, frontier_database_dir, Block, HostFunctions, IdentifyVariant};
use parity_scale_codec::Encode;
#[cfg(feature = "westend-native")]
use polkadot_service::WestendChainSpec;
use sc_cli::{
ChainSpec, CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams,
NetworkParams, Result, RpcEndpoint, RuntimeVersion, SharedParams, SubstrateCli,
NetworkParams, Result, RpcEndpoint, SharedParams, SubstrateCli,
};
use sc_executor::WasmtimeInstantiationStrategy;
use sc_service::{
config::{BasePath, PrometheusConfig},
DatabaseSource, PartialComponents,
Expand Down Expand Up @@ -148,21 +149,6 @@ impl SubstrateCli for Cli {
}
}

impl Cli {
fn runtime_version(spec: &Box<dyn sc_service::ChainSpec>) -> &'static RuntimeVersion {
match spec {
#[cfg(feature = "moonriver-native")]
spec if spec.is_moonriver() => return &moonbeam_service::moonriver_runtime::VERSION,
#[cfg(feature = "moonbeam-native")]
spec if spec.is_moonbeam() => return &moonbeam_service::moonbeam_runtime::VERSION,
#[cfg(feature = "moonbase-native")]
_ => return &moonbeam_service::moonbase_runtime::VERSION,
#[cfg(not(feature = "moonbase-native"))]
_ => panic!("invalid chain spec"),
}
}
}

impl SubstrateCli for RelayChainCli {
fn impl_name() -> String {
"Moonbeam Parachain Collator".into()
Expand Down Expand Up @@ -418,26 +404,34 @@ pub fn run() -> Result<()> {
_ => panic!("invalid chain spec"),
}
}
Some(Subcommand::ExportGenesisHead(params)) => {
Some(Subcommand::ExportGenesisHead(cmd)) => {
let runner = cli.create_runner(cmd)?;
let chain_spec = runner.config().chain_spec.cloned_box();

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.as_deref().unwrap_or_default(),
params.parachain_id.unwrap_or(1000).into(),
&cli.run,
)?;
let state_version = Cli::runtime_version(&chain_spec).state_version();
let storage = chain_spec.build_storage()?;
let executor = sc_executor::WasmExecutor::<HostFunctions>::builder()
.with_execution_method(sc_executor::WasmExecutionMethod::Compiled {
instantiation_strategy: WasmtimeInstantiationStrategy::PoolingCopyOnWrite,
})
.with_max_runtime_instances(runner.config().executor.max_runtime_instances)
.build();

let state_version = sc_chain_spec::resolve_state_version_from_wasm::<
_,
HashingFor<Block>,
>(&storage, &executor)?;
Comment on lines +423 to +426
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running moonbeam export-genesis-state --chain moonriver, the expected state_version is StateVersion::V1, but it returns StateVersion::V0.

I think the issue is on the executor.


let output_buf = match chain_spec {
#[cfg(feature = "moonriver-native")]
chain_spec if chain_spec.is_moonriver() => {
let block: moonbeam_service::moonriver_runtime::Block =
generate_genesis_block(&*chain_spec, state_version)?;
let raw_header = block.header().encode();
let output_buf = if params.raw {
let output_buf = if cmd.raw {
raw_header
} else {
format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
Expand All @@ -449,7 +443,7 @@ pub fn run() -> Result<()> {
let block: moonbeam_service::moonbeam_runtime::Block =
generate_genesis_block(&*chain_spec, state_version)?;
let raw_header = block.header().encode();
let output_buf = if params.raw {
let output_buf = if cmd.raw {
raw_header
} else {
format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
Expand All @@ -461,7 +455,7 @@ pub fn run() -> Result<()> {
let block: moonbeam_service::moonbase_runtime::Block =
generate_genesis_block(&*chain_spec, state_version)?;
let raw_header = block.header().encode();
let output_buf = if params.raw {
let output_buf = if cmd.raw {
raw_header
} else {
format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
Expand All @@ -472,7 +466,7 @@ pub fn run() -> Result<()> {
_ => panic!("invalid chain spec"),
};

if let Some(output) = &params.output {
if let Some(output) = &cmd.output {
std::fs::write(output, output_buf)?;
} else {
std::io::stdout().write_all(&output_buf)?;
Expand Down
1 change: 0 additions & 1 deletion scripts/generate-parachain-specs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ $MOONBEAM_BINARY export-genesis-wasm \
echo $ALPHANET_WASM generated

$MOONBEAM_BINARY export-genesis-state \
--parachain-id $ALPHANET_PARACHAIN_ID \
--chain $ALPHANET_PARACHAIN_SPEC_RAW \
> $ALPHANET_GENESIS;
echo $ALPHANET_GENESIS generated
Expand Down
Loading