referrerpolicy=no-referrer-when-downgrade

sc_cli/commands/
export_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::{DatabaseParams, GenericNumber, PruningParams, SharedParams},
22	CliConfiguration,
23};
24use clap::Parser;
25use log::info;
26use sc_client_api::{BlockBackend, HeaderBackend, UsageProvider};
27use sc_service::{chain_ops::export_blocks, config::DatabaseSource};
28use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
29use std::{fmt::Debug, fs, io, path::PathBuf, str::FromStr, sync::Arc};
30
31/// The `export-blocks` command used to export blocks.
32#[derive(Debug, Clone, Parser)]
33pub struct ExportBlocksCmd {
34	/// Output file name or stdout if unspecified.
35	#[arg()]
36	pub output: Option<PathBuf>,
37
38	/// Specify starting block number.
39	/// Default is 1.
40	#[arg(long, value_name = "BLOCK")]
41	pub from: Option<GenericNumber>,
42
43	/// Specify last block number.
44	/// Default is best block.
45	#[arg(long, value_name = "BLOCK")]
46	pub to: Option<GenericNumber>,
47
48	/// Use binary output 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 pruning_params: PruningParams,
59
60	#[allow(missing_docs)]
61	#[clap(flatten)]
62	pub database_params: DatabaseParams,
63}
64
65impl ExportBlocksCmd {
66	/// Run the export-blocks command
67	pub async fn run<B, C>(
68		&self,
69		client: Arc<C>,
70		database_config: DatabaseSource,
71	) -> error::Result<()>
72	where
73		B: BlockT,
74		C: HeaderBackend<B> + BlockBackend<B> + UsageProvider<B> + 'static,
75		<<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
76	{
77		if let Some(path) = database_config.path() {
78			info!("DB path: {}", path.display());
79		}
80
81		let from = self.from.as_ref().and_then(|f| f.parse().ok()).unwrap_or(1u32);
82		let to = self.to.as_ref().and_then(|t| t.parse().ok());
83
84		let binary = self.binary;
85
86		let file: Box<dyn io::Write> = match &self.output {
87			Some(filename) => Box::new(fs::File::create(filename)?),
88			None => Box::new(io::stdout()),
89		};
90
91		export_blocks(client, file, from.into(), to, binary).await.map_err(Into::into)
92	}
93}
94
95impl CliConfiguration for ExportBlocksCmd {
96	fn shared_params(&self) -> &SharedParams {
97		&self.shared_params
98	}
99
100	fn pruning_params(&self) -> Option<&PruningParams> {
101		Some(&self.pruning_params)
102	}
103
104	fn database_params(&self) -> Option<&DatabaseParams> {
105		Some(&self.database_params)
106	}
107}