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
+18 -7
View File
@@ -34,7 +34,7 @@ use sp_application_crypto::Public;
use sp_runtime::{
generic::DigestItem,
traits::{IsMember, One, SaturatedConversion, Saturating, Zero},
ConsensusEngineId, KeyTypeId,
ConsensusEngineId, KeyTypeId, Percent,
};
use sp_session::{GetSessionNumber, GetValidatorCount};
use sp_std::prelude::*;
@@ -780,14 +780,25 @@ impl<T: Config> frame_support::traits::EstimateNextSessionRotation<T::BlockNumbe
T::EpochDuration::get().saturated_into()
}
fn estimate_next_session_rotation(now: T::BlockNumber) -> Option<T::BlockNumber> {
Self::next_expected_epoch_change(now)
fn estimate_current_session_progress(_now: T::BlockNumber) -> (Option<Percent>, Weight) {
let elapsed = CurrentSlot::get().saturating_sub(Self::current_epoch_start()) + 1;
(
Some(Percent::from_rational_approximation(
*elapsed,
T::EpochDuration::get(),
)),
// Read: Current Slot, Epoch Index, Genesis Slot
T::DbWeight::get().reads(3),
)
}
// The validity of this weight depends on the implementation of `estimate_next_session_rotation`
fn weight(_now: T::BlockNumber) -> Weight {
// Read: Current Slot, Epoch Index, Genesis Slot
T::DbWeight::get().reads(3)
fn estimate_next_session_rotation(now: T::BlockNumber) -> (Option<T::BlockNumber>, Weight) {
(
Self::next_expected_epoch_change(now),
// Read: Current Slot, Epoch Index, Genesis Slot
T::DbWeight::get().reads(3),
)
}
}