1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#![cfg_attr(not(feature = "std"), no_std)]
//!
//! # Ban logic
//! In case of insufficient validator's uptime, we need to remove such validators from
//! the committee, so that the network is as healthy as possible. This is achieved by calculating
//! number of _underperformance_ sessions, which means that number of blocks produced by the
//! validator is less than some predefined threshold.
//! In other words, if a validator:
//! * performance in a session is less or equal to a configurable threshold
//! `BanConfig::minimal_expected_performance` (from 0 to 100%), and,
//! * it happened at least `BanConfig::underperformed_session_count_threshold` times,
//! then the validator is considered an underperformer and hence removed (ie _banned out_) from the
//! committee.
//!
//! ## Thresholds
//! There are two ban thresholds described above, see [`BanConfig`].
//!
//! ### Next era vs current era
//! Current and next era have distinct thresholds values, as we calculate bans during the start of the new era.
//! They follow the same logic as next era committee seats: at the time of planning the first
//! session of next the era, next values become current ones.

extern crate core;

mod impls;
mod manager;
mod traits;

#[cfg(test)]
mod tests;

use frame_support::{pallet_prelude::Get, traits::StorageVersion};
pub use manager::SessionAndEraManager;
pub use pallet::*;
use parity_scale_codec::{Decode, Encode};
use core_primitives::{BanConfig as BanConfigStruct, BanInfo, SessionValidators, LENIENT_THRESHOLD};
use scale_info::TypeInfo;
use sp_runtime::Perquintill;
use sp_std::{collections::btree_map::BTreeMap, default::Default};
pub use traits::*;

pub type TotalReward = u32;
#[derive(Decode, Encode, TypeInfo, PartialEq, Eq)]
pub struct ValidatorTotalRewards<T>(pub BTreeMap<T, TotalReward>);

#[derive(Decode, Encode, TypeInfo)]
struct CurrentAndNextSessionValidators<T> {
    pub next: SessionValidators<T>,
    pub current: SessionValidators<T>,
}

impl<T> Default for CurrentAndNextSessionValidators<T> {
    fn default() -> Self {
        Self {
            next: Default::default(),
            current: Default::default(),
        }
    }
}

pub struct DefaultLenientThreshold;

impl Get<Perquintill> for DefaultLenientThreshold {
    fn get() -> Perquintill {
        LENIENT_THRESHOLD
    }
}

const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);
pub(crate) const LOG_TARGET: &str = "pallet-committee-management";

#[frame_support::pallet]
pub mod pallet {
    use frame_support::{
        dispatch::DispatchResult, ensure, pallet_prelude::*, BoundedVec, Twox64Concat,
    };
    use frame_system::{ensure_root, pallet_prelude::OriginFor};
    use core_primitives::{
        BanHandler, BanReason, BlockCount, FinalityCommitteeManager, SessionCount,
        SessionValidators, ValidatorProvider,
    };
    use sp_runtime::{Perbill, Perquintill};
    use sp_staking::EraIndex;
    use sp_std::vec::Vec;

    use crate::{
        traits::{EraInfoProvider, ValidatorRewardsHandler},
        BanConfigStruct, BanInfo, CurrentAndNextSessionValidators, DefaultLenientThreshold,
        ValidatorExtractor, ValidatorTotalRewards, STORAGE_VERSION,
    };

    #[pallet::config]
    pub trait Config: frame_system::Config {
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
        /// Something that handles bans
        type BanHandler: BanHandler<AccountId = Self::AccountId>;
        /// Something that provides information about era.
        type EraInfoProvider: EraInfoProvider<AccountId = Self::AccountId>;
        /// Something that provides information about validator.
        type ValidatorProvider: ValidatorProvider<AccountId = Self::AccountId>;
        /// Something that handles addition of rewards for validators.
        type ValidatorRewardsHandler: ValidatorRewardsHandler<AccountId = Self::AccountId>;
        /// Something that handles removal of the validators
        type ValidatorExtractor: ValidatorExtractor<AccountId = Self::AccountId>;
        type FinalityCommitteeManager: FinalityCommitteeManager<Self::AccountId>;
        /// Nr of blocks in the session.
        #[pallet::constant]
        type SessionPeriod: Get<u32>;
    }

    #[pallet::pallet]
    #[pallet::storage_version(STORAGE_VERSION)]
    #[pallet::without_storage_info]
    pub struct Pallet<T>(_);

    #[pallet::storage]
    pub type LenientThreshold<T: Config> =
    StorageValue<_, Perquintill, ValueQuery, DefaultLenientThreshold>;

    /// A lookup how many blocks a validator produced.
    #[pallet::storage]
    pub type SessionValidatorBlockCount<T: Config> =
    StorageMap<_, Twox64Concat, T::AccountId, BlockCount, ValueQuery>;

    /// Total possible reward per validator for the current era.
    #[pallet::storage]
    pub type ValidatorEraTotalReward<T: Config> =
    StorageValue<_, ValidatorTotalRewards<T::AccountId>, OptionQuery>;

    /// Current era config for ban functionality, see [`BanConfig`]
    #[pallet::storage]
    pub type BanConfig<T> = StorageValue<_, BanConfigStruct, ValueQuery>;

    /// A lookup for a number of underperformance sessions for a given validator
    #[pallet::storage]
    pub type UnderperformedValidatorSessionCount<T: Config> =
    StorageMap<_, Twox64Concat, T::AccountId, SessionCount, ValueQuery>;

