Limit longevity of im-online heartbeats. (#4011)

* Limit longevity of im-online heartbeats.

* Unused import.

* Use parameter for session duration.
This commit is contained in:
Tomasz Drwięga
2019-11-05 12:16:46 +01:00
committed by Gavin Wood
parent b92674d88a
commit 279391f9c1
3 changed files with 22 additions and 4 deletions
+5
View File
@@ -419,12 +419,17 @@ impl sudo::Trait for Runtime {
type SubmitTransaction = TransactionSubmitter<ImOnlineId, Runtime, UncheckedExtrinsic>;
parameter_types! {
pub const SessionDuration: BlockNumber = EPOCH_DURATION_IN_SLOTS as _;
}
impl im_online::Trait for Runtime {
type AuthorityId = ImOnlineId;
type Call = Call;
type Event = Event;
type SubmitTransaction = SubmitTransaction;
type ReportUnresponsiveness = Offences;
type SessionDuration = SessionDuration;
}
impl offences::Trait for Runtime {
+16 -4
View File
@@ -74,12 +74,13 @@ use app_crypto::RuntimeAppPublic;
use codec::{Encode, Decode};
use primitives::offchain::{OpaqueNetworkState, StorageKind};
use rstd::prelude::*;
use rstd::convert::TryInto;
use session::historical::IdentificationTuple;
use sr_primitives::{
RuntimeDebug,
traits::{Convert, Member, Printable, Saturating}, Perbill,
transaction_validity::{
TransactionValidity, TransactionLongevity, ValidTransaction, InvalidTransaction,
TransactionValidity, ValidTransaction, InvalidTransaction,
TransactionPriority,
},
};
@@ -88,7 +89,8 @@ use sr_staking_primitives::{
offence::{ReportOffence, Offence, Kind},
};
use support::{
decl_module, decl_event, decl_storage, print, Parameter, debug
decl_module, decl_event, decl_storage, print, Parameter, debug,
traits::Get,
};
use system::ensure_none;
use system::offchain::SubmitUnsignedTransaction;
@@ -188,6 +190,12 @@ pub trait Trait: system::Trait + session::historical::Trait {
/// A transaction submitter.
type SubmitTransaction: SubmitUnsignedTransaction<Self, <Self as Trait>::Call>;
/// An expected duration of the session.
///
/// This parameter is used to determine the longevity of `heartbeat` transaction
/// and a rough time when the heartbeat should be sent.
type SessionDuration: Get<Self::BlockNumber>;
/// A type that gives us the ability to submit unresponsiveness offence reports.
type ReportUnresponsiveness:
ReportOffence<
@@ -519,7 +527,11 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
where I: Iterator<Item=(&'a T::AccountId, T::AuthorityId)>
{
// Tell the offchain worker to start making the next session's heartbeats.
<GossipAt<T>>::put(<system::Module<T>>::block_number());
// Since we consider producing blocks as being online,
// the hearbeat is defered a bit to prevent spaming.
let block_number = <system::Module<T>>::block_number();
let half_session = T::SessionDuration::get() / 2.into();
<GossipAt<T>>::put(block_number + half_session);
// Remember who the authorities are for the new session.
Keys::<T>::put(validators.map(|x| x.1).collect::<Vec<_>>());
@@ -596,7 +608,7 @@ impl<T: Trait> support::unsigned::ValidateUnsigned for Module<T> {
priority: TransactionPriority::max_value(),
requires: vec![],
provides: vec![(current_session, authority_id).encode()],
longevity: TransactionLongevity::max_value(),
longevity: TryInto::<u64>::try_into(T::SessionDuration::get() / 2.into()).unwrap_or(64_u64),
propagate: true,
})
} else {
+1
View File
@@ -162,6 +162,7 @@ impl Trait for Runtime {
type Call = Call;
type SubmitTransaction = SubmitTransaction;
type ReportUnresponsiveness = OffenceHandler;
type SessionDuration = Period;
}
/// Im Online module.