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 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};
2930/// 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")]
35pub num: GenericNumber,
3637#[allow(missing_docs)]
38 #[clap(flatten)]
39pub shared_params: SharedParams,
4041#[allow(missing_docs)]
42 #[clap(flatten)]
43pub pruning_params: PruningParams,
4445#[allow(missing_docs)]
46 #[clap(flatten)]
47pub database_params: DatabaseParams,
48}
4950/// 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<()>>;
5354impl RevertCmd {
55/// Run the revert command
56pub 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<()>
62where
63B: BlockT,
64 BA: Backend<B>,
65 C: UsageProvider<B>,
66 <<<B as BlockT>::Header as HeaderT>::Number as FromStr>::Err: Debug,
67 {
68let blocks = self.num.parse()?;
69if let Some(aux_revert) = aux_revert {
70 aux_revert(client.clone(), backend.clone(), blocks)?;
71 }
72 revert_chain(client, backend, blocks)?;
7374Ok(())
75 }
76}
7778impl CliConfiguration for RevertCmd {
79fn shared_params(&self) -> &SharedParams {
80&self.shared_params
81 }
8283fn pruning_params(&self) -> Option<&PruningParams> {
84Some(&self.pruning_params)
85 }
8687fn database_params(&self) -> Option<&DatabaseParams> {
88Some(&self.database_params)
89 }
90}