From 31e519187e0aad2f2c1c4afabaa4dc7c792844a8 Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Mon, 31 Jan 2022 20:01:43 +0100 Subject: [PATCH] Implement export-header subcommand (#433) * Implement export-header subcommand * Use subcommand only when parachain feature is set * Add explicit type to pass cargo checks --- node/src/cli.rs | 4 ++++ node/src/command.rs | 24 ++++++++++++++++++++++-- node/src/service.rs | 2 +- node/src/service/service_parachain.rs | 20 +++++++++----------- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/node/src/cli.rs b/node/src/cli.rs index 32058cf11..ec3bbbc95 100644 --- a/node/src/cli.rs +++ b/node/src/cli.rs @@ -31,6 +31,10 @@ pub enum Subcommand { /// Export blocks. ExportBlocks(sc_cli::ExportBlocksCmd), + /// Export block header in hex format. + #[cfg(feature = "parachain")] + ExportHeader(sc_cli::CheckBlockCmd), + /// Export the genesis state of the parachain. #[cfg(feature = "parachain")] #[structopt(name = "export-genesis-state")] diff --git a/node/src/command.rs b/node/src/command.rs index c1a31d0c3..1be92433c 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -6,8 +6,8 @@ use sc_cli::SubstrateCli; use sc_service::PartialComponents; #[cfg(feature = "parachain")] use { - parity_scale_codec::Encode, sp_core::hexdisplay::HexDisplay, - sp_runtime::traits::Block as BlockT, std::io::Write, + parity_scale_codec::Encode, sc_client_api::client::BlockBackend, + sp_core::hexdisplay::HexDisplay, sp_runtime::traits::Block as BlockT, std::io::Write, }; use zeitgeist_runtime::RuntimeApi; @@ -61,6 +61,26 @@ pub fn run() -> sc_cli::Result<()> { }) } #[cfg(feature = "parachain")] + Some(Subcommand::ExportHeader(cmd)) => { + let runner = cli.create_runner(cmd)?; + + runner.sync_run(|config| { + let PartialComponents { client, .. }: crate::service::ParachainPartialComponents< + ExecutorDispatch, + RuntimeApi, + > = crate::service::new_partial(&config)?; + + match client.block(&cmd.input.parse()?) { + Ok(Some(block)) => { + println!("0x{:?}", HexDisplay::from(&block.block.header.encode())); + Ok(()) + } + Ok(None) => Err("Unknown block".into()), + Err(e) => Err(format!("Error reading block: {:?}", e).into()), + } + }) + } + #[cfg(feature = "parachain")] Some(Subcommand::ExportGenesisState(params)) => { let mut builder = sc_cli::LoggerBuilder::new(""); builder.with_profiling(sc_tracing::TracingReceiver::Log, ""); diff --git a/node/src/service.rs b/node/src/service.rs index 8f1007ce1..7c7bf19eb 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -8,7 +8,7 @@ use zeitgeist_primitives::types::{AccountId, Balance, Index, MarketId, PoolId}; use zeitgeist_runtime::opaque::Block; #[cfg(feature = "parachain")] -pub use service_parachain::{new_full, new_partial}; +pub use service_parachain::{new_full, new_partial, ParachainPartialComponents}; #[cfg(not(feature = "parachain"))] pub use service_standalone::{new_full, new_light, new_partial}; diff --git a/node/src/service/service_parachain.rs b/node/src/service/service_parachain.rs index 81194f347..713bcb41b 100644 --- a/node/src/service/service_parachain.rs +++ b/node/src/service/service_parachain.rs @@ -19,6 +19,14 @@ use zeitgeist_runtime::{opaque::Block, RuntimeApi}; type FullBackend = TFullBackend; type FullClient = TFullClient>; +pub type ParachainPartialComponents = PartialComponents< + FullClient, + FullBackend, + (), + sc_consensus::DefaultImportQueue>, + sc_transaction_pool::FullPool>, + (Option, Option), +>; /// Start a parachain node. pub async fn new_full( @@ -37,17 +45,7 @@ pub async fn new_full( #[allow(clippy::type_complexity)] pub fn new_partial( config: &Configuration, -) -> Result< - PartialComponents< - FullClient, - FullBackend, - (), - sc_consensus::DefaultImportQueue>, - sc_transaction_pool::FullPool>, - (Option, Option), - >, - sc_service::error::Error, -> +) -> Result, sc_service::error::Error> where RuntimeApi: ConstructRuntimeApi> + Send + Sync + 'static,