referrerpolicy=no-referrer-when-downgrade

rococo_runtime/
validator_manager.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! A pallet for managing validators on Rococo.
18
19use alloc::vec::Vec;
20use sp_staking::SessionIndex;
21
22pub use pallet::*;
23
24type Session<T> = pallet_session::Pallet<T>;
25
26#[frame_support::pallet]
27pub mod pallet {
28	use super::*;
29	use frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::EnsureOrigin};
30	use frame_system::pallet_prelude::*;
31
32	#[pallet::pallet]
33	#[pallet::without_storage_info]
34	pub struct Pallet<T>(_);
35
36	/// Configuration for the parachain proposer.
37	#[pallet::config]
38	pub trait Config: frame_system::Config + pallet_session::Config {
39		/// The overreaching event type.
40		#[allow(deprecated)]
41		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
42
43		/// Privileged origin that can add or remove validators.
44		type PrivilegedOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;
45	}
46
47	#[pallet::event]
48	#[pallet::generate_deposit(pub(super) fn deposit_event)]
49	pub enum Event<T: Config> {
50		/// New validators were added to the set.
51		ValidatorsRegistered(Vec<T::ValidatorId>),
52		/// Validators were removed from the set.
53		ValidatorsDeregistered(Vec<T::ValidatorId>),
54	}
55
56	/// Validators that should be retired, because their Parachain was deregistered.
57	#[pallet::storage]
58	pub(crate) type ValidatorsToRetire<T: Config> =
59		StorageValue<_, Vec<T::ValidatorId>, ValueQuery>;
60
61	/// Validators that should be added.
62	#[pallet::storage]
63	pub(crate) type ValidatorsToAdd<T: Config> = StorageValue<_, Vec<T::ValidatorId>, ValueQuery>;
64
65	#[pallet::call]
66	impl<T: Config> Pallet<T> {
67		/// Add new validators to the set.
68		///
69		/// The new validators will be active from current session + 2.
70		#[pallet::call_index(0)]
71		#[pallet::weight({100_000})]
72		pub fn register_validators(
73			origin: OriginFor<T>,
74			validators: Vec<T::ValidatorId>,
75		) -> DispatchResult {
76			T::PrivilegedOrigin::ensure_origin(origin)?;
77
78			validators.clone().into_iter().for_each(|v| ValidatorsToAdd::<T>::append(v));
79
80			Self::deposit_event(Event::ValidatorsRegistered(validators));
81			Ok(())
82		}
83
84		/// Remove validators from the set.
85		///
86		/// The removed validators will be deactivated from current session + 2.
87		#[pallet::call_index(1)]
88		#[pallet::weight({100_000})]
89		pub fn deregister_validators(
90			origin: OriginFor<T>,
91			validators: Vec<T::ValidatorId>,
92		) -> DispatchResult {
93			T::PrivilegedOrigin::ensure_origin(origin)?;
94
95			validators.clone().into_iter().for_each(|v| ValidatorsToRetire::<T>::append(v));
96
97			Self::deposit_event(Event::ValidatorsDeregistered(validators));
98			Ok(())
99		}
100	}
101}
102
103impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Pallet<T> {
104	fn new_session(new_index: SessionIndex) -> Option<Vec<T::ValidatorId>> {
105		if new_index <= 1 {
106			return None
107		}
108
109		let mut validators = Session::<T>::validators();
110
111		ValidatorsToRetire::<T>::take().iter().for_each(|v| {
112			if let Some(pos) = validators.iter().position(|r| r == v) {
113				validators.swap_remove(pos);
114			}
115		});
116
117		ValidatorsToAdd::<T>::take().into_iter().for_each(|v| {
118			if !validators.contains(&v) {
119				validators.push(v);
120			}
121		});
122
123		Some(validators)
124	}
125
126	fn end_session(_: SessionIndex) {}
127
128	fn start_session(_start_index: SessionIndex) {}
129}
130
131impl<T: Config> pallet_session::historical::SessionManager<T::ValidatorId, ()> for Pallet<T> {
132	fn new_session(new_index: SessionIndex) -> Option<Vec<(T::ValidatorId, ())>> {
133		<Self as pallet_session::SessionManager<_>>::new_session(new_index)
134			.map(|r| r.into_iter().map(|v| (v, Default::default())).collect())
135	}
136
137	fn start_session(start_index: SessionIndex) {
138		<Self as pallet_session::SessionManager<_>>::start_session(start_index)
139	}
140
141	fn end_session(end_index: SessionIndex) {
142		<Self as pallet_session::SessionManager<_>>::end_session(end_index)
143	}
144}