Move ExportGenesisStateCommand and ExportGenesisWasmCommand to cumulus-client-cli (#1325)

* Move ExportGenesisStateCommand and ExportGenesisWasmCommand to cumulus-client-cli

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Remove useless

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Use shared_params.chain

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
Qinxuan Chen
2022-06-18 21:33:52 +08:00
committed by GitHub
parent e5810bb5fd
commit 7d0ae5ea3a
14 changed files with 332 additions and 434 deletions
Generated
+4 -2
View File
@@ -1575,8 +1575,12 @@ name = "cumulus-client-cli"
version = "0.1.0"
dependencies = [
"clap 3.2.5",
"parity-scale-codec",
"sc-chain-spec",
"sc-cli",
"sc-service",
"sp-core",
"sp-runtime",
"url",
]
@@ -1758,11 +1762,9 @@ dependencies = [
"cumulus-client-pov-recovery",
"cumulus-primitives-core",
"cumulus-relay-chain-interface",
"parity-scale-codec",
"parking_lot 0.12.1",
"polkadot-overseer",
"polkadot-primitives",
"sc-chain-spec",
"sc-client-api",
"sc-consensus",
"sc-consensus-babe",
+5 -1
View File
@@ -6,8 +6,12 @@ edition = "2021"
[dependencies]
clap = { version = "3.2.5", features = ["derive", "deprecated"] }
codec = { package = "parity-scale-codec", version = "3.0.0" }
url = "2.2.2"
# Substrate
sc-cli = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-chain-spec = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "master" }
url = "2.2.2"
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
+164 -22
View File
@@ -18,20 +18,28 @@
#![warn(missing_docs)]
use clap::Parser;
use sc_service::{
config::{PrometheusConfig, TelemetryEndpoints},
BasePath, TransactionPoolOptions,
};
use std::{
fs,
io::{self, Write},
net::SocketAddr,
path::PathBuf,
};
use codec::Encode;
use sc_chain_spec::ChainSpec;
use sc_service::{
config::{PrometheusConfig, TelemetryEndpoints},
BasePath, TransactionPoolOptions,
};
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::{
traits::{Block as BlockT, Hash as HashT, Header as HeaderT, Zero},
StateVersion,
};
use url::Url;
/// The `purge-chain` command used to remove the whole chain: the parachain and the relay chain.
#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
pub struct PurgeChainCmd {
/// The base struct of the purge-chain command.
#[clap(flatten)]
@@ -119,6 +127,140 @@ impl sc_cli::CliConfiguration for PurgeChainCmd {
}
}
/// Command for exporting the genesis state of the parachain
#[derive(Debug, clap::Parser)]
pub struct ExportGenesisStateCommand {
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,
/// Write output in binary. Default is to write in hex.
#[clap(short, long)]
pub raw: bool,
#[allow(missing_docs)]
#[clap(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,
genesis_state_version: StateVersion,
) -> sc_cli::Result<()> {
let block: Block = generate_genesis_block(chain_spec, genesis_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(())
}
}
/// Generate the genesis block from a given ChainSpec.
pub fn generate_genesis_block<Block: BlockT>(
chain_spec: &dyn ChainSpec,
genesis_state_version: StateVersion,
) -> Result<Block, String> {
let storage = chain_spec.build_storage()?;
let child_roots = storage.children_default.iter().map(|(sk, child_content)| {
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_content.data.clone().into_iter().collect(),
genesis_state_version,
);
(sk.clone(), state_root.encode())
});
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.top.clone().into_iter().chain(child_roots).collect(),
genesis_state_version,
);
let extrinsics_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
Vec::new(),
sp_runtime::StateVersion::V0,
);
Ok(Block::new(
<<Block as BlockT>::Header as HeaderT>::new(
Zero::zero(),
extrinsics_root,
state_root,
Default::default(),
Default::default(),
),
Default::default(),
))
}
impl sc_cli::CliConfiguration for ExportGenesisStateCommand {
fn shared_params(&self) -> &sc_cli::SharedParams {
&self.shared_params
}
}
/// Command for exporting the genesis wasm file.
#[derive(Debug, clap::Parser)]
pub struct ExportGenesisWasmCommand {
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,
/// Write output in binary. Default is to write in hex.
#[clap(short, long)]
pub raw: bool,
#[allow(missing_docs)]
#[clap(flatten)]
pub shared_params: sc_cli::SharedParams,
}
impl ExportGenesisWasmCommand {
/// Run the export-genesis-state command
pub fn run(&self, chain_spec: &dyn ChainSpec) -> sc_cli::Result<()> {
let raw_wasm_blob = extract_genesis_wasm(chain_spec)?;
let output_buf = if self.raw {
raw_wasm_blob
} else {
format!("0x{:?}", HexDisplay::from(&raw_wasm_blob)).into_bytes()
};
if let Some(output) = &self.output {
fs::write(output, output_buf)?;
} else {
io::stdout().write_all(&output_buf)?;
}
Ok(())
}
}
/// Extract the genesis code from a given ChainSpec.
pub fn extract_genesis_wasm(chain_spec: &dyn ChainSpec) -> sc_cli::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())
}
impl sc_cli::CliConfiguration for ExportGenesisWasmCommand {
fn shared_params(&self) -> &sc_cli::SharedParams {
&self.shared_params
}
}
fn validate_relay_chain_url(arg: &str) -> Result<Url, String> {
let url = Url::parse(arg).map_err(|e| e.to_string())?;
@@ -133,7 +275,7 @@ fn validate_relay_chain_url(arg: &str) -> Result<Url, String> {
}
/// The `run` command used to run a node.
#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
pub struct RunCmd {
/// The cumulus RunCmd inherents from sc_cli's
#[clap(flatten)]
@@ -154,21 +296,6 @@ pub struct RunCmd {
pub relay_chain_rpc_url: Option<Url>,
}
/// Options only relevant for collator nodes
#[derive(Clone, Debug)]
pub struct CollatorOptions {
/// Location of relay chain full node
pub relay_chain_rpc_url: Option<Url>,
}
/// A non-redundant version of the `RunCmd` that sets the `validator` field when the
/// original `RunCmd` had the `collator` field.
/// This is how we make `--collator` imply `--validator`.
pub struct NormalizedRunCmd {
/// The cumulus RunCmd inherents from sc_cli's
pub base: sc_cli::RunCmd,
}
impl RunCmd {
/// Create a [`NormalizedRunCmd`] which merges the `collator` cli argument into `validator` to have only one.
pub fn normalize(&self) -> NormalizedRunCmd {
@@ -185,6 +312,21 @@ impl RunCmd {
}
}
/// Options only relevant for collator nodes
#[derive(Clone, Debug)]
pub struct CollatorOptions {
/// Location of relay chain full node
pub relay_chain_rpc_url: Option<Url>,
}
/// A non-redundant version of the `RunCmd` that sets the `validator` field when the
/// original `RunCmd` had the `collator` field.
/// This is how we make `--collator` imply `--validator`.
pub struct NormalizedRunCmd {
/// The cumulus RunCmd inherents from sc_cli's
pub base: sc_cli::RunCmd,
}
impl sc_cli::CliConfiguration for NormalizedRunCmd {
fn shared_params(&self) -> &sc_cli::SharedParams {
self.base.shared_params()
-2
View File
@@ -5,12 +5,10 @@ authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0" }
parking_lot = "0.12.1"
tracing = "0.1.34"
# Substrate
sc-chain-spec = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" }
-55
View File
@@ -1,55 +0,0 @@
// Copyright 2020-2021 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.
// Cumulus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Cumulus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use codec::Encode;
use sc_chain_spec::ChainSpec;
use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, Zero};
/// Generate the genesis block from a given ChainSpec.
pub fn generate_genesis_block<Block: BlockT>(
chain_spec: &Box<dyn ChainSpec>,
genesis_state_version: sp_runtime::StateVersion,
) -> Result<Block, String> {
let storage = chain_spec.build_storage()?;
let child_roots = storage.children_default.iter().map(|(sk, child_content)| {
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_content.data.clone().into_iter().collect(),
genesis_state_version,
);
(sk.clone(), state_root.encode())
});
let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.top.clone().into_iter().chain(child_roots).collect(),
genesis_state_version,
);
let extrinsics_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
Vec::new(),
sp_runtime::StateVersion::V0,
);
Ok(Block::new(
<<Block as BlockT>::Header as HeaderT>::new(
Zero::zero(),
extrinsics_root,
state_root,
Default::default(),
Default::default(),
),
Default::default(),
))
}
-2
View File
@@ -41,8 +41,6 @@ use sp_runtime::{
};
use std::{sync::Arc, time::Duration};
pub mod genesis;
/// Parameters given to [`start_collator`].
pub struct StartCollatorParams<'a, Block: BlockT, BS, Client, RCInterface, Spawner, IQ> {
pub block_status: Arc<BS>,
+11 -47
View File
@@ -1,18 +1,8 @@
use crate::chain_spec;
use clap::Parser;
use std::path::PathBuf;
/// Sub-commands supported by the collator.
#[derive(Debug, clap::Subcommand)]
pub enum Subcommand {
/// Export the genesis state of the parachain.
#[clap(name = "export-genesis-state")]
ExportGenesisState(ExportGenesisStateCommand),
/// Export the genesis wasm of the parachain.
#[clap(name = "export-genesis-wasm")]
ExportGenesisWasm(ExportGenesisWasmCommand),
/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),
@@ -28,11 +18,17 @@ pub enum Subcommand {
/// Import blocks.
ImportBlocks(sc_cli::ImportBlocksCmd),
/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),
/// Remove the whole chain.
PurgeChain(cumulus_client_cli::PurgeChainCmd),
/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),
/// Export the genesis state of the parachain.
ExportGenesisState(cumulus_client_cli::ExportGenesisStateCommand),
/// Export the genesis wasm of the parachain.
ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),
/// Sub-commands concerned with benchmarking.
/// The pallet benchmarking moved to the `pallet` sub-command.
@@ -43,39 +39,7 @@ pub enum Subcommand {
TryRuntime(try_runtime_cli::TryRuntimeCmd),
}
/// Command for exporting the genesis state of the parachain
#[derive(Debug, Parser)]
pub struct ExportGenesisStateCommand {
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,
/// 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 {
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,
/// Write output in binary. Default is to write in hex.
#[clap(short, long, action)]
pub raw: bool,
/// The name of the chain for that the genesis wasm file should be exported.
#[clap(long, action)]
pub chain: Option<String>,
}
#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
#[clap(
propagate_version = true,
args_conflicts_with_subcommands = true,
@@ -121,9 +85,9 @@ impl RelayChainCli {
para_config: &sc_service::Configuration,
relay_chain_args: impl Iterator<Item = &'a String>,
) -> Self {
let extension = chain_spec::Extensions::try_get(&*para_config.chain_spec);
let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
let chain_id = extension.map(|e| e.relay_chain.clone());
let base_path = para_config.base_path.as_ref().map(|x| x.path().join("polkadot"));
Self { base_path, chain_id, base: polkadot_cli::RunCmd::parse_from(relay_chain_args) }
Self { base_path, chain_id, base: clap::Parser::parse_from(relay_chain_args) }
}
}
+28 -65
View File
@@ -1,10 +1,7 @@
use crate::{
chain_spec,
cli::{Cli, RelayChainCli, Subcommand},
service::{new_partial, TemplateRuntimeExecutor},
};
use std::net::SocketAddr;
use codec::Encode;
use cumulus_client_service::genesis::generate_genesis_block;
use cumulus_client_cli::generate_genesis_block;
use cumulus_primitives_core::ParaId;
use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE};
use log::info;
@@ -19,9 +16,14 @@ use sc_service::{
};
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::traits::{AccountIdConversion, Block as BlockT};
use std::{io::Write, net::SocketAddr};
fn load_spec(id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
use crate::{
chain_spec,
cli::{Cli, RelayChainCli, Subcommand},
service::{new_partial, TemplateRuntimeExecutor},
};
fn load_spec(id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
Ok(match id {
"dev" => Box::new(chain_spec::development_config()),
"template-rococo" => Box::new(chain_spec::local_testnet_config()),
@@ -106,16 +108,6 @@ impl SubstrateCli for RelayChainCli {
}
}
#[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! construct_async_run {
(|$components:ident, $cli:ident, $cmd:ident, $config:ident| $( $code:tt )* ) => {{
let runner = $cli.create_runner($cmd)?;
@@ -163,6 +155,11 @@ pub fn run() -> Result<()> {
Ok(cmd.run(components.client, components.import_queue))
})
},
Some(Subcommand::Revert(cmd)) => {
construct_async_run!(|components, cli, cmd, config| {
Ok(cmd.run(components.client, components.backend, None))
})
},
Some(Subcommand::PurgeChain(cmd)) => {
let runner = cli.create_runner(cmd)?;
@@ -182,54 +179,20 @@ pub fn run() -> Result<()> {
cmd.run(config, polkadot_config)
})
},
Some(Subcommand::Revert(cmd)) => {
construct_async_run!(|components, cli, cmd, config| {
Ok(cmd.run(components.client, components.backend, None))
Some(Subcommand::ExportGenesisState(cmd)) => {
let runner = cli.create_runner(cmd)?;
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();
cmd.run::<Block>(&*spec, state_version)
})
},
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::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)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|_config| {
let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;
cmd.run(&*spec)
})
},
Some(Subcommand::Benchmark(cmd)) => {
let runner = cli.create_runner(cmd)?;
@@ -311,7 +274,7 @@ pub fn run() -> Result<()> {
AccountIdConversion::<polkadot_primitives::v2::AccountId>::into_account_truncating(&id);
let state_version = Cli::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()));
+14 -51
View File
@@ -14,21 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use crate::chain_spec;
use clap::Parser;
use sc_cli;
use std::path::PathBuf;
/// Sub-commands supported by the collator.
#[derive(Debug, clap::Subcommand)]
pub enum Subcommand {
/// Export the genesis state of the parachain.
#[clap(name = "export-genesis-state")]
ExportGenesisState(ExportGenesisStateCommand),
/// Export the genesis wasm of the parachain.
#[clap(name = "export-genesis-wasm")]
ExportGenesisWasm(ExportGenesisWasmCommand),
/// Key management CLI utilities
#[clap(subcommand)]
Key(sc_cli::KeySubcommand),
/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),
@@ -45,11 +38,17 @@ pub enum Subcommand {
/// Import blocks.
ImportBlocks(sc_cli::ImportBlocksCmd),
/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),
/// Remove the whole chain.
PurgeChain(cumulus_client_cli::PurgeChainCmd),
/// Revert the chain to a previous state.
Revert(sc_cli::RevertCmd),
/// Export the genesis state of the parachain.
ExportGenesisState(cumulus_client_cli::ExportGenesisStateCommand),
/// Export the genesis wasm of the parachain.
ExportGenesisWasm(cumulus_client_cli::ExportGenesisWasmCommand),
/// Sub-commands concerned with benchmarking.
/// The pallet benchmarking moved to the `pallet` sub-command.
@@ -58,45 +57,9 @@ pub enum Subcommand {
/// Try some testing command against a specified runtime state.
TryRuntime(try_runtime_cli::TryRuntimeCmd),
/// Key management CLI utilities
#[clap(subcommand)]
Key(sc_cli::KeySubcommand),
}
/// Command for exporting the genesis state of the parachain
#[derive(Debug, Parser)]
pub struct ExportGenesisStateCommand {
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,
/// 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 {
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,
/// 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 wasm file should be exported.
#[clap(long)]
pub chain: Option<String>,
}
#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
#[clap(
propagate_version = true,
args_conflicts_with_subcommands = true,
@@ -142,9 +105,9 @@ impl RelayChainCli {
para_config: &sc_service::Configuration,
relay_chain_args: impl Iterator<Item = &'a String>,
) -> Self {
let extension = chain_spec::Extensions::try_get(&*para_config.chain_spec);
let extension = crate::chain_spec::Extensions::try_get(&*para_config.chain_spec);
let chain_id = extension.map(|e| e.relay_chain.clone());
let base_path = para_config.base_path.as_ref().map(|x| x.path().join("polkadot"));
Self { base_path, chain_id, base: polkadot_cli::RunCmd::parse_from(relay_chain_args) }
Self { base_path, chain_id, base: clap::Parser::parse_from(relay_chain_args) }
}
}
+30 -67
View File
@@ -14,16 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use crate::{
chain_spec,
cli::{Cli, RelayChainCli, Subcommand},
service::{
new_partial, Block, ShellRuntimeExecutor, StatemineRuntimeExecutor,
StatemintRuntimeExecutor, WestmintRuntimeExecutor,
},
};
use std::net::SocketAddr;
use codec::Encode;
use cumulus_client_service::genesis::generate_genesis_block;
use cumulus_client_cli::generate_genesis_block;
use cumulus_primitives_core::ParaId;
use frame_benchmarking_cli::{BenchmarkCmd, SUBSTRATE_REFERENCE_HARDWARE};
use log::info;
@@ -38,7 +32,15 @@ use sc_service::{
};
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::traits::{AccountIdConversion, Block as BlockT};
use std::{io::Write, net::SocketAddr};
use crate::{
chain_spec,
cli::{Cli, RelayChainCli, Subcommand},
service::{
new_partial, Block, ShellRuntimeExecutor, StatemineRuntimeExecutor,
StatemintRuntimeExecutor, WestmintRuntimeExecutor,
},
};
enum Runtime {
/// This is the default runtime (based on rococo)
@@ -88,7 +90,7 @@ fn runtime(id: &str) -> Runtime {
}
}
fn load_spec(id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
fn load_spec(id: &str) -> std::result::Result<Box<dyn ChainSpec>, String> {
Ok(match id {
"staging" => Box::new(chain_spec::staging_test_net()),
"tick" => Box::new(chain_spec::ChainSpec::from_json_bytes(
@@ -254,15 +256,6 @@ impl SubstrateCli for RelayChainCli {
}
}
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())
}
/// Creates partial components for the runtimes that are supported by the benchmarks.
macro_rules! construct_benchmark_partials {
($config:expr, |$partials:ident| $code:expr) => {
@@ -403,6 +396,9 @@ pub fn run() -> Result<()> {
Ok(cmd.run(components.client, components.import_queue))
})
},
Some(Subcommand::Revert(cmd)) => construct_async_run!(|components, cli, cmd, config| {
Ok(cmd.run(components.client, components.backend, None))
}),
Some(Subcommand::PurgeChain(cmd)) => {
let runner = cli.create_runner(cmd)?;
@@ -424,53 +420,20 @@ pub fn run() -> Result<()> {
cmd.run(config, polkadot_config)
})
},
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: crate::service::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)) => {
let runner = cli.create_runner(cmd)?;
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();
cmd.run::<crate::service::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)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|_config| {
let spec = cli.load_spec(&cmd.shared_params.chain.clone().unwrap_or_default())?;
cmd.run(&*spec)
})
},
Some(Subcommand::Benchmark(cmd)) => {
let runner = cli.create_runner(cmd)?;
@@ -570,7 +533,7 @@ pub fn run() -> Result<()> {
let state_version = Cli::native_runtime_version(&config.chain_spec).state_version();
let block: crate::service::Block =
generate_genesis_block(&config.chain_spec, state_version)
generate_genesis_block(&*config.chain_spec, state_version)
.map_err(|e| format!("{:?}", e))?;
let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));
+2 -2
View File
@@ -10,6 +10,7 @@ path = "src/main.rs"
[dependencies]
async-trait = "0.1.56"
clap = { version = "3.2.5", features = ["derive", "deprecated"] }
codec = { package = "parity-scale-codec", version = "3.0.0" }
criterion = { version = "0.3.5", features = [ "async_tokio" ] }
jsonrpsee = { version = "0.14.0", features = ["server"] }
@@ -17,6 +18,7 @@ parking_lot = "0.12.1"
rand = "0.8.5"
serde = { version = "1.0.137", features = ["derive"] }
tokio = { version = "1.19.2", features = ["macros"] }
tracing = "0.1.25"
url = "2.2.2"
# Substrate
@@ -47,8 +49,6 @@ sp-timestamp = { git = "https://github.com/paritytech/substrate", branch = "mast
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-cli = { git = "https://github.com/paritytech/substrate", branch = "master" }
clap = { version = "3.2.5", features = ["derive", "deprecated"] }
tracing = "0.1.25"
# Polkadot
polkadot-primitives = { git = "https://github.com/paritytech/polkadot", branch = "master" }
+48 -53
View File
@@ -14,64 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use clap::{Parser, Subcommand};
use std::{net::SocketAddr, path::PathBuf};
use polkadot_service::{ChainSpec, ParaId, PrometheusConfig};
use sc_cli::{
CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams, NetworkParams,
Result as CliResult, RuntimeVersion, SharedParams, SubstrateCli,
};
use sc_service::BasePath;
use std::{net::SocketAddr, path::PathBuf};
#[derive(Debug, Parser)]
pub struct ExportGenesisStateCommand {
#[clap(default_value_t = 2000u32)]
pub parachain_id: u32,
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,
/// Write output in binary. Default is to write in hex.
#[clap(short, long, action)]
pub raw: bool,
/// The name of the chain for that the genesis state should be exported.
#[clap(long, action)]
pub chain: Option<String>,
}
/// Command for exporting the genesis wasm file.
#[derive(Debug, Parser)]
pub struct ExportGenesisWasmCommand {
#[clap(default_value_t = 2000u32, action)]
pub parachain_id: u32,
/// Output file name or stdout if unspecified.
#[clap(action)]
pub output: Option<PathBuf>,
/// Write output in binary. Default is to write in hex.
#[clap(short, long, action)]
pub raw: bool,
/// The name of the chain for that the genesis wasm file should be exported.
#[clap(long, action)]
pub chain: Option<String>,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
#[clap(name = "export-genesis-wasm")]
ExportGenesisWasm(ExportGenesisWasmCommand),
#[clap(name = "export-genesis-state")]
ExportGenesisState(ExportGenesisStateCommand),
/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),
}
#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
#[clap(
version,
propagate_version = true,
@@ -80,7 +32,7 @@ pub enum Commands {
)]
pub struct TestCollatorCli {
#[clap(subcommand)]
pub subcommand: Option<Commands>,
pub subcommand: Option<Subcommand>,
#[clap(flatten)]
pub run: cumulus_client_cli::RunCmd,
@@ -99,6 +51,49 @@ pub struct TestCollatorCli {
pub disable_block_announcements: bool,
}
#[derive(Debug, clap::Subcommand)]
pub enum Subcommand {
/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),
/// Export the genesis state of the parachain.
ExportGenesisState(ExportGenesisStateCommand),
/// Export the genesis wasm of the parachain.
ExportGenesisWasm(ExportGenesisWasmCommand),
}
#[derive(Debug, clap::Parser)]
pub struct ExportGenesisStateCommand {
#[clap(default_value_t = 2000u32)]
pub parachain_id: u32,
#[clap(flatten)]
pub base: cumulus_client_cli::ExportGenesisStateCommand,
}
impl CliConfiguration for ExportGenesisStateCommand {
fn shared_params(&self) -> &SharedParams {
&self.base.shared_params
}
}
/// Command for exporting the genesis wasm file.
#[derive(Debug, clap::Parser)]
pub struct ExportGenesisWasmCommand {
#[clap(default_value_t = 2000u32)]
pub parachain_id: u32,
#[clap(flatten)]
pub base: cumulus_client_cli::ExportGenesisWasmCommand,
}
impl CliConfiguration for ExportGenesisWasmCommand {
fn shared_params(&self) -> &SharedParams {
&self.base.shared_params
}
}
#[derive(Debug)]
pub struct RelayChainCli {
/// The actual relay chain cli object.
@@ -118,7 +113,7 @@ impl RelayChainCli {
relay_chain_args: impl Iterator<Item = &'a String>,
) -> Self {
let base_path = para_config.base_path.as_ref().map(|x| x.path().join("polkadot"));
Self { base_path, chain_id: None, base: polkadot_cli::RunCmd::parse_from(relay_chain_args) }
Self { base_path, chain_id: None, base: clap::Parser::parse_from(relay_chain_args) }
}
}
+3 -4
View File
@@ -15,7 +15,7 @@
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
use codec::Encode;
use cumulus_client_service::genesis::generate_genesis_block;
use cumulus_client_cli::generate_genesis_block;
use cumulus_primitives_core::ParaId;
use cumulus_test_runtime::Block;
use polkadot_primitives::v2::HeadData;
@@ -23,9 +23,8 @@ use sp_runtime::traits::Block as BlockT;
/// Returns the initial head data for a parachain ID.
pub fn initial_head_data(para_id: ParaId) -> HeadData {
let spec = Box::new(crate::chain_spec::get_chain_spec(para_id));
let block: Block =
generate_genesis_block(&(spec as Box<_>), sp_runtime::StateVersion::V1).unwrap();
let spec = crate::chain_spec::get_chain_spec(para_id);
let block: Block = generate_genesis_block(&spec, sp_runtime::StateVersion::V1).unwrap();
let genesis_state = block.header().encode();
genesis_state.into()
}
+23 -61
View File
@@ -13,27 +13,19 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
mod cli;
use clap::Parser;
use cli::{Commands, RelayChainCli, TestCollatorCli};
use cumulus_client_service::genesis::generate_genesis_block;
use std::sync::Arc;
use cli::{RelayChainCli, Subcommand, TestCollatorCli};
use cumulus_client_cli::generate_genesis_block;
use cumulus_primitives_core::{relay_chain::v2::CollatorPair, ParaId};
use cumulus_test_service::AnnounceBlockFn;
use polkadot_service::runtime_traits::AccountIdConversion;
use sc_cli::{CliConfiguration, SubstrateCli};
use sp_core::{hexdisplay::HexDisplay, Encode, Pair};
use sp_runtime::traits::Block;
use std::{io::Write, sync::Arc};
fn extract_genesis_wasm(chain_spec: &Box<dyn sc_service::ChainSpec>) -> Result<Vec<u8>, String> {
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())
}
pub fn wrap_announce_block() -> Box<dyn FnOnce(AnnounceBlockFn) -> AnnounceBlockFn> {
tracing::info!("Block announcements disabled.");
@@ -44,59 +36,29 @@ pub fn wrap_announce_block() -> Box<dyn FnOnce(AnnounceBlockFn) -> AnnounceBlock
}
fn main() -> Result<(), sc_cli::Error> {
let cli = TestCollatorCli::parse();
let cli = TestCollatorCli::from_args();
match &cli.subcommand {
Some(Commands::BuildSpec(cmd)) => {
Some(Subcommand::BuildSpec(cmd)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
},
Some(Commands::ExportGenesisState(params)) => {
let mut builder = sc_cli::LoggerBuilder::new("");
builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
let _ = builder.init();
let parachain_id = ParaId::from(params.parachain_id);
let spec = Box::new(cumulus_test_service::get_chain_spec(parachain_id)) as Box<_>;
let state_version = cumulus_test_service::runtime::VERSION.state_version();
let block: parachains_common::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)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|_config| {
let parachain_id = ParaId::from(cmd.parachain_id);
let spec = cumulus_test_service::get_chain_spec(parachain_id);
let state_version = cumulus_test_service::runtime::VERSION.state_version();
cmd.base.run::<parachains_common::Block>(&spec, state_version)
})
},
Some(Commands::ExportGenesisWasm(params)) => {
let mut builder = sc_cli::LoggerBuilder::new("");
builder.with_profiling(sc_tracing::TracingReceiver::Log, "");
let _ = builder.init();
let parachain_id = ParaId::from(params.parachain_id);
let spec = Box::new(cumulus_test_service::get_chain_spec(parachain_id)) as Box<_>;
let raw_wasm_blob = extract_genesis_wasm(&spec)?;
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)) => {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|_config| {
let parachain_id = ParaId::from(cmd.parachain_id);
let spec = cumulus_test_service::get_chain_spec(parachain_id);
cmd.base.run(&spec)
})
},
None => {
let mut builder = sc_cli::LoggerBuilder::new("");
@@ -129,7 +91,7 @@ fn main() -> Result<(), sc_cli::Error> {
RelayChainCli::native_runtime_version(&config.chain_spec).state_version();
let block: parachains_common::Block =
generate_genesis_block(&config.chain_spec, state_version)
generate_genesis_block(&*config.chain_spec, state_version)
.map_err(|e| format!("{:?}", e))?;
let genesis_state = format!("0x{:?}", HexDisplay::from(&block.header().encode()));