validator_manager.rs 4.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! A pallet for managing validators on Rococo.

use sp_staking::SessionIndex;
use sp_std::vec::Vec;

22
23
pub use pallet::*;

24
type Session<T> = pallet_session::Pallet<T>;
25

26
27
28
29
30
#[frame_support::pallet]
pub mod pallet {
	use super::*;
	use frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::EnsureOrigin};
	use frame_system::pallet_prelude::*;
31

32
33
	#[pallet::pallet]
	pub struct Pallet<T>(_);
34

35
36
37
38
39
	/// Configuration for the parachain proposer.
	#[pallet::config]
	pub trait Config: frame_system::Config + pallet_session::Config {
		/// The overreaching event type.
		type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
40

41
42
43
		/// Privileged origin that can add or remove validators.
		type PrivilegedOrigin: EnsureOrigin<<Self as frame_system::Config>::Origin>;
	}
44

45
46
47
48
49
50
51
	#[pallet::event]
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
	pub enum Event<T: Config> {
		/// New validators were added to the set.
		ValidatorsRegistered(Vec<T::ValidatorId>),
		/// Validators were removed from the set.
		ValidatorsDeregistered(Vec<T::ValidatorId>),
52
53
	}

54
55
56
57
	/// Validators that should be retired, because their Parachain was deregistered.
	#[pallet::storage]
	pub(crate) type ValidatorsToRetire<T: Config> =
		StorageValue<_, Vec<T::ValidatorId>, ValueQuery>;
58

59
60
61
	/// Validators that should be added.
	#[pallet::storage]
	pub(crate) type ValidatorsToAdd<T: Config> = StorageValue<_, Vec<T::ValidatorId>, ValueQuery>;
62

63
64
	#[pallet::call]
	impl<T: Config> Pallet<T> {
65
66
67
		/// Add new validators to the set.
		///
		/// The new validators will be active from current session + 2.
68
69
70
		#[pallet::weight(100_000)]
		pub fn register_validators(
			origin: OriginFor<T>,
71
			validators: Vec<T::ValidatorId>,
72
		) -> DispatchResult {
73
74
75
76
			T::PrivilegedOrigin::ensure_origin(origin)?;

			validators.clone().into_iter().for_each(|v| ValidatorsToAdd::<T>::append(v));

77
78
			Self::deposit_event(Event::ValidatorsRegistered(validators));
			Ok(())
79
80
81
82
83
		}

		/// Remove validators from the set.
		///
		/// The removed validators will be deactivated from current session + 2.
84
85
86
		#[pallet::weight(100_000)]
		pub fn deregister_validators(
			origin: OriginFor<T>,
87
			validators: Vec<T::ValidatorId>,
88
		) -> DispatchResult {
89
90
91
92
			T::PrivilegedOrigin::ensure_origin(origin)?;

			validators.clone().into_iter().for_each(|v| ValidatorsToRetire::<T>::append(v));

93
94
			Self::deposit_event(Event::ValidatorsDeregistered(validators));
			Ok(())
95
96
97
98
		}
	}
}

99
impl<T: Config> pallet_session::SessionManager<T::ValidatorId> for Pallet<T> {
100
101
	fn new_session(new_index: SessionIndex) -> Option<Vec<T::ValidatorId>> {
		if new_index <= 1 {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
102
			return None
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
		}

		let mut validators = Session::<T>::validators();

		ValidatorsToRetire::<T>::take().iter().for_each(|v| {
			if let Some(pos) = validators.iter().position(|r| r == v) {
				validators.swap_remove(pos);
			}
		});

		ValidatorsToAdd::<T>::take().into_iter().for_each(|v| {
			if !validators.contains(&v) {
				validators.push(v);
			}
		});

		Some(validators)
	}

	fn end_session(_: SessionIndex) {}

	fn start_session(_start_index: SessionIndex) {}
}

127
impl<T: Config> pallet_session::historical::SessionManager<T::ValidatorId, ()> for Pallet<T> {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
128
	fn new_session(new_index: SessionIndex) -> Option<Vec<(T::ValidatorId, ())>> {
129
130
131
132
133
134
135
136
137
138
139
140
		<Self as pallet_session::SessionManager<_>>::new_session(new_index)
			.map(|r| r.into_iter().map(|v| (v, Default::default())).collect())
	}

	fn start_session(start_index: SessionIndex) {
		<Self as pallet_session::SessionManager<_>>::start_session(start_index)
	}

	fn end_session(end_index: SessionIndex) {
		<Self as pallet_session::SessionManager<_>>::end_session(end_index)
	}
}