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