|
17 | 17 | use crate::{ |
18 | 18 | BeaconConfig, BeaconConfigurationPayload, BeaconInfoResponse, Call, DrandResponseBody, |
19 | 19 | ENDPOINTS, Error, HasMigrationRun, LastStoredRound, MAX_KEPT_PULSES, OldestStoredRound, Pulse, |
20 | | - Pulses, PulsesPayload, QUICKNET_CHAIN_HASH, migrations::migrate_prune_old_pulses, mock::*, |
| 20 | + Pulses, PulsesPayload, QUICKNET_CHAIN_HASH, migrations::migrate_prune_old_pulses, |
| 21 | + migrations::migrate_set_oldest_round, mock::*, |
21 | 22 | }; |
22 | 23 | use codec::Encode; |
23 | 24 | use frame_support::{ |
@@ -705,3 +706,40 @@ fn test_prune_maximum_of_100_pulses_per_call() { |
705 | 706 | ); |
706 | 707 | }); |
707 | 708 | } |
| 709 | + |
| 710 | +#[test] |
| 711 | +fn test_migrate_set_oldest_round() { |
| 712 | + new_test_ext().execute_with(|| { |
| 713 | + let migration_name = BoundedVec::truncate_from(b"migrate_set_oldest_round".to_vec()); |
| 714 | + let db_weight: RuntimeDbWeight = <Test as frame_system::Config>::DbWeight::get(); |
| 715 | + let pulse = Pulse::default(); |
| 716 | + |
| 717 | + assert_eq!(Pulses::<Test>::iter().count(), 0); |
| 718 | + assert!(!HasMigrationRun::<Test>::get(&migration_name)); |
| 719 | + assert_eq!(OldestStoredRound::<Test>::get(), 0); |
| 720 | + assert_eq!(LastStoredRound::<Test>::get(), 0); |
| 721 | + |
| 722 | + // Insert out-of-order rounds: oldest should be 5 |
| 723 | + for r in [10u64, 7, 5].into_iter() { |
| 724 | + Pulses::<Test>::insert(r, pulse.clone()); |
| 725 | + } |
| 726 | + let num_rounds = 3u64; |
| 727 | + |
| 728 | + // Run migration |
| 729 | + let weight = migrate_set_oldest_round::<Test>(); |
| 730 | + |
| 731 | + assert_eq!(OldestStoredRound::<Test>::get(), 5); |
| 732 | + // Migration does NOT touch LastStoredRound |
| 733 | + assert_eq!(LastStoredRound::<Test>::get(), 0); |
| 734 | + // Pulses untouched |
| 735 | + assert!(Pulses::<Test>::contains_key(5)); |
| 736 | + assert!(Pulses::<Test>::contains_key(7)); |
| 737 | + assert!(Pulses::<Test>::contains_key(10)); |
| 738 | + // Flag set |
| 739 | + assert!(HasMigrationRun::<Test>::get(&migration_name)); |
| 740 | + |
| 741 | + // Weight: reads(1 + num_rounds) + writes(2) [Oldest + HasMigrationRun] |
| 742 | + let expected = db_weight.reads(1 + num_rounds) + db_weight.writes(2); |
| 743 | + assert_eq!(weight, expected); |
| 744 | + }); |
| 745 | +} |
0 commit comments