Skip to content

Commit

Permalink
Implement export-header subcommand (#433)
Browse files Browse the repository at this point in the history
* Implement export-header subcommand

* Use subcommand only when parachain feature is set

* Add explicit type to pass cargo checks
  • Loading branch information
sea212 authored Jan 31, 2022
1 parent 5f0b7f5 commit 31e5191
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
4 changes: 4 additions & 0 deletions node/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
24 changes: 22 additions & 2 deletions node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "");
Expand Down
2 changes: 1 addition & 1 deletion node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
20 changes: 9 additions & 11 deletions node/src/service/service_parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ use zeitgeist_runtime::{opaque::Block, RuntimeApi};
type FullBackend = TFullBackend<Block>;
type FullClient<RuntimeApi, Executor> =
TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>;
pub type ParachainPartialComponents<Executor, RuntimeApi> = PartialComponents<
FullClient<RuntimeApi, Executor>,
FullBackend,
(),
sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, Executor>>,
sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, Executor>>,
(Option<Telemetry>, Option<TelemetryWorkerHandle>),
>;

/// Start a parachain node.
pub async fn new_full(
Expand All @@ -37,17 +45,7 @@ pub async fn new_full(
#[allow(clippy::type_complexity)]
pub fn new_partial<RuntimeApi, Executor>(
config: &Configuration,
) -> Result<
PartialComponents<
FullClient<RuntimeApi, Executor>,
FullBackend,
(),
sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, Executor>>,
sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, Executor>>,
(Option<Telemetry>, Option<TelemetryWorkerHandle>),
>,
sc_service::error::Error,
>
) -> Result<ParachainPartialComponents<Executor, RuntimeApi>, sc_service::error::Error>
where
RuntimeApi:
ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
Expand Down

0 comments on commit 31e5191

Please sign in to comment.