improve staking interface methods (#14023)

* improve staking interface methods

* fix

* fix

* fix build

* restart

* fix

---------

Co-authored-by: parity-processbot <>
This commit is contained in:
Kian Paimani
2023-04-27 13:27:12 +02:00
committed by GitHub
parent 31e1329da0
commit 2320bdd388
8 changed files with 99 additions and 24 deletions
+32 -6
View File
@@ -20,6 +20,8 @@
//! A crate which contains primitives that are useful for implementation that uses staking
//! approaches in general. Definitions related to sessions, slashing, etc go here.
use scale_info::TypeInfo;
use sp_core::RuntimeDebug;
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};
@@ -31,6 +33,18 @@ pub type SessionIndex = u32;
/// Counter for the number of eras that have passed.
pub type EraIndex = u32;
/// Representation of the status of a staker.
#[derive(RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, PartialEq, Eq, Clone))]
pub enum StakerStatus<AccountId> {
/// Chilling.
Idle,
/// Declaring desire in validate, i.e author blocks.
Validator,
/// Declaring desire to nominate, delegate, or generally approve of the given set of others.
Nominator(Vec<AccountId>),
}
/// Trait describing something that implements a hook for any operations to perform when a staker is
/// slashed.
pub trait OnStakerSlash<AccountId, Balance> {
@@ -57,7 +71,7 @@ impl<AccountId, Balance> OnStakerSlash<AccountId, Balance> for () {
/// A struct that reflects stake that an account has in the staking system. Provides a set of
/// methods to operate on it's properties. Aimed at making `StakingInterface` more concise.
pub struct Stake<T: StakingInterface + ?Sized> {
pub struct Stake<Balance> {
/// The total stake that `stash` has in the staking system. This includes the
/// `active` stake, and any funds currently in the process of unbonding via
/// [`StakingInterface::unbond`].
@@ -67,10 +81,10 @@ pub struct Stake<T: StakingInterface + ?Sized> {
/// This is only guaranteed to reflect the amount locked by the staking system. If there are
/// non-staking locks on the bonded pair's balance this amount is going to be larger in
/// reality.
pub total: T::Balance,
pub total: Balance,
/// The total amount of the stash's balance that will be at stake in any forthcoming
/// rounds.
pub active: T::Balance,
pub active: Balance,
}
/// A generic representation of a staking implementation.
@@ -109,21 +123,25 @@ pub trait StakingInterface {
/// This should be the latest planned era that the staking system knows about.
fn current_era() -> EraIndex;
/// Returns the stake of `who`.
fn stake(who: &Self::AccountId) -> Result<Stake<Self>, DispatchError>;
/// Returns the [`Stake`] of `who`.
fn stake(who: &Self::AccountId) -> Result<Stake<Self::Balance>, DispatchError>;
/// Total stake of a staker, `Err` if not a staker.
fn total_stake(who: &Self::AccountId) -> Result<Self::Balance, DispatchError> {
Self::stake(who).map(|s| s.total)
}
/// Total active portion of a staker's [`Stake`], `Err` if not a staker.
fn active_stake(who: &Self::AccountId) -> Result<Self::Balance, DispatchError> {
Self::stake(who).map(|s| s.active)
}
/// Returns whether a staker is unbonding, `Err` if not a staker at all.
fn is_unbonding(who: &Self::AccountId) -> Result<bool, DispatchError> {
Self::stake(who).map(|s| s.active != s.total)
}
/// Returns whether a staker is FULLY unbonding, `Err` if not a staker at all.
fn fully_unbond(who: &Self::AccountId) -> DispatchResult {
Self::unbond(who, Self::stake(who)?.active)
}
@@ -174,9 +192,17 @@ pub trait StakingInterface {
/// Checks whether an account `staker` has been exposed in an era.
fn is_exposed_in_era(who: &Self::AccountId, era: &EraIndex) -> bool;
/// Return the status of the given staker, `None` if not staked at all.
fn status(who: &Self::AccountId) -> Result<StakerStatus<Self::AccountId>, DispatchError>;
/// Get the nominations of a stash, if they are a nominator, `None` otherwise.
#[cfg(feature = "runtime-benchmarks")]
fn nominations(who: Self::AccountId) -> Option<Vec<Self::AccountId>>;
fn nominations(who: &Self::AccountId) -> Option<Vec<Self::AccountId>> {
match Self::status(who) {
Ok(StakerStatus::Nominator(t)) => Some(t),
_ => None,
}
}
#[cfg(feature = "runtime-benchmarks")]
fn add_era_stakers(