Skip to content

Commit

Permalink
Add safe_epoch_retention_limit to Configuration
Browse files Browse the repository at this point in the history
In order to ensure that if a retention limit is set it's not lower than
a minimum value in order to not prune data that will be used at later
epochs.
  • Loading branch information
Alenar committed Jul 25, 2023
1 parent 3abf4b2 commit d31de78
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
46 changes: 46 additions & 0 deletions mithril-aggregator/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> {
self.store_retention_limit
.map(|limit| if limit > 3 { limit as u64 } else { 3 })
}
}

/// Default configuration with all the default values for configurations.
Expand Down Expand Up @@ -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));
}
}
}
6 changes: 3 additions & 3 deletions mithril-aggregator/src/dependency_injection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl DependenciesBuilder {
async fn build_stake_store(&mut self) -> Result<Arc<StakePoolStore>> {
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)
Expand Down Expand Up @@ -422,7 +422,7 @@ impl DependenciesBuilder {
) -> Result<Arc<dyn ProtocolParametersStorer>> {
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(),
)))
}

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit d31de78

Please sign in to comment.