im-online: use EstimateNextSessionRotation to get better estimates of session progress (#8242)

* frame-support: add method to estimate current session progress

* im-online: use EstimateNextSessionRotation trait to delay heartbeats

* node: fix im-online pallet instantiation

* frame-support: fix docs

* frame: fix tests

* pallet-session: last block of periodic session means 100% session progress

* pallet-session: add test for periodic session progress

* pallet-babe: fix epoch progress and add test

* frame-support: return weight with session estimates

* pallet-im-online: add test for session progress logic
This commit is contained in:
André Silva
2021-03-12 11:50:07 +00:00
committed by GitHub
parent a4e8875897
commit 5182209788
12 changed files with 403 additions and 142 deletions
+48 -6
View File
@@ -253,7 +253,6 @@ fn session_changed_flag_works() {
#[test]
fn periodic_session_works() {
frame_support::parameter_types! {
const Period: u64 = 10;
const Offset: u64 = 3;
@@ -261,24 +260,67 @@ fn periodic_session_works() {
type P = PeriodicSessions<Period, Offset>;
// make sure that offset phase behaves correctly
for i in 0u64..3 {
assert!(!P::should_end_session(i));
assert_eq!(P::estimate_next_session_rotation(i).unwrap(), 3);
assert_eq!(P::estimate_next_session_rotation(i).0.unwrap(), 3);
// the last block of the session (i.e. the one before session rotation)
// should have progress 100%.
if P::estimate_next_session_rotation(i).0.unwrap() - 1 == i {
assert_eq!(
P::estimate_current_session_progress(i).0.unwrap(),
Percent::from_percent(100)
);
} else {
assert!(
P::estimate_current_session_progress(i).0.unwrap() < Percent::from_percent(100)
);
}
}
// we end the session at block #3 and we consider this block the first one
// from the next session. since we're past the offset phase it represents
// 1/10 of progress.
assert!(P::should_end_session(3u64));
assert_eq!(P::estimate_next_session_rotation(3u64).unwrap(), 3);
assert_eq!(P::estimate_next_session_rotation(3u64).0.unwrap(), 3);
assert_eq!(
P::estimate_current_session_progress(3u64).0.unwrap(),
Percent::from_percent(10),
);
for i in (1u64..10).map(|i| 3 + i) {
assert!(!P::should_end_session(i));
assert_eq!(P::estimate_next_session_rotation(i).unwrap(), 13);
assert_eq!(P::estimate_next_session_rotation(i).0.unwrap(), 13);
// as with the offset phase the last block of the session must have 100%
// progress.
if P::estimate_next_session_rotation(i).0.unwrap() - 1 == i {
assert_eq!(
P::estimate_current_session_progress(i).0.unwrap(),
Percent::from_percent(100)
);
} else {
assert!(
P::estimate_current_session_progress(i).0.unwrap() < Percent::from_percent(100)
);
}
}
// the new session starts and we proceed in 1/10 increments.
assert!(P::should_end_session(13u64));
assert_eq!(P::estimate_next_session_rotation(13u64).unwrap(), 23);
assert_eq!(P::estimate_next_session_rotation(13u64).0.unwrap(), 23);
assert_eq!(
P::estimate_current_session_progress(13u64).0.unwrap(),
Percent::from_percent(10)
);
assert!(!P::should_end_session(14u64));
assert_eq!(P::estimate_next_session_rotation(14u64).unwrap(), 23);
assert_eq!(P::estimate_next_session_rotation(14u64).0.unwrap(), 23);
assert_eq!(
P::estimate_current_session_progress(14u64).0.unwrap(),
Percent::from_percent(20)
);
}
#[test]