mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 16:11:05 +00:00
Phase 1 of repo reorg (#719)
* Remove unneeded script * Rename Substrate Demo -> Substrate * Rename demo -> node * Build wasm from last rename. * Merge ed25519 into substrate-primitives * Minor tweak * Rename substrate -> core * Move substrate-runtime-support to core/runtime/support * Rename/move substrate-runtime-version * Move codec up a level * Rename substrate-codec -> parity-codec * Move environmental up a level * Move pwasm-* up to top, ready for removal * Remove requirement of s-r-support from s-r-primitives * Move core/runtime/primitives into core/runtime-primitives * Remove s-r-support dep from s-r-version * Remove dep of s-r-support from bft * Remove dep of s-r-support from node/consensus * Sever all other core deps from s-r-support * Forgot the no_std directive * Rename non-SRML modules to sr-* to avoid match clashes * Move runtime/* to srml/* * Rename substrate-runtime-* -> srml-* * Move srml to top-level
This commit is contained in:
committed by
Arkadiy Paronyan
parent
8fe5aa4c81
commit
1e01162505
@@ -0,0 +1,77 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Build a staking genesis block.
|
||||
|
||||
#![cfg(feature = "std")]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use rstd::prelude::*;
|
||||
use codec::Encode;
|
||||
use runtime_support::StorageValue;
|
||||
use primitives::traits::As;
|
||||
use substrate_primitives::Blake2Hasher;
|
||||
use {runtime_io, primitives};
|
||||
use super::{Trait, Intentions, CurrentEra, OfflineSlashGrace, MinimumValidatorCount,
|
||||
BondingDuration, SessionsPerEra, ValidatorCount, SessionReward, OfflineSlash};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct GenesisConfig<T: Trait> {
|
||||
pub sessions_per_era: T::BlockNumber,
|
||||
pub current_era: T::BlockNumber,
|
||||
pub intentions: Vec<T::AccountId>,
|
||||
pub validator_count: u32,
|
||||
pub minimum_validator_count: u32,
|
||||
pub bonding_duration: T::BlockNumber,
|
||||
pub session_reward: T::Balance,
|
||||
pub offline_slash: T::Balance,
|
||||
pub offline_slash_grace: u32,
|
||||
}
|
||||
|
||||
impl<T: Trait> Default for GenesisConfig<T> {
|
||||
fn default() -> Self {
|
||||
GenesisConfig {
|
||||
sessions_per_era: T::BlockNumber::sa(1000),
|
||||
current_era: T::BlockNumber::sa(0),
|
||||
intentions: vec![],
|
||||
validator_count: 0,
|
||||
minimum_validator_count: 0,
|
||||
bonding_duration: T::BlockNumber::sa(1000),
|
||||
session_reward: T::Balance::sa(0),
|
||||
offline_slash: T::Balance::sa(0),
|
||||
offline_slash_grace: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> primitives::BuildStorage for GenesisConfig<T> {
|
||||
fn build_storage(self) -> ::std::result::Result<HashMap<Vec<u8>, Vec<u8>>, String> {
|
||||
let r: runtime_io::TestExternalities<Blake2Hasher> = map![
|
||||
Self::hash(<Intentions<T>>::key()).to_vec() => self.intentions.encode(),
|
||||
Self::hash(<SessionsPerEra<T>>::key()).to_vec() => self.sessions_per_era.encode(),
|
||||
Self::hash(<ValidatorCount<T>>::key()).to_vec() => self.validator_count.encode(),
|
||||
Self::hash(<MinimumValidatorCount<T>>::key()).to_vec() => self.minimum_validator_count.encode(),
|
||||
Self::hash(<BondingDuration<T>>::key()).to_vec() => self.bonding_duration.encode(),
|
||||
Self::hash(<CurrentEra<T>>::key()).to_vec() => self.current_era.encode(),
|
||||
Self::hash(<SessionReward<T>>::key()).to_vec() => self.session_reward.encode(),
|
||||
Self::hash(<OfflineSlash<T>>::key()).to_vec() => self.offline_slash.encode(),
|
||||
Self::hash(<OfflineSlashGrace<T>>::key()).to_vec() => self.offline_slash_grace.encode()
|
||||
];
|
||||
Ok(r.into())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,576 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Staking manager: Periodically determines the best set of validators.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
extern crate serde;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
#[macro_use]
|
||||
extern crate srml_support as runtime_support;
|
||||
|
||||
#[cfg_attr(feature = "std", macro_use)]
|
||||
extern crate sr_std as rstd;
|
||||
|
||||
#[macro_use]
|
||||
extern crate parity_codec_derive;
|
||||
|
||||
extern crate parity_codec as codec;
|
||||
extern crate substrate_primitives;
|
||||
extern crate sr_io as runtime_io;
|
||||
extern crate sr_primitives as primitives;
|
||||
extern crate srml_balances as balances;
|
||||
extern crate srml_consensus as consensus;
|
||||
extern crate sr_sandbox as sandbox;
|
||||
extern crate srml_session as session;
|
||||
extern crate srml_system as system;
|
||||
extern crate srml_timestamp as timestamp;
|
||||
|
||||
use rstd::prelude::*;
|
||||
use runtime_support::{Parameter, StorageValue, StorageMap};
|
||||
use runtime_support::dispatch::Result;
|
||||
use session::OnSessionChange;
|
||||
use primitives::traits::{Zero, One, Bounded, OnFinalise,
|
||||
As, Lookup};
|
||||
use balances::{address::Address, OnDilution};
|
||||
use system::{ensure_root, ensure_signed};
|
||||
|
||||
mod mock;
|
||||
|
||||
mod tests;
|
||||
mod genesis_config;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use genesis_config::GenesisConfig;
|
||||
|
||||
const DEFAULT_MINIMUM_VALIDATOR_COUNT: usize = 4;
|
||||
|
||||
#[derive(PartialEq, Clone)]
|
||||
#[cfg_attr(test, derive(Debug))]
|
||||
pub enum LockStatus<BlockNumber: Parameter> {
|
||||
Liquid,
|
||||
LockedUntil(BlockNumber),
|
||||
Bonded,
|
||||
}
|
||||
|
||||
/// Preference of what happens on a slash event.
|
||||
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
|
||||
pub struct ValidatorPrefs<Balance> {
|
||||
/// Validator should ensure this many more slashes than is necessary before being unstaked.
|
||||
pub unstake_threshold: u32,
|
||||
// Reward that validator takes up-front; only the rest is split between themself and nominators.
|
||||
pub validator_payment: Balance,
|
||||
}
|
||||
|
||||
impl<B: Default> Default for ValidatorPrefs<B> {
|
||||
fn default() -> Self {
|
||||
ValidatorPrefs {
|
||||
unstake_threshold: 3,
|
||||
validator_payment: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Trait: balances::Trait + session::Trait {
|
||||
/// Some tokens minted.
|
||||
type OnRewardMinted: OnDilution<<Self as balances::Trait>::Balance>;
|
||||
|
||||
/// The overarching event type.
|
||||
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
|
||||
}
|
||||
|
||||
decl_module! {
|
||||
#[cfg_attr(feature = "std", serde(bound(deserialize = "T::Balance: ::serde::de::DeserializeOwned")))]
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
fn stake(origin) -> Result;
|
||||
fn unstake(origin, intentions_index: u32) -> Result;
|
||||
fn nominate(origin, target: Address<T::AccountId, T::AccountIndex>) -> Result;
|
||||
fn unnominate(origin, target_index: u32) -> Result;
|
||||
fn register_preferences(origin, intentions_index: u32, prefs: ValidatorPrefs<T::Balance>) -> Result;
|
||||
|
||||
fn set_sessions_per_era(origin, new: T::BlockNumber) -> Result;
|
||||
fn set_bonding_duration(origin, new: T::BlockNumber) -> Result;
|
||||
fn set_validator_count(origin, new: u32) -> Result;
|
||||
fn force_new_era(origin, apply_rewards: bool) -> Result;
|
||||
fn set_offline_slash_grace(origin, new: u32) -> Result;
|
||||
}
|
||||
}
|
||||
|
||||
/// An event in this module.
|
||||
decl_event!(
|
||||
pub enum Event<T> with RawEvent<Balance, AccountId>
|
||||
where <T as balances::Trait>::Balance, <T as system::Trait>::AccountId
|
||||
{
|
||||
/// All validators have been rewarded by the given balance.
|
||||
Reward(Balance),
|
||||
/// One validator (and their nominators) has been given a offline-warning (they're still
|
||||
/// within their grace). The accrued number of slashes is recorded, too.
|
||||
OfflineWarning(AccountId, u32),
|
||||
/// One validator (and their nominators) has been slashed by the given amount.
|
||||
OfflineSlash(AccountId, Balance),
|
||||
}
|
||||
);
|
||||
|
||||
pub type PairOf<T> = (T, T);
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Trait> as Staking {
|
||||
|
||||
/// The ideal number of staking participants.
|
||||
pub ValidatorCount get(validator_count): required u32;
|
||||
/// Minimum number of staking participants before emergency conditions are imposed.
|
||||
pub MinimumValidatorCount: u32;
|
||||
/// The length of a staking era in sessions.
|
||||
pub SessionsPerEra get(sessions_per_era): required T::BlockNumber;
|
||||
/// Maximum reward, per validator, that is provided per acceptable session.
|
||||
pub SessionReward get(session_reward): required T::Balance;
|
||||
/// Slash, per validator that is taken for the first time they are found to be offline.
|
||||
pub OfflineSlash get(offline_slash): required T::Balance;
|
||||
/// Number of instances of offline reports before slashing begins for validators.
|
||||
pub OfflineSlashGrace get(offline_slash_grace): default u32;
|
||||
/// The length of the bonding duration in blocks.
|
||||
pub BondingDuration get(bonding_duration): required T::BlockNumber;
|
||||
|
||||
/// The current era index.
|
||||
pub CurrentEra get(current_era): required T::BlockNumber;
|
||||
/// Preferences that a validator has.
|
||||
pub ValidatorPreferences get(validator_preferences): default map [ T::AccountId => ValidatorPrefs<T::Balance> ];
|
||||
/// All the accounts with a desire to stake.
|
||||
pub Intentions get(intentions): default Vec<T::AccountId>;
|
||||
/// All nominator -> nominee relationships.
|
||||
pub Nominating get(nominating): map [ T::AccountId => T::AccountId ];
|
||||
/// Nominators for a particular account.
|
||||
pub NominatorsFor get(nominators_for): default map [ T::AccountId => Vec<T::AccountId> ];
|
||||
/// Nominators for a particular account that is in action right now.
|
||||
pub CurrentNominatorsFor get(current_nominators_for): default map [ T::AccountId => Vec<T::AccountId> ];
|
||||
/// The next value of sessions per era.
|
||||
pub NextSessionsPerEra get(next_sessions_per_era): T::BlockNumber;
|
||||
/// The session index at which the era length last changed.
|
||||
pub LastEraLengthChange get(last_era_length_change): default T::BlockNumber;
|
||||
|
||||
/// The highest and lowest staked validator slashable balances.
|
||||
pub StakeRange get(stake_range): default PairOf<T::Balance>;
|
||||
|
||||
/// The block at which the `who`'s funds become entirely liquid.
|
||||
pub Bondage get(bondage): default map [ T::AccountId => T::BlockNumber ];
|
||||
/// The number of times a given validator has been reported offline. This gets decremented by one each era that passes.
|
||||
pub SlashCount get(slash_count): default map [ T::AccountId => u32 ];
|
||||
|
||||
/// We are forcing a new era.
|
||||
pub ForcingNewEra get(forcing_new_era): ();
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> Module<T> {
|
||||
|
||||
/// Deposit one of this module's events.
|
||||
fn deposit_event(event: Event<T>) {
|
||||
<system::Module<T>>::deposit_event(<T as Trait>::Event::from(event).into());
|
||||
}
|
||||
|
||||
// PUBLIC IMMUTABLES
|
||||
|
||||
/// MinimumValidatorCount getter, introduces a default.
|
||||
pub fn minimum_validator_count() -> usize {
|
||||
<MinimumValidatorCount<T>>::get().map(|v| v as usize).unwrap_or(DEFAULT_MINIMUM_VALIDATOR_COUNT)
|
||||
}
|
||||
|
||||
/// The length of a staking era in blocks.
|
||||
pub fn era_length() -> T::BlockNumber {
|
||||
Self::sessions_per_era() * <session::Module<T>>::length()
|
||||
}
|
||||
|
||||
/// Balance of a (potential) validator that includes all nominators.
|
||||
pub fn nomination_balance(who: &T::AccountId) -> T::Balance {
|
||||
Self::nominators_for(who).iter()
|
||||
.map(<balances::Module<T>>::total_balance)
|
||||
.fold(Zero::zero(), |acc, x| acc + x)
|
||||
}
|
||||
|
||||
/// The total balance that can be slashed from an account.
|
||||
pub fn slashable_balance(who: &T::AccountId) -> T::Balance {
|
||||
Self::nominators_for(who).iter()
|
||||
.map(<balances::Module<T>>::total_balance)
|
||||
.fold(<balances::Module<T>>::total_balance(who), |acc, x| acc + x)
|
||||
}
|
||||
|
||||
/// The block at which the `who`'s funds become entirely liquid.
|
||||
pub fn unlock_block(who: &T::AccountId) -> LockStatus<T::BlockNumber> {
|
||||
match Self::bondage(who) {
|
||||
i if i == T::BlockNumber::max_value() => LockStatus::Bonded,
|
||||
i if i <= <system::Module<T>>::block_number() => LockStatus::Liquid,
|
||||
i => LockStatus::LockedUntil(i),
|
||||
}
|
||||
}
|
||||
|
||||
// PUBLIC DISPATCH
|
||||
|
||||
/// Declare the desire to stake for the transactor.
|
||||
///
|
||||
/// Effects will be felt at the beginning of the next era.
|
||||
fn stake(origin: T::Origin) -> Result {
|
||||
let who = ensure_signed(origin)?;
|
||||
ensure!(Self::nominating(&who).is_none(), "Cannot stake if already nominating.");
|
||||
let mut intentions = <Intentions<T>>::get();
|
||||
// can't be in the list twice.
|
||||
ensure!(intentions.iter().find(|&t| t == &who).is_none(), "Cannot stake if already staked.");
|
||||
|
||||
<Bondage<T>>::insert(&who, T::BlockNumber::max_value());
|
||||
intentions.push(who);
|
||||
<Intentions<T>>::put(intentions);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Retract the desire to stake for the transactor.
|
||||
///
|
||||
/// Effects will be felt at the beginning of the next era.
|
||||
fn unstake(origin: T::Origin, intentions_index: u32) -> Result {
|
||||
let who = ensure_signed(origin)?;
|
||||
// unstake fails in degenerate case of having too few existing staked parties
|
||||
if Self::intentions().len() <= Self::minimum_validator_count() {
|
||||
return Err("cannot unstake when there are too few staked participants")
|
||||
}
|
||||
Self::apply_unstake(&who, intentions_index as usize)
|
||||
}
|
||||
|
||||
fn nominate(origin: T::Origin, target: Address<T::AccountId, T::AccountIndex>) -> Result {
|
||||
let who = ensure_signed(origin)?;
|
||||
let target = <balances::Module<T>>::lookup(target)?;
|
||||
|
||||
ensure!(Self::nominating(&who).is_none(), "Cannot nominate if already nominating.");
|
||||
ensure!(Self::intentions().iter().find(|&t| t == &who).is_none(), "Cannot nominate if already staked.");
|
||||
|
||||
// update nominators_for
|
||||
let mut t = Self::nominators_for(&target);
|
||||
t.push(who.clone());
|
||||
<NominatorsFor<T>>::insert(&target, t);
|
||||
|
||||
// update nominating
|
||||
<Nominating<T>>::insert(&who, &target);
|
||||
|
||||
// Update bondage
|
||||
<Bondage<T>>::insert(&who, T::BlockNumber::max_value());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Will panic if called when source isn't currently nominating target.
|
||||
/// Updates Nominating, NominatorsFor and NominationBalance.
|
||||
fn unnominate(origin: T::Origin, target_index: u32) -> Result {
|
||||
let source = ensure_signed(origin)?;
|
||||
let target_index = target_index as usize;
|
||||
|
||||
let target = <Nominating<T>>::get(&source).ok_or("Account must be nominating")?;
|
||||
|
||||
let mut t = Self::nominators_for(&target);
|
||||
if t.get(target_index) != Some(&source) {
|
||||
return Err("Invalid target index")
|
||||
}
|
||||
|
||||
// Ok - all valid.
|
||||
|
||||
// update nominators_for
|
||||
t.swap_remove(target_index);
|
||||
<NominatorsFor<T>>::insert(&target, t);
|
||||
|
||||
// update nominating
|
||||
<Nominating<T>>::remove(&source);
|
||||
|
||||
// update bondage
|
||||
<Bondage<T>>::insert(source, <system::Module<T>>::block_number() + Self::bonding_duration());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set the given account's preference for slashing behaviour should they be a validator.
|
||||
///
|
||||
/// An error (no-op) if `Self::intentions()[intentions_index] != origin`.
|
||||
fn register_preferences(
|
||||
origin: T::Origin,
|
||||
intentions_index: u32,
|
||||
prefs: ValidatorPrefs<T::Balance>
|
||||
) -> Result {
|
||||
let who = ensure_signed(origin)?;
|
||||
|
||||
if Self::intentions().get(intentions_index as usize) != Some(&who) {
|
||||
return Err("Invalid index")
|
||||
}
|
||||
|
||||
<ValidatorPreferences<T>>::insert(who, prefs);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// PRIV DISPATCH
|
||||
|
||||
/// Set the number of sessions in an era.
|
||||
fn set_sessions_per_era(origin: T::Origin, new: T::BlockNumber) -> Result {
|
||||
ensure_root(origin)?;
|
||||
<NextSessionsPerEra<T>>::put(&new);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The length of the bonding duration in eras.
|
||||
fn set_bonding_duration(origin: T::Origin, new: T::BlockNumber) -> Result {
|
||||
ensure_root(origin)?;
|
||||
<BondingDuration<T>>::put(&new);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The length of a staking era in sessions.
|
||||
fn set_validator_count(origin: T::Origin, new: u32) -> Result {
|
||||
ensure_root(origin)?;
|
||||
<ValidatorCount<T>>::put(&new);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Force there to be a new era. This also forces a new session immediately after.
|
||||
/// `apply_rewards` should be true for validators to get the session reward.
|
||||
fn force_new_era(origin: T::Origin, apply_rewards: bool) -> Result {
|
||||
ensure_root(origin)?;
|
||||
Self::apply_force_new_era(apply_rewards)
|
||||
}
|
||||
|
||||
// Just force_new_era without origin check.
|
||||
fn apply_force_new_era(apply_rewards: bool) -> Result {
|
||||
<ForcingNewEra<T>>::put(());
|
||||
<session::Module<T>>::apply_force_new_session(apply_rewards)
|
||||
}
|
||||
|
||||
|
||||
/// Set the offline slash grace period.
|
||||
fn set_offline_slash_grace(origin: T::Origin, new: u32) -> Result {
|
||||
ensure_root(origin)?;
|
||||
<OfflineSlashGrace<T>>::put(&new);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// PUBLIC MUTABLES (DANGEROUS)
|
||||
|
||||
/// Slash a given validator by a specific amount. Removes the slash from their balance by preference,
|
||||
/// and reduces the nominators' balance if needed.
|
||||
fn slash_validator(v: &T::AccountId, slash: T::Balance) {
|
||||
// skip the slash in degenerate case of having only 4 staking participants despite having a larger
|
||||
// desired number of validators (validator_count).
|
||||
if Self::intentions().len() <= Self::minimum_validator_count() {
|
||||
return
|
||||
}
|
||||
|
||||
if let Some(rem) = <balances::Module<T>>::slash(v, slash) {
|
||||
let noms = Self::current_nominators_for(v);
|
||||
let total = noms.iter().map(<balances::Module<T>>::total_balance).fold(T::Balance::zero(), |acc, x| acc + x);
|
||||
if !total.is_zero() {
|
||||
let safe_mul_rational = |b| b * rem / total;// TODO: avoid overflow
|
||||
for n in noms.iter() {
|
||||
let _ = <balances::Module<T>>::slash(n, safe_mul_rational(<balances::Module<T>>::total_balance(n))); // best effort - not much that can be done on fail.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reward a given validator by a specific amount. Add the reward to their, and their nominators'
|
||||
/// balance, pro-rata.
|
||||
fn reward_validator(who: &T::AccountId, reward: T::Balance) {
|
||||
let off_the_table = reward.min(Self::validator_preferences(who).validator_payment);
|
||||
let reward = reward - off_the_table;
|
||||
let validator_cut = if reward.is_zero() {
|
||||
Zero::zero()
|
||||
} else {
|
||||
let noms = Self::current_nominators_for(who);
|
||||
let total = noms.iter()
|
||||
.map(<balances::Module<T>>::total_balance)
|
||||
.fold(<balances::Module<T>>::total_balance(who), |acc, x| acc + x)
|
||||
.max(One::one());
|
||||
let safe_mul_rational = |b| b * reward / total;// TODO: avoid overflow
|
||||
for n in noms.iter() {
|
||||
let _ = <balances::Module<T>>::reward(n, safe_mul_rational(<balances::Module<T>>::total_balance(n)));
|
||||
}
|
||||
safe_mul_rational(<balances::Module<T>>::total_balance(who))
|
||||
};
|
||||
let _ = <balances::Module<T>>::reward(who, validator_cut + off_the_table);
|
||||
}
|
||||
|
||||
/// Actually carry out the unstake operation.
|
||||
/// Assumes `intentions()[intentions_index] == who`.
|
||||
fn apply_unstake(who: &T::AccountId, intentions_index: usize) -> Result {
|
||||
let mut intentions = Self::intentions();
|
||||
if intentions.get(intentions_index) != Some(who) {
|
||||
return Err("Invalid index");
|
||||
}
|
||||
intentions.swap_remove(intentions_index);
|
||||
<Intentions<T>>::put(intentions);
|
||||
<ValidatorPreferences<T>>::remove(who);
|
||||
<SlashCount<T>>::remove(who);
|
||||
<Bondage<T>>::insert(who, <system::Module<T>>::block_number() + Self::bonding_duration());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the reward for the session, assuming it ends with this block.
|
||||
fn this_session_reward(actual_elapsed: T::Moment) -> T::Balance {
|
||||
let ideal_elapsed = <session::Module<T>>::ideal_session_duration();
|
||||
let per65536: u64 = (T::Moment::sa(65536u64) * ideal_elapsed.clone() / actual_elapsed.max(ideal_elapsed)).as_();
|
||||
Self::session_reward() * T::Balance::sa(per65536) / T::Balance::sa(65536u64)
|
||||
}
|
||||
|
||||
/// Session has just changed. We need to determine whether we pay a reward, slash and/or
|
||||
/// move to a new era.
|
||||
fn new_session(actual_elapsed: T::Moment, should_reward: bool) {
|
||||
if should_reward {
|
||||
// apply good session reward
|
||||
let reward = Self::this_session_reward(actual_elapsed);
|
||||
let validators = <session::Module<T>>::validators();
|
||||
for v in validators.iter() {
|
||||
Self::reward_validator(v, reward);
|
||||
}
|
||||
Self::deposit_event(RawEvent::Reward(reward));
|
||||
let total_minted = reward * <T::Balance as As<usize>>::sa(validators.len());
|
||||
let total_rewarded_stake = Self::stake_range().0 * <T::Balance as As<usize>>::sa(validators.len());
|
||||
T::OnRewardMinted::on_dilution(total_minted, total_rewarded_stake);
|
||||
}
|
||||
|
||||
let session_index = <session::Module<T>>::current_index();
|
||||
if <ForcingNewEra<T>>::take().is_some()
|
||||
|| ((session_index - Self::last_era_length_change()) % Self::sessions_per_era()).is_zero()
|
||||
{
|
||||
Self::new_era();
|
||||
}
|
||||
}
|
||||
|
||||
/// The era has changed - enact new staking set.
|
||||
///
|
||||
/// NOTE: This always happens immediately before a session change to ensure that new validators
|
||||
/// get a chance to set their session keys.
|
||||
fn new_era() {
|
||||
// Increment current era.
|
||||
<CurrentEra<T>>::put(&(<CurrentEra<T>>::get() + One::one()));
|
||||
|
||||
// Enact era length change.
|
||||
if let Some(next_spe) = Self::next_sessions_per_era() {
|
||||
if next_spe != Self::sessions_per_era() {
|
||||
<SessionsPerEra<T>>::put(&next_spe);
|
||||
<LastEraLengthChange<T>>::put(&<session::Module<T>>::current_index());
|
||||
}
|
||||
}
|
||||
|
||||
// evaluate desired staking amounts and nominations and optimise to find the best
|
||||
// combination of validators, then use session::internal::set_validators().
|
||||
// for now, this just orders would-be stakers by their balances and chooses the top-most
|
||||
// <ValidatorCount<T>>::get() of them.
|
||||
// TODO: this is not sound. this should be moved to an off-chain solution mechanism.
|
||||
let mut intentions = Self::intentions()
|
||||
.into_iter()
|
||||
.map(|v| (Self::slashable_balance(&v), v))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Avoid reevaluate validator set if it would leave us with fewer than the minimum
|
||||
// needed validators
|
||||
if intentions.len() < Self::minimum_validator_count() {
|
||||
return
|
||||
}
|
||||
|
||||
intentions.sort_unstable_by(|&(ref b1, _), &(ref b2, _)| b2.cmp(&b1));
|
||||
|
||||
<StakeRange<T>>::put(
|
||||
if !intentions.is_empty() {
|
||||
let n = <ValidatorCount<T>>::get() as usize;
|
||||
(intentions[0].0, intentions[n - 1].0)
|
||||
} else {
|
||||
(Zero::zero(), Zero::zero())
|
||||
}
|
||||
);
|
||||
let vals = &intentions.into_iter()
|
||||
.map(|(_, v)| v)
|
||||
.take(<ValidatorCount<T>>::get() as usize)
|
||||
.collect::<Vec<_>>();
|
||||
for v in <session::Module<T>>::validators().iter() {
|
||||
<CurrentNominatorsFor<T>>::remove(v);
|
||||
let slash_count = <SlashCount<T>>::take(v);
|
||||
if slash_count > 1 {
|
||||
<SlashCount<T>>::insert(v, slash_count - 1);
|
||||
}
|
||||
}
|
||||
for v in vals.iter() {
|
||||
<CurrentNominatorsFor<T>>::insert(v, Self::nominators_for(v));
|
||||
}
|
||||
<session::Module<T>>::set_validators(vals);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> OnFinalise<T::BlockNumber> for Module<T> {
|
||||
fn on_finalise(_n: T::BlockNumber) {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> OnSessionChange<T::Moment> for Module<T> {
|
||||
fn on_session_change(elapsed: T::Moment, should_reward: bool) {
|
||||
Self::new_session(elapsed, should_reward);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> balances::EnsureAccountLiquid<T::AccountId> for Module<T> {
|
||||
fn ensure_account_liquid(who: &T::AccountId) -> Result {
|
||||
if Self::bondage(who) <= <system::Module<T>>::block_number() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("cannot transfer illiquid funds")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> balances::OnFreeBalanceZero<T::AccountId> for Module<T> {
|
||||
fn on_free_balance_zero(who: &T::AccountId) {
|
||||
<Bondage<T>>::remove(who);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> consensus::OnOfflineValidator for Module<T> {
|
||||
fn on_offline_validator(validator_index: usize) {
|
||||
let v = <session::Module<T>>::validators()[validator_index].clone();
|
||||
let slash_count = Self::slash_count(&v);
|
||||
<SlashCount<T>>::insert(v.clone(), slash_count + 1);
|
||||
let grace = Self::offline_slash_grace();
|
||||
|
||||
let event = if slash_count >= grace {
|
||||
let instances = slash_count - grace;
|
||||
let slash = Self::offline_slash() << instances;
|
||||
let next_slash = slash << 1u32;
|
||||
let _ = Self::slash_validator(&v, slash);
|
||||
if instances >= Self::validator_preferences(&v).unstake_threshold
|
||||
|| Self::slashable_balance(&v) < next_slash
|
||||
{
|
||||
if let Some(pos) = Self::intentions().into_iter().position(|x| &x == &v) {
|
||||
Self::apply_unstake(&v, pos)
|
||||
.expect("pos derived correctly from Self::intentions(); \
|
||||
apply_unstake can only fail if pos wrong; \
|
||||
Self::intentions() doesn't change; qed");
|
||||
}
|
||||
let _ = Self::apply_force_new_era(false);
|
||||
}
|
||||
RawEvent::OfflineSlash(v, slash)
|
||||
} else {
|
||||
RawEvent::OfflineWarning(v, slash_count)
|
||||
};
|
||||
Self::deposit_event(event);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// Copyright 2018 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test utilities
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use primitives::BuildStorage;
|
||||
use primitives::traits::{Identity};
|
||||
use primitives::testing::{Digest, Header};
|
||||
use substrate_primitives::{H256, Blake2Hasher};
|
||||
use runtime_io;
|
||||
use {GenesisConfig, Module, Trait, consensus, session, system, timestamp, balances};
|
||||
|
||||
impl_outer_origin!{
|
||||
pub enum Origin for Test {}
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||
pub struct Test;
|
||||
impl consensus::Trait for Test {
|
||||
const NOTE_OFFLINE_POSITION: u32 = 1;
|
||||
type Log = u64;
|
||||
type SessionKey = u64;
|
||||
type OnOfflineValidator = ();
|
||||
}
|
||||
impl system::Trait for Test {
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Hash = H256;
|
||||
type Hashing = ::primitives::traits::BlakeTwo256;
|
||||
type Digest = Digest;
|
||||
type AccountId = u64;
|
||||
type Header = Header;
|
||||
type Event = ();
|
||||
}
|
||||
impl balances::Trait for Test {
|
||||
type Balance = u64;
|
||||
type AccountIndex = u64;
|
||||
type OnFreeBalanceZero = Staking;
|
||||
type EnsureAccountLiquid = Staking;
|
||||
type Event = ();
|
||||
}
|
||||
impl session::Trait for Test {
|
||||
type ConvertAccountIdToSessionKey = Identity;
|
||||
type OnSessionChange = Staking;
|
||||
type Event = ();
|
||||
}
|
||||
impl timestamp::Trait for Test {
|
||||
const TIMESTAMP_SET_POSITION: u32 = 0;
|
||||
type Moment = u64;
|
||||
}
|
||||
impl Trait for Test {
|
||||
type OnRewardMinted = ();
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
pub fn new_test_ext(ext_deposit: u64, session_length: u64, sessions_per_era: u64, current_era: u64, monied: bool, reward: u64) -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap();
|
||||
let balance_factor = if ext_deposit > 0 {
|
||||
256
|
||||
} else {
|
||||
1
|
||||
};
|
||||
t.extend(consensus::GenesisConfig::<Test>{
|
||||
code: vec![],
|
||||
authorities: vec![],
|
||||
}.build_storage().unwrap());
|
||||
t.extend(session::GenesisConfig::<Test>{
|
||||
session_length,
|
||||
validators: vec![10, 20],
|
||||
}.build_storage().unwrap());
|
||||
t.extend(balances::GenesisConfig::<Test>{
|
||||
balances: if monied {
|
||||
if reward > 0 {
|
||||
vec![(1, 10 * balance_factor), (2, 20 * balance_factor), (3, 30 * balance_factor), (4, 40 * balance_factor), (10, balance_factor), (20, balance_factor)]
|
||||
} else {
|
||||
vec![(1, 10 * balance_factor), (2, 20 * balance_factor), (3, 30 * balance_factor), (4, 40 * balance_factor)]
|
||||
}
|
||||
} else {
|
||||
vec![(10, balance_factor), (20, balance_factor)]
|
||||
},
|
||||
transaction_base_fee: 0,
|
||||
transaction_byte_fee: 0,
|
||||
existential_deposit: ext_deposit,
|
||||
transfer_fee: 0,
|
||||
creation_fee: 0,
|
||||
reclaim_rebate: 0,
|
||||
}.build_storage().unwrap());
|
||||
t.extend(GenesisConfig::<Test>{
|
||||
sessions_per_era,
|
||||
current_era,
|
||||
intentions: vec![10, 20],
|
||||
validator_count: 2,
|
||||
minimum_validator_count: 0,
|
||||
bonding_duration: sessions_per_era * session_length * 3,
|
||||
session_reward: reward,
|
||||
offline_slash: if monied { 20 } else { 0 },
|
||||
offline_slash_grace: 0,
|
||||
}.build_storage().unwrap());
|
||||
t.extend(timestamp::GenesisConfig::<Test>{
|
||||
period: 5
|
||||
}.build_storage().unwrap());
|
||||
t.into()
|
||||
}
|
||||
|
||||
pub type System = system::Module<Test>;
|
||||
pub type Balances = balances::Module<Test>;
|
||||
pub type Session = session::Module<Test>;
|
||||
pub type Timestamp = timestamp::Module<Test>;
|
||||
pub type Staking = Module<Test>;
|
||||
@@ -0,0 +1,502 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Tests for the module.
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
use consensus::OnOfflineValidator;
|
||||
use runtime_io::with_externalities;
|
||||
use mock::{Balances, Session, Staking, System, Timestamp, Test, new_test_ext, Origin};
|
||||
|
||||
#[test]
|
||||
fn note_null_offline_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
|
||||
assert_eq!(Staking::offline_slash_grace(), 0);
|
||||
assert_eq!(Staking::slash_count(&10), 0);
|
||||
assert_eq!(Balances::free_balance(&10), 1);
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
assert_eq!(Staking::slash_count(&10), 0);
|
||||
assert_eq!(Balances::free_balance(&10), 1);
|
||||
assert!(Staking::forcing_new_era().is_none());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn note_offline_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
|
||||
Balances::set_free_balance(&10, 70);
|
||||
assert_eq!(Staking::offline_slash_grace(), 0);
|
||||
assert_eq!(Staking::slash_count(&10), 0);
|
||||
assert_eq!(Balances::free_balance(&10), 70);
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
assert_eq!(Staking::slash_count(&10), 1);
|
||||
assert_eq!(Balances::free_balance(&10), 50);
|
||||
assert!(Staking::forcing_new_era().is_none());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn note_offline_exponent_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
|
||||
Balances::set_free_balance(&10, 150);
|
||||
assert_eq!(Staking::offline_slash_grace(), 0);
|
||||
assert_eq!(Staking::slash_count(&10), 0);
|
||||
assert_eq!(Balances::free_balance(&10), 150);
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
assert_eq!(Staking::slash_count(&10), 1);
|
||||
assert_eq!(Balances::free_balance(&10), 130);
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
assert_eq!(Staking::slash_count(&10), 2);
|
||||
assert_eq!(Balances::free_balance(&10), 90);
|
||||
assert!(Staking::forcing_new_era().is_none());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn note_offline_grace_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
|
||||
Balances::set_free_balance(&10, 70);
|
||||
Balances::set_free_balance(&20, 70);
|
||||
assert_ok!(Staking::set_offline_slash_grace(Origin::ROOT, 1));
|
||||
assert_eq!(Staking::offline_slash_grace(), 1);
|
||||
|
||||
assert_eq!(Staking::slash_count(&10), 0);
|
||||
assert_eq!(Balances::free_balance(&10), 70);
|
||||
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
assert_eq!(Staking::slash_count(&10), 1);
|
||||
assert_eq!(Balances::free_balance(&10), 70);
|
||||
assert_eq!(Staking::slash_count(&20), 0);
|
||||
assert_eq!(Balances::free_balance(&20), 70);
|
||||
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
Staking::on_offline_validator(1);
|
||||
assert_eq!(Staking::slash_count(&10), 2);
|
||||
assert_eq!(Balances::free_balance(&10), 50);
|
||||
assert_eq!(Staking::slash_count(&20), 1);
|
||||
assert_eq!(Balances::free_balance(&20), 70);
|
||||
assert!(Staking::forcing_new_era().is_none());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn note_offline_force_unstake_session_change_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
|
||||
Balances::set_free_balance(&10, 70);
|
||||
Balances::set_free_balance(&20, 70);
|
||||
assert_ok!(Staking::stake(Origin::signed(1)));
|
||||
|
||||
assert_eq!(Staking::slash_count(&10), 0);
|
||||
assert_eq!(Balances::free_balance(&10), 70);
|
||||
assert_eq!(Staking::intentions(), vec![10, 20, 1]);
|
||||
assert_eq!(Session::validators(), vec![10, 20]);
|
||||
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
assert_eq!(Balances::free_balance(&10), 50);
|
||||
assert_eq!(Staking::slash_count(&10), 1);
|
||||
assert_eq!(Staking::intentions(), vec![10, 20, 1]);
|
||||
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
assert_eq!(Staking::intentions(), vec![1, 20]);
|
||||
assert_eq!(Balances::free_balance(&10), 10);
|
||||
assert!(Staking::forcing_new_era().is_some());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn note_offline_auto_unstake_session_change_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
|
||||
Balances::set_free_balance(&10, 7000);
|
||||
Balances::set_free_balance(&20, 7000);
|
||||
assert_ok!(Staking::register_preferences(Origin::signed(10), 0, ValidatorPrefs { unstake_threshold: 1, validator_payment: 0 }));
|
||||
|
||||
assert_eq!(Staking::intentions(), vec![10, 20]);
|
||||
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
Staking::on_offline_validator(1);
|
||||
assert_eq!(Balances::free_balance(&10), 6980);
|
||||
assert_eq!(Balances::free_balance(&20), 6980);
|
||||
assert_eq!(Staking::intentions(), vec![10, 20]);
|
||||
assert!(Staking::forcing_new_era().is_none());
|
||||
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
Staking::on_offline_validator(1);
|
||||
assert_eq!(Balances::free_balance(&10), 6940);
|
||||
assert_eq!(Balances::free_balance(&20), 6940);
|
||||
assert_eq!(Staking::intentions(), vec![20]);
|
||||
assert!(Staking::forcing_new_era().is_some());
|
||||
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(1);
|
||||
assert_eq!(Balances::free_balance(&10), 6940);
|
||||
assert_eq!(Balances::free_balance(&20), 6860);
|
||||
assert_eq!(Staking::intentions(), vec![20]);
|
||||
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(1);
|
||||
assert_eq!(Balances::free_balance(&10), 6940);
|
||||
assert_eq!(Balances::free_balance(&20), 6700);
|
||||
assert_eq!(Staking::intentions(), vec![0u64; 0]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn rewards_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
|
||||
assert_eq!(Staking::era_length(), 9);
|
||||
assert_eq!(Staking::sessions_per_era(), 3);
|
||||
assert_eq!(Staking::last_era_length_change(), 0);
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
assert_eq!(Session::current_index(), 0);
|
||||
assert_eq!(Balances::total_balance(&10), 1);
|
||||
|
||||
System::set_block_number(3);
|
||||
Timestamp::set_timestamp(15); // on time.
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
assert_eq!(Session::current_index(), 1);
|
||||
assert_eq!(Balances::total_balance(&10), 11);
|
||||
System::set_block_number(6);
|
||||
Timestamp::set_timestamp(31); // a little late
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
assert_eq!(Session::current_index(), 2);
|
||||
assert_eq!(Balances::total_balance(&10), 20); // less reward
|
||||
System::set_block_number(9);
|
||||
Timestamp::set_timestamp(50); // very late
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 1);
|
||||
assert_eq!(Session::current_index(), 3);
|
||||
assert_eq!(Balances::total_balance(&10), 27); // much less reward
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slashing_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
|
||||
assert_eq!(Staking::era_length(), 9);
|
||||
assert_eq!(Staking::sessions_per_era(), 3);
|
||||
assert_eq!(Staking::last_era_length_change(), 0);
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
assert_eq!(Session::current_index(), 0);
|
||||
assert_eq!(Balances::total_balance(&10), 1);
|
||||
|
||||
System::set_block_number(3);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
assert_eq!(Session::current_index(), 1);
|
||||
assert_eq!(Balances::total_balance(&10), 11);
|
||||
|
||||
System::set_block_number(6);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
assert_eq!(Session::current_index(), 2);
|
||||
assert_eq!(Balances::total_balance(&10), 21);
|
||||
|
||||
System::set_block_number(7);
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
Staking::on_offline_validator(1);
|
||||
assert_eq!(Balances::total_balance(&10), 1);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[test]
|
||||
fn staking_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 1, 2, 0, true, 0), || {
|
||||
|
||||
assert_eq!(Staking::era_length(), 2);
|
||||
assert_eq!(Staking::validator_count(), 2);
|
||||
assert_eq!(Session::validators(), vec![10, 20]);
|
||||
|
||||
assert_ok!(Staking::set_bonding_duration(Origin::ROOT, 2));
|
||||
assert_eq!(Staking::bonding_duration(), 2);
|
||||
|
||||
// Block 1: Add three validators. No obvious change.
|
||||
System::set_block_number(1);
|
||||
assert_ok!(Staking::stake(Origin::signed(1)));
|
||||
assert_ok!(Staking::stake(Origin::signed(2)));
|
||||
assert_ok!(Staking::stake(Origin::signed(4)));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
assert_eq!(Session::validators(), vec![10, 20]);
|
||||
|
||||
// Block 2: New validator set now.
|
||||
System::set_block_number(2);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 1);
|
||||
assert_eq!(Session::validators(), vec![4, 2]);
|
||||
|
||||
// Block 3: Unstake highest, introduce another staker. No change yet.
|
||||
System::set_block_number(3);
|
||||
assert_ok!(Staking::stake(Origin::signed(3)));
|
||||
assert_ok!(Staking::unstake(Origin::signed(4), Staking::intentions().iter().position(|&x| x == 4).unwrap() as u32));
|
||||
assert_eq!(Staking::current_era(), 1);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
|
||||
// Block 4: New era - validators change.
|
||||
System::set_block_number(4);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 2);
|
||||
assert_eq!(Session::validators(), vec![3, 2]);
|
||||
|
||||
// Block 5: Transfer stake from highest to lowest. No change yet.
|
||||
System::set_block_number(5);
|
||||
assert_ok!(Balances::transfer(Origin::signed(4), 1.into(), 40));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
|
||||
// Block 6: Lowest now validator.
|
||||
System::set_block_number(6);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::validators(), vec![1, 3]);
|
||||
|
||||
// Block 7: Unstake three. No change yet.
|
||||
System::set_block_number(7);
|
||||
assert_ok!(Staking::unstake(Origin::signed(3), Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::validators(), vec![1, 3]);
|
||||
|
||||
// Block 8: Back to one and two.
|
||||
System::set_block_number(8);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::validators(), vec![1, 2]);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nominating_and_rewards_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 1, 1, 0, true, 10), || {
|
||||
assert_eq!(Staking::era_length(), 1);
|
||||
assert_eq!(Staking::validator_count(), 2);
|
||||
assert_eq!(Staking::bonding_duration(), 3);
|
||||
assert_eq!(Session::validators(), vec![10, 20]);
|
||||
|
||||
System::set_block_number(1);
|
||||
assert_ok!(Staking::stake(Origin::signed(1)));
|
||||
assert_ok!(Staking::stake(Origin::signed(2)));
|
||||
assert_ok!(Staking::stake(Origin::signed(3)));
|
||||
assert_ok!(Staking::nominate(Origin::signed(4), 1.into()));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 1);
|
||||
assert_eq!(Session::validators(), vec![1, 3]); // 4 + 1, 3
|
||||
assert_eq!(Balances::total_balance(&1), 10);
|
||||
assert_eq!(Balances::total_balance(&2), 20);
|
||||
assert_eq!(Balances::total_balance(&3), 30);
|
||||
assert_eq!(Balances::total_balance(&4), 40);
|
||||
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Staking::unnominate(Origin::signed(4), 0));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Staking::current_era(), 2);
|
||||
assert_eq!(Session::validators(), vec![3, 2]);
|
||||
assert_eq!(Balances::total_balance(&1), 12);
|
||||
assert_eq!(Balances::total_balance(&2), 20);
|
||||
assert_eq!(Balances::total_balance(&3), 40);
|
||||
assert_eq!(Balances::total_balance(&4), 48);
|
||||
|
||||
System::set_block_number(3);
|
||||
assert_ok!(Staking::stake(Origin::signed(4)));
|
||||
assert_ok!(Staking::unstake(Origin::signed(3), Staking::intentions().iter().position(|&x| x == 3).unwrap() as u32));
|
||||
assert_ok!(Staking::nominate(Origin::signed(3), 1.into()));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::validators(), vec![1, 4]);
|
||||
assert_eq!(Balances::total_balance(&1), 12);
|
||||
assert_eq!(Balances::total_balance(&2), 30);
|
||||
assert_eq!(Balances::total_balance(&3), 50);
|
||||
assert_eq!(Balances::total_balance(&4), 48);
|
||||
|
||||
System::set_block_number(4);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Balances::total_balance(&1), 13);
|
||||
assert_eq!(Balances::total_balance(&2), 30);
|
||||
assert_eq!(Balances::total_balance(&3), 58);
|
||||
assert_eq!(Balances::total_balance(&4), 58);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewards_with_off_the_table_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 1, 1, 0, true, 10), || {
|
||||
System::set_block_number(1);
|
||||
assert_ok!(Staking::stake(Origin::signed(1)));
|
||||
assert_ok!(Staking::nominate(Origin::signed(2), 1.into()));
|
||||
assert_ok!(Staking::stake(Origin::signed(3)));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::validators(), vec![1, 3]); // 1 + 2, 3
|
||||
assert_eq!(Balances::total_balance(&1), 10);
|
||||
assert_eq!(Balances::total_balance(&2), 20);
|
||||
assert_eq!(Balances::total_balance(&3), 30);
|
||||
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Staking::register_preferences(Origin::signed(1), Staking::intentions().into_iter().position(|i| i == 1).unwrap() as u32, ValidatorPrefs { unstake_threshold: 3, validator_payment: 4 }));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Balances::total_balance(&1), 16);
|
||||
assert_eq!(Balances::total_balance(&2), 24);
|
||||
assert_eq!(Balances::total_balance(&3), 40);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nominating_slashes_should_work() {
|
||||
with_externalities(&mut new_test_ext(0, 2, 2, 0, true, 10), || {
|
||||
assert_eq!(Staking::era_length(), 4);
|
||||
assert_eq!(Staking::validator_count(), 2);
|
||||
assert_eq!(Staking::bonding_duration(), 12);
|
||||
assert_eq!(Session::validators(), vec![10, 20]);
|
||||
|
||||
System::set_block_number(2);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
|
||||
Timestamp::set_timestamp(15);
|
||||
System::set_block_number(4);
|
||||
assert_ok!(Staking::stake(Origin::signed(1)));
|
||||
assert_ok!(Staking::stake(Origin::signed(3)));
|
||||
assert_ok!(Staking::nominate(Origin::signed(2), 3.into()));
|
||||
assert_ok!(Staking::nominate(Origin::signed(4), 1.into()));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
|
||||
assert_eq!(Staking::current_era(), 1);
|
||||
assert_eq!(Session::validators(), vec![1, 3]); // 1 + 4, 3 + 2
|
||||
assert_eq!(Balances::total_balance(&1), 10);
|
||||
assert_eq!(Balances::total_balance(&2), 20);
|
||||
assert_eq!(Balances::total_balance(&3), 30);
|
||||
assert_eq!(Balances::total_balance(&4), 40);
|
||||
|
||||
System::set_block_number(5);
|
||||
::system::ExtrinsicIndex::<Test>::put(1);
|
||||
Staking::on_offline_validator(0);
|
||||
Staking::on_offline_validator(1);
|
||||
assert_eq!(Balances::total_balance(&1), 0);
|
||||
assert_eq!(Balances::total_balance(&2), 20);
|
||||
assert_eq!(Balances::total_balance(&3), 10);
|
||||
assert_eq!(Balances::total_balance(&4), 30);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn double_staking_should_fail() {
|
||||
with_externalities(&mut new_test_ext(0, 1, 2, 0, true, 0), || {
|
||||
System::set_block_number(1);
|
||||
assert_ok!(Staking::stake(Origin::signed(1)));
|
||||
assert_noop!(Staking::stake(Origin::signed(1)), "Cannot stake if already staked.");
|
||||
assert_noop!(Staking::nominate(Origin::signed(1), 1.into()), "Cannot nominate if already staked.");
|
||||
assert_ok!(Staking::nominate(Origin::signed(2), 1.into()));
|
||||
assert_noop!(Staking::stake(Origin::signed(2)), "Cannot stake if already nominating.");
|
||||
assert_noop!(Staking::nominate(Origin::signed(2), 1.into()), "Cannot nominate if already nominating.");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn staking_eras_work() {
|
||||
with_externalities(&mut new_test_ext(0, 1, 2, 0, true, 0), || {
|
||||
assert_eq!(Staking::era_length(), 2);
|
||||
assert_eq!(Staking::sessions_per_era(), 2);
|
||||
assert_eq!(Staking::last_era_length_change(), 0);
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
assert_eq!(Session::current_index(), 0);
|
||||
|
||||
// Block 1: No change.
|
||||
System::set_block_number(1);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::current_index(), 1);
|
||||
assert_eq!(Staking::sessions_per_era(), 2);
|
||||
assert_eq!(Staking::last_era_length_change(), 0);
|
||||
assert_eq!(Staking::current_era(), 0);
|
||||
|
||||
// Block 2: Simple era change.
|
||||
System::set_block_number(2);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::current_index(), 2);
|
||||
assert_eq!(Staking::sessions_per_era(), 2);
|
||||
assert_eq!(Staking::last_era_length_change(), 0);
|
||||
assert_eq!(Staking::current_era(), 1);
|
||||
|
||||
// Block 3: Schedule an era length change; no visible changes.
|
||||
System::set_block_number(3);
|
||||
assert_ok!(Staking::set_sessions_per_era(Origin::ROOT, 3));
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::current_index(), 3);
|
||||
assert_eq!(Staking::sessions_per_era(), 2);
|
||||
assert_eq!(Staking::last_era_length_change(), 0);
|
||||
assert_eq!(Staking::current_era(), 1);
|
||||
|
||||
// Block 4: Era change kicks in.
|
||||
System::set_block_number(4);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::current_index(), 4);
|
||||
assert_eq!(Staking::sessions_per_era(), 3);
|
||||
assert_eq!(Staking::last_era_length_change(), 4);
|
||||
assert_eq!(Staking::current_era(), 2);
|
||||
|
||||
// Block 5: No change.
|
||||
System::set_block_number(5);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::current_index(), 5);
|
||||
assert_eq!(Staking::sessions_per_era(), 3);
|
||||
assert_eq!(Staking::last_era_length_change(), 4);
|
||||
assert_eq!(Staking::current_era(), 2);
|
||||
|
||||
// Block 6: No change.
|
||||
System::set_block_number(6);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::current_index(), 6);
|
||||
assert_eq!(Staking::sessions_per_era(), 3);
|
||||
assert_eq!(Staking::last_era_length_change(), 4);
|
||||
assert_eq!(Staking::current_era(), 2);
|
||||
|
||||
// Block 7: Era increment.
|
||||
System::set_block_number(7);
|
||||
Session::check_rotate_session(System::block_number());
|
||||
assert_eq!(Session::current_index(), 7);
|
||||
assert_eq!(Staking::sessions_per_era(), 3);
|
||||
assert_eq!(Staking::last_era_length_change(), 4);
|
||||
assert_eq!(Staking::current_era(), 3);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn staking_balance_transfer_when_bonded_should_not_work() {
|
||||
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
|
||||
Balances::set_free_balance(&1, 111);
|
||||
assert_ok!(Staking::stake(Origin::signed(1)));
|
||||
assert_noop!(Balances::transfer(Origin::signed(1), 2.into(), 69), "cannot transfer illiquid funds");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deducting_balance_when_bonded_should_not_work() {
|
||||
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
|
||||
Balances::set_free_balance(&1, 111);
|
||||
<Bondage<Test>>::insert(1, 2);
|
||||
System::set_block_number(1);
|
||||
assert_eq!(Staking::unlock_block(&1), LockStatus::LockedUntil(2));
|
||||
assert_noop!(Balances::reserve(&1, 69), "cannot transfer illiquid funds");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user