Skip to main content

soil_cli/commands/
revert_cmd.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7use crate::{
8	error,
9	params::{DatabaseParams, GenericNumber, PruningParams, SharedParams},
10	CliConfiguration,
11};
12use clap::Parser;
13use soil_client::client_api::{Backend, UsageProvider};
14use soil_service::chain_ops::revert_chain;
15use std::{fmt::Debug, str::FromStr, sync::Arc};
16use subsoil::runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
17
18/// The `revert` command used revert the chain to a previous state.
19#[derive(Debug, Parser)]
20pub struct RevertCmd {
21	/// Number of blocks to revert.
22	#[arg(default_value = "256")]
23	pub num: GenericNumber,
24
25	#[allow(missing_docs)]
26	#[clap(flatten)]
27	pub shared_params: SharedParams,
28
29	#[allow(missing_docs)]
30	#[clap(flatten)]
31	pub pruning_params: PruningParams,
32
33	#[allow(missing_docs)]
34	#[clap(flatten)]
35	pub database_params: DatabaseParams,
36}
37
38/// Revert handler for auxiliary data (e.g. consensus).
39type AuxRevertHandler<C, BA, B> =
40	Box<dyn FnOnce(Arc<C>, Arc<BA>, NumberFor<B>) -> error::Result<()>>;
41
42impl RevertCmd {
43	/// Run the revert command
44	pub async fn run<B, BA, C>(
45		&self,
46		client: Arc<C>,
47		backend: Arc<BA>,
48		aux_revert: Option<AuxRevertHandler<C, BA, B>>,
49	) -> error::Result<()>
50	where
51		B: BlockT,
52		BA: Backend<B>,
53		C: UsageProvider<B>,
54		<<<B as BlockT>::Header as HeaderT>::Number as FromStr>::Err: Debug,
55	{
56		let blocks = self.num.parse()?;
57		if let Some(aux_revert) = aux_revert {
58			aux_revert(client.clone(), backend.clone(), blocks)?;
59		}
60		revert_chain(client, backend, blocks)?;
61
62		Ok(())
63	}
64}
65
66impl CliConfiguration for RevertCmd {
67	fn shared_params(&self) -> &SharedParams {
68		&self.shared_params
69	}
70
71	fn pruning_params(&self) -> Option<&PruningParams> {
72		Some(&self.pruning_params)
73	}
74
75	fn database_params(&self) -> Option<&DatabaseParams> {
76		Some(&self.database_params)
77	}
78}