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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! # Plasm rewards Module
//!
//! The Plasm rewards module provides functionality for handling whole rewards and era.
//!
//! - [`plasm_rewards::Trait`](./trait.Trait.html)
//! - [`Call`](./enum.Call.html)
//! - [`Module`](./struct.Module.html)
//!
//! ## Overview
//!
//! The Plasm staking module puts together the management and compensation payment logic of the ERA.
//! The Plasm Rewards module calls the Dapps Staking and Validator.
//! It also allocates rewards to each module according to the [Plasm Token Ecosystem inflation model](https://docs.plasmnet.io/learn/token-economy#inflation-model).
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use frame_support::{
    decl_error, decl_event, decl_module, decl_storage,
    traits::{Currency, Get, LockableCurrency, Time},
    weights::Weight,
    StorageMap, StorageValue,
};
use frame_system::{self as system, ensure_root};
use pallet_session::SessionManager;
pub use pallet_staking::Forcing;
use sp_runtime::{
    traits::{SaturatedConversion, Zero},
    Perbill, RuntimeDebug,
};
use sp_std::{prelude::*, vec::Vec};

pub mod inflation;
#[cfg(test)]
mod mock;
pub mod traits;
pub use traits::*;
#[cfg(test)]
mod tests;

pub use sp_staking::SessionIndex;

pub type EraIndex = u32;
pub type BalanceOf<T> =
    <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
pub type MomentOf<T> = <<T as Trait>::Time as Time>::Moment;

// A value placed in storage that represents the current version of the Staking storage.
// This value is used by the `on_runtime_upgrade` logic to determine whether we run
// storage migration logic. This should match directly with the semantic versions of the Rust crate.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug)]
pub enum Releases {
    V1_0_0,
}

impl Default for Releases {
    fn default() -> Self {
        Releases::V1_0_0
    }
}

/// Information regarding the active era (era in used in session).
#[derive(Encode, Decode, RuntimeDebug, PartialEq, Eq)]
pub struct ActiveEraInfo<Moment> {
    /// Index of era.
    pub index: EraIndex,
    /// Moment of start
    ///
    /// Start can be none if start hasn't been set for the era yet,
    /// Start is set on the first on_finalize of the era to guarantee usage of `Time`.
    pub start: Option<Moment>,
}

pub trait Trait: pallet_session::Trait {
    /// The staking balance.
    type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>;

    /// Time used for computing era duration.
    type Time: Time;

    /// Number of sessions per era.
    type SessionsPerEra: Get<SessionIndex>;

    /// Number of eras that staked funds must remain bonded for.
    type BondingDuration: Get<EraIndex>;

    /// Get the amount of staking for dapps per era.
    type ComputeEraForDapps: ComputeEraWithParam<EraIndex>;

    /// Get the amount of staking for security per era.
    type ComputeEraForSecurity: ComputeEraWithParam<EraIndex>;

    /// How to compute total issue PLM for rewards.
    type ComputeTotalPayout: ComputeTotalPayout<
        <Self::ComputeEraForSecurity as ComputeEraWithParam<EraIndex>>::Param,
        <Self::ComputeEraForDapps as ComputeEraWithParam<EraIndex>>::Param,
    >;

    /// Maybe next validators.
    type MaybeValidators: traits::MaybeValidators<EraIndex, Self::AccountId>;

    /// The overarching event type.
    type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
}

