Skip to main content

soil_cli/commands/
import_blocks_cmd.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use crate::{
8	error,
9	params::{ImportParams, SharedParams},
10	CliConfiguration,
11};
12use clap::Parser;
13use soil_client::client_api::HeaderBackend;
14use soil_service::chain_ops::import_blocks;
15use std::{
16	fmt::Debug,
17	fs,
18	io::{self, Read},
19	path::PathBuf,
20	sync::Arc,
21};
22use subsoil::runtime::traits::Block as BlockT;
23
24/// The `import-blocks` command used to import blocks.
25#[derive(Debug, Parser)]
26pub struct ImportBlocksCmd {
27	/// Input file or stdin if unspecified.
28	#[arg()]
29	pub input: Option<PathBuf>,
30
31	/// The default number of 64KB pages to ever allocate for Wasm execution.
32	/// Don't alter this unless you know what you're doing.
33	#[arg(long, value_name = "COUNT")]
34	pub default_heap_pages: Option<u32>,
35
36	/// Try importing blocks from binary format rather than JSON.
37	#[arg(long)]
38	pub binary: bool,
39
40	#[allow(missing_docs)]
41	#[clap(flatten)]
42	pub shared_params: SharedParams,
43
44	#[allow(missing_docs)]
45	#[clap(flatten)]
46	pub import_params: ImportParams,
47}
48
49impl ImportBlocksCmd {
50	/// Run the import-blocks command
51	pub async fn run<B, C, IQ>(&self, client: Arc<C>, import_queue: IQ) -> error::Result<()>
52	where
53		C: HeaderBackend<B> + Send + Sync + 'static,
54		B: BlockT + for<'de> serde::Deserialize<'de>,
55		IQ: soil_service::ImportQueue<B> + 'static,
56	{
57		let file: Box<dyn Read + Send> = match &self.input {
58			Some(filename) => Box::new(fs::File::open(filename)?),
59			None => Box::new(io::stdin()),
60		};
61
62		import_blocks(client, import_queue, file, false, self.binary)
63			.await
64			.map_err(Into::into)
65	}
66}
67
68impl CliConfiguration for ImportBlocksCmd {
69	fn shared_params(&self) -> &SharedParams {
70		&self.shared_params
71	}
72
73	fn import_params(&self) -> Option<&ImportParams> {
74		Some(&self.import_params)
75	}
76}