sc_cli/commands/
revert_cmd.rs1use 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#[derive(Debug, Parser)]
32pub struct RevertCmd {
33	#[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
50type AuxRevertHandler<C, BA, B> =
52	Box<dyn FnOnce(Arc<C>, Arc<BA>, NumberFor<B>) -> error::Result<()>>;
53
54impl RevertCmd {
55	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}