referrerpolicy=no-referrer-when-downgrade

pallet_im_online/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # I'm online Pallet
19//!
20//! If the local node is a validator (i.e. contains an authority key), this pallet
21//! gossips a heartbeat transaction with each new session. The heartbeat functions
22//! as a simple mechanism to signal that the node is online in the current era.
23//!
24//! Received heartbeats are tracked for one era and reset with each new era. The
25//! pallet exposes two public functions to query if a heartbeat has been received
26//! in the current era or session.
27//!
28//! The heartbeat is a signed transaction, which was signed using the session key
29//! and includes the recent best block number of the local validators chain.
30//! It is submitted as an Unsigned Transaction via off-chain workers.
31//!
32//! - [`Config`]
33//! - [`Call`]
34//! - [`Pallet`]
35//!
36//! ## Interface
37//!
38//! ### Public Functions
39//!
40//! - `is_online` - True if the validator sent a heartbeat in the current session.
41//!
42//! ## Usage
43//!
44//! ```
45//! use pallet_im_online::{self as im_online};
46//!
47//! #[frame_support::pallet]
48//! pub mod pallet {
49//! 	use super::*;
50//! 	use frame_support::pallet_prelude::*;
51//! 	use frame_system::pallet_prelude::*;
52//!
53//! 	#[pallet::pallet]
54//! 	pub struct Pallet<T>(_);
55//!
56//! 	#[pallet::config]
57//! 	pub trait Config: frame_system::Config + im_online::Config {}
58//!
59//! 	#[pallet::call]
60//! 	impl<T: Config> Pallet<T> {
61//! 		#[pallet::weight(0)]
62//! 		pub fn is_online(origin: OriginFor<T>, authority_index: u32) -> DispatchResult {
63//! 			let _sender = ensure_signed(origin)?;
64//! 			let _is_online = <im_online::Pallet<T>>::is_online(authority_index);
65//! 			Ok(())
66//! 		}
67//! 	}
68//! }
69//! # fn main() { }
70//! ```
71//!
72//! ## Dependencies
73//!
74//! This pallet depends on the [Session pallet](../pallet_session/index.html).
75
76// Ensure we're `no_std` when compiling for Wasm.
77#![cfg_attr(not(feature = "std"), no_std)]
78
79mod benchmarking;
80pub mod migration;
81mod mock;
82mod tests;
83pub mod weights;
84
85extern crate alloc;
86
87use alloc::{vec, vec::Vec};
88use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
89use frame_support::{
90	pallet_prelude::*,
91	traits::{
92		EstimateNextSessionRotation, Get, OneSessionHandler, ValidatorSet,
93		ValidatorSetWithIdentification,
94	},
95	BoundedSlice, WeakBoundedVec,
96};
97use frame_system::{
98	offchain::{CreateBare, SubmitTransaction},
99	pallet_prelude::*,
100};
101pub use pallet::*;
102use scale_info::TypeInfo;
103use sp_application_crypto::RuntimeAppPublic;
104use sp_runtime::{
105	offchain::storage::{MutateStorageError, StorageRetrievalError, StorageValueRef},
106	traits::{AtLeast32BitUnsigned, Convert, Saturating, TrailingZeroInput},
107	PerThing, Perbill, Permill, RuntimeDebug, SaturatedConversion,
108};
109use sp_staking::{
110	offence::{Kind, Offence, ReportOffence},
111	SessionIndex,
112};
113pub use weights::WeightInfo;
114
115pub mod sr25519 {
116	mod app_sr25519 {
117		use sp_application_crypto::{app_crypto, key_types::IM_ONLINE, sr25519};
118		app_crypto!(sr25519, IM_ONLINE);
119	}
120
121	sp_application_crypto::with_pair! {
122		/// An i'm online keypair using sr25519 as its crypto.
123		pub type AuthorityPair = app_sr25519::Pair;
124	}
125
126	/// An i'm online signature using sr25519 as its crypto.
127	pub type AuthoritySignature = app_sr25519::Signature;
128
129	/// An i'm online identifier using sr25519 as its crypto.
130	pub type AuthorityId = app_sr25519::Public;
131}
132
133pub mod ed25519 {
134	mod app_ed25519 {
135		use sp_application_crypto::{app_crypto, ed25519, key_types::IM_ONLINE};
136		app_crypto!(ed25519, IM_ONLINE);
137	}
138
139	sp_application_crypto::with_pair! {
140		/// An i'm online keypair using ed25519 as its crypto.
141		pub type AuthorityPair = app_ed25519::Pair;
142	}
143
144	/// An i'm online signature using ed25519 as its crypto.
145	pub type AuthoritySignature = app_ed25519::Signature;
146
147	/// An i'm online identifier using ed25519 as its crypto.
148	pub type AuthorityId = app_ed25519::Public;
149}
150
151const DB_PREFIX: &[u8] = b"parity/im-online-heartbeat/";
152/// How many blocks do we wait for heartbeat transaction to be included
153/// before sending another one.
154const INCLUDE_THRESHOLD: u32 = 3;
155
156/// Status of the offchain worker code.
157///
158/// This stores the block number at which heartbeat was requested and when the worker
159/// has actually managed to produce it.
160/// Note we store such status for every `authority_index` separately.
161#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
162struct HeartbeatStatus<BlockNumber> {
163	/// An index of the session that we are supposed to send heartbeat for.
164	pub session_index: SessionIndex,
165	/// A block number at which the heartbeat for that session has been actually sent.
166	///
167	/// It may be 0 in case the sending failed. In such case we should just retry
168	/// as soon as possible (i.e. in a worker running for the next block).
169	pub sent_at: BlockNumber,
170}
171
172impl<BlockNumber: PartialEq + AtLeast32BitUnsigned + Copy> HeartbeatStatus<BlockNumber> {
173	/// Returns true if heartbeat has been recently sent.
174	///
175	/// Parameters:
176	/// `session_index` - index of current session.
177	/// `now` - block at which the offchain worker is running.
178	///
179	/// This function will return `true` iff:
180	/// 1. the session index is the same (we don't care if it went up or down)
181	/// 2. the heartbeat has been sent recently (within the threshold)
182	///
183	/// The reasoning for 1. is that it's better to send an extra heartbeat than
184	/// to stall or not send one in case of a bug.
185	fn is_recent(&self, session_index: SessionIndex, now: BlockNumber) -> bool {
186		self.session_index == session_index && self.sent_at + INCLUDE_THRESHOLD.into() > now
187	}
188}
189
190/// Error which may occur while executing the off-chain code.
191#[cfg_attr(test, derive(PartialEq))]
192enum OffchainErr<BlockNumber> {
193	TooEarly,
194	WaitingForInclusion(BlockNumber),
195	AlreadyOnline(u32),
196	FailedSigning,
197	FailedToAcquireLock,
198	SubmitTransaction,
199}
200
201impl<BlockNumber: core::fmt::Debug> core::fmt::Debug for OffchainErr<BlockNumber> {
202	fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
203		match *self {
204			OffchainErr::TooEarly => write!(fmt, "Too early to send heartbeat."),
205			OffchainErr::WaitingForInclusion(ref block) => {
206				write!(fmt, "Heartbeat already sent at {:?}. Waiting for inclusion.", block)
207			},
208			OffchainErr::AlreadyOnline(auth_idx) => {
209				write!(fmt, "Authority {} is already online", auth_idx)
210			},
211			OffchainErr::FailedSigning => write!(fmt, "Failed to sign heartbeat"),
212			OffchainErr::FailedToAcquireLock => write!(fmt, "Failed to acquire lock"),
213			OffchainErr::SubmitTransaction => write!(fmt, "Failed to submit transaction"),
214		}
215	}
216}
217
218pub type AuthIndex = u32;
219
220/// Heartbeat which is sent/received.
221#[derive(Encode, Decode, DecodeWithMemTracking, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
222pub struct Heartbeat<BlockNumber>
223where
224	BlockNumber: PartialEq + Eq + Decode + Encode,
225{
226	/// Block number at the time heartbeat is created..
227	pub block_number: BlockNumber,
228	/// Index of the current session.
229	pub session_index: SessionIndex,
230	/// An index of the authority on the list of validators.
231	pub authority_index: AuthIndex,
232	/// The length of session validator set
233	pub validators_len: u32,
234}
235
236/// A type for representing the validator id in a session.
237pub type ValidatorId<T> = <<T as Config>::ValidatorSet as ValidatorSet<
238	<T as frame_system::Config>::AccountId,
239>>::ValidatorId;
240
241/// A tuple of (ValidatorId, Identification) where `Identification` is the full identification of
242/// `ValidatorId`.
243pub type IdentificationTuple<T> = (
244	ValidatorId<T>,
245	<<T as Config>::ValidatorSet as ValidatorSetWithIdentification<
246		<T as frame_system::Config>::AccountId,
247	>>::Identification,
248);
249
250type OffchainResult<T, A> = Result<A, OffchainErr<BlockNumberFor<T>>>;
251
252#[frame_support::pallet]
253pub mod pallet {
254	use super::*;
255
256	/// The in-code storage version.
257	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
258
259	#[pallet::pallet]
260	#[pallet::storage_version(STORAGE_VERSION)]
261	pub struct Pallet<T>(_);
262
263	#[pallet::config]
264	pub trait Config: CreateBare<Call<Self>> + frame_system::Config {
265		/// The identifier type for an authority.
266		type AuthorityId: Member
267			+ Parameter
268			+ RuntimeAppPublic
269			+ Ord
270			+ MaybeSerializeDeserialize
271			+ MaxEncodedLen;
272
273		/// The maximum number of keys that can be added.
274		type MaxKeys: Get<u32>;
275
276		/// The maximum number of peers to be stored in `ReceivedHeartbeats`
277		type MaxPeerInHeartbeats: Get<u32>;
278
279		/// The overarching event type.
280		#[allow(deprecated)]
281		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
282
283		/// A type for retrieving the validators supposed to be online in a session.
284		type ValidatorSet: ValidatorSetWithIdentification<Self::AccountId>;
285
286		/// A trait that allows us to estimate the current session progress and also the
287		/// average session length.
288		///
289		/// This parameter is used to determine the longevity of `heartbeat` transaction and a
290		/// rough time when we should start considering sending heartbeats, since the workers
291		/// avoids sending them at the very beginning of the session, assuming there is a
292		/// chance the authority will produce a block and they won't be necessary.
293		type NextSessionRotation: EstimateNextSessionRotation<BlockNumberFor<Self>>;
294
295		/// A type that gives us the ability to submit unresponsiveness offence reports.
296		type ReportUnresponsiveness: ReportOffence<
297			Self::AccountId,
298			IdentificationTuple<Self>,
299			UnresponsivenessOffence<IdentificationTuple<Self>>,
300		>;
301
302		/// A configuration for base priority of unsigned transactions.
303		///
304		/// This is exposed so that it can be tuned for particular runtime, when
305		/// multiple pallets send unsigned transactions.
306		#[pallet::constant]
307		type UnsignedPriority: Get<TransactionPriority>;
308
309		/// Weight information for extrinsics in this pallet.
310		type WeightInfo: WeightInfo;
311	}
312
313	#[pallet::event]
314	#[pallet::generate_deposit(pub(super) fn deposit_event)]
315	pub enum Event<T: Config> {
316		/// A new heartbeat was received from `AuthorityId`.
317		HeartbeatReceived { authority_id: T::AuthorityId },
318		/// At the end of the session, no offence was committed.
319		AllGood,
320		/// At the end of the session, at least one validator was found to be offline.
321		SomeOffline { offline: Vec<IdentificationTuple<T>> },
322	}
323
324	#[pallet::error]
325	pub enum Error<T> {
326		/// Non existent public key.
327		InvalidKey,
328		/// Duplicated heartbeat.
329		DuplicatedHeartbeat,
330	}
331
332	/// The block number after which it's ok to send heartbeats in the current
333	/// session.
334	///
335	/// At the beginning of each session we set this to a value that should fall
336	/// roughly in the middle of the session duration. The idea is to first wait for
337	/// the validators to produce a block in the current session, so that the
338	/// heartbeat later on will not be necessary.
339	///
340	/// This value will only be used as a fallback if we fail to get a proper session
341	/// progress estimate from `NextSessionRotation`, as those estimates should be
342	/// more accurate then the value we calculate for `HeartbeatAfter`.
343	#[pallet::storage]
344	pub type HeartbeatAfter<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
345
346	/// The current set of keys that may issue a heartbeat.
347	#[pallet::storage]
348	pub type Keys<T: Config> =
349		StorageValue<_, WeakBoundedVec<T::AuthorityId, T::MaxKeys>, ValueQuery>;
350
351	/// For each session index, we keep a mapping of `SessionIndex` and `AuthIndex`.
352	#[pallet::storage]
353	pub type ReceivedHeartbeats<T: Config> =
354		StorageDoubleMap<_, Twox64Concat, SessionIndex, Twox64Concat, AuthIndex, bool>;
355
356	/// For each session index, we keep a mapping of `ValidatorId<T>` to the
357	/// number of blocks authored by the given authority.
358	#[pallet::storage]
359	pub type AuthoredBlocks<T: Config> = StorageDoubleMap<
360		_,
361		Twox64Concat,
362		SessionIndex,
363		Twox64Concat,
364		ValidatorId<T>,
365		u32,
366		ValueQuery,
367	>;
368
369	#[pallet::genesis_config]
370	#[derive(frame_support::DefaultNoBound)]
371	pub struct GenesisConfig<T: Config> {
372		pub keys: Vec<T::AuthorityId>,
373	}
374
375	#[pallet::genesis_build]
376	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
377		fn build(&self) {
378			Pallet::<T>::initialize_keys(&self.keys);
379		}
380	}
381
382	#[pallet::call]
383	impl<T: Config> Pallet<T> {
384		/// ## Complexity:
385		/// - `O(K)` where K is length of `Keys` (heartbeat.validators_len)
386		///   - `O(K)`: decoding of length `K`
387		// NOTE: the weight includes the cost of validate_unsigned as it is part of the cost to
388		// import block with such an extrinsic.
389		#[pallet::call_index(0)]
390		#[pallet::weight(<T as Config>::WeightInfo::validate_unsigned_and_then_heartbeat(
391			heartbeat.validators_len,
392		))]
393		pub fn heartbeat(
394			origin: OriginFor<T>,
395			heartbeat: Heartbeat<BlockNumberFor<T>>,
396			// since signature verification is done in `validate_unsigned`
397			// we can skip doing it here again.
398			_signature: <T::AuthorityId as RuntimeAppPublic>::Signature,
399		) -> DispatchResult {
400			ensure_none(origin)?;
401
402			let current_session = T::ValidatorSet::session_index();
403			let exists =
404				ReceivedHeartbeats::<T>::contains_key(current_session, heartbeat.authority_index);
405			let keys = Keys::<T>::get();
406			let public = keys.get(heartbeat.authority_index as usize);
407			if let (false, Some(public)) = (exists, public) {
408				Self::deposit_event(Event::<T>::HeartbeatReceived { authority_id: public.clone() });
409
410				ReceivedHeartbeats::<T>::insert(current_session, heartbeat.authority_index, true);
411
412				Ok(())
413			} else if exists {
414				Err(Error::<T>::DuplicatedHeartbeat.into())
415			} else {
416				Err(Error::<T>::InvalidKey.into())
417			}
418		}
419	}
420
421	#[pallet::hooks]
422	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
423		fn offchain_worker(now: BlockNumberFor<T>) {
424			// Only send messages if we are a potential validator.
425			if sp_io::offchain::is_validator() {
426				for res in Self::send_heartbeats(now).into_iter().flatten() {
427					if let Err(e) = res {
428						log::debug!(
429							target: "runtime::im-online",
430							"Skipping heartbeat at {:?}: {:?}",
431							now,
432							e,
433						)
434					}
435				}
436			} else {
437				log::trace!(
438					target: "runtime::im-online",
439					"Skipping heartbeat at {:?}. Not a validator.",
440					now,
441				)
442			}
443		}
444	}
445
446	/// Invalid transaction custom error. Returned when validators_len field in heartbeat is
447	/// incorrect.
448	pub(crate) const INVALID_VALIDATORS_LEN: u8 = 10;
449
450	#[pallet::validate_unsigned]
451	impl<T: Config> ValidateUnsigned for Pallet<T> {
452		type Call = Call<T>;
453
454		fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
455			if let Call::heartbeat { heartbeat, signature } = call {
456				if <Pallet<T>>::is_online(heartbeat.authority_index) {
457					// we already received a heartbeat for this authority
458					return InvalidTransaction::Stale.into()
459				}
460
461				// check if session index from heartbeat is recent
462				let current_session = T::ValidatorSet::session_index();
463				if heartbeat.session_index != current_session {
464					return InvalidTransaction::Stale.into()
465				}
466
467				// verify that the incoming (unverified) pubkey is actually an authority id
468				let keys = Keys::<T>::get();
469				if keys.len() as u32 != heartbeat.validators_len {
470					return InvalidTransaction::Custom(INVALID_VALIDATORS_LEN).into()
471				}
472				let authority_id = match keys.get(heartbeat.authority_index as usize) {
473					Some(id) => id,
474					None => return InvalidTransaction::BadProof.into(),
475				};
476
477				// check signature (this is expensive so we do it last).
478				let signature_valid = heartbeat.using_encoded(|encoded_heartbeat| {
479					authority_id.verify(&encoded_heartbeat, signature)
480				});
481
482				if !signature_valid {
483					return InvalidTransaction::BadProof.into()
484				}
485
486				ValidTransaction::with_tag_prefix("ImOnline")
487					.priority(T::UnsignedPriority::get())
488					.and_provides((current_session, authority_id))
489					.longevity(
490						TryInto::<u64>::try_into(
491							T::NextSessionRotation::average_session_length() / 2u32.into(),
492						)
493						.unwrap_or(64_u64),
494					)
495					.propagate(true)
496					.build()
497			} else {
498				InvalidTransaction::Call.into()
499			}
500		}
501	}
502}
503
504/// Keep track of number of authored blocks per authority, uncles are counted as
505/// well since they're a valid proof of being online.
506impl<T: Config + pallet_authorship::Config>
507	pallet_authorship::EventHandler<ValidatorId<T>, BlockNumberFor<T>> for Pallet<T>
508{
509	fn note_author(author: ValidatorId<T>) {
510		Self::note_authorship(author);
511	}
512}
513
514impl<T: Config> Pallet<T> {
515	/// Returns `true` if a heartbeat has been received for the authority at
516	/// `authority_index` in the authorities series or if the authority has
517	/// authored at least one block, during the current session. Otherwise
518	/// `false`.
519	pub fn is_online(authority_index: AuthIndex) -> bool {
520		let current_validators = T::ValidatorSet::validators();
521
522		if authority_index >= current_validators.len() as u32 {
523			return false
524		}
525
526		let authority = &current_validators[authority_index as usize];
527
528		Self::is_online_aux(authority_index, authority)
529	}
530
531	fn is_online_aux(authority_index: AuthIndex, authority: &ValidatorId<T>) -> bool {
532		let current_session = T::ValidatorSet::session_index();
533
534		ReceivedHeartbeats::<T>::contains_key(current_session, authority_index) ||
535			AuthoredBlocks::<T>::get(current_session, authority) != 0
536	}
537
538	/// Returns `true` if a heartbeat has been received for the authority at `authority_index` in
539	/// the authorities series, during the current session. Otherwise `false`.
540	pub fn received_heartbeat_in_current_session(authority_index: AuthIndex) -> bool {
541		let current_session = T::ValidatorSet::session_index();
542		ReceivedHeartbeats::<T>::contains_key(current_session, authority_index)
543	}
544
545	/// Note that the given authority has authored a block in the current session.
546	fn note_authorship(author: ValidatorId<T>) {
547		let current_session = T::ValidatorSet::session_index();
548
549		AuthoredBlocks::<T>::mutate(current_session, author, |authored| *authored += 1);
550	}
551
552	pub(crate) fn send_heartbeats(
553		block_number: BlockNumberFor<T>,
554	) -> OffchainResult<T, impl Iterator<Item = OffchainResult<T, ()>>> {
555		const START_HEARTBEAT_RANDOM_PERIOD: Permill = Permill::from_percent(10);
556		const START_HEARTBEAT_FINAL_PERIOD: Permill = Permill::from_percent(80);
557
558		// this should give us a residual probability of 1/SESSION_LENGTH of sending an heartbeat,
559		// i.e. all heartbeats spread uniformly, over most of the session. as the session progresses
560		// the probability of sending an heartbeat starts to increase exponentially.
561		let random_choice = |progress: Permill| {
562			// given session progress `p` and session length `l`
563			// the threshold formula is: p^6 + 1/l
564			let session_length = T::NextSessionRotation::average_session_length();
565			let residual = Permill::from_rational(1u32, session_length.saturated_into());
566			let threshold: Permill = progress.saturating_pow(6).saturating_add(residual);
567
568			let seed = sp_io::offchain::random_seed();
569			let random = <u32>::decode(&mut TrailingZeroInput::new(seed.as_ref()))
570				.expect("input is padded with zeroes; qed");
571			let random = Permill::from_parts(random % Permill::ACCURACY);
572
573			random <= threshold
574		};
575
576		let should_heartbeat = if let (Some(progress), _) =
577			T::NextSessionRotation::estimate_current_session_progress(block_number)
578		{
579			// we try to get an estimate of the current session progress first since it should
580			// provide more accurate results. we will start an early heartbeat period where we'll
581			// randomly pick whether to heartbeat. after 80% of the session has elapsed, if we
582			// haven't sent an heartbeat yet we'll send one unconditionally. the idea is to prevent
583			// all nodes from sending the heartbeats at the same block and causing a temporary (but
584			// deterministic) spike in transactions.
585			progress >= START_HEARTBEAT_FINAL_PERIOD ||
586				progress >= START_HEARTBEAT_RANDOM_PERIOD && random_choice(progress)
587		} else {
588			// otherwise we fallback to using the block number calculated at the beginning
589			// of the session that should roughly correspond to the middle of the session
590			let heartbeat_after = <HeartbeatAfter<T>>::get();
591			block_number >= heartbeat_after
592		};
593
594		if !should_heartbeat {
595			return Err(OffchainErr::TooEarly)
596		}
597
598		let session_index = T::ValidatorSet::session_index();
599		let validators_len = Keys::<T>::decode_len().unwrap_or_default() as u32;
600
601		Ok(Self::local_authority_keys().map(move |(authority_index, key)| {
602			Self::send_single_heartbeat(
603				authority_index,
604				key,
605				session_index,
606				block_number,
607				validators_len,
608			)
609		}))
610	}
611
612	fn send_single_heartbeat(
613		authority_index: u32,
614		key: T::AuthorityId,
615		session_index: SessionIndex,
616		block_number: BlockNumberFor<T>,
617		validators_len: u32,
618	) -> OffchainResult<T, ()> {
619		// A helper function to prepare heartbeat call.
620		let prepare_heartbeat = || -> OffchainResult<T, Call<T>> {
621			let heartbeat =
622				Heartbeat { block_number, session_index, authority_index, validators_len };
623
624			let signature = key.sign(&heartbeat.encode()).ok_or(OffchainErr::FailedSigning)?;
625
626			Ok(Call::heartbeat { heartbeat, signature })
627		};
628
629		if Self::is_online(authority_index) {
630			return Err(OffchainErr::AlreadyOnline(authority_index))
631		}
632
633		// acquire lock for that authority at current heartbeat to make sure we don't
634		// send concurrent heartbeats.
635		Self::with_heartbeat_lock(authority_index, session_index, block_number, || {
636			let call = prepare_heartbeat()?;
637			log::info!(
638				target: "runtime::im-online",
639				"[index: {:?}] Reporting im-online at block: {:?} (session: {:?}): {:?}",
640				authority_index,
641				block_number,
642				session_index,
643				call,
644			);
645
646			let xt = T::create_bare(call.into());
647			SubmitTransaction::<T, Call<T>>::submit_transaction(xt)
648				.map_err(|_| OffchainErr::SubmitTransaction)?;
649
650			Ok(())
651		})
652	}
653
654	fn local_authority_keys() -> impl Iterator<Item = (u32, T::AuthorityId)> {
655		// on-chain storage
656		//
657		// At index `idx`:
658		// 1. A (ImOnline) public key to be used by a validator at index `idx` to send im-online
659		//    heartbeats.
660		let authorities = Keys::<T>::get();
661
662		// local keystore
663		//
664		// All `ImOnline` public (+private) keys currently in the local keystore.
665		let mut local_keys = T::AuthorityId::all();
666
667		local_keys.sort();
668
669		authorities.into_iter().enumerate().filter_map(move |(index, authority)| {
670			local_keys
671				.binary_search(&authority)
672				.ok()
673				.map(|location| (index as u32, local_keys[location].clone()))
674		})
675	}
676
677	fn with_heartbeat_lock<R>(
678		authority_index: u32,
679		session_index: SessionIndex,
680		now: BlockNumberFor<T>,
681		f: impl FnOnce() -> OffchainResult<T, R>,
682	) -> OffchainResult<T, R> {
683		let key = {
684			let mut key = DB_PREFIX.to_vec();
685			key.extend(authority_index.encode());
686			key
687		};
688		let storage = StorageValueRef::persistent(&key);
689		let res = storage.mutate(
690			|status: Result<Option<HeartbeatStatus<BlockNumberFor<T>>>, StorageRetrievalError>| {
691				// Check if there is already a lock for that particular block.
692				// This means that the heartbeat has already been sent, and we are just waiting
693				// for it to be included. However if it doesn't get included for INCLUDE_THRESHOLD
694				// we will re-send it.
695				match status {
696					// we are still waiting for inclusion.
697					Ok(Some(status)) if status.is_recent(session_index, now) =>
698						Err(OffchainErr::WaitingForInclusion(status.sent_at)),
699					// attempt to set new status
700					_ => Ok(HeartbeatStatus { session_index, sent_at: now }),
701				}
702			},
703		);
704		if let Err(MutateStorageError::ValueFunctionFailed(err)) = res {
705			return Err(err)
706		}
707
708		let mut new_status = res.map_err(|_| OffchainErr::FailedToAcquireLock)?;
709
710		// we got the lock, let's try to send the heartbeat.
711		let res = f();
712
713		// clear the lock in case we have failed to send transaction.
714		if res.is_err() {
715			new_status.sent_at = 0u32.into();
716			storage.set(&new_status);
717		}
718
719		res
720	}
721
722	fn initialize_keys(keys: &[T::AuthorityId]) {
723		if !keys.is_empty() {
724			assert!(Keys::<T>::get().is_empty(), "Keys are already initialized!");
725			let bounded_keys = <BoundedSlice<'_, _, T::MaxKeys>>::try_from(keys)
726				.expect("More than the maximum number of keys provided");
727			Keys::<T>::put(bounded_keys);
728		}
729	}
730
731	#[cfg(test)]
732	fn set_keys(keys: Vec<T::AuthorityId>) {
733		let bounded_keys = WeakBoundedVec::<_, T::MaxKeys>::try_from(keys)
734			.expect("More than the maximum number of keys provided");
735		Keys::<T>::put(bounded_keys);
736	}
737}
738
739impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
740	type Public = T::AuthorityId;
741}
742
743impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
744	type Key = T::AuthorityId;
745
746	fn on_genesis_session<'a, I: 'a>(validators: I)
747	where
748		I: Iterator<Item = (&'a T::AccountId, T::AuthorityId)>,
749	{
750		let keys = validators.map(|x| x.1).collect::<Vec<_>>();
751		Self::initialize_keys(&keys);
752	}
753
754	fn on_new_session<'a, I: 'a>(_changed: bool, validators: I, _queued_validators: I)
755	where
756		I: Iterator<Item = (&'a T::AccountId, T::AuthorityId)>,
757	{
758		// Tell the offchain worker to start making the next session's heartbeats.
759		// Since we consider producing blocks as being online,
760		// the heartbeat is deferred a bit to prevent spamming.
761		let block_number = <frame_system::Pallet<T>>::block_number();
762		let half_session = T::NextSessionRotation::average_session_length() / 2u32.into();
763		<HeartbeatAfter<T>>::put(block_number + half_session);
764
765		// Remember who the authorities are for the new session.
766		let keys = validators.map(|x| x.1).collect::<Vec<_>>();
767		let bounded_keys = WeakBoundedVec::<_, T::MaxKeys>::force_from(
768			keys,
769			Some(
770				"Warning: The session has more keys than expected. \
771  				A runtime configuration adjustment may be needed.",
772			),
773		);
774		Keys::<T>::put(bounded_keys);
775	}
776
777	fn on_before_session_ending() {
778		let session_index = T::ValidatorSet::session_index();
779		let keys = Keys::<T>::get();
780		let current_validators = T::ValidatorSet::validators();
781
782		let offenders = current_validators
783			.into_iter()
784			.enumerate()
785			.filter(|(index, id)| !Self::is_online_aux(*index as u32, id))
786			.filter_map(|(_, id)| {
787				<T::ValidatorSet as ValidatorSetWithIdentification<T::AccountId>>::IdentificationOf::convert(
788					id.clone()
789				).map(|full_id| (id, full_id))
790			})
791			.collect::<Vec<IdentificationTuple<T>>>();
792
793		// Remove all received heartbeats and number of authored blocks from the
794		// current session, they have already been processed and won't be needed
795		// anymore.
796		#[allow(deprecated)]
797		ReceivedHeartbeats::<T>::remove_prefix(T::ValidatorSet::session_index(), None);
798		#[allow(deprecated)]
799		AuthoredBlocks::<T>::remove_prefix(T::ValidatorSet::session_index(), None);
800
801		if offenders.is_empty() {
802			Self::deposit_event(Event::<T>::AllGood);
803		} else {
804			Self::deposit_event(Event::<T>::SomeOffline { offline: offenders.clone() });
805
806			let validator_set_count = keys.len() as u32;
807			let offence = UnresponsivenessOffence { session_index, validator_set_count, offenders };
808			if let Err(e) = T::ReportUnresponsiveness::report_offence(vec![], offence) {
809				sp_runtime::print(e);
810			}
811		}
812	}
813
814	fn on_disabled(_i: u32) {
815		// ignore
816	}
817}
818
819/// An offence that is filed if a validator didn't send a heartbeat message.
820#[derive(RuntimeDebug, TypeInfo)]
821#[cfg_attr(feature = "std", derive(Clone, PartialEq, Eq))]
822pub struct UnresponsivenessOffence<Offender> {
823	/// The current session index in which we report the unresponsive validators.
824	///
825	/// It acts as a time measure for unresponsiveness reports and effectively will always point
826	/// at the end of the session.
827	pub session_index: SessionIndex,
828	/// The size of the validator set in current session/era.
829	pub validator_set_count: u32,
830	/// Authorities that were unresponsive during the current era.
831	pub offenders: Vec<Offender>,
832}
833
834impl<Offender: Clone> Offence<Offender> for UnresponsivenessOffence<Offender> {
835	const ID: Kind = *b"im-online:offlin";
836	type TimeSlot = SessionIndex;
837
838	fn offenders(&self) -> Vec<Offender> {
839		self.offenders.clone()
840	}
841
842	fn session_index(&self) -> SessionIndex {
843		self.session_index
844	}
845
846	fn validator_set_count(&self) -> u32 {
847		self.validator_set_count
848	}
849
850	fn time_slot(&self) -> Self::TimeSlot {
851		self.session_index
852	}
853
854	fn slash_fraction(&self, offenders: u32) -> Perbill {
855		// the formula is min((3 * (k - (n / 10 + 1))) / n, 1) * 0.07
856		// basically, 10% can be offline with no slash, but after that, it linearly climbs up to 7%
857		// when 13/30 are offline (around 5% when 1/3 are offline).
858		if let Some(threshold) = offenders.checked_sub(self.validator_set_count / 10 + 1) {
859			let x = Perbill::from_rational(3 * threshold, self.validator_set_count);
860			x.saturating_mul(Perbill::from_percent(7))
861		} else {
862			Perbill::default()
863		}
864	}
865}