    /// Validators to be removed from non reserved list in the next era
    #[pallet::storage]
    pub type Banned<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, BanInfo>;

    /// SessionValidators in the current session.
    #[pallet::storage]
    pub(crate) type CurrentAndNextSessionValidatorsStorage<T: Config> =
    StorageValue<_, CurrentAndNextSessionValidators<T::AccountId>, ValueQuery>;

    #[pallet::error]
    pub enum Error<T> {
        /// Raised in any scenario [`BanConfig`] is invalid
        /// * `performance_ratio_threshold` must be a number in range [0; 100]
        /// * `underperformed_session_count_threshold` must be a positive number,
        /// * `clean_session_counter_delay` must be a positive number.
        InvalidBanConfig,

        /// Ban reason is too big, ie given vector of bytes is greater than
        /// [`primitives::DEFAULT_BAN_REASON_LENGTH`]
        BanReasonTooBig,

        /// Lenient threshold not in [0-100] range
        InvalidLenientThreshold,
    }

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// Ban thresholds for the next era has changed
        SetBanConfig(BanConfigStruct),

        /// Validators have been banned from the committee
        BanValidators(Vec<(T::AccountId, BanInfo)>),

        /// One Validator have been banned from the committee
        ValidatorBanned {
            /// The account for which the validator was banned.
            banned: T::AccountId,
            /// The reason for ban.
            ban_info: BanInfo,
        },

        /// Validator Unbanned
        ValidatorUnbanned(T::AccountId),

        NewLenientThreshold(u8),
    }

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Sets ban config, it has an immediate effect
        #[pallet::call_index(1)]
        #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))]
        pub fn set_ban_config(
            origin: OriginFor<T>,
            minimal_expected_performance: Option<u8>,
            underperformed_session_count_threshold: Option<u32>,
            clean_session_counter_delay: Option<u32>,
            ban_period: Option<EraIndex>,
        ) -> DispatchResult {
            ensure_root(origin)?;

            let mut current_committee_ban_config = BanConfig::<T>::get();

            if let Some(minimal_expected_performance) = minimal_expected_performance {
                ensure!(
                    minimal_expected_performance <= 100,
                    Error::<T>::InvalidBanConfig
                );
                current_committee_ban_config.minimal_expected_performance =
                    Perbill::from_percent(minimal_expected_performance as u32);
            }
            if let Some(underperformed_session_count_threshold) =
                underperformed_session_count_threshold
            {
                ensure!(
                    underperformed_session_count_threshold > 0,
                    Error::<T>::InvalidBanConfig
                );
                current_committee_ban_config.underperformed_session_count_threshold =
                    underperformed_session_count_threshold;
            }
            if let Some(clean_session_counter_delay) = clean_session_counter_delay {
                ensure!(
                    clean_session_counter_delay > 0,
                    Error::<T>::InvalidBanConfig
                );
                current_committee_ban_config.clean_session_counter_delay =
                    clean_session_counter_delay;
            }
            if let Some(ban_period) = ban_period {
                ensure!(ban_period > 0, Error::<T>::InvalidBanConfig);
                current_committee_ban_config.ban_period = ban_period;
            }

            BanConfig::<T>::put(current_committee_ban_config.clone());
            Self::deposit_event(Event::SetBanConfig(current_committee_ban_config));

            Ok(())
        }

        /// Schedule a non-reserved node to be banned out from the committee at the end of the era
        #[pallet::call_index(2)]
        #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))]
        pub fn ban_from_committee(
            origin: OriginFor<T>,
            banned: T::AccountId,
            ban_reason: Vec<u8>,
        ) -> DispatchResult {
            ensure_root(origin)?;
            let bounded_description: BoundedVec<_, _> = ban_reason
                .try_into()
                .map_err(|_| Error::<T>::BanReasonTooBig)?;

            let reason = BanReason::OtherReason(bounded_description);
            Self::ban_validator(&banned, reason);

            let ban_info = Banned::<T>::get(&banned).unwrap();
            Self::deposit_event(Event::<T>::ValidatorBanned { banned, ban_info });

            Ok(())
        }

        /// Cancel the ban of the node
        #[pallet::call_index(3)]
        #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))]
        pub fn cancel_ban(origin: OriginFor<T>, banned: T::AccountId) -> DispatchResult {
            ensure_root(origin)?;
            Banned::<T>::remove(banned.clone());

            Self::deposit_event(Event::ValidatorUnbanned(banned));
            Ok(())
        }

        /// Set lenient threshold
        #[pallet::call_index(4)]
        #[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))]
        pub fn set_lenient_threshold(
            origin: OriginFor<T>,
            threshold_percent: u8,
        ) -> DispatchResult {
            ensure_root(origin)?;
            ensure!(
                threshold_percent <= 100,
                Error::<T>::InvalidLenientThreshold
            );

            LenientThreshold::<T>::put(Perquintill::from_percent(threshold_percent as u64));

            Ok(())
        }
    }

    #[pallet::genesis_config]
    #[derive(frame_support::DefaultNoBound)]
    pub struct GenesisConfig<T: Config> {
        pub committee_ban_config: BanConfigStruct,
        pub session_validators: SessionValidators<T::AccountId>,
    }

    #[pallet::genesis_build]
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
        fn build(&self) {
            <BanConfig<T>>::put(self.committee_ban_config.clone());
            <CurrentAndNextSessionValidatorsStorage<T>>::put(CurrentAndNextSessionValidators {
                current: self.session_validators.clone(),
                next: self.session_validators.clone(),
            })
        }
    }
}