Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Limit longevity of im-online heartbeats. #4011

Merged
merged 3 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 16 additions & 4 deletions srml/im-online/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
Expand All @@ -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;
Expand Down Expand Up @@ -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<
Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this prevent spamming?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of sending heartbeats at the beginning of the session we will wait a bit and only the validators that didn't have a chance to produce a block so far will send the hearbeats. So roughly we will decrease the amount of "spammy" heartbeat transactions by half - I consider all of those transactions spammy, as they are just part of the inner workings and just "take place" for users transactions / introduce a bit of noise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh makes sense :)

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<_>>());
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions srml/im-online/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl Trait for Runtime {
type Call = Call;
type SubmitTransaction = SubmitTransaction;
type ReportUnresponsiveness = OffenceHandler;
type SessionDuration = Period;
}

/// Im Online module.
Expand Down