From a28fb20264cff2be8744b66979bb023f83a1aeff Mon Sep 17 00:00:00 2001 From: "Demi M. Obenour" Date: Fri, 15 May 2020 16:25:28 -0400 Subject: [PATCH] Staking: use proc macros --- src/frame/staking.rs | 248 +++++++++++++------------------------------ 1 file changed, 75 insertions(+), 173 deletions(-) diff --git a/src/frame/staking.rs b/src/frame/staking.rs index 8d171ee115..44277c6b29 100644 --- a/src/frame/staking.rs +++ b/src/frame/staking.rs @@ -16,19 +16,11 @@ //! Implements support for the frame_staking module. -use crate::{ - frame::Store, - metadata::{ - Metadata, - MetadataError, - }, -}; use codec::{ Decode, Encode, HasCompact, }; -use sp_core::storage::StorageKey; use sp_runtime::{ Perbill, RuntimeDebug, @@ -159,207 +151,117 @@ const MODULE: &str = "Staking"; /// Must be more than the number of eras delayed by session otherwise. /// I.e. active era must always be in history. /// I.e. `active_era > current_era - history_depth` must be guaranteed. -#[derive(Encode, Decode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct HistoryDepth(PhantomData); - -impl Store for HistoryDepth { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "HistoryDepth"; - type Returns = u32; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .plain()? - .key()) - } +#[derive( + Encode, Decode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store, +)] +pub struct HistoryDepthStore { + #[store(returns = u32)] + /// Number of eras to keep in history. + /// + /// Information is kept for eras in `[current_era - history_depth; current_era]`. + /// + /// Must be more than the number of eras delayed by session otherwise. + /// I.e. active era must always be in history. + /// I.e. `active_era > current_era - history_depth` must be guaranteed. + pub _runtime: PhantomData, } /// The ideal number of staking participants. -#[derive(Encode, Decode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct ValidatorCount(PhantomData); - -impl Store for ValidatorCount { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "ValidatorCount"; - type Returns = u32; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .plain()? - .key()) - } +#[derive( + Encode, Decode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store, +)] +pub struct ValidatorCountStore { + #[store(returns = u32)] + /// The ideal number of staking participants. + pub _runtime: PhantomData, } /// Minimum number of staking participants before emergency conditions are imposed. -#[derive(Encode, Decode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct MinimumValidatorCount(PhantomData); - -impl Store for MinimumValidatorCount { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "MinimumValidatorCount"; - type Returns = u32; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .plain()? - .key()) - } +#[derive( + Encode, Decode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store, +)] +pub struct MinimumValidatorCountStore { + #[store(returns = u32)] + /// Minimum number of staking participants before emergency conditions are imposed. + pub _runtime: PhantomData, } /// Any validators that may never be slashed or forcibly kicked. It's a Vec since they're /// easy to initialize and the performance hit is minimal (we expect no more than four /// invulnerables) and restricted to testnets. -#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct Invulnerables(pub core::marker::PhantomData); - -impl Store for Invulnerables { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "Invulnerables"; - type Returns = Vec; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .plain()? - .key()) - } +#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] +pub struct InvulnerablesStore { + #[store(returns = Vec)] + /// Any validators that may never be slashed or forcibly kicked. It's a Vec since they're + /// easy to initialize and the performance hit is minimal (we expect no more than four + /// invulnerables) and restricted to testnets. + pub _runtime: core::marker::PhantomData, } /// Map from all locked "stash" accounts to the controller account. -#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct Bonded(pub PhantomData); - -impl Store for Bonded { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "Bonded"; - type Returns = Vec; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .map()? - .key(&self.0)) - } +#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] +pub struct BondedStore { + #[store(returns = Vec)] + /// Map from all locked "stash" accounts to the controller account. + pub _runtime: PhantomData, } /// Map from all (unlocked) "controller" accounts to the info regarding the staking. -#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct Ledger(pub T::AccountId); - -impl Store for Ledger { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "Ledger"; - type Returns = Option>; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .map()? - .key(&self.0)) - } +#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] +pub struct LedgerStore { + #[store(returns = Option>)] + /// Map from all (unlocked) "controller" accounts to the info regarding the staking. + pub _runtime: PhantomData, } /// Where the reward payment should be made. Keyed by stash. -#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct Payee(pub T::AccountId); - -impl Store for Payee { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "Payee"; - type Returns = RewardDestination; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .map()? - .key(&self.0)) - } +#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] +pub struct PayeeStore { + #[store(returns = RewardDestination)] + /// Where the reward payment should be made. Keyed by stash. + pub _runtime: PhantomData, } /// The map from (wannabe) validator stash key to the preferences of that validator. -#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct Validators(pub T::AccountId); - -impl Store for Validators { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "Validators"; - type Returns = ValidatorPrefs; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .map()? - .key(&self.0)) - } +#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] +pub struct ValidatorsStore { + #[store(returns = ValidatorPrefs)] + /// The map from (wannabe) validator stash key to the preferences of that validator. + pub _runtime: PhantomData, } /// 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)] -pub struct Nominators(pub T::AccountId); - -impl Store for Nominators { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "Nominators"; - type Returns = Option>; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .map()? - .key(&self.0)) - } +#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] +pub struct NominatorsStore { + #[store(returns = Option>)] + /// The map from nominator stash key to the set of stash keys of all validators to nominate. + pub _runtime: PhantomData, } /// The current era index. /// /// This is the latest planned era, depending on how the Session pallet queues the validator /// set, it might be active or not. -#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct CurrentEra(pub PhantomData); - -impl Store for CurrentEra { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "CurrentEra"; - type Returns = Option; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .map()? - .key(&self.0)) - } +#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] +pub struct CurrentEraStore { + #[store(returns = Option)] + /// The current era index. + /// + /// This is the latest planned era, depending on how the Session pallet queues the validator + /// set, it might be active or not. + pub _runtime: PhantomData, } /// 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`. -#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd)] -pub struct ActiveEra(pub PhantomData); - -impl Store for ActiveEra { - const MODULE: &'static str = MODULE; - const FIELD: &'static str = "ActiveEra"; - type Returns = Option; - - fn key(&self, metadata: &Metadata) -> Result { - Ok(metadata - .module(Self::MODULE)? - .storage(Self::FIELD)? - .map()? - .key(&self.0)) - } +#[derive(Encode, Copy, Clone, Debug, Hash, PartialEq, Eq, Ord, PartialOrd, Store)] +pub struct ActiveEraStore { + #[store(returns = Option)] + /// 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 _runtime: PhantomData, }