git.delta.rocks / unique-network / refs/commits / e1980179e647

difftreelog

refactor(node) use export-genesis state from cumulus

Yaroslav Bolyukin2022-07-21parent: #26734e9.patch.diff
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

modifiednode/cli/src/cli.rsdiffbeforeafterboth
24#[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),
2928
30 /// 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),
3331
34 /// 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}
62
63/// Command for exporting the genesis state of the parachain
64#[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>,
69
70 /// Id of the parachain this state is for.
71 ///
72 /// Default: 100
73 #[clap(long, conflicts_with = "chain")]
74 pub parachain_id: Option<u32>,
75
76 /// Write output in binary. Default is to write in hex.
77 #[clap(short, long)]
78 pub raw: bool,
79
80 /// 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}
84
85/// 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>,
91
92 /// Write output in binary. Default is to write in hex.
93 #[clap(short, long)]
94 pub raw: bool,
95
96 /// The name of the chain for that the genesis wasm file should be exported.
97 #[clap(long)]
98 pub chain: Option<String>,
99}
10060
101#[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)]
modifiednode/cli/src/command.rsdiffbeforeafterboth
5050
51use 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};
6666
67use up_common::types::opaque::Block;67use up_common::types::opaque::Block;
6868
191 }191 }
192}192}
193
194#[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()?;
197
198 storage
199 .top
200 .remove(sp_core::storage::well_known_keys::CODE)
201 .ok_or_else(|| "Could not find wasm file in genesis state!".into())
202}
203193
204macro_rules! async_run_with_runtime {194macro_rules! async_run_with_runtime {
205 (195 (
248 }}238 }}
249}239}
240
241macro_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}
252
253macro_rules! construct_sync_run {
254 (|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{
255 let runner = $cli.create_runner($cmd)?;
256
257 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 ),
263
264 #[cfg(feature = "quartz-runtime")]
265 RuntimeId::Quartz => sync_run_with_runtime!(
266 quartz_runtime::RuntimeApi, QuartzRuntimeExecutor,
267 runner, $components, $cli, $cmd, $config, $( $code )*
268 ),
269
270 RuntimeId::Opal => sync_run_with_runtime!(
271 opal_runtime::RuntimeApi, OpalRuntimeExecutor,
272 runner, $components, $cli, $cmd, $config, $( $code )*
273 ),
274
275 RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into())
276 }
277 }}
278}
250279
251macro_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())?;
336
337 let spec = load_spec(&params.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_header
343 } else {
344 format!("0x{:?}", HexDisplay::from(&block.header().encode())).into_bytes()
345 };
346
347 if let Some(output) = &params.output {
348 std::fs::write(output, output_buf)?;
349 } else {
350 std::io::stdout().write_all(&output_buf)?;
351 }
352
353 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();
359
360 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(&params.chain.clone().unwrap_or_default())?)?;371 cmd.run(&*spec)
362 let output_buf = if params.raw {372 })
363 raw_wasm_blob
364 } else {
365 format!("0x{:?}", HexDisplay::from(&raw_wasm_blob)).into_bytes()
366 };
367
368 if let Some(output) = &params.output {
369 std::fs::write(output, output_buf)?;
370 } else {
371 std::io::stdout().write_all(&output_buf)?;
372 }
373
374 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};
500498
501 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));