pallet_session_validator_management/
lib.rs

1//! Pallet for setting the Partner Chain validators using inherent data
2//!
3//! # Purpose of the pallet
4//!
5//! This pallet provides a mechanism to rotate Partner Chain's block producing committees
6//! based on candidate registrations and chain configuration sourced from Cardano. It works
7//! by integrating with `pallet_session` as a [SessionManager] and [ShouldEndSession] to provide it
8//! with committee information and to rotate sessions. In addition to managing the sessions,
9//! the pallet automatically registers session keys of all active block producers, alleviating
10//! the need for manual key registration, and ensures that all necessary chain-local accounts
11//! exist.
12//!
13//! # Committee selection overview
14//!
15//! Committees are selected for sessions corresponding roughly to Partner Chain epochs, whose
16//! duration is configurable for each Partner Chain. Due to the way session rotation works in
17//! `pallet_session`, these sessions are delayed by *2 blocks* relative to their respective
18//! epoch.
19//!
20//! Committees are selected based on the following inputs sourced from Cardano:
21//! - `Registered candidates`:
22//!   Cardano SPOs who have registered themselves as willing to participate as block producers.
23//!   These candidates need to control an ADA stake pool to be eligible for selection to a
24//!   committee, and their chance at securing a seat is proportional to their pool's size.
25//!   This candidate group corresponds to a typical "trustless" Proof of Stake block producers.
26//! - `Permissioned candidates`:
27//!   A list of trusted block producers that do not need to register themselves or control any
28//!   ADA stake on Cardano to be eligible for a Partner Chain committee.
29//!   This candidate group serves a special role as trusted block producers during initial phase
30//!   of a Partner Chain's lifetime (when there may not be enough registered candidates to ensure
31//!   proper security and decentralization of the network), and are intended to be phased out as
32//!   the number of trustless participants grows.
33//! - `D-Parameter`:
34//!   A pair of two values `R` and `P`, controlling the number of committee seats alloted for
35//!   registered and permissioned candidates respectively, which means that a committee has R+P
36//!   seats overall. This parameter gives the Partner Chain the ability to bootstrap itself using
37//!   an initial pool of permissioned candidates running trusted nodes, and then gradually shift
38//!   to registered (trustless) candidates when proper decentralization is achieved
39//! - `randomness seed`:
40//!   All randomness when selecting the committee is seeded from data sourced from Cardano so that
41//!   it is tamper-proof and agreed upon by all nodes.
42//!
43//! The permissioned candidate list and the D-Parameter are controlled by the Partner Chain's
44//! governance authority and are crucial in ensuring the chain's security in initial phases of
45//! its existence
46//!
47//! # Observability parameters
48//!
49//! All input data used when selecting a committee of a Partner Chain is sourced from Cardano.
50//! To correctly identify it, each node needs access to the current values of:
51//! - `the registration validator address`, at which all registration UTXOs are located
52//! - `the D-Parameter minting policy`, whose tokens mark the UTXO containing D-Parameter value
53//! - `the permissioned candidate minting policy`, whose tokens mark the UTXO containing the
54//!   permissioned candidate list
55//!
56//! These values are stored in the pallet storage, ensuring that they're available for all nodes
57//! to use and agreed upon through the consensus mechanism, and can be updated using a governance
58//! level extrinsic [set_main_chain_scripts].
59//!
60//! # Usage
61//!
62//! ## Prerequisites
63//!
64//! This pallet's operation requires the appropriate inherent data provider and data source
65//! be present in the node. As this pallet is crucial for the operation of the chain itself,
66//! these must be present before at the chain start or before the pallet is migrated to, to
67//! avoid down time. See documentation of `sp_session_validator_management` for information
68//! on how to add the IDP to your node. A Db-Sync-based data source implementation is provided
69//! by the `partner_chains_db_sync_data_sources` crate.
70//!
71//! Aside from the node components, the pallet requires the Partner Chain smart contracts to
72//! have been initialized on Cardano and that at least one candidate - either a registered or
73//! permissioned one - exists. See `docs/user-guides/governance/governance.md` and
74//! `docs/user-guides/chain-builder.md` for more information about governance and how to set
75//! up the Partner Chain on Cardano.
76//!
77//! ## Adding into the runtime
78//!
79//! ### Defining key types
80//!
81//! As with a stock Substrate chain, a Partner Chain needs to define its session keys. What
82//! these keys are depends on the consensus mechanisms used by the chain. For a Partner Chain
83//! using Aura as its consensus with a Grandpa finality gadget, the session keys can be defined
84//! as following:
85//!
86//! ```rust, ignore
87//! sp_runtime::impl_opaque_keys! {
88//! 	#[derive(MaxEncodedLen, PartialOrd, Ord)]
89//! 	pub struct SessionKeys {
90//! 		pub aura: Aura
91//! 		pub grandpa: Grandpa,
92//! 	}
93//! }
94//! ```
95//!
96//! ### Adding the pallet
97//!
98//! The pallet should be added to the runtime _before_ `pallet_session`, but after the consensus
99//! pallets used by the chain:
100//!
101//! ```rust, ignore
102//! construct_runtime!(
103//! 	pub struct Runtime {
104//! 		System: frame_system,
105//! 		Timestamp: pallet_timestamp,
106//! 		Aura: pallet_aura,
107//! 		Grandpa: pallet_grandpa,
108//! 		Sidechain: pallet_sidechain,
109//! 		SessionCommitteeManagement: pallet_session_validator_management,
110//! 		Session: pallet_session exclude_parts { Call },
111//!         // ... other pallets
112//! 	}
113//! );
114//! ```
115//!
116//! *Important*:
117//! It is recommended that when `pallet_session` is wired into the runtime, its extrinsics are
118//! hidden, using `exclude_parts` like in the example above. This ensures that chain users can't
119//! manually register their keys in the pallet and so the registrations done on Cardano remain
120//! the sole source of truth about key ownership. Proper operation in presence of manually set
121//! user keys is not guaranteed by the toolkit and its behavior is left unspecified.
122//!
123//! ### Configuring the pallet
124//!
125//! Configuring the pallet is straightforward and mostly consists of passing to it types already
126//! defined by other crates and in previous steps:
127//!
128//! ```rust, ignore
129//! use sidechain_domain::cross_chain_app::Public as CrossChainPublic;
130//!
131//! impl pallet_session_validator_management::Config for Runtime {
132//! 	type MaxValidators = MaxValidators;
133//! 	type AuthorityId = CrossChainPublic;
134//! 	type AuthorityKeys = SessionKeys;
135//! 	type WeightInfo = pallet_session_validator_management::weights::SubstrateWeight<Runtime>;
136//! 	type MainChainScriptsOrigin = EnsureRoot<Self::AccountId>;
137//!
138//! 	fn select_authorities(
139//! 		input: AuthoritySelectionInputs,
140//! 		sidechain_epoch: ScEpochNumber,
141//! 	) -> Option<BoundedVec<CommitteeMemberOf<Self>, Self::MaxValidators>> {
142//! 		authority_selection_inherents::select_authorities::<CrossChainPublic, SessionKeys, MaxValidators>(
143//! 			Sidechain::genesis_utxo(),
144//! 			input,
145//! 			sidechain_epoch,
146//! 		)
147//! 	}
148//!
149//! 	fn current_epoch_number() -> ScEpochNumber {
150//! 		Sidechain::current_epoch_number()
151//! 	}
152//! }
153//! ```
154//!
155//! One value that needs to be decided upon by the chain builder is `MaxValidators` which dictates
156//! the maximum size of a committee. This value should be higher than the P + R of the D-Parameter
157//! used and should be adjusted accordingly before any D-Parameter changes that would exceed the
158//! previous value. In case a committee selected is bigger than `MaxValidators`, it will be truncated,
159//! potentially leading to a skewed seat allocation and threatening the security of the consensus.
160//!
161//! ## Genesis configuration
162//!
163//! Genesis config can be created programmatically:
164//!
165//! ```rust
166//! # use sp_session_validator_management::{ MainChainScripts, CommitteeMember };
167//! # use pallet_session_validator_management::{ Config, GenesisConfig };
168//! # fn create_genesis_config<T: Config>(cross_chain_pubkey_1: T::AuthorityId, session_keys_1: T::AuthorityKeys) -> GenesisConfig<T> {
169//! GenesisConfig {
170//! 	initial_authorities: vec![
171//!        CommitteeMember::permissioned(cross_chain_pubkey_1, session_keys_1),
172//!     ],
173//! 	main_chain_scripts: MainChainScripts::read_from_env().unwrap(),
174//! }
175//! # }
176//! ```
177//!
178//! However, it is more typical for production chains to define their specs using Json. In that case
179//! an example configuration could look like this:
180//!
181//! ```json
182//! {
183//!   "initialAuthorities": [
184//!     {
185//!       "Permissioned": {
186//!         "id": "KW39r9CJjAVzmkf9zQ4YDb2hqfAVGdRqn53eRqyruqpxAP5YL",
187//!         "keys": {
188//!           "aura": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
189//!           "grandpa": "5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu"
190//!         }
191//!       }
192//!     }
193//!   ],
194//!   "mainChainScripts": {
195//!     "committee_candidate_address": "addr_test1wrp8p2c5h7encl55gv26d5fpz9r99jxjcm0rxgny3993dxs2xy42p",
196//!     "d_parameter_policy_id": "0x434dc797fd036b0b654c005551ec08f39d25fa7f0eecdf4b170d46cf",
197//!     "permissioned_candidates_policy_id": "0xe1ce5d1b8b3e93a7493ecc11556790f915aabbc44a56b0b5145770b2"
198//!   }
199//! }
200//! ```
201//!
202//! *Important*:
203//! Notice, that since the pallet's operation is necessary for block production, all main chain script
204//! values and at least one initial authority (block producer) must be provided by the genesis config.
205//!
206//!
207//! ## Updating pallet configuration
208//!
209//! ### MaxValidators
210//!
211//! The maximum number of committee seats. As this value is not typically expected to change, it is
212//! configured as part of the pallet's [Config]. This means that it can only be updated as part of a
213//! runtime upgrade. The chain builder should release a new runtime version with this value updated
214//! and the Partner Chain's governance mechanism should be used to apply it using [set_code].
215//!
216//! ### Main chain scripts
217//!
218//! The main chain scripts can change over time as the Partner Chain migrates to new versions of the
219//! Partner Chain smart contracts, either due to bug fixes or new features being released. This is
220//! necessary, because the script addresses are derived by hashing their Plutus code and are affected
221//! by any change made to it.
222//!
223//! The scripts are updated by invoking the [set_main_chain_scripts] extrinsic using the Partner Chain's
224//! governance mechanism.
225//!
226//! *Important*: Setting incorrect main chain script values will result in stalling block production
227//!              indefinitely, requiring a network-wide roll-back. As such, main chain scripts update
228//!              should be carried out with special care.
229//!
230//! [SessionManager]: pallet_session::SessionManager
231//! [set_code]: frame_system::Pallet::set_code
232
233#![cfg_attr(not(feature = "std"), no_std)]
234#![allow(clippy::type_complexity)]
235#![deny(missing_docs)]
236
237pub mod migrations;
238/// [`pallet_session`] integration.
239pub mod pallet_session_support;
240
241pub use pallet::*;
242
243#[cfg(test)]
244mod mock;
245
246#[cfg(feature = "runtime-benchmarks")]
247mod benchmarking;
248
249#[cfg(test)]
250mod tests;
251
252pub mod weights;
253
254use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
255use scale_info::TypeInfo;
256pub use sp_session_validator_management::CommitteeMember;
257pub use weights::WeightInfo;
258
259#[frame_support::pallet]
260pub mod pallet {
261	use super::*;
262	use frame_support::pallet_prelude::*;
263	use frame_system::pallet_prelude::*;
264	use log::{info, warn};
265	use sidechain_domain::byte_string::SizedByteString;
266	use sidechain_domain::{MainchainAddress, PolicyId, ScEpochNumber};
267	use sp_core::blake2_256;
268	use sp_runtime::traits::{MaybeSerializeDeserialize, One, Zero};
269	use sp_session_validator_management::*;
270	use sp_std::collections::btree_set::BTreeSet;
271	use sp_std::vec::Vec;
272
273	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
274
275	#[pallet::pallet]
276	#[pallet::storage_version(STORAGE_VERSION)]
277	pub struct Pallet<T>(_);
278
279	#[pallet::config]
280	pub trait Config: frame_system::Config {
281		#[pallet::constant]
282		/// Maximum amount of validators.
283		type MaxValidators: Get<u32>;
284		/// Type identifying authorities.
285		type AuthorityId: Member
286			+ Parameter
287			+ MaybeSerializeDeserialize
288			+ MaxEncodedLen
289			+ Ord
290			+ Into<Self::AccountId>;
291		/// Type of authority keys.
292		type AuthorityKeys: Parameter + Member + MaybeSerializeDeserialize + Ord + MaxEncodedLen;
293
294		/// Origin for governance calls
295		type MainChainScriptsOrigin: EnsureOrigin<Self::RuntimeOrigin>;
296
297		/// Should select a committee for `sidechain_epoch` based on selection inputs `input`.
298		/// Should return [None] if selection was impossible for the given input.
299		fn select_authorities(
300			input: AuthoritySelectionInputs,
301			sidechain_epoch: ScEpochNumber,
302		) -> Option<BoundedVec<CommitteeMemberOf<Self>, Self::MaxValidators>>;
303
304		/// Should return the current partner chain epoch.
305		fn current_epoch_number() -> ScEpochNumber;
306
307		/// Weight functions needed for pallet_session_validator_management.
308		type WeightInfo: WeightInfo;
309
310		/// Helper for creating mock data used by benchmarks
311		#[cfg(feature = "runtime-benchmarks")]
312		type BenchmarkHelper: benchmarking::BenchmarkHelper<Self>;
313	}
314
315	#[pallet::event]
316	pub enum Event<T: Config> {}
317
318	use frame_support::{BoundedVec, CloneNoBound};
319	use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
320	use scale_info::TypeInfo;
321
322	/// Committee member type used by the pallet
323	pub type CommitteeMemberOf<T> =
324		CommitteeMember<<T as Config>::AuthorityId, <T as Config>::AuthorityKeys>;
325
326	#[derive(CloneNoBound, Encode, Decode, TypeInfo, MaxEncodedLen)]
327	#[scale_info(skip_type_params(MaxValidators))]
328	/// Committee info type used on-chain.
329	pub struct CommitteeInfo<AuthorityId: Clone, AuthorityKeys: Clone, MaxValidators> {
330		/// Epoch number the committee is selected for.
331		pub epoch: ScEpochNumber,
332		/// List of committee members.
333		pub committee: BoundedVec<CommitteeMember<AuthorityId, AuthorityKeys>, MaxValidators>,
334	}
335
336	/// Committee information type used by the pallet
337	pub type CommitteeInfoOf<T> = CommitteeInfo<
338		<T as Config>::AuthorityId,
339		<T as Config>::AuthorityKeys,
340		<T as Config>::MaxValidators,
341	>;
342
343	impl<AuthorityId: Clone, AuthorityKeys: Clone, MaxValidators>
344		CommitteeInfo<AuthorityId, AuthorityKeys, MaxValidators>
345	{
346		/// Returns committee info as a pair of epoch number and list of committee members
347		pub fn as_pair(self) -> (ScEpochNumber, Vec<CommitteeMember<AuthorityId, AuthorityKeys>>) {
348			(self.epoch, self.committee.to_vec())
349		}
350	}
351
352	impl<AuthorityId: Clone, AuthorityKeys: Clone, MaxValidators> Default
353		for CommitteeInfo<AuthorityId, AuthorityKeys, MaxValidators>
354	{
355		fn default() -> Self {
356			Self { epoch: ScEpochNumber::zero(), committee: BoundedVec::new() }
357		}
358	}
359
360	#[pallet::storage]
361	pub type ProvidedAccounts<T: Config> =
362		StorageValue<_, BoundedBTreeSet<T::AccountId, T::MaxValidators>, ValueQuery>;
363
364	#[pallet::storage]
365	pub type CurrentCommittee<T: Config> = StorageValue<_, CommitteeInfoOf<T>, ValueQuery>;
366
367	#[pallet::storage]
368	pub type NextCommittee<T: Config> = StorageValue<_, CommitteeInfoOf<T>, OptionQuery>;
369
370	/// Stores the stage of handling the inputs change. Used by session manager, to decide
371	/// if the session should be ended quickly, to speed up using the newly selected committee.
372	#[pallet::storage]
373	pub type CommitteeRotationStage<T: Config> =
374		StorageValue<_, CommitteeRotationStages, ValueQuery>;
375
376	#[pallet::storage]
377	pub type MainChainScriptsConfiguration<T: Config> =
378		StorageValue<_, MainChainScripts, ValueQuery>;
379
380	/// Stores the current version of `AuthorityKeys` type.
381	///
382	/// This value is different from the pallet's storage version and is only used for versioning
383	/// `AuthorityKeys` which can change independently from other pallet storages during evolution
384	/// of a Partner Chain.
385	///
386	/// This value should only be modified when the `AuthorityKeys` is changed, by scheduling
387	/// [AuthorityKeysMigration] during runtime upgrade.
388	///
389	/// [AuthorityKeysMigration]: migrations::authority_keys::AuthorityKeysMigration
390	#[pallet::storage]
391	pub type AuthorityKeysVersion<T: Config> = StorageValue<_, u32, ValueQuery, GetDefault>;
392
393	#[pallet::error]
394	pub enum Error<T> {
395		/// [Pallet::set] has been called with epoch number that is not current epoch + 1
396		InvalidEpoch,
397		/// [Pallet::set] has been called a second time for the same next epoch
398		NextCommitteeAlreadySet,
399	}
400
401	#[pallet::genesis_config]
402	#[derive(frame_support::DefaultNoBound)]
403	pub struct GenesisConfig<T: Config> {
404		/// Initial committee members of the partner chain.
405		pub initial_authorities: Vec<CommitteeMemberOf<T>>,
406		/// Initial [MainChainScripts] of the partner chain.
407		pub main_chain_scripts: MainChainScripts,
408	}
409
410	#[pallet::genesis_build]
411	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
412		fn build(&self) {
413			let initial_authorities = BoundedVec::truncate_from(self.initial_authorities.clone());
414
415			let provided_accounts: BTreeSet<T::AccountId> =
416				initial_authorities.iter().map(|m| m.authority_id().into()).collect();
417			for account in &provided_accounts {
418				frame_system::Pallet::<T>::inc_providers(&account);
419			}
420			ProvidedAccounts::<T>::set(provided_accounts.try_into().unwrap());
421
422			let committee_info =
423				CommitteeInfo { epoch: ScEpochNumber::zero(), committee: initial_authorities };
424			CurrentCommittee::<T>::put(committee_info);
425			MainChainScriptsConfiguration::<T>::put(self.main_chain_scripts.clone());
426		}
427	}
428
429	#[pallet::hooks]
430	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
431		// Only reason for this hook is to set the genesis committee as the committee for first block's epoch.
432		fn on_initialize(block_nr: BlockNumberFor<T>) -> Weight {
433			if block_nr.is_one() {
434				CurrentCommittee::<T>::mutate(|committee| {
435					committee.epoch = T::current_epoch_number();
436				});
437				T::DbWeight::get().reads_writes(2, 1)
438			} else {
439				Weight::zero()
440			}
441		}
442	}
443
444	#[pallet::inherent]
445	impl<T: Config> ProvideInherent for Pallet<T> {
446		type Call = Call<T>;
447		type Error = InherentError;
448		const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
449
450		/// Responsible for calling [Call::set] on each block by the block author, if the validator list changed
451		fn create_inherent(data: &InherentData) -> Option<Self::Call> {
452			if NextCommittee::<T>::exists() {
453				None
454			} else {
455				let for_epoch_number = CurrentCommittee::<T>::get().epoch + One::one();
456				let (authority_selection_inputs, selection_inputs_hash) =
457					Self::inherent_data_to_authority_selection_inputs(data);
458				if let Some(validators) =
459					T::select_authorities(authority_selection_inputs, for_epoch_number)
460				{
461					Some(Call::set { validators, for_epoch_number, selection_inputs_hash })
462				} else {
463					let current_committee = CurrentCommittee::<T>::get();
464					let current_committee_epoch = current_committee.epoch;
465					warn!(
466						"Committee for epoch {for_epoch_number} is the same as for epoch {current_committee_epoch}"
467					);
468					let validators = current_committee.committee;
469					Some(Call::set { validators, for_epoch_number, selection_inputs_hash })
470				}
471			}
472		}
473
474		fn check_inherent(call: &Self::Call, data: &InherentData) -> Result<(), Self::Error> {
475			let (validators_param, for_epoch_number_param, call_selection_inputs_hash) = match call
476			{
477				Call::set { validators, for_epoch_number, selection_inputs_hash } => {
478					(validators, for_epoch_number, selection_inputs_hash)
479				},
480				_ => return Ok(()),
481			};
482
483			let (authority_selection_inputs, computed_selection_inputs_hash) =
484				Self::inherent_data_to_authority_selection_inputs(data);
485			let validators =
486				T::select_authorities(authority_selection_inputs, *for_epoch_number_param)
487					.unwrap_or_else(|| {
488						// Proposed block should keep the same committee if calculation of new one was impossible.
489						// This is code is executed before the committee rotation, so the NextCommittee should be used.
490						let committee_info = NextCommittee::<T>::get()
491							// Needed only for verification of the block no 1, before any `set` call is executed.
492							.unwrap_or_else(CurrentCommittee::<T>::get);
493						committee_info.committee
494					});
495
496			if *validators_param != validators {
497				if *call_selection_inputs_hash == computed_selection_inputs_hash {
498					return Err(InherentError::InvalidValidatorsMatchingHash(
499						computed_selection_inputs_hash,
500					));
501				} else {
502					return Err(InherentError::InvalidValidatorsHashMismatch(
503						computed_selection_inputs_hash,
504						call_selection_inputs_hash.clone(),
505					));
506				}
507			}
508
509			Ok(())
510		}
511
512		fn is_inherent(call: &Self::Call) -> bool {
513			matches!(call, Call::set { .. })
514		}
515
516		fn is_inherent_required(_: &InherentData) -> Result<Option<Self::Error>, Self::Error> {
517			if !NextCommittee::<T>::exists() {
518				Ok(Some(InherentError::CommitteeNeedsToBeStoredOneEpochInAdvance)) // change error
519			} else {
520				Ok(None)
521			}
522		}
523	}
524
525	#[pallet::call]
526	impl<T: Config> Pallet<T> {
527		/// 'for_epoch_number' parameter is needed only for validation purposes, because we need to make sure that
528		/// check_inherent uses the same epoch_number as was used to create inherent data.
529		/// Alternative approach would be to put epoch number inside InherentData. However, sidechain
530		/// epoch number is set in Runtime, thus, inherent data provider doesn't have to know about it.
531		/// On top of that, the latter approach is slightly more complicated to code.
532		#[pallet::call_index(0)]
533		#[pallet::weight((
534		T::WeightInfo::set(validators.len() as u32),
535		DispatchClass::Mandatory
536		))]
537		pub fn set(
538			origin: OriginFor<T>,
539			validators: BoundedVec<CommitteeMemberOf<T>, T::MaxValidators>,
540			for_epoch_number: ScEpochNumber,
541			selection_inputs_hash: SizedByteString<32>,
542		) -> DispatchResult {
543			ensure_none(origin)?;
544			let expected_epoch_number = CurrentCommittee::<T>::get().epoch + One::one();
545			ensure!(for_epoch_number == expected_epoch_number, Error::<T>::InvalidEpoch);
546			ensure!(!NextCommittee::<T>::exists(), Error::<T>::NextCommitteeAlreadySet);
547			let len = validators.len();
548			info!(
549				"💼 Storing committee of size {len} for epoch {for_epoch_number}, input data hash: {}",
550				selection_inputs_hash.to_hex_string()
551			);
552			NextCommittee::<T>::put(CommitteeInfo {
553				epoch: for_epoch_number,
554				committee: validators,
555			});
556			Ok(())
557		}
558
559		/// Changes the main chain scripts used for committee rotation.
560		///
561		/// This extrinsic must be run either using `sudo` or some other chain governance mechanism.
562		#[pallet::call_index(1)]
563		#[pallet::weight(T::WeightInfo::set_main_chain_scripts())]
564		pub fn set_main_chain_scripts(
565			origin: OriginFor<T>,
566			committee_candidate_address: MainchainAddress,
567			d_parameter_policy_id: PolicyId,
568			permissioned_candidates_policy_id: PolicyId,
569		) -> DispatchResult {
570			T::MainChainScriptsOrigin::ensure_origin(origin)?;
571			let new_scripts = MainChainScripts {
572				committee_candidate_address,
573				d_parameter_policy_id,
574				permissioned_candidates_policy_id,
575			};
576			MainChainScriptsConfiguration::<T>::put(new_scripts);
577			Ok(())
578		}
579	}
580
581	impl<T: Config> Pallet<T> {
582		/// Returns epoch number for which next committee hasn't been set yet.
583		pub fn get_next_unset_epoch_number() -> ScEpochNumber {
584			NextCommittee::<T>::get()
585				.map(|next_committee| next_committee.epoch + One::one())
586				.unwrap_or(CurrentCommittee::<T>::get().epoch + One::one())
587		}
588
589		/// Returns current committee member for an index, repeating them in a round-robin fashion if needed.
590		pub fn get_current_authority_round_robin(index: usize) -> Option<CommitteeMemberOf<T>> {
591			let committee = CurrentCommittee::<T>::get().committee;
592			if committee.is_empty() {
593				return None;
594			}
595
596			committee.get(index % committee.len() as usize).cloned()
597		}
598
599		/// Returns current committee from storage.
600		pub fn current_committee_storage() -> CommitteeInfoOf<T> {
601			CurrentCommittee::<T>::get()
602		}
603
604		/// Returns next committee from storage.
605		pub fn next_committee_storage()
606		-> Option<CommitteeInfo<T::AuthorityId, T::AuthorityKeys, T::MaxValidators>> {
607			NextCommittee::<T>::get()
608		}
609
610		/// Returns the `AuthorityId`s of next committee from storage.
611		///
612		/// This function's result should be always defined after inherent call of 1st block of each epoch
613		pub fn next_committee() -> Option<BoundedVec<T::AuthorityId, T::MaxValidators>> {
614			Some(BoundedVec::truncate_from(
615				NextCommittee::<T>::get()?
616					.committee
617					.into_iter()
618					.map(|member| member.authority_id())
619					.collect::<Vec<T::AuthorityId>>(),
620			))
621		}
622
623		fn inherent_data_to_authority_selection_inputs(
624			data: &InherentData,
625		) -> (AuthoritySelectionInputs, SizedByteString<32>) {
626			let decoded_data = data
627				.get_data::<AuthoritySelectionInputs>(&INHERENT_IDENTIFIER)
628				.expect("Validator inherent data not correctly encoded")
629				.expect("Validator inherent data must be provided");
630			let data_hash = SizedByteString(blake2_256(&decoded_data.encode()));
631
632			(decoded_data, data_hash)
633		}
634
635		/// Calculates committee using configured `select_authorities` function
636		pub fn calculate_committee(
637			authority_selection_inputs: AuthoritySelectionInputs,
638			sidechain_epoch: ScEpochNumber,
639		) -> Option<Vec<CommitteeMemberOf<T>>> {
640			T::select_authorities(authority_selection_inputs, sidechain_epoch).map(|c| c.to_vec())
641		}
642
643		/// If [NextCommittee] is defined, it moves its value to [CurrentCommittee] storage.
644		/// Returns the value taken from [NextCommittee].
645		pub fn rotate_committee_to_next_epoch() -> Option<Vec<CommitteeMemberOf<T>>> {
646			let next_committee = NextCommittee::<T>::take()?;
647
648			CurrentCommittee::<T>::put(next_committee.clone());
649
650			let validators = next_committee.committee.to_vec();
651			let len = validators.len();
652			info!(
653				"Committee rotated: Returning {len} validators, stored in epoch {}",
654				next_committee.epoch
655			);
656			Some(validators)
657		}
658
659		/// Returns main chain scripts.
660		pub fn get_main_chain_scripts() -> MainChainScripts {
661			MainChainScriptsConfiguration::<T>::get()
662		}
663	}
664}
665
666/// For session state machine
667#[derive(Encode, Decode, Default, Debug, MaxEncodedLen, TypeInfo, PartialEq, Eq)]
668pub enum CommitteeRotationStages {
669	/// No action is required until the current committee becomes obsolete
670	#[default]
671	AwaitEpochChange,
672	/// Session ended because of epoch change
673	NewSessionDueEpochChange,
674	/// Session ended to accelerate use of validators queued in the previous block
675	AdditionalSession,
676}