decl_storage! {
    trait Store for Module<T: Trait> as DappsStaking {
        /// This is the compensation paid for the dapps operator of the Plasm Network.
        /// This is stored on a per-era basis.
        pub ForDappsEraReward get(fn for_dapps_era_reward): map hasher(twox_64_concat) EraIndex => Option<BalanceOf<T>>;

        /// This is the compensation paid for the security of the Plasm Network.
        /// This is stored on a per-era basis.
        pub ForSecurityEraReward get(fn for_security_era_reward): map hasher(twox_64_concat) EraIndex => Option<BalanceOf<T>>;

        /// Number of era to keep in history.
        ///
        /// Information is kept for eras in `[current_era - history_depth; current_era]`
        ///
        /// Must be more than the number of era delayed by session otherwise.
        /// i.e. active era must always be in history.
        /// i.e. `active_era > current_era - history_depth` must be guaranteed.
        ///
        /// 24 * 28 = 672 eras is roughly 28 days on current Plasm Network.
        /// That seems like a reasonable length of time for users to claim a payout
        pub HistoryDepth get(fn history_depth) config(): u32 = 672;

        /// A mapping from still-bonded eras to the first session index of that era.
        ///
        /// Must contains information for eras for the range:
        /// `[active_era - bounding_duration; active_era]`
        pub BondedEras: Vec<(EraIndex, SessionIndex)>;

        /// The current era index.
        ///
        /// This is the latest planned era, depending on how session module queues the validator
        /// set, it might be active or not.
        pub CurrentEra get(fn current_era): Option<EraIndex>;

        /// The active era information, it holds index and start.
        ///
        /// The active era is the era currently rewarded.
        /// Validator set of this era must be equal to `SessionInterface::validators`.
        pub ActiveEra get(fn active_era): Option<ActiveEraInfo<MomentOf<T>>>;

        /// The session index at which the era start for the last `HISTORY_DEPTH` eras
        pub ErasStartSessionIndex get(fn eras_start_session_index):
            map hasher(twox_64_concat) EraIndex => Option<SessionIndex>;

        /// True if the next session change will be a new era regardless of index.
        pub ForceEra get(fn force_era) config(): Forcing;

        /// Storage version of the pallet.
        ///
        /// This is set to v1.0.0 for new networks.
        StorageVersion build(|_: &GenesisConfig| Releases::V1_0_0): Releases;
    }
}

decl_event!(
    pub enum Event<T>
    where
        Balance = BalanceOf<T>,
    {
        /// The whole reward issued in that Era.
        /// (era_index: EraIndex, reward: Balance)
        WholeEraReward(EraIndex, Balance),
    }
);

decl_error! {
    /// Error for the staking module.
    pub enum Error for Module<T: Trait> {
        /// Duplicate index.
        DuplicateIndex,
        /// Invalid era to reward.
        InvalidEraToReward,
    }
}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        /// Number of sessions per era.
        const SessionsPerEra: SessionIndex = T::SessionsPerEra::get();

        type Error = Error<T>;

        fn deposit_event() = default;

        fn on_runtime_upgrade() -> Weight {
            migrate::<T>();
            50_000
        }

        /// On finalize is called at after rotate session.
        fn on_finalize() {
            // Set the start of the first era.
            if let Some(mut active_era) = Self::active_era() {
                if active_era.start.is_none() {
                    active_era.start = Some(T::Time::now());
                    <ActiveEra<T>>::put(active_era);
                }
            }
        }

        // ----- Root calls.
        /// Force there to be no new eras indefinitely.
        ///
        /// # <weight>
        /// - No arguments.
        /// # </weight>
        #[weight = 5_000]
        fn force_no_eras(origin) {
            ensure_root(origin)?;
            ForceEra::put(Forcing::ForceNone);
        }

        /// Force there to be a new era at the end of the next session. After this, it will be
        /// reset to normal (non-forced) behaviour.
        ///
        /// # <weight>
        /// - No arguments.
        /// # </weight>
        #[weight = 5_000]
        fn force_new_era(origin) {
            ensure_root(origin)?;
            ForceEra::put(Forcing::ForceNew);
        }

        /// Force there to be a new era at the end of sessions indefinitely.
        ///
        /// # <weight>
        /// - One storage write
        /// # </weight>
        #[weight = 5_000]
        fn force_new_era_always(origin) {
            ensure_root(origin)?;
            ForceEra::put(Forcing::ForceAlways);
        }

        /// Set history_depth value.
        ///
        /// Origin must be root.
        #[weight = 500_000]
        fn set_history_depth(origin, #[compact] new_history_depth: EraIndex) {
            ensure_root(origin)?;
            if let Some(current_era) = Self::current_era() {
                HistoryDepth::mutate(|history_depth| {
                    let last_kept = current_era.checked_sub(*history_depth).unwrap_or(0);
                    let new_last_kept = current_era.checked_sub(new_history_depth).unwrap_or(0);
                    for era_index in last_kept..new_last_kept {
                        Self::clear_era_information(era_index);
                    }
                    *history_depth = new_history_depth
                })
            }
        }
    }
}

