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::{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};
3031/// 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()]
36pub output: Option<PathBuf>,
3738/// Specify starting block number.
39 /// Default is 1.
40#[arg(long, value_name = "BLOCK")]
41pub from: Option<GenericNumber>,
4243/// Specify last block number.
44 /// Default is best block.
45#[arg(long, value_name = "BLOCK")]
46pub to: Option<GenericNumber>,
4748/// Use binary output 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 pruning_params: PruningParams,
5960#[allow(missing_docs)]
61 #[clap(flatten)]
62pub database_params: DatabaseParams,
63}
6465impl ExportBlocksCmd {
66/// Run the export-blocks command
67pub async fn run<B, C>(
68&self,
69 client: Arc<C>,
70 database_config: DatabaseSource,
71 ) -> error::Result<()>
72where
73B: BlockT,
74 C: HeaderBackend<B> + BlockBackend<B> + UsageProvider<B> + 'static,
75 <<B::Header as HeaderT>::Number as FromStr>::Err: Debug,
76 {
77if let Some(path) = database_config.path() {
78info!("DB path: {}", path.display());
79 }
8081let from = self.from.as_ref().and_then(|f| f.parse().ok()).unwrap_or(1u32);
82let to = self.to.as_ref().and_then(|t| t.parse().ok());
8384let binary = self.binary;
8586let file: Box<dyn io::Write> = match &self.output {
87Some(filename) => Box::new(fs::File::create(filename)?),
88None => Box::new(io::stdout()),
89 };
9091 export_blocks(client, file, from.into(), to, binary).await.map_err(Into::into)
92 }
93}
9495impl CliConfiguration for ExportBlocksCmd {
96fn shared_params(&self) -> &SharedParams {
97&self.shared_params
98 }
99100fn pruning_params(&self) -> Option<&PruningParams> {
101Some(&self.pruning_params)
102 }
103104fn database_params(&self) -> Option<&DatabaseParams> {
105Some(&self.database_params)
106 }
107}