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
before · node/cli/src/cli.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use crate::chain_spec;18use std::{path::PathBuf, env};19use clap::Parser;2021const NODE_NAME_ENV: &str = "UNIQUE_NODE_NAME";2223/// Sub-commands supported by the collator.24#[derive(Debug, Parser)]25pub enum Subcommand {26	/// Export the genesis state of the parachain.27	#[clap(name = "export-genesis-state")]28	ExportGenesisState(ExportGenesisStateCommand),2930	/// Export the genesis wasm of the parachain.31	#[clap(name = "export-genesis-wasm")]32	ExportGenesisWasm(ExportGenesisWasmCommand),3334	/// Build a chain specification.35	BuildSpec(sc_cli::BuildSpecCmd),3637	/// Validate blocks.38	CheckBlock(sc_cli::CheckBlockCmd),3940	/// Export blocks.41	ExportBlocks(sc_cli::ExportBlocksCmd),4243	/// Export the state of a given block into a chain spec.44	ExportState(sc_cli::ExportStateCmd),4546	/// Import blocks.47	ImportBlocks(sc_cli::ImportBlocksCmd),4849	/// Remove the whole chain.50	PurgeChain(cumulus_client_cli::PurgeChainCmd),5152	/// Revert the chain to a previous state.53	Revert(sc_cli::RevertCmd),5455	/// The custom benchmark subcommmand benchmarking runtime pallets.56	#[clap(subcommand)]57	Benchmark(frame_benchmarking_cli::BenchmarkCmd),5859	/// Try runtime60	TryRuntime(try_runtime_cli::TryRuntimeCmd),61}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}100101#[derive(Debug, Parser)]102#[clap(args_conflicts_with_subcommands = true, subcommand_negates_reqs = true)]103pub struct Cli {104	#[structopt(subcommand)]105	pub subcommand: Option<Subcommand>,106107	#[structopt(flatten)]108	pub run: cumulus_client_cli::RunCmd,109110	/// When running the node in the `--dev` mode and111	/// there is no transaction in the transaction pool,112	/// an empty block will be sealed automatically113	/// after the `--idle-autoseal-interval` milliseconds.114	///115	/// The default interval is 500 milliseconds116	#[structopt(default_value = "500", long)]117	pub idle_autoseal_interval: u64,118119	/// Disable automatic hardware benchmarks.120	///121	/// By default these benchmarks are automatically ran at startup and measure122	/// the CPU speed, the memory bandwidth and the disk speed.123	///124	/// The results are then printed out in the logs, and also sent as part of125	/// telemetry, if telemetry is enabled.126	#[clap(long)]127	pub no_hardware_benchmarks: bool,128129	/// Relaychain arguments130	#[structopt(raw = true)]131	pub relaychain_args: Vec<String>,132}133134impl Cli {135	pub fn node_name() -> String {136		match env::var(NODE_NAME_ENV).ok() {137			Some(name) => name,138			None => {139				if cfg!(feature = "unique-runtime") {140					"Unique"141				} else if cfg!(feature = "quartz-runtime") {142					"Quartz"143				} else {144					"Opal"145				}146			}147			.into(),148		}149	}150}151152#[derive(Debug)]153pub struct RelayChainCli {154	/// The actual relay chain cli object.155	pub base: polkadot_cli::RunCmd,156157	/// Optional chain id that should be passed to the relay chain.158	pub chain_id: Option<String>,159160	/// The base path that should be used by the relay chain.161	pub base_path: Option<PathBuf>,162}163164impl RelayChainCli {165	/// Parse the relay chain CLI parameters using the para chain `Configuration`.166	pub fn new<'a>(167		para_config: &sc_service::Configuration,168		relay_chain_args: impl Iterator<Item = &'a String>,169	) -> Self {170		let extension = chain_spec::Extensions::try_get(&*para_config.chain_spec);171		let chain_id = extension.map(|e| e.relay_chain.clone());172		let base_path = para_config173			.base_path174			.as_ref()175			.map(|x| x.path().join("polkadot"));176		Self {177			base_path,178			chain_id,179			base: polkadot_cli::RunCmd::parse_from(relay_chain_args),180		}181	}182}
after · node/cli/src/cli.rs
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use crate::chain_spec;18use std::{path::PathBuf, env};19use clap::Parser;2021const NODE_NAME_ENV: &str = "UNIQUE_NODE_NAME";2223/// Sub-commands supported by the collator.24#[derive(Debug, Parser)]25pub enum Subcommand {26	/// Export the genesis state of the parachain.27	ExportGenesisState(cumulus_client_cli::ExportGenesisStateCommand),2829	/// Export the genesis wasm of the parachain.30	ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),3132	/// Build a chain specification.33	BuildSpec(sc_cli::BuildSpecCmd),3435	/// Validate blocks.36	CheckBlock(sc_cli::CheckBlockCmd),3738	/// Export blocks.39	ExportBlocks(sc_cli::ExportBlocksCmd),4041	/// Export the state of a given block into a chain spec.42	ExportState(sc_cli::ExportStateCmd),4344	/// Import blocks.45	ImportBlocks(sc_cli::ImportBlocksCmd),4647	/// Remove the whole chain.48	PurgeChain(cumulus_client_cli::PurgeChainCmd),4950	/// Revert the chain to a previous state.51	Revert(sc_cli::RevertCmd),5253	/// The custom benchmark subcommmand benchmarking runtime pallets.54	#[clap(subcommand)]55	Benchmark(frame_benchmarking_cli::BenchmarkCmd),5657	/// Try runtime58	TryRuntime(try_runtime_cli::TryRuntimeCmd),59}6061#[derive(Debug, Parser)]62#[clap(args_conflicts_with_subcommands = true, subcommand_negates_reqs = true)]63pub struct Cli {64	#[structopt(subcommand)]65	pub subcommand: Option<Subcommand>,6667	#[structopt(flatten)]68	pub run: cumulus_client_cli::RunCmd,6970	/// When running the node in the `--dev` mode and71	/// there is no transaction in the transaction pool,72	/// an empty block will be sealed automatically73	/// after the `--idle-autoseal-interval` milliseconds.74	///75	/// The default interval is 500 milliseconds76	#[structopt(default_value = "500", long)]77	pub idle_autoseal_interval: u64,7879	/// Disable automatic hardware benchmarks.80	///81	/// By default these benchmarks are automatically ran at startup and measure82	/// the CPU speed, the memory bandwidth and the disk speed.83	///84	/// The results are then printed out in the logs, and also sent as part of85	/// telemetry, if telemetry is enabled.86	#[clap(long)]87	pub no_hardware_benchmarks: bool,8889	/// Relaychain arguments90	#[structopt(raw = true)]91	pub relaychain_args: Vec<String>,92}9394impl Cli {95	pub fn node_name() -> String {96		match env::var(NODE_NAME_ENV).ok() {97			Some(name) => name,98			None => {99				if cfg!(feature = "unique-runtime") {100					"Unique"101				} else if cfg!(feature = "quartz-runtime") {102					"Quartz"103				} else {104					"Opal"105				}106			}107			.into(),108		}109	}110}111112#[derive(Debug)]113pub struct RelayChainCli {114	/// The actual relay chain cli object.115	pub base: polkadot_cli::RunCmd,116117	/// Optional chain id that should be passed to the relay chain.118	pub chain_id: Option<String>,119120	/// The base path that should be used by the relay chain.121	pub base_path: Option<PathBuf>,122}123124impl RelayChainCli {125	/// Parse the relay chain CLI parameters using the para chain `Configuration`.126	pub fn new<'a>(127		para_config: &sc_service::Configuration,128		relay_chain_args: impl Iterator<Item = &'a String>,129	) -> Self {130		let extension = chain_spec::Extensions::try_get(&*para_config.chain_spec);131		let chain_id = extension.map(|e| e.relay_chain.clone());132		let base_path = para_config133			.base_path134			.as_ref()135			.map(|x| x.path().join("polkadot"));136		Self {137			base_path,138			chain_id,139			base: polkadot_cli::RunCmd::parse_from(relay_chain_args),140		}141	}142}
modifiednode/cli/src/command.rsdiffbeforeafterboth
--- a/node/cli/src/command.rs
+++ b/node/cli/src/command.rs
@@ -50,7 +50,7 @@
 
 use codec::Encode;
 use cumulus_primitives_core::ParaId;
