referrerpolicy=no-referrer-when-downgrade

sc_cli/params/
pruning_params.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::error;
20use clap::Args;
21use sc_service::{BlocksPruning, PruningMode};
22
23/// Parameters to define the pruning mode
24#[derive(Debug, Clone, Args)]
25pub struct PruningParams {
26	/// Specify the state pruning mode.
27	///
28	/// This mode specifies when the block's state (ie, storage)
29	/// should be pruned (ie, removed) from the database.
30	/// This setting can only be set on the first creation of the database. Every subsequent run
31	/// will load the pruning mode from the database and will error if the stored mode doesn't
32	/// match this CLI value. It is fine to drop this CLI flag for subsequent runs. The only
33	/// exception is that `NUMBER` can change between subsequent runs (increasing it will not
34	/// lead to restoring pruned state).
35	///
36	/// Possible values:
37	///
38	/// - archive: Keep the data of all blocks.
39	///
40	/// - archive-canonical: Keep only the data of finalized blocks.
41	///
42	/// - NUMBER: Keep the data of the last NUMBER of finalized blocks.
43	///
44	/// [default: 256]
45	#[arg(alias = "pruning", long, value_name = "PRUNING_MODE")]
46	pub state_pruning: Option<DatabasePruningMode>,
47
48	/// Specify the blocks pruning mode.
49	///
50	/// This mode specifies when the block's body (including justifications)
51	/// should be pruned (ie, removed) from the database.
52	///
53	/// Possible values:
54	///
55	/// - archive: Keep the data of all blocks.
56	///
57	/// - archive-canonical: Keep only the data of finalized blocks.
58	///
59	/// - NUMBER: Keep the data of the last NUMBER of finalized blocks.
60	#[arg(
61		alias = "keep-blocks",
62		long,
63		value_name = "PRUNING_MODE",
64		default_value = "archive-canonical"
65	)]
66	pub blocks_pruning: DatabasePruningMode,
67}
68
69impl PruningParams {
70	/// Get the pruning value from the parameters
71	pub fn state_pruning(&self) -> error::Result<Option<PruningMode>> {
72		Ok(self.state_pruning.map(|v| v.into()))
73	}
74
75	/// Get the block pruning value from the parameters
76	pub fn blocks_pruning(&self) -> error::Result<BlocksPruning> {
77		Ok(self.blocks_pruning.into())
78	}
79}
80
81/// Specifies the pruning mode of the database.
82///
83/// This specifies when the block's data (either state via `--state-pruning`
84/// or body via `--blocks-pruning`) should be pruned (ie, removed) from
85/// the database.
86#[derive(Debug, Clone, Copy, PartialEq)]
87pub enum DatabasePruningMode {
88	/// Keep the data of all blocks.
89	Archive,
90	/// Keep only the data of finalized blocks.
91	ArchiveCanonical,
92	/// Keep the data of the last number of finalized blocks.
93	Custom(u32),
94}
95
96impl std::str::FromStr for DatabasePruningMode {
97	type Err = String;
98
99	fn from_str(input: &str) -> Result<Self, Self::Err> {
100		match input {
101			"archive" => Ok(Self::Archive),
102			"archive-canonical" => Ok(Self::ArchiveCanonical),
103			bc => bc
104				.parse()
105				.map_err(|_| "Invalid pruning mode specified".to_string())
106				.map(Self::Custom),
107		}
108	}
109}
110
111impl Into<PruningMode> for DatabasePruningMode {
112	fn into(self) -> PruningMode {
113		match self {
114			DatabasePruningMode::Archive => PruningMode::ArchiveAll,
115			DatabasePruningMode::ArchiveCanonical => PruningMode::ArchiveCanonical,
116			DatabasePruningMode::Custom(n) => PruningMode::blocks_pruning(n),
117		}
118	}
119}
120
121impl Into<BlocksPruning> for DatabasePruningMode {
122	fn into(self) -> BlocksPruning {
123		match self {
124			DatabasePruningMode::Archive => BlocksPruning::KeepAll,
125			DatabasePruningMode::ArchiveCanonical => BlocksPruning::KeepFinalized,
126			DatabasePruningMode::Custom(n) => BlocksPruning::Some(n),
127		}
128	}
129}
130
131#[cfg(test)]
132mod tests {
133	use super::*;
134	use clap::Parser;
135
136	#[derive(Parser)]
137	struct Cli {
138		#[clap(flatten)]
139		pruning: PruningParams,
140	}
141
142	#[test]
143	fn pruning_params_parse_works() {
144		let Cli { pruning } =
145			Cli::parse_from(["", "--state-pruning=1000", "--blocks-pruning=1000"]);
146
147		assert!(matches!(pruning.state_pruning, Some(DatabasePruningMode::Custom(1000))));
148		assert!(matches!(pruning.blocks_pruning, DatabasePruningMode::Custom(1000)));
149
150		let Cli { pruning } =
151			Cli::parse_from(["", "--state-pruning=archive", "--blocks-pruning=archive"]);
152
153		assert!(matches!(dbg!(pruning.state_pruning), Some(DatabasePruningMode::Archive)));
154		assert!(matches!(pruning.blocks_pruning, DatabasePruningMode::Archive));
155
156		let Cli { pruning } = Cli::parse_from([
157			"",
158			"--state-pruning=archive-canonical",
159			"--blocks-pruning=archive-canonical",
160		]);
161
162		assert!(matches!(dbg!(pruning.state_pruning), Some(DatabasePruningMode::ArchiveCanonical)));
163		assert!(matches!(pruning.blocks_pruning, DatabasePruningMode::ArchiveCanonical));
164	}
165}