remove OnStakerSlash replace with OnStakingEvents (#14527)

* remove 'OnStakerSlash', replace with 'OnStakingEvents'

* fix other features in pallets

* small fixes

* fix docs

* fix docs

* fix docs

* Update primitives/staking/src/lib.rs

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>

---------

Co-authored-by: Gonçalo Pestana <g6pestana@gmail.com>
This commit is contained in:
Kian Paimani
2023-07-09 14:46:18 +02:00
committed by GitHub
parent 53244d64bd
commit 6dd625d568
28 changed files with 261 additions and 177 deletions
+87 -30
View File
@@ -20,13 +20,17 @@
//! 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 crate::currency_to_vote::CurrencyToVote;
use codec::{FullCodec, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::RuntimeDebug;
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};
use sp_runtime::{DispatchError, DispatchResult, Saturating};
use sp_std::{collections::btree_map::BTreeMap, ops::Sub, vec::Vec};
pub mod offence;
pub mod currency_to_vote;
/// Simple index type with which we can count sessions.
pub type SessionIndex = u32;
@@ -45,32 +49,9 @@ pub enum StakerStatus<AccountId> {
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> {
/// A hook for any operations to perform when a staker is slashed.
///
/// # Arguments
///
/// * `stash` - The stash of the staker whom the slash was applied to.
/// * `slashed_active` - The new bonded balance of the staker after the slash was applied.
/// * `slashed_unlocking` - A map of slashed eras, and the balance of that unlocking chunk after
/// the slash is applied. Any era not present in the map is not affected at all.
fn on_slash(
stash: &AccountId,
slashed_active: Balance,
slashed_unlocking: &BTreeMap<EraIndex, Balance>,
);
}
impl<AccountId, Balance> OnStakerSlash<AccountId, Balance> for () {
fn on_slash(_: &AccountId, _: Balance, _: &BTreeMap<EraIndex, Balance>) {
// Nothing to do here
}
}
/// 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.
#[derive(RuntimeDebug, Clone, Copy, Eq, PartialEq, Default)]
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
@@ -87,16 +68,88 @@ pub struct Stake<Balance> {
pub active: Balance,
}
/// A generic staking event listener.
///
/// Note that the interface is designed in a way that the events are fired post-action, so any
/// pre-action data that is needed needs to be passed to interface methods. The rest of the data can
/// be retrieved by using `StakingInterface`.
#[impl_trait_for_tuples::impl_for_tuples(10)]
pub trait OnStakingUpdate<AccountId, Balance> {
/// Fired when the stake amount of someone updates.
///
/// This is effectively any changes to the bond amount, such as bonding more funds, and
/// unbonding.
fn on_stake_update(_who: &AccountId, _prev_stake: Option<Stake<Balance>>) {}
/// Fired when someone sets their intention to nominate.
///
/// This should never be fired for existing nominators.
fn on_nominator_add(_who: &AccountId) {}
/// Fired when an existing nominator updates their nominations.
///
/// Note that this is not fired when a nominator changes their stake. For that,
/// `on_stake_update` should be used, followed by querying whether `who` was a validator or a
/// nominator.
fn on_nominator_update(_who: &AccountId, _prev_nominations: Vec<AccountId>) {}
/// Fired when someone removes their intention to nominate, either due to chill or validating.
///
/// The set of nominations at the time of removal is provided as it can no longer be fetched in
/// any way.
fn on_nominator_remove(_who: &AccountId, _nominations: Vec<AccountId>) {}
/// Fired when someone sets their intention to validate.
///
/// Note validator preference changes are not communicated, but could be added if needed.
fn on_validator_add(_who: &AccountId) {}
/// Fired when an existing validator updates their preferences.
///
/// Note validator preference changes are not communicated, but could be added if needed.
fn on_validator_update(_who: &AccountId) {}
/// Fired when someone removes their intention to validate, either due to chill or nominating.
fn on_validator_remove(_who: &AccountId) {}
/// Fired when someone is fully unstaked.
fn on_unstake(_who: &AccountId) {}
/// Fired when a staker is slashed.
///
/// * `stash` - The stash of the staker whom the slash was applied to.
/// * `slashed_active` - The new bonded balance of the staker after the slash was applied.
/// * `slashed_unlocking` - A map of slashed eras, and the balance of that unlocking chunk after
/// the slash is applied. Any era not present in the map is not affected at all.
fn on_slash(
_stash: &AccountId,
_slashed_active: Balance,
_slashed_unlocking: &BTreeMap<EraIndex, Balance>,
) {
}
}
/// A generic representation of a staking implementation.
///
/// This interface uses the terminology of NPoS, but it is aims to be generic enough to cover other
/// implementations as well.
pub trait StakingInterface {
/// Balance type used by the staking system.
type Balance: PartialEq;
type Balance: Sub<Output = Self::Balance>
+ Ord
+ PartialEq
+ Default
+ Copy
+ MaxEncodedLen
+ FullCodec
+ TypeInfo
+ Saturating;
/// AccountId type used by the staking system
type AccountId;
/// AccountId type used by the staking system.
type AccountId: Clone + sp_std::fmt::Debug;
/// Means of converting Currency to VoteWeight.
type CurrencyToVote: CurrencyToVote<Self::Balance>;
/// The minimum amount required to bond in order to set nomination intentions. This does not
/// necessarily mean the nomination will be counted in an election, but instead just enough to
@@ -195,8 +248,12 @@ pub trait StakingInterface {
/// Return the status of the given staker, `None` if not staked at all.
fn status(who: &Self::AccountId) -> Result<StakerStatus<Self::AccountId>, DispatchError>;
/// Checks whether or not this is a validator account.
fn is_validator(who: &Self::AccountId) -> bool {
Self::status(who).map(|s| matches!(s, StakerStatus::Validator)).unwrap_or(false)
}
/// 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>> {
match Self::status(who) {
Ok(StakerStatus::Nominator(t)) => Some(t),