diff --git a/src/frame/staking.rs b/src/frame/staking.rs index 7f213154d5..4e31c568d9 100644 --- a/src/frame/staking.rs +++ b/src/frame/staking.rs @@ -16,16 +16,23 @@ //! Implements support for the frame_staking module. -use super::system::{ - System, - SystemEventsDecoder as _, +use super::balances::{ + Balances, + BalancesEventsDecoder as _, }; use codec::{ Decode, Encode, - HasCompact, }; -use sp_runtime::Perbill; +use frame_support::Parameter; +use sp_runtime::{ + traits::{ + AtLeast32Bit, + MaybeSerialize, + Member, + }, + Perbill, +}; use std::{ fmt::Debug, marker::PhantomData, @@ -33,22 +40,22 @@ use std::{ /// A record of the nominations made by a specific account. #[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, Ord, PartialOrd, Hash)] -pub struct Nominations { +pub struct Nominations { /// The targets of nomination. - pub targets: Vec, + pub targets: Vec, /// The era the nominations were submitted. /// /// Except for initial nominations which are considered submitted at era 0. - pub submitted_in: EraIndex, + pub submitted_in: T::EraIndex, /// Whether the nominations have been suppressed. pub suppressed: bool, } /// Information regarding the active era (era in used in session). #[derive(Encode, Decode, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] -pub struct ActiveEraInfo { +pub struct ActiveEraInfo { /// Index of era. - pub index: EraIndex, + pub index: T::EraIndex, /// Moment of start expresed as millisecond from `$UNIX_EPOCH`. /// /// Start can be none if start hasn't been set for the era yet, @@ -56,24 +63,6 @@ pub struct ActiveEraInfo { pub start: Option, } -/// Data type used to index nominators in the compact type -pub type NominatorIndex = u32; - -/// Data type used to index validators in the compact type. -pub type ValidatorIndex = u16; - -/// Maximum number of validators that can be stored in a snapshot. -pub const MAX_VALIDATORS: usize = ValidatorIndex::max_value() as usize; - -/// Maximum number of nominators that can be stored in a snapshot. -pub const MAX_NOMINATORS: usize = NominatorIndex::max_value() as usize; - -/// Counter for the number of eras that have passed. -pub type EraIndex = u32; - -/// Counter for the number of "reward" points earned by a given validator. -pub type RewardPoint = u32; - /// A destination account for payment. #[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug)] pub enum RewardDestination { @@ -89,7 +78,7 @@ pub enum RewardDestination { #[derive(PartialEq, Eq, Clone, Encode, Decode, Debug, Call)] pub struct SetPayeeCall { /// The payee - pub payee: Perbill, + pub payee: RewardDestination, /// Marker for the runtime pub _runtime: PhantomData, } @@ -113,38 +102,84 @@ impl Default for ValidatorPrefs { /// The subset of the `frame::Trait` that a client must implement. #[module] -pub trait Staking: System {} +pub trait Staking: Balances { + /// Data type used to index nominators in the compact type + type NominatorIndex: Parameter + + codec::Codec + + Member + + Default + + Copy + + MaybeSerialize + + Debug; + + /// Data type used to index validators in the compact type. + type ValidatorIndex: Parameter + + codec::Codec + + Send + + Sync + + Default + + Member + + Copy + + MaybeSerialize + + Debug; + + /// Maximum number of validators that can be stored in a snapshot. + const MAX_VALIDATORS: usize; + + /// Maximum number of nominators that can be stored in a snapshot. + const MAX_NOMINATORS: usize; + + /// Counter for the number of eras that have passed. + type EraIndex: Parameter + + Member + + AtLeast32Bit + + codec::Codec + + Default + + Copy + + MaybeSerialize + + Debug; + + /// Counter for the number of "reward" points earned by a given validator. + type RewardPoint: Parameter + + Member + + AtLeast32Bit + + codec::Codec + + Default + + Copy + + MaybeSerialize + + Debug; +} /// Just a Balance/BlockNumber tuple to encode when a chunk of funds will be unlocked. #[derive(PartialEq, Eq, Clone, Encode, Decode, Ord, PartialOrd, Hash)] -pub struct UnlockChunk { +pub struct UnlockChunk { /// Amount of funds to be unlocked. #[codec(compact)] - pub value: Balance, + pub value: T::Balance, /// Era number at which point it'll be unlocked. #[codec(compact)] - pub era: EraIndex, + pub era: T::EraIndex, } /// The ledger of a (bonded) stash. #[derive(PartialEq, Eq, Clone, Encode, Decode, Ord, PartialOrd, Hash)] -pub struct StakingLedger { +pub struct StakingLedger { /// The stash account whose balance is actually locked and at stake. - pub stash: AccountId, + pub stash: T::AccountId, /// The total amount of the stash's balance that we are currently accounting for. /// It's just `active` plus all the `unlocking` balances. #[codec(compact)] - pub total: Balance, + pub total: T::Balance, /// The total amount of the stash's balance that will be at stake in any forthcoming /// rounds. #[codec(compact)] - pub active: Balance, + pub active: T::Balance, /// Any balance that is becoming free, which may eventually be transferred out /// of the stash (assuming it doesn't get slashed first). - pub unlocking: Vec>, + pub unlocking: Vec>, /// List of eras for which the stakers behind a validator have claimed rewards. Only updated /// for validators. - pub claimed_rewards: Vec, + pub claimed_rewards: Vec, } /// Number of eras to keep in history. @@ -204,7 +239,7 @@ pub struct BondedStore { /// Map from all (unlocked) "controller" accounts to the info regarding the staking. #[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] pub struct LedgerStore { - #[store(returns = Option>)] + #[store(returns = Option>)] /// The controller account pub controller: T::AccountId, } @@ -228,7 +263,7 @@ pub struct ValidatorsStore { /// The map from nominator stash key to the set of stash keys of all validators to nominate. #[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] pub struct NominatorsStore { - #[store(returns = Option>)] + #[store(returns = Option>)] /// TŮ—he stash account pub stash: T::AccountId, } @@ -239,7 +274,7 @@ pub struct NominatorsStore { /// set, it might be active or not. #[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] pub struct CurrentEraStore { - #[store(returns = Option)] + #[store(returns = Option)] /// Marker for the runtime pub _runtime: PhantomData, } @@ -250,7 +285,7 @@ pub struct CurrentEraStore { /// Validator set of this era must be equal to `SessionInterface::validators`. #[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] pub struct ActiveEraStore { - #[store(returns = Option)] + #[store(returns = Option>)] /// Marker for the runtime pub _runtime: PhantomData, } @@ -307,7 +342,7 @@ pub struct NominateCall { /// Claim a payout. #[derive(PartialEq, Eq, Clone, Call, Encode, Decode)] -struct PayoutStakersCall<'a, T: System> { +struct PayoutStakersCall<'a, T: Staking> { pub validator_stash: &'a T::AccountId, - pub era: EraIndex, + pub era: T::EraIndex, } diff --git a/src/frame/system.rs b/src/frame/system.rs index 6734d47322..410e9a5299 100644 --- a/src/frame/system.rs +++ b/src/frame/system.rs @@ -100,6 +100,8 @@ pub trait System { + MaybeSerialize + Debug + MaybeDisplay + + Encode + + Decode + Ord + Default; diff --git a/src/runtimes.rs b/src/runtimes.rs index 26f5d9bbb9..c7a02044c1 100644 --- a/src/runtimes.rs +++ b/src/runtimes.rs @@ -45,6 +45,15 @@ use crate::frame::{ #[derive(Debug, Clone, Eq, PartialEq)] pub struct DefaultNodeRuntime; +impl Staking for DefaultNodeRuntime { + type NominatorIndex = u32; + type ValidatorIndex = u16; + const MAX_VALIDATORS: usize = Self::ValidatorIndex::max_value() as usize; + const MAX_NOMINATORS: usize = Self::NominatorIndex::max_value() as usize; + type EraIndex = u32; + type RewardPoint = u32; +} + impl System for DefaultNodeRuntime { type Index = u32; type BlockNumber = u32; @@ -66,8 +75,6 @@ impl Session for DefaultNodeRuntime { type ValidatorId = ::AccountId; } -impl Staking for DefaultNodeRuntime {} - impl Contracts for DefaultNodeRuntime {} /// Concrete type definitions compatible with those for kusama, v0.7 @@ -96,7 +103,14 @@ impl Session for KusamaRuntime { type ValidatorId = ::AccountId; } -impl Staking for KusamaRuntime {} +impl Staking for KusamaRuntime { + type NominatorIndex = u32; + type ValidatorIndex = u16; + const MAX_VALIDATORS: usize = Self::ValidatorIndex::max_value() as usize; + const MAX_NOMINATORS: usize = Self::NominatorIndex::max_value() as usize; + type EraIndex = u32; + type RewardPoint = u32; +} impl Balances for KusamaRuntime { type Balance = u128;