frame/beefy: prune entries in set id session mapping (#13411)

Add limit for the number of entries in the `SetIdSession` mapping.
For example, it can be set to the bonding duration (in sessions).

Signed-off-by: acatangiu <adrian@parity.io>
This commit is contained in:
Adrian Catangiu authored and GitHub committed 2023-02-21 10:50:41 +00:00
1 parent 7a10154188
commit 67c250fecb
4 files changed
+59 -1

No files matched your search

+33
View File
@@ -162,6 +162,39 @@ fn validator_set_updates_work() {
});
}
#[test]
fn cleans_up_old_set_id_session_mappings() {
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
let max_set_id_session_entries = MaxSetIdSessionEntries::get();
// we have 3 sessions per era
let era_limit = max_set_id_session_entries / 3;
// sanity check against division precision loss
assert_eq!(0, max_set_id_session_entries % 3);
// go through `max_set_id_session_entries` sessions
start_era(era_limit);
// we should have a session id mapping for all the set ids from
// `max_set_id_session_entries` eras we have observed
for i in 1..=max_set_id_session_entries {
assert!(Beefy::session_for_set(i as u64).is_some());
}
// go through another `max_set_id_session_entries` sessions
start_era(era_limit * 2);
// we should keep tracking the new mappings for new sessions
for i in max_set_id_session_entries + 1..=max_set_id_session_entries * 2 {
assert!(Beefy::session_for_set(i as u64).is_some());
}
// but the old ones should have been pruned by now
for i in 1..=max_set_id_session_entries {
assert!(Beefy::session_for_set(i as u64).is_none());
}
});
}
/// Returns a list with 3 authorities with known keys:
/// Alice, Bob and Charlie.
pub fn test_authorities() -> Vec<BeefyId> {