|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#![cfg_attr(not(feature = "std"), no_std)] |
| 19 | + |
| 20 | +//! A crate which contains primitives that are useful for implementation that uses staking |
| 21 | +//! approaches in general. Definitions related to sessions, slashing, etc go here. |
| 22 | +
|
| 23 | +use sp_runtime::{DispatchError, DispatchResult}; |
| 24 | +use sp_std::{collections::btree_map::BTreeMap, vec::Vec}; |
| 25 | + |
| 26 | +pub mod offence; |
| 27 | + |
| 28 | +/// Simple index type with which we can count sessions. |
| 29 | +pub type SessionIndex = u32; |
| 30 | + |
| 31 | +/// Counter for the number of eras that have passed. |
| 32 | +pub type EraIndex = u32; |
| 33 | + |
| 34 | +/// Trait describing something2 that implements a hook for any operations to perform when a staker is |
| 35 | +/// slashed. |
| 36 | +pub trait OnStakerSlash<AccountId, Balance> { |
| 37 | + /// A hook for any operations to perform when a staker is slashed. |
| 38 | + /// |
| 39 | + /// # Arguments |
| 40 | + /// |
| 41 | + /// * `stash` - The stash of the staker whom the slash was applied to. |
| 42 | + /// * `slashed_active` - The new bonded balance of the staker after the slash was applied. |
| 43 | + /// * `slashed_unlocking` - A map of slashed eras, and the balance of that unlocking chunk after |
| 44 | + /// the slash is applied. Any era not present in the map is not affected at all. |
| 45 | + fn on_slash( |
| 46 | + stash: &AccountId, |
| 47 | + slashed_active: Balance, |
| 48 | + slashed_unlocking: &BTreeMap<EraIndex, Balance>, |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +impl<AccountId, Balance> OnStakerSlash<AccountId, Balance> for () { |
| 53 | + fn on_slash(_: &AccountId, _: Balance, _: &BTreeMap<EraIndex, Balance>) { |
| 54 | + // Nothing to do here |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// A struct that reflects stake that an account has in the staking system. Provides a set of |
| 59 | +/// methods to operate on it's properties. Aimed at making `StakingInterface` more concise. |
| 60 | +pub struct Stake<T: StakingInterface + ?Sized> { |
| 61 | + /// The stash account whose balance is actually locked and at stake. |
| 62 | + pub stash: T::AccountId, |
| 63 | + /// The total stake that `stash` has in the staking system. This includes the |
| 64 | + /// `active` stake, and any funds currently in the process of unbonding via |
| 65 | + /// [`StakingInterface::unbond`]. |
| 66 | + /// |
| 67 | + /// # Note |
| 68 | + /// |
| 69 | + /// This is only guaranteed to reflect the amount locked by the staking system. If there are |
| 70 | + /// non-staking locks on the bonded pair's balance this amount is going to be larger in |
| 71 | + /// reality. |
| 72 | + pub total: T::Balance, |
| 73 | + /// The total amount of the stash's balance that will be at stake in any forthcoming |
| 74 | + /// rounds. |
| 75 | + pub active: T::Balance, |
| 76 | +} |
| 77 | + |
| 78 | +/// A generic representation of a staking implementation. |
| 79 | +/// |
| 80 | +/// This interface uses the terminology of NPoS, but it is aims to be generic enough to cover other |
| 81 | +/// implementations as well. |
| 82 | +pub trait StakingInterface { |
| 83 | + /// Balance type used by the staking system. |
| 84 | + type Balance: PartialEq; |
| 85 | + |
| 86 | + /// AccountId type used by the staking system |
| 87 | + type AccountId; |
| 88 | + |
| 89 | + /// The minimum amount required to bond in order to set nomination intentions. This does not |
| 90 | + /// necessarily mean the nomination will be counted in an election, but instead just enough to |
| 91 | + /// be stored as a nominator. In other words, this is the minimum amount to register the |
| 92 | + /// intention to nominate. |
| 93 | + fn minimum_nominator_bond() -> Self::Balance; |
| 94 | + |
| 95 | + /// The minimum amount required to bond in order to set validation intentions. |
| 96 | + fn minimum_validator_bond() -> Self::Balance; |
| 97 | + |
| 98 | + /// Return a stash account that is controlled by a `controller`. |
| 99 | + /// |
| 100 | + /// ## Note |
| 101 | + /// |
| 102 | + /// The controller abstraction is not permanent and might go away. Avoid using this as much as |
| 103 | + /// possible. |
| 104 | + fn stash_by_ctrl(controller: &Self::AccountId) -> Result<Self::AccountId, DispatchError>; |
| 105 | + |
| 106 | + /// Number of eras that staked funds must remain bonded for. |
| 107 | + fn bonding_duration() -> EraIndex; |
| 108 | + |
| 109 | + /// The current era index. |
| 110 | + /// |
| 111 | + /// This should be the latest planned era that the staking system knows about. |
| 112 | + fn current_era() -> EraIndex; |
| 113 | + |
| 114 | + /// Returns the stake of `who`. |
| 115 | + fn stake(who: &Self::AccountId) -> Result<Stake<Self>, DispatchError>; |
| 116 | + |
| 117 | + fn total_stake(who: &Self::AccountId) -> Result<Self::Balance, DispatchError> { |
| 118 | + Self::stake(who).map(|s| s.total) |
| 119 | + } |
| 120 | + |
| 121 | + fn active_stake(who: &Self::AccountId) -> Result<Self::Balance, DispatchError> { |
| 122 | + Self::stake(who).map(|s| s.active) |
| 123 | + } |
| 124 | + |
| 125 | + fn is_unbonding(who: &Self::AccountId) -> Result<bool, DispatchError> { |
| 126 | + Self::stake(who).map(|s| s.active != s.total) |
| 127 | + } |
| 128 | + |
| 129 | + fn fully_unbond(who: &Self::AccountId) -> DispatchResult { |
| 130 | + Self::unbond(who, Self::stake(who)?.active) |
| 131 | + } |
| 132 | + |
| 133 | + /// Bond (lock) `value` of `who`'s balance, while forwarding any rewards to `payee`. |
| 134 | + fn bond(who: &Self::AccountId, value: Self::Balance, payee: &Self::AccountId) |
| 135 | + -> DispatchResult; |
| 136 | + |
| 137 | + /// Have `who` nominate `validators`. |
| 138 | + fn nominate(who: &Self::AccountId, validators: Vec<Self::AccountId>) -> DispatchResult; |
| 139 | + |
| 140 | + /// Chill `who`. |
| 141 | + fn chill(who: &Self::AccountId) -> DispatchResult; |
| 142 | + |
| 143 | + /// Bond some extra amount in `who`'s free balance against the active bonded balance of |
| 144 | + /// the account. The amount extra actually bonded will never be more than `who`'s free |
| 145 | + /// balance. |
| 146 | + fn bond_extra(who: &Self::AccountId, extra: Self::Balance) -> DispatchResult; |
| 147 | + |
| 148 | + /// Schedule a portion of the active bonded balance to be unlocked at era |
| 149 | + /// [Self::current_era] + [`Self::bonding_duration`]. |
| 150 | + /// |
| 151 | + /// Once the unlock era has been reached, [`Self::withdraw_unbonded`] can be called to unlock |
| 152 | + /// the funds. |
| 153 | + /// |
| 154 | + /// The amount of times this can be successfully called is limited based on how many distinct |
| 155 | + /// eras funds are schedule to unlock in. Calling [`Self::withdraw_unbonded`] after some unlock |
| 156 | + /// schedules have reached their unlocking era should allow more calls to this function. |
| 157 | + fn unbond(stash: &Self::AccountId, value: Self::Balance) -> DispatchResult; |
| 158 | + |
| 159 | + /// Unlock any funds schedule to unlock before or at the current era. |
| 160 | + /// |
| 161 | + /// Returns whether the stash was killed because of this withdraw or not. |
| 162 | + fn withdraw_unbonded( |
| 163 | + stash: Self::AccountId, |
| 164 | + num_slashing_spans: u32, |
| 165 | + ) -> Result<bool, DispatchError>; |
| 166 | + |
| 167 | + /// The ideal number of active validators. |
| 168 | + fn desired_validator_count() -> u32; |
| 169 | + |
| 170 | + /// Whether or not there is an ongoing election. |
| 171 | + fn election_ongoing() -> bool; |
| 172 | + |
| 173 | + /// Force a current staker to become completely unstaked, immediately. |
| 174 | + fn force_unstake(who: Self::AccountId) -> DispatchResult; |
| 175 | + |
| 176 | + /// Checks whether an account `staker` has been exposed in an era. |
| 177 | + fn is_exposed_in_era(who: &Self::AccountId, era: &EraIndex) -> bool; |
| 178 | + |
| 179 | + /// Get the nominations of a stash, if they are a nominator, `None` otherwise. |
| 180 | + #[cfg(feature = "runtime-benchmarks")] |
| 181 | + fn nominations(who: Self::AccountId) -> Option<Vec<Self::AccountId>>; |
| 182 | + |
| 183 | + #[cfg(feature = "runtime-benchmarks")] |
| 184 | + fn add_era_stakers( |
| 185 | + current_era: &EraIndex, |
| 186 | + stash: &Self::AccountId, |
| 187 | + exposures: Vec<(Self::AccountId, Self::Balance)>, |
| 188 | + ); |
| 189 | + |
| 190 | + #[cfg(feature = "runtime-benchmarks")] |
| 191 | + fn set_current_era(era: EraIndex); |
| 192 | +} |
| 193 | + |
| 194 | +// sp_core::generate_feature_enabled_macro!(runtime_benchmarks_enabled, feature = "runtime-benchmarks", $); |
0 commit comments