1// This file is part of Substrate.
23// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
56// 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.
1011// 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.
1516// 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/>.
1819use 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};
3536/// The `import-blocks` command used to import blocks.
37#[derive(Debug, Parser)]
38pub struct ImportBlocksCmd {
39/// Input file or stdin if unspecified.
40#[arg()]
41pub input: Option<PathBuf>,
4243/// 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")]
46pub default_heap_pages: Option<u32>,
4748/// Try importing blocks from binary format rather than JSON.
49#[arg(long)]
50pub binary: bool,
5152#[allow(missing_docs)]
53 #[clap(flatten)]
54pub shared_params: SharedParams,
5556#[allow(missing_docs)]
57 #[clap(flatten)]
58pub import_params: ImportParams,
59}
6061impl ImportBlocksCmd {
62/// Run the import-blocks command
63pub async fn run<B, C, IQ>(&self, client: Arc<C>, import_queue: IQ) -> error::Result<()>
64where
65C: HeaderBackend<B> + Send + Sync + 'static,
66 B: BlockT + for<'de> serde::Deserialize<'de>,
67 IQ: sc_service::ImportQueue<B> + 'static,
68 {
69let file: Box<dyn Read + Send> = match &self.input {
70Some(filename) => Box::new(fs::File::open(filename)?),
71None => Box::new(io::stdin()),
72 };
7374 import_blocks(client, import_queue, file, false, self.binary)
75 .await
76.map_err(Into::into)
77 }
78}
7980impl CliConfiguration for ImportBlocksCmd {
81fn shared_params(&self) -> &SharedParams {
82&self.shared_params
83 }
8485fn import_params(&self) -> Option<&ImportParams> {
86Some(&self.import_params)
87 }
88}