diff --git a/mithril-aggregator/src/configuration.rs b/mithril-aggregator/src/configuration.rs index 605094bd133..e9054746936 100644 --- a/mithril-aggregator/src/configuration.rs +++ b/mithril-aggregator/src/configuration.rs @@ -193,6 +193,16 @@ impl Configuration { self.data_stores_directory.clone() } + + /// Same as the [store retention limit][Configuration::store_retention_limit] but will never + /// yield a value lower than 3. + /// + /// This is in order to avoid pruning data that will be used in future epochs (like the protocol + /// parameters). + pub fn safe_epoch_retention_limit(&self) -> Option { + self.store_retention_limit + .map(|limit| if limit > 3 { limit as u64 } else { 3 }) + } } /// Default configuration with all the default values for configurations. @@ -323,3 +333,39 @@ impl Source for DefaultConfiguration { Ok(result) } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn safe_epoch_retention_limit_wont_change_a_value_higher_than_three() { + for limit in 4..=10u64 { + let configuration = Configuration { + store_retention_limit: Some(limit as usize), + ..Configuration::new_sample() + }; + assert_eq!(configuration.safe_epoch_retention_limit(), Some(limit)); + } + } + + #[test] + fn safe_epoch_retention_limit_wont_change_a_none_value() { + let configuration = Configuration { + store_retention_limit: None, + ..Configuration::new_sample() + }; + assert_eq!(configuration.safe_epoch_retention_limit(), None); + } + + #[test] + fn safe_epoch_retention_limit_wont_yield_a_value_lower_than_three() { + for limit in 0..=3 { + let configuration = Configuration { + store_retention_limit: Some(limit), + ..Configuration::new_sample() + }; + assert_eq!(configuration.safe_epoch_retention_limit(), Some(3)); + } + } +} diff --git a/mithril-aggregator/src/dependency_injection/builder.rs b/mithril-aggregator/src/dependency_injection/builder.rs index 7c50a499134..2a245d6bb60 100644 --- a/mithril-aggregator/src/dependency_injection/builder.rs +++ b/mithril-aggregator/src/dependency_injection/builder.rs @@ -275,7 +275,7 @@ impl DependenciesBuilder { async fn build_stake_store(&mut self) -> Result> { let stake_pool_store = Arc::new(StakePoolStore::new( self.get_sqlite_connection().await?, - self.configuration.store_retention_limit.map(|l| l as u64), + self.configuration.safe_epoch_retention_limit(), )); Ok(stake_pool_store) @@ -422,7 +422,7 @@ impl DependenciesBuilder { ) -> Result> { Ok(Arc::new(EpochSettingStore::new( self.get_sqlite_connection().await?, - self.configuration.store_retention_limit.map(|l| l as u64), + self.configuration.safe_epoch_retention_limit(), ))) } @@ -648,7 +648,7 @@ impl DependenciesBuilder { self.get_chain_observer().await?, self.get_verification_key_store().await?, self.get_signer_recorder().await?, - self.configuration.store_retention_limit.map(|l| l as u64), + self.configuration.safe_epoch_retention_limit(), ); Ok(Arc::new(registerer))