Staking: use proc macros

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