referrerpolicy=no-referrer-when-downgrade

sc_cli/commands/
revert_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 sc_client_api::{Backend, UsageProvider};
26use sc_service::chain_ops::revert_chain;
27use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
28use std::{fmt::Debug, str::FromStr, sync::Arc};
29
30/// The `revert` command used revert the chain to a previous state.
31#[derive(Debug, Parser)]
32pub struct RevertCmd {
33	/// Number of blocks to revert.
34	#[arg(default_value = "256")]
35	pub num: GenericNumber,
36
37	#[allow(missing_docs)]
38	#[clap(flatten)]
39	pub shared_params: SharedParams,
40
41	#[allow(missing_docs)]
42	#[clap(flatten)]
43	pub pruning_params: PruningParams,
44
45	#[allow(missing_docs)]
46	#[clap(flatten)]
47	pub database_params: DatabaseParams,
48}
49
50/// Revert handler for auxiliary data (e.g. consensus).
51type AuxRevertHandler<C, BA, B> =
52	Box<dyn FnOnce(Arc<C>, Arc<BA>, NumberFor<B>) -> error::Result<()>>;
53
54impl RevertCmd {
55	/// Run the revert command
56	pub async fn run<B, BA, C>(
57		&self,
58		client: Arc<C>,
59		backend: Arc<BA>,
60		aux_revert: Option<AuxRevertHandler<C, BA, B>>,
61	) -> error::Result<()>
62	where
63		B: BlockT,
64		BA: Backend<B>,
65		C: UsageProvider<B>,
66		<<<B as BlockT>::Header as HeaderT>::Number as FromStr>::Err: Debug,
67	{
68		let blocks = self.num.parse()?;
69		if let Some(aux_revert) = aux_revert {
70			aux_revert(client.clone(), backend.clone(), blocks)?;
71		}
72		revert_chain(client, backend, blocks)?;
73
74		Ok(())
75	}
76}
77
78impl CliConfiguration for RevertCmd {
79	fn shared_params(&self) -> &SharedParams {
80		&self.shared_params
81	}
82
83	fn pruning_params(&self) -> Option<&PruningParams> {
84		Some(&self.pruning_params)
85	}
86
87	fn database_params(&self) -> Option<&DatabaseParams> {
88		Some(&self.database_params)
89	}
90}