Skip to content

Commit 26e46c3

Browse files
committed
Add missing 'remove' in StoreAdapter
Which was creating crashes when pruning happens.
1 parent ee8b407 commit 26e46c3

File tree

1 file changed

+72
-3
lines changed

1 file changed

+72
-3
lines changed

mithril-aggregator/src/database/provider/epoch_setting.rs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,20 @@ impl<'conn> DeleteEpochSettingProvider<'conn> {
218218
Self { connection }
219219
}
220220

221+
/// Create the SQL condition to delete a record given the Epoch.
222+
fn get_delete_condition_by_epoch(&self, epoch: Epoch) -> WhereCondition {
223+
let epoch_setting_id_value = Value::Integer(i64::try_from(epoch.0).unwrap());
224+
225+
WhereCondition::new("epoch_setting_id = ?*", vec![epoch_setting_id_value])
226+
}
227+
228+
/// Delete the epoch setting data given the Epoch
229+
pub fn delete(&self, epoch: Epoch) -> Result<EntityCursor<EpochSettingRecord>, StdError> {
230+
let filters = self.get_delete_condition_by_epoch(epoch);
231+
232+
self.find(filters)
233+
}
234+
221235
/// Create the SQL condition to prune data older than the given Epoch.
222236
fn get_prune_condition(&self, epoch_threshold: Epoch) -> WhereCondition {
223237
let epoch_setting_id_value = Value::Integer(i64::try_from(epoch_threshold.0).unwrap());
@@ -312,8 +326,15 @@ impl StoreAdapter for EpochSettingStore {
312326
Ok(protocol_parameters)
313327
}
314328

315-
async fn remove(&mut self, _key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> {
316-
unimplemented!()
329+
async fn remove(&mut self, key: &Self::Key) -> Result<Option<Self::Record>, AdapterError> {
330+
let connection = &*self.connection.lock().await;
331+
let provider = DeleteEpochSettingProvider::new(connection);
332+
let mut cursor = provider
333+
.delete(*key)
334+
.map_err(|e| AdapterError::GeneralError(format!("{e}")))?;
335+
let epoch_setting_record = cursor.next();
336+
337+
Ok(epoch_setting_record.map(|es| es.protocol_parameters))
317338
}
318339

319340
async fn get_iter(&self) -> Result<Box<dyn Iterator<Item = Self::Record> + '_>, AdapterError> {
@@ -461,6 +482,17 @@ mod tests {
461482
);
462483
}
463484

485+
#[test]
486+
fn delete() {
487+
let connection = Connection::open(":memory:").unwrap();
488+
let provider = DeleteEpochSettingProvider::new(&connection);
489+
let condition = provider.get_delete_condition_by_epoch(Epoch(5));
490+
let (condition, params) = condition.expand();
491+
492+
assert_eq!("epoch_setting_id = ?1".to_string(), condition);
493+
assert_eq!(vec![Value::Integer(5)], params);
494+
}
495+
464496
#[test]
465497
fn prune() {
466498
let connection = Connection::open(":memory:").unwrap();
@@ -533,6 +565,26 @@ mod tests {
533565
assert_eq!(0, cursor.count());
534566
}
535567

568+
#[test]
569+
fn test_delete() {
570+
let connection = Connection::open(":memory:").unwrap();
571+
setup_epoch_setting_db(&connection).unwrap();
572+
573+
let provider = DeleteEpochSettingProvider::new(&connection);
574+
let cursor = provider.delete(Epoch(2)).unwrap();
575+
576+
assert_eq!(1, cursor.count());
577+
578+
let provider = EpochSettingProvider::new(&connection);
579+
let cursor = provider.get_by_epoch(&Epoch(1)).unwrap();
580+
581+
assert_eq!(1, cursor.count());
582+
583+
let cursor = provider.get_by_epoch(&Epoch(2)).unwrap();
584+
585+
assert_eq!(0, cursor.count());
586+
}
587+
536588
#[test]
537589
fn test_prune() {
538590
let connection = Connection::open(":memory:").unwrap();
@@ -599,6 +651,23 @@ mod tests {
599651
.into_iter()
600652
.rev()
601653
.collect::<Vec<(Epoch, ProtocolParameters)>>()
602-
)
654+
);
655+
656+
for epoch_setting in &epoch_settings {
657+
assert!(epoch_setting_store_adapter
658+
.remove(&epoch_setting.0)
659+
.await
660+
.is_ok());
661+
}
662+
663+
assert_eq!(
664+
0,
665+
epoch_setting_store_adapter
666+
.get_last_n_records(epoch_settings.len())
667+
.await
668+
.unwrap()
669+
.into_iter()
670+
.len()
671+
);
603672
}
604673
}

0 commit comments

Comments
 (0)