From 0aa2336f7c947b59a42ed8ca4d4c066b9860478a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Tue, 5 Nov 2019 12:16:46 +0100 Subject: [PATCH] Limit longevity of im-online heartbeats. (#4011) * Limit longevity of im-online heartbeats. * Unused import. * Use parameter for session duration. --- node/runtime/src/lib.rs | 5 +++++ srml/im-online/src/lib.rs | 20 ++++++++++++++++---- srml/im-online/src/mock.rs | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index dfed5b4678bc6..02a0fa7cf0e30 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -419,12 +419,17 @@ impl sudo::Trait for Runtime { type SubmitTransaction = TransactionSubmitter; +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 { diff --git a/srml/im-online/src/lib.rs b/srml/im-online/src/lib.rs index b90c23279814f..3d8cf99bc5fca 100644 --- a/srml/im-online/src/lib.rs +++ b/srml/im-online/src/lib.rs @@ -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::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; + /// A type that gives us the ability to submit unresponsiveness offence reports. type ReportUnresponsiveness: ReportOffence< @@ -519,7 +527,11 @@ impl session::OneSessionHandler for Module { where I: Iterator { // Tell the offchain worker to start making the next session's heartbeats. - >::put(>::block_number()); + // Since we consider producing blocks as being online, + // the hearbeat is defered a bit to prevent spaming. + let block_number = >::block_number(); + let half_session = T::SessionDuration::get() / 2.into(); + >::put(block_number + half_session); // Remember who the authorities are for the new session. Keys::::put(validators.map(|x| x.1).collect::>()); @@ -596,7 +608,7 @@ impl support::unsigned::ValidateUnsigned for Module { priority: TransactionPriority::max_value(), requires: vec![], provides: vec![(current_session, authority_id).encode()], - longevity: TransactionLongevity::max_value(), + longevity: TryInto::::try_into(T::SessionDuration::get() / 2.into()).unwrap_or(64_u64), propagate: true, }) } else { diff --git a/srml/im-online/src/mock.rs b/srml/im-online/src/mock.rs index 233e055f887f1..4be33c44ed59e 100644 --- a/srml/im-online/src/mock.rs +++ b/srml/im-online/src/mock.rs @@ -162,6 +162,7 @@ impl Trait for Runtime { type Call = Call; type SubmitTransaction = SubmitTransaction; type ReportUnresponsiveness = OffenceHandler; + type SessionDuration = Period; } /// Im Online module.