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 3e67ab361c
commit ad6fe6d191
14 changed files with 332 additions and 434 deletions
+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()));