fn migrate<T: Trait>() {
    // TODO: When runtime upgrade, migrate stroage.
    // if let Some(current_era) = CurrentEra::get() {
    //     let history_depth = HistoryDepth::get();
    //     for era in current_era.saturating_sub(history_depth)..=current_era {
    //         ErasStartSessionIndex::migrate_key_from_blake(era);
    //     }
    // }
}

impl<T: Trait> Module<T> {
    // MUTABLES (DANGEROUS)

    /// Plan a new session potentially trigger a new era.
    fn new_session(session_index: SessionIndex) -> Option<Vec<T::AccountId>> {
        if let Some(current_era) = Self::current_era() {
            // Initial era has been set.

            let current_era_start_session_index = Self::eras_start_session_index(current_era)
                .unwrap_or_else(|| {
                    frame_support::print("Error: start_session_index must be set for current_era");
                    0
                });

            let era_length = session_index
                .checked_sub(current_era_start_session_index)
                .unwrap_or(0); // Must never happen.

            match ForceEra::get() {
                Forcing::ForceNew => ForceEra::kill(),
                Forcing::ForceAlways => (),
                Forcing::NotForcing if era_length >= T::SessionsPerEra::get() => (),
                _ => return None,
            }

            Self::new_era(session_index)
        } else {
            // Set initial era
            Self::new_era(session_index)
        }
    }

    /// Start a session potentially starting an era.
    fn start_session(start_session: SessionIndex) {
        let next_active_era = Self::active_era().map(|e| e.index + 1).unwrap_or(0);
        if let Some(next_active_era_start_session_index) =
            Self::eras_start_session_index(next_active_era)
        {
            if next_active_era_start_session_index == start_session {
                Self::start_era(start_session);
            } else if next_active_era_start_session_index < start_session {
                // This arm should never happen, but better handle it than to stall the
                // staking pallet.
                frame_support::print("Warning: A session appears to have been skipped.");
                Self::start_era(start_session);
            }
        }
    }

    /// End a session potentially ending an era.
    fn end_session(session_index: SessionIndex) {
        if let Some(active_era) = Self::active_era() {
            if let Some(next_active_era_start_session_index) =
                Self::eras_start_session_index(active_era.index + 1)
            {
                if next_active_era_start_session_index == session_index + 1 {
                    Self::end_era(active_era, session_index);
                }
            }
        }
    }

    /// * Increment `active_era.index`,
    /// * reset `active_era.start`,
    /// * update `BondedEras` and apply slashes.
    fn start_era(start_session: SessionIndex) {
        let active_era = <ActiveEra<T>>::mutate(|active_era| {
            let new_index = active_era.as_ref().map(|info| info.index + 1).unwrap_or(0);
            *active_era = Some(ActiveEraInfo {
                index: new_index,
                // Set new active era start in next `on_finalize`. To guarantee usage of `Time`
                start: None,
            });
            new_index
        });

        // let bonding_duration = T::BondingDuration::get();

        BondedEras::mutate(|bonded| {
            bonded.push((active_era, start_session));

            // if active_era > bonding_duration {
            //     let first_kept = active_era - bonding_duration;
            //
            //     // prune out everything that's from before the first-kept index.
            //     let n_to_prune = bonded.iter()
            //         .take_while(|&&(era_idx, _)| era_idx < first_kept)
            //         .count();
            //
            //     // kill slashing metadata.
            //     for (pruned_era, _) in bonded.drain(..n_to_prune) {
            //         slashing::clear_era_metadata::<T>(pruned_era);
            //     }
            //
            //     if let Some(&(_, first_session)) = bonded.first() {
            //         T::SessionInterface::prune_historical_up_to(first_session);
            //     }
            // }
        });
    }

