mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 15:47:58 +00:00
im-online: send heartbeats at a random period (#8819)
* im-online: send heartbeats at a random period * support: use permill to represent session progress * im-online: increase probability of heartbeating with session progress * babe, session: fix tests * babe: fix test
This commit is contained in:
@@ -118,7 +118,7 @@ use sp_std::{prelude::*, marker::PhantomData, ops::{Sub, Rem}};
|
||||
use codec::Decode;
|
||||
use sp_runtime::{
|
||||
traits::{AtLeast32BitUnsigned, Convert, Member, One, OpaqueKeys, Zero},
|
||||
KeyTypeId, Perbill, Percent, RuntimeAppPublic,
|
||||
KeyTypeId, Perbill, Permill, RuntimeAppPublic,
|
||||
};
|
||||
use sp_staking::SessionIndex;
|
||||
use frame_support::{
|
||||
@@ -168,7 +168,7 @@ impl<
|
||||
Period::get()
|
||||
}
|
||||
|
||||
fn estimate_current_session_progress(now: BlockNumber) -> (Option<Percent>, Weight) {
|
||||
fn estimate_current_session_progress(now: BlockNumber) -> (Option<Permill>, Weight) {
|
||||
let offset = Offset::get();
|
||||
let period = Period::get();
|
||||
|
||||
@@ -177,12 +177,12 @@ impl<
|
||||
// (0% is never returned).
|
||||
let progress = if now >= offset {
|
||||
let current = (now - offset) % period.clone() + One::one();
|
||||
Some(Percent::from_rational(
|
||||
Some(Permill::from_rational(
|
||||
current.clone(),
|
||||
period.clone(),
|
||||
))
|
||||
} else {
|
||||
Some(Percent::from_rational(
|
||||
Some(Permill::from_rational(
|
||||
now + One::one(),
|
||||
offset,
|
||||
))
|
||||
|
||||
@@ -274,11 +274,11 @@ fn periodic_session_works() {
|
||||
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)
|
||||
Permill::from_percent(100)
|
||||
);
|
||||
} else {
|
||||
assert!(
|
||||
P::estimate_current_session_progress(i).0.unwrap() < Percent::from_percent(100)
|
||||
P::estimate_current_session_progress(i).0.unwrap() < Permill::from_percent(100)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -290,7 +290,7 @@ fn periodic_session_works() {
|
||||
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),
|
||||
Permill::from_percent(10),
|
||||
);
|
||||
|
||||
for i in (1u64..10).map(|i| 3 + i) {
|
||||
@@ -302,11 +302,11 @@ fn periodic_session_works() {
|
||||
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)
|
||||
Permill::from_percent(100)
|
||||
);
|
||||
} else {
|
||||
assert!(
|
||||
P::estimate_current_session_progress(i).0.unwrap() < Percent::from_percent(100)
|
||||
P::estimate_current_session_progress(i).0.unwrap() < Permill::from_percent(100)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -316,14 +316,14 @@ fn periodic_session_works() {
|
||||
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)
|
||||
Permill::from_percent(10)
|
||||
);
|
||||
|
||||
assert!(!P::should_end_session(14u64));
|
||||
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)
|
||||
Permill::from_percent(20)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user