@@ -15,12 +15,16 @@ use crate::services::{EpochPruningTask, SignedBeaconStore};
1515/// A [SignedBeaconStore] implementation using SQLite.
1616pub struct SignedBeaconRepository {
1717 connection : Arc < SqliteConnection > ,
18+ store_retention_limit : Option < u64 > ,
1819}
1920
2021impl SignedBeaconRepository {
2122 /// Create a new instance of the `SignedBeaconRepository`.
22- pub fn new ( connection : Arc < SqliteConnection > ) -> Self {
23- Self { connection }
23+ pub fn new ( connection : Arc < SqliteConnection > , store_retention_limit : Option < u64 > ) -> Self {
24+ Self {
25+ connection,
26+ store_retention_limit,
27+ }
2428 }
2529
2630 /// Get the last signed beacon.
@@ -71,8 +75,14 @@ impl EpochPruningTask for SignedBeaconRepository {
7175 "Signed Beacon"
7276 }
7377
74- async fn prune_below_epoch_threshold ( & self , epoch_threshold : Epoch ) -> StdResult < ( ) > {
75- self . prune_below_epoch ( epoch_threshold)
78+ async fn prune ( & self , current_epoch : Epoch ) -> StdResult < ( ) > {
79+ match self
80+ . store_retention_limit
81+ . map ( |limit| current_epoch - limit)
82+ {
83+ Some ( threshold) if * threshold > 0 => self . prune_below_epoch ( threshold) ,
84+ _ => Ok ( ( ) ) ,
85+ }
7686 }
7787}
7888
@@ -102,7 +112,7 @@ mod tests {
102112 #[ test]
103113 fn get_last_stored_signed_beacon ( ) {
104114 let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
105- let repository = SignedBeaconRepository :: new ( connection. clone ( ) ) ;
115+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , None ) ;
106116
107117 let last_signed_beacon = repository. get_last ( ) . unwrap ( ) ;
108118 assert_eq ! ( None , last_signed_beacon) ;
@@ -151,7 +161,7 @@ mod tests {
151161 #[ tokio:: test]
152162 async fn filter_out_nothing_if_nothing_was_previously_signed ( ) {
153163 let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
154- let repository = SignedBeaconRepository :: new ( connection. clone ( ) ) ;
164+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , None ) ;
155165
156166 let to_filter = all_signed_entity_type_for ( & TimePoint :: dummy ( ) ) ;
157167 let available_entities = repository
@@ -165,7 +175,7 @@ mod tests {
165175 #[ tokio:: test]
166176 async fn filter_out_nothing_if_previously_signed_entities_doesnt_match_passed_entities ( ) {
167177 let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
168- let repository = SignedBeaconRepository :: new ( connection. clone ( ) ) ;
178+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , None ) ;
169179
170180 let time_point = TimePoint :: dummy ( ) ;
171181 insert_signed_beacons (
@@ -189,7 +199,7 @@ mod tests {
189199 #[ tokio:: test]
190200 async fn filter_out_everything_if_previously_signed_entities_match_all_passed_entities ( ) {
191201 let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
192- let repository = SignedBeaconRepository :: new ( connection. clone ( ) ) ;
202+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , None ) ;
193203
194204 let to_filter = all_signed_entity_type_for ( & TimePoint :: dummy ( ) ) ;
195205 insert_signed_beacons (
@@ -210,7 +220,7 @@ mod tests {
210220 #[ tokio:: test]
211221 async fn filter_out_partially_if_some_previously_signed_entities_match_passed_entities ( ) {
212222 let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
213- let repository = SignedBeaconRepository :: new ( connection. clone ( ) ) ;
223+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , None ) ;
214224
215225 let time_point = TimePoint :: dummy ( ) ;
216226 let signed_beacons = [
@@ -253,7 +263,7 @@ mod tests {
253263 #[ tokio:: test]
254264 async fn mark_beacon_as_signed ( ) {
255265 let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
256- let repository = SignedBeaconRepository :: new ( connection. clone ( ) ) ;
266+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , None ) ;
257267
258268 let beacon_to_sign = BeaconToSign {
259269 epoch : Epoch ( 13 ) ,
@@ -278,10 +288,50 @@ mod tests {
278288 assert_eq ! ( beacon_to_sign, signed_beacon) ;
279289 }
280290
281- #[ test]
282- fn test_prune ( ) {
291+ #[ tokio:: test]
292+ async fn test_dont_execute_pruning_tasks_if_no_retention_limit_set ( ) {
293+ let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
294+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , None ) ;
295+ insert_signed_beacons (
296+ & connection,
297+ SignedBeaconRecord :: fakes ( & [ (
298+ Epoch ( 8 ) ,
299+ vec ! [ SignedEntityType :: MithrilStakeDistribution ( Epoch ( 8 ) ) ] ,
300+ ) ] ) ,
301+ ) ;
302+
303+ EpochPruningTask :: prune ( & repository, Epoch ( 1000 ) )
304+ . await
305+ . unwrap ( ) ;
306+
307+ let cursor = connection. fetch ( GetSignedBeaconQuery :: all ( ) ) . unwrap ( ) ;
308+ assert_eq ! ( 1 , cursor. count( ) , ) ;
309+ }
310+
311+ #[ tokio:: test]
312+ async fn test_dont_execute_pruning_tasks_if_current_epoch_minus_retention_limit_is_0 ( ) {
283313 let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
284- let repository = SignedBeaconRepository :: new ( connection. clone ( ) ) ;
314+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , Some ( 10 ) ) ;
315+ insert_signed_beacons (
316+ & connection,
317+ SignedBeaconRecord :: fakes ( & [ (
318+ Epoch ( 8 ) ,
319+ vec ! [ SignedEntityType :: MithrilStakeDistribution ( Epoch ( 8 ) ) ] ,
320+ ) ] ) ,
321+ ) ;
322+
323+ EpochPruningTask :: prune ( & repository, Epoch ( 9 ) )
324+ . await
325+ . unwrap ( ) ;
326+
327+ let cursor = connection. fetch ( GetSignedBeaconQuery :: all ( ) ) . unwrap ( ) ;
328+ assert_eq ! ( 1 , cursor. count( ) , ) ;
329+ }
330+
331+ #[ tokio:: test]
332+ async fn test_prune_task_substract_set_retention_limit_to_given_epoch ( ) {
333+ let connection = Arc :: new ( main_db_connection ( ) . unwrap ( ) ) ;
334+ let repository = SignedBeaconRepository :: new ( connection. clone ( ) , Some ( 10 ) ) ;
285335 insert_signed_beacons (
286336 & connection,
287337 SignedBeaconRecord :: fakes ( & [
@@ -299,7 +349,9 @@ mod tests {
299349 ] ) ,
300350 ) ;
301351
302- repository. prune_below_epoch ( Epoch ( 8 ) ) . unwrap ( ) ;
352+ EpochPruningTask :: prune ( & repository, Epoch ( 18 ) )
353+ . await
354+ . unwrap ( ) ;
303355
304356 let signed_beacons: Vec < SignedBeaconRecord > = connection
305357 . fetch_collect ( GetSignedBeaconQuery :: all ( ) )
0 commit comments