1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::error;
use clap::Args;
use sc_service::{BlocksPruning, PruningMode};

/// Parameters to define the pruning mode
#[derive(Debug, Clone, Args)]
pub struct PruningParams {
	/// Specify the state pruning mode.
	/// This mode specifies when the block's state (ie, storage)
	/// should be pruned (ie, removed) from the database.
	/// This setting can only be set on the first creation of the database. Every subsequent run
	/// will load the pruning mode from the database and will error if the stored mode doesn't
	/// match this CLI value. It is fine to drop this CLI flag for subsequent runs.
	/// Possible values:
	///  - archive: Keep the state of all blocks.
	///  - 'archive-canonical' Keep only the state of finalized blocks.
	///  - number Keep the state of the last number of finalized blocks.
	/// [default: 256]
	#[arg(alias = "pruning", long, value_name = "PRUNING_MODE")]
	pub state_pruning: Option<DatabasePruningMode>,

	/// Specify the blocks pruning mode.
	/// This mode specifies when the block's body (including justifications)
	/// should be pruned (ie, removed) from the database.
	/// Possible values:
	///  - 'archive' Keep all blocks.
	///  - 'archive-canonical' Keep only finalized blocks.
	///  - number
	///  Keep the last `number` of finalized blocks.
	#[arg(
		alias = "keep-blocks",
		long,
		value_name = "PRUNING_MODE",
		default_value = "archive-canonical"
	)]
	pub blocks_pruning: DatabasePruningMode,
}

impl PruningParams {
	/// Get the pruning value from the parameters
	pub fn state_pruning(&self) -> error::Result<Option<PruningMode>> {
		Ok(self.state_pruning.map(|v| v.into()))
	}

	/// Get the block pruning value from the parameters
	pub fn blocks_pruning(&self) -> error::Result<BlocksPruning> {
		Ok(self.blocks_pruning.into())
	}
}

/// Specifies the pruning mode of the database.
///
/// This specifies when the block's data (either state via `--state-pruning`
/// or body via `--blocks-pruning`) should be pruned (ie, removed) from
/// the database.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DatabasePruningMode {
	/// Keep the data of all blocks.
	Archive,
	/// Keep only the data of finalized blocks.
	ArchiveCanonical,
	/// Keep the data of the last number of finalized blocks.
	Custom(u32),
}

impl std::str::FromStr for DatabasePruningMode {
	type Err = String;

	fn from_str(input: &str) -> Result<Self, Self::Err> {
		match input {
			"archive" => Ok(Self::Archive),
			"archive-canonical" => Ok(Self::ArchiveCanonical),
			bc => bc
				.parse()
				.map_err(|_| "Invalid pruning mode specified".to_string())
				.map(Self::Custom),
		}
	}
}

impl Into<PruningMode> for DatabasePruningMode {
	fn into(self) -> PruningMode {
		match self {
			DatabasePruningMode::Archive => PruningMode::ArchiveAll,
			DatabasePruningMode::ArchiveCanonical => PruningMode::ArchiveCanonical,
			DatabasePruningMode::Custom(n) => PruningMode::blocks_pruning(n),
		}
	}
}

impl Into<BlocksPruning> for DatabasePruningMode {
	fn into(self) -> BlocksPruning {
		match self {
			DatabasePruningMode::Archive => BlocksPruning::KeepAll,
			DatabasePruningMode::ArchiveCanonical => BlocksPruning::KeepFinalized,
			DatabasePruningMode::Custom(n) => BlocksPruning::Some(n),
		}
	}
}