@@ -242,39 +242,42 @@ impl SPDDStore {
242242mod tests {
243243 use super :: * ;
244244 use acropolis_common:: crypto:: keyhash_224;
245- use acropolis_common:: { NetworkId , PoolId , StakeCredential } ;
246-
247- const DB_PATH : & str = "spdd_db" ;
245+ use acropolis_common:: NetworkId :: Mainnet ;
246+ use acropolis_common :: { PoolId , StakeCredential } ;
247+ use tempfile :: TempDir ;
248248
249249 fn test_pool_hash ( byte : u8 ) -> PoolId {
250250 keyhash_224 ( & [ byte] ) . into ( )
251251 }
252252
253- fn test_stake_address ( byte : u8 , network : NetworkId ) -> StakeAddress {
254- StakeAddress :: new ( StakeCredential :: AddrKeyHash ( keyhash_224 ( & [ byte] ) ) , network )
253+ fn test_stake_address ( byte : u8 ) -> StakeAddress {
254+ StakeAddress :: new ( StakeCredential :: AddrKeyHash ( keyhash_224 ( & [ byte] ) ) , Mainnet )
255255 }
256256
257257 #[ test]
258- fn test_store_spdd_state ( ) {
258+ fn test_store_and_query_spdd ( ) {
259+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
259260 let mut spdd_store =
260- SPDDStore :: new ( std:: path:: Path :: new ( DB_PATH ) , 1 ) . expect ( "Failed to create SPDD store" ) ;
261- let mut spdd_state: HashMap < PoolId , Vec < ( StakeAddress , u64 ) > > = HashMap :: new ( ) ;
261+ SPDDStore :: new ( temp_dir. path ( ) , 10 ) . expect ( "Failed to create SPDD store" ) ;
262262
263+ let mut spdd_state: HashMap < PoolId , Vec < ( StakeAddress , u64 ) > > = HashMap :: new ( ) ;
263264 spdd_state. insert (
264265 test_pool_hash ( 0x01 ) ,
265266 vec ! [
266- ( test_stake_address( 0x10 , NetworkId :: Mainnet ) , 100 ) ,
267- ( test_stake_address( 0x11 , NetworkId :: Mainnet ) , 150 ) ,
267+ ( test_stake_address( 0x10 ) , 100 ) ,
268+ ( test_stake_address( 0x11 ) , 150 ) ,
268269 ] ,
269270 ) ;
270271 spdd_state. insert (
271272 test_pool_hash ( 0x02 ) ,
272273 vec ! [
273- ( test_stake_address( 0x20 , NetworkId :: Testnet ) , 200 ) ,
274- ( test_stake_address( 0x21 , NetworkId :: Testnet ) , 250 ) ,
274+ ( test_stake_address( 0x20 ) , 200 ) ,
275+ ( test_stake_address( 0x21 ) , 250 ) ,
275276 ] ,
276277 ) ;
278+
277279 assert ! ( spdd_store. store_spdd( 1 , spdd_state) . is_ok( ) ) ;
280+ assert ! ( spdd_store. is_epoch_complete( 1 ) . unwrap( ) ) ;
278281
279282 let result = spdd_store. query_by_epoch ( 1 ) . unwrap ( ) ;
280283 assert_eq ! ( result. len( ) , 4 ) ;
@@ -286,31 +289,83 @@ mod tests {
286289 }
287290
288291 #[ test]
289- fn test_prune_old_epochs ( ) {
290- let mut spdd_store = SPDDStore :: new ( std:: path:: Path :: new ( "spdd_prune_test_db" ) , 2 )
291- . expect ( "Failed to create SPDD store" ) ;
292+ fn test_retention_pruning ( ) {
293+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
294+ let mut spdd_store =
295+ SPDDStore :: new ( temp_dir. path ( ) , 2 ) . expect ( "Failed to create SPDD store" ) ;
292296
297+ // Store epochs 1, 2, 3
293298 for epoch in 1 ..=3 {
294299 let mut spdd_state: HashMap < PoolId , Vec < ( StakeAddress , u64 ) > > = HashMap :: new ( ) ;
295-
296300 spdd_state. insert (
297301 test_pool_hash ( epoch as u8 ) ,
298302 vec ! [
299- ( test_stake_address( 0x10 , NetworkId :: Mainnet ) , epoch * 100 ) ,
300- ( test_stake_address( 0x11 , NetworkId :: Mainnet ) , epoch * 150 ) ,
303+ ( test_stake_address( 0x10 ) , epoch * 100 ) ,
304+ ( test_stake_address( 0x11 ) , epoch * 150 ) ,
301305 ] ,
302306 ) ;
303307 spdd_store. store_spdd ( epoch, spdd_state) . expect ( "Failed to store SPDD state" ) ;
304308 }
305309
310+ // Epoch 1 should be pruned (retention=2, so keep epochs 2 and 3)
306311 assert ! ( !spdd_store. is_epoch_complete( 1 ) . unwrap( ) ) ;
307312 assert ! ( spdd_store. is_epoch_complete( 2 ) . unwrap( ) ) ;
308313 assert ! ( spdd_store. is_epoch_complete( 3 ) . unwrap( ) ) ;
309314
310315 assert ! ( spdd_store. query_by_epoch( 1 ) . is_err( ) ) ;
311- let result = spdd_store. query_by_epoch ( 2 ) . unwrap ( ) ;
312- assert_eq ! ( result. len( ) , 2 ) ;
313- let result = spdd_store. query_by_epoch ( 3 ) . unwrap ( ) ;
314- assert_eq ! ( result. len( ) , 2 ) ;
316+ assert ! ( spdd_store. query_by_epoch( 2 ) . is_ok( ) ) ;
317+ assert ! ( spdd_store. query_by_epoch( 3 ) . is_ok( ) ) ;
318+ }
319+
320+ #[ test]
321+ fn test_query_incomplete_epoch ( ) {
322+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
323+ let spdd_store = SPDDStore :: new ( temp_dir. path ( ) , 10 ) . expect ( "Failed to create SPDD store" ) ;
324+
325+ assert ! ( !spdd_store. is_epoch_complete( 999 ) . unwrap( ) ) ;
326+ assert ! ( spdd_store. query_by_epoch( 999 ) . is_err( ) ) ;
327+ assert ! ( spdd_store. query_by_epoch_and_pool( 999 , & test_pool_hash( 0x01 ) ) . is_err( ) ) ;
328+ }
329+
330+ #[ test]
331+ fn test_remove_epoch_data ( ) {
332+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
333+ let mut spdd_store =
334+ SPDDStore :: new ( temp_dir. path ( ) , 10 ) . expect ( "Failed to create SPDD store" ) ;
335+
336+ let mut spdd_state: HashMap < PoolId , Vec < ( StakeAddress , u64 ) > > = HashMap :: new ( ) ;
337+ spdd_state. insert (
338+ test_pool_hash ( 0x01 ) ,
339+ vec ! [
340+ ( test_stake_address( 0x10 ) , 100 ) ,
341+ ( test_stake_address( 0x11 ) , 150 ) ,
342+ ] ,
343+ ) ;
344+
345+ spdd_store. store_spdd ( 1 , spdd_state) . unwrap ( ) ;
346+ assert ! ( spdd_store. is_epoch_complete( 1 ) . unwrap( ) ) ;
347+
348+ let deleted = spdd_store. remove_epoch_data ( 1 ) . unwrap ( ) ;
349+ assert_eq ! ( deleted, 2 ) ;
350+ assert ! ( !spdd_store. is_epoch_complete( 1 ) . unwrap( ) ) ;
351+
352+ // Removing nonexistent epoch returns 0
353+ let deleted = spdd_store. remove_epoch_data ( 999 ) . unwrap ( ) ;
354+ assert_eq ! ( deleted, 0 ) ;
355+ }
356+
357+ #[ test]
358+ fn test_encode_decode_roundtrip ( ) {
359+ // Test normal values
360+ let epoch = 12345u64 ;
361+ let pool_id = test_pool_hash ( 0x42 ) ;
362+ let stake_address = test_stake_address ( 0x99 ) ;
363+
364+ let encoded = encode_key ( epoch, & pool_id, & stake_address) ;
365+ let ( decoded_epoch, decoded_pool, decoded_stake) = decode_key ( & encoded) . unwrap ( ) ;
366+
367+ assert_eq ! ( decoded_epoch, epoch) ;
368+ assert_eq ! ( decoded_pool, pool_id) ;
369+ assert_eq ! ( decoded_stake, stake_address) ;
315370 }
316371}
0 commit comments