    /// Compute payout for era.
    fn end_era(active_era: ActiveEraInfo<MomentOf<T>>, _session_index: SessionIndex) {
        // Note: active_era_start can be None if end era is called during genesis config.
        if let Some(active_era_start) = active_era.start {
            // The set of total amount of staking.
            let now = T::Time::now();
            let era_duration = now - active_era_start;

            if !era_duration.is_zero() {
                let total_payout = T::Currency::total_issuance();
                let for_dapps = T::ComputeEraForDapps::compute(&active_era.index);
                let for_security = T::ComputeEraForSecurity::compute(&active_era.index);

                let (for_security_reward, for_dapps_rewards) = T::ComputeTotalPayout::compute(
                    total_payout,
                    era_duration.saturated_into::<u64>(),
                    for_security,
                    for_dapps,
                );

                <ForSecurityEraReward<T>>::insert(active_era.index, for_security_reward);
                <ForDappsEraReward<T>>::insert(active_era.index, for_dapps_rewards);
            }
        }
    }

    /// Plan a new era. Return the potential new staking set.
    fn new_era(start_session_index: SessionIndex) -> Option<Vec<T::AccountId>> {
        // Increment or set current era.
        let current_era = CurrentEra::get().map(|s| s + 1).unwrap_or(0);
        CurrentEra::put(current_era.clone());
        ErasStartSessionIndex::insert(&current_era, &start_session_index);

        // Clean old era information.
        if let Some(old_era) = current_era.checked_sub(Self::history_depth() + 1) {
            Self::clear_era_information(old_era);
        }

        // Return maybe validators.
        T::MaybeValidators::compute(current_era)
    }

    /// Clear all era information for given era.
    fn clear_era_information(era_index: EraIndex) {
        ErasStartSessionIndex::remove(era_index);
        <ForDappsEraReward<T>>::remove(era_index);
        <ForSecurityEraReward<T>>::remove(era_index);
    }
}

/// In this implementation `new_session(session)` must be called before `end_session(session-1)`
/// i.e. the new session must be planned before the ending of the previous session.
///
/// Once the first new_session is planned, all session must start and then end in order, though
/// some session can lag in between the newest session planned and the latest session started.
impl<T: Trait> SessionManager<T::AccountId> for Module<T> {
    fn new_session(new_index: SessionIndex) -> Option<Vec<T::AccountId>> {
        Self::new_session(new_index)
    }
    fn start_session(start_index: SessionIndex) {
        Self::start_session(start_index)
    }
    fn end_session(end_index: SessionIndex) {
        Self::end_session(end_index)
    }
}

/// In this implementation using validator and dapps rewards module.
impl<T: Trait> EraFinder<EraIndex, SessionIndex, MomentOf<T>> for Module<T> {
    fn current() -> Option<EraIndex> {
        Self::current_era()
    }
    fn active() -> Option<ActiveEraInfo<MomentOf<T>>> {
        Self::active_era()
    }
    fn start_session_index(era: &EraIndex) -> Option<SessionIndex> {
        Self::eras_start_session_index(&era)
    }
}

/// Get the security rewards for validator module.
impl<T: Trait> ForSecurityEraRewardFinder<BalanceOf<T>> for Module<T> {
    fn get(era: &EraIndex) -> Option<BalanceOf<T>> {
        Self::for_security_era_reward(&era)
    }
}

/// Get the dapps rewards for dapps staking module.
impl<T: Trait> ForDappsEraRewardFinder<BalanceOf<T>> for Module<T> {
    fn get(era: &EraIndex) -> Option<BalanceOf<T>> {
        Self::for_dapps_era_reward(&era)
    }
}

/// Get the history depth
impl<T: Trait> HistoryDepthFinder for Module<T> {
    fn get() -> u32 {
        Self::history_depth()
    }
}