referrerpolicy=no-referrer-when-downgrade

sc_cli/commands/
import_blocks_cmd.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19use crate::{
20	error,
21	params::{ImportParams, SharedParams},
22	CliConfiguration,
23};
24use clap::Parser;
25use sc_client_api::HeaderBackend;
26use sc_service::chain_ops::import_blocks;
27use sp_runtime::traits::Block as BlockT;
28use std::{
29	fmt::Debug,
30	fs,
31	io::{self, Read},
32	path::PathBuf,
33	sync::Arc,
34};
35
36/// The `import-blocks` command used to import blocks.
37#[derive(Debug, Parser)]
38pub struct ImportBlocksCmd {
39	/// Input file or stdin if unspecified.
40	#[arg()]
41	pub input: Option<PathBuf>,
42
43	/// The default number of 64KB pages to ever allocate for Wasm execution.
44	/// Don't alter this unless you know what you're doing.
45	#[arg(long, value_name = "COUNT")]
46	pub default_heap_pages: Option<u32>,
47
48	/// Try importing blocks from binary format rather than JSON.
49	#[arg(long)]
50	pub binary: bool,
51
52	#[allow(missing_docs)]
53	#[clap(flatten)]
54	pub shared_params: SharedParams,
55
56	#[allow(missing_docs)]
57	#[clap(flatten)]
58	pub import_params: ImportParams,
59}
60
61impl ImportBlocksCmd {
62	/// Run the import-blocks command
63	pub async fn run<B, C, IQ>(&self, client: Arc<C>, import_queue: IQ) -> error::Result<()>
64	where
65		C: HeaderBackend<B> + Send + Sync + 'static,
66		B: BlockT + for<'de> serde::Deserialize<'de>,
67		IQ: sc_service::ImportQueue<B> + 'static,
68	{
69		let file: Box<dyn Read + Send> = match &self.input {
70			Some(filename) => Box::new(fs::File::open(filename)?),
71			None => Box::new(io::stdin()),
72		};
73
74		import_blocks(client, import_queue, file, false, self.binary)
75			.await
76			.map_err(Into::into)
77	}
78}
79
80impl CliConfiguration for ImportBlocksCmd {
81	fn shared_params(&self) -> &SharedParams {
82		&self.shared_params
83	}
84
85	fn import_params(&self) -> Option<&ImportParams> {
86		Some(&self.import_params)
87	}
88}