-use cumulus_client_service::genesis::generate_genesis_block;
+use cumulus_client_cli::generate_genesis_block;
 use std::{future::Future, pin::Pin};
 use log::info;
 use sc_cli::{
@@ -62,7 +62,7 @@
 };
 use sp_core::hexdisplay::HexDisplay;
 use sp_runtime::traits::{AccountIdConversion, Block as BlockT};
-use std::{io::Write, net::SocketAddr, time::Duration};
+use std::{net::SocketAddr, time::Duration};
 
 use up_common::types::opaque::Block;
 
@@ -189,16 +189,6 @@
 	fn native_runtime_version(chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
 		polkadot_cli::Cli::native_runtime_version(chain_spec)
 	}
-}
-
-#[allow(clippy::borrowed_box)]
-fn extract_genesis_wasm(chain_spec: &Box<dyn sc_service::ChainSpec>) -> Result<Vec<u8>> {
-	let mut storage = chain_spec.build_storage()?;
-
-	storage
-		.top
-		.remove(sp_core::storage::well_known_keys::CODE)
-		.ok_or_else(|| "Could not find wasm file in genesis state!".into())
 }
 
 macro_rules! async_run_with_runtime {
@@ -248,6 +238,45 @@
 	}}
 }
 
+macro_rules! sync_run_with_runtime {
+	(
+		$runtime_api:path, $executor:path,
+		$runner:ident, $components:ident, $cli:ident, $cmd:ident, $config:ident,
+		$( $code:tt )*
+	) => {
+		$runner.sync_run(|$config| {
+			$( $code )*
+		})
+	};
+}
+
+macro_rules! construct_sync_run {
+	(|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{
+		let runner = $cli.create_runner($cmd)?;
+
+		match runner.config().chain_spec.runtime_id() {
+			#[cfg(feature = "unique-runtime")]
+			RuntimeId::Unique => sync_run_with_runtime!(
+				unique_runtime::RuntimeApi, UniqueRuntimeExecutor,
+				runner, $components, $cli, $cmd, $config, $( $code )*
+			),
+
+			#[cfg(feature = "quartz-runtime")]
+			RuntimeId::Quartz => sync_run_with_runtime!(
+				quartz_runtime::RuntimeApi, QuartzRuntimeExecutor,
+				runner, $components, $cli, $cmd, $config, $( $code )*
+			),
+
+			RuntimeId::Opal => sync_run_with_runtime!(
+				opal_runtime::RuntimeApi, OpalRuntimeExecutor,
+				runner, $components, $cli, $cmd, $config, $( $code )*
+			),
+
+			RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into())
+		}
+	}}
+}
+
 macro_rules! start_node_using_chain_runtime {
 	($start_node_fn:ident($config:expr $(, $($args:expr),+)?) $($code:tt)*) => {
 		match $config.chain_spec.runtime_id() {
@@ -329,49 +358,18 @@
 		Some(Subcommand::Revert(cmd)) => construct_async_run!(|components, cli, cmd, config| {
 			Ok(cmd.run(components.client, components.backend, None))
 		}),
-		Some(Subcommand::ExportGenesisState(params)) => {
-			let mut builder = sc_cli::LoggerBuilder::new("");
-			builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
-			let _ = builder.init();
-
-			let spec = load_spec(&params.chain.clone().unwrap_or_default())?;
-			let state_version = Cli::native_runtime_version(&spec).state_version();
-			let block: Block = generate_genesis_block(&spec, state_version)?;
-			let raw_header = block.header().encode();
-			let output_buf = 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)) => {
+			construct_sync_run!(|components, cli, cmd, _config| {
+				let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;
+				let state_version = Cli::native_runtime_version(&spec).state_version();
+				cmd.run::<Block>(&*spec, state_version)
+			})
 		}
-		Some(Subcommand::ExportGenesisWasm(params)) => {
-			let mut builder = sc_cli::LoggerBuilder::new("");
-			builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
-			let _ = builder.init();
-
-			let raw_wasm_blob =
-				extract_genesis_wasm(&cli.load_spec(&params.chain.clone().unwrap_or_default())?)?;
-			let output_buf = if params.raw {
-				raw_wasm_blob
-			} else {
-				format!("0x{:?}", HexDisplay::from(&raw_wasm_blob)).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::ExportGenesisWasm(cmd)) => {
+			construct_sync_run!(|components, cli, cmd, _config| {
+				let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;
+				cmd.run(&*spec)
+			})
 		}
 		Some(Subcommand::Benchmark(cmd)) => {
 			use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE};
@@ -500,7 +498,7 @@
 
 				let state_version =
 					RelayChainCli::native_runtime_version(&config.chain_spec).state_version();
-				let block: Block = generate_genesis_block(&config.chain_spec, state_version)
+				let block: Block = generate_genesis_block(&*config.chain_spec, state_version)
 					.map_err(|e| format!("{:?}", e))?;
 				let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
 				let genesis_hash = format!("0x{:?}", HexDisplay::from(&block.header().hash().0));