nyxd_scraper_shared/block_processor/
pruning.rs

1// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::error::ScraperError;
5use serde::{Deserialize, Serialize};
6
7pub const DEFAULT_PRUNING_KEEP_RECENT: u32 = 362880;
8pub const DEFAULT_PRUNING_INTERVAL: u32 = 10;
9pub const EVERYTHING_PRUNING_KEEP_RECENT: u32 = 2;
10pub const EVERYTHING_PRUNING_INTERVAL: u32 = 10;
11
12/// We follow cosmos-sdk pruning strategies for convenience’s sake.
13#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
14#[serde(rename_all = "snake_case")]
15pub enum PruningStrategy {
16    /// 'Default' strategy defines a pruning strategy where the last 362880 heights are
17    /// kept where to-be pruned heights are pruned at every 10th height.
18    /// The last 362880 heights are kept(approximately 3.5 weeks worth of state) assuming the typical
19    /// block time is 6s. If these values do not match the applications' requirements, use the "custom" option.
20    #[default]
21    Default,
22
23    /// 'Everything' strategy defines a pruning strategy where all committed heights are
24    /// deleted, storing only the current height and last 2 states. To-be pruned heights are
25    /// pruned at every 10th height.
26    Everything,
27
28    /// 'Nothing' strategy defines a pruning strategy where all heights are kept on disk.
29    Nothing,
30
31    /// 'Custom' strategy defines a pruning strategy where the user specifies the pruning.
32    Custom,
33}
34
35impl PruningStrategy {
36    pub fn is_custom(&self) -> bool {
37        matches!(self, PruningStrategy::Custom)
38    }
39
40    pub fn is_nothing(&self) -> bool {
41        matches!(self, PruningStrategy::Nothing)
42    }
43
44    pub fn is_everything(&self) -> bool {
45        matches!(self, PruningStrategy::Everything)
46    }
47}
48
49#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
50pub struct PruningOptions {
51    /// keep_recent defines how many recent heights to keep on disk.
52    pub keep_recent: u32,
53
54    /// interval defines the frequency of removing the pruned heights from the disk.
55    pub interval: u32,
56
57    /// strategy defines the currently used kind of [PruningStrategy].
58    pub strategy: PruningStrategy,
59}
60
61impl PruningOptions {
62    pub fn validate(&self) -> Result<(), ScraperError> {
63        // if strategy is not set to custom, other options are meaningless since they won't be applied
64        if !self.strategy.is_custom() {
65            return Ok(());
66        }
67
68        if self.interval == 0 {
69            return Err(ScraperError::ZeroPruningInterval);
70        }
71
72        if self.interval < EVERYTHING_PRUNING_INTERVAL {
73            return Err(ScraperError::TooSmallPruningInterval {
74                interval: self.interval,
75            });
76        }
77
78        if self.keep_recent < EVERYTHING_PRUNING_KEEP_RECENT {
79            return Err(ScraperError::TooSmallKeepRecent {
80                keep_recent: self.keep_recent,
81            });
82        }
83
84        Ok(())
85    }
86
87    pub fn nothing() -> Self {
88        PruningOptions {
89            keep_recent: 0,
90            interval: 0,
91            strategy: PruningStrategy::Nothing,
92        }
93    }
94
95    pub fn strategy_interval(&self) -> u32 {
96        match self.strategy {
97            PruningStrategy::Default => DEFAULT_PRUNING_INTERVAL,
98            PruningStrategy::Everything => EVERYTHING_PRUNING_INTERVAL,
99            PruningStrategy::Nothing => 0,
100            PruningStrategy::Custom => self.interval,
101        }
102    }
103
104    pub fn strategy_keep_recent(&self) -> u32 {
105        match self.strategy {
106            PruningStrategy::Default => DEFAULT_PRUNING_KEEP_RECENT,
107            PruningStrategy::Everything => EVERYTHING_PRUNING_KEEP_RECENT,
108            PruningStrategy::Nothing => 0,
109            PruningStrategy::Custom => self.keep_recent,
110        }
111    }
112}
113
114impl Default for PruningOptions {
115    fn default() -> Self {
116        PruningOptions {
117            keep_recent: DEFAULT_PRUNING_KEEP_RECENT,
118            interval: DEFAULT_PRUNING_INTERVAL,
119            strategy: Default::default(),
120        }
121    }
122}