diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index 84b2de336ed58..cc5992a514555 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -445,13 +445,7 @@ mod tests { ] ); - // let mut digest = generic::Digest::::default(); - // digest.push(Log::from(::grandpa::RawLog::AuthoritiesChangeSignal(0, vec![ - // (Keyring::Charlie.to_raw_public().into(), 1), - // (Keyring::Bob.to_raw_public().into(), 1), - // (Keyring::Alice.to_raw_public().into(), 1), - // ]))); - let digest = generic::Digest::::default(); // TODO test this + let digest = generic::Digest::::default(); assert_eq!(Header::decode(&mut &block2.0[..]).unwrap().digest, digest); (block1, block2) @@ -584,14 +578,6 @@ mod tests { phase: Phase::Finalization, event: Event::session(session::RawEvent::NewSession(1)) }, - // EventRecord { // TODO: this might be wrong. - // phase: Phase::Finalization, - // event: Event::grandpa(::grandpa::RawEvent::NewAuthorities(vec![ - // (Keyring::Charlie.to_raw_public().into(), 1), - // (Keyring::Bob.to_raw_public().into(), 1), - // (Keyring::Alice.to_raw_public().into(), 1), - // ])), - // }, EventRecord { phase: Phase::Finalization, event: Event::treasury(treasury::RawEvent::Spending(0)) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 4650bff981d77..e243b58f7895f 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -16,7 +16,205 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . -//! Staking manager: Periodically determines the best set of validators. +//! # Staking Module +//! +//! +//! The staking module is the means by which a set of network maintainers (known as "authorities" in some contexts and "validators" in others) +//! are chosen based upon those who voluntarily place funds under deposit. Under deposit, those funds are rewarded under +//! normal operation but are held at pain of "slash" (expropriation) should the staked maintainer be found not to be +//! discharging their duties properly. +//! You can start using the Staking module by implementing the staking [`Trait`]. +//! +//! ## Overview +//! +//! ### Terminology +//! +//! +//! - Staking: The process of locking up funds for some time, placing them at risk of slashing (loss) in order to become a rewarded maintainer of the network. +//! - Validating: The process of running a node to actively maintain the network, either by producing blocks or guaranteeing finality of the chain. +//! - Nominating: The process of placing staked funds behind one or more validators in order to share in any reward, and punishment, they take. +//! - Stash account: The account holding an owner's funds used for staking. +//! - Controller account: The account which controls an owner's funds for staking. +//! - Era: A (whole) number of sessions, which is the period that the validator set (and each validator's active nominator set) is recalculated and where rewards are paid out. +//! - Slash: The punishment of a staker by reducing their funds ([reference](#references)). +//! +//! ### Goals +//! +//! +//! The staking system in Substrate NPoS is designed to achieve three goals: +//! - It should be possible to stake funds that are controlled by a cold wallet. +//! - It should be possible to withdraw some, or deposit more, funds without interrupting the role of an entity. +//! - It should be possible to switch between roles (nominator, validator, idle) with minimal overhead. +//! +//! ### Scenarios +//! +//! #### Staking +//! +//! Almost any interaction with the staking module requires at least one account to become **bonded**, also known as +//! being a **staker**. For this, all that it is needed is a secondary _**stash account**_ which will hold the staked funds. +//! Henceforth, the former account that initiated the interest is called the **controller** and the latter, holding the +//! funds, is named the **stash**. Also, note that this implies that entering the staking process requires an _account +//! pair_, one to take the role of the controller and one to be the frozen stash account (any value locked in +//! stash cannot be used, hence called _frozen_). This process in the public API is mostly referred to as _bonding_ via +//! the `bond()` function. +//! +//! Any account pair successfully placed at stake can accept three possible roles, namely: `validate`, `nominate` or +//! simply `chill`. Note that during the process of accepting these roles, the _controller_ account is always responsible +//! for declaring interest and the _stash_ account stays untouched, without directly interacting in any operation. +//! +//! #### Validating +//! +//! A **validator** takes the role of either validating blocks or ensuring their finality, maintaining the veracity of +//! the network. A validator should avoid both any sort of malicious misbehavior and going offline. +//! Bonded accounts that state interest in being a validator do NOT get immediately chosen as a validator. Instead, they +//! are declared as a _candidate_ and they _might_ get elected at the _next **era**_ as a validator. The result of the +//! election is determined by nominators and their votes. An account can become a validator via the `validate()` call. +//! +//! #### Nomination +//! +//! A **nominator** does not take any _direct_ role in maintaining the network, instead, it votes on a set of validators +//! to be elected. Once interest in nomination is stated by an account, it takes effect _immediately_, meaning that its +//! votes will be taken into account at the next election round. As mentioned above, a nominator must also place some +//! funds in a stash account, essentially indicating the _weight_ of its vote. In some sense, the nominator bets on the +//! honesty of a set of validators by voting for them, with the goal of having a share of the reward granted to them. +//! Any rewards given to a validator is shared among that validator and all of the nominators that voted for it. The +//! same logic applies to the slash of a validator; if a validator misbehaves all of its nominators also get slashed. +//! This rule incentivizes the nominators to NOT vote for the misbehaving/offline validators as much as possible, simply +//! because the nominators will also lose funds if they vote poorly. An account can become a nominator via the +//! `nominate()` call. +//! +//! #### Rewards and Slash +//! +//! The **reward and slashing** procedure are the core of the staking module, attempting to _embrace valid behavior_ +//! while _punishing any misbehavior or lack of availability_. Slashing can occur at any point in time, once +//! misbehavior is reported. One such misbehavior is a validator being detected as offline more than a certain number of +//! times. Once slashing is determined, a value is deducted from the balance of the validator and all the nominators who +//! voted for this validator. Same rules apply to the rewards in the sense of being shared among a validator and its +//! associated nominators. +//! +//! Finally, any of the roles above can choose to step back temporarily and just chill for a while. This means that if +//! they are a nominator, they will not be considered as voters anymore and if they are validators, they will no longer +//! be a candidate for the next election (again, both effects apply at the beginning of the next era). An account can +//! step back via the `chill()` call. +//! +//! ## Interface +//! +//! ### Types +//! +//! - `Currency`: Used as the measurement means of staking and funds management. +//! +//! ### Dispatchable +//! +//! The Dispatchable functions of the staking module enable the steps needed for entities to accept and change their +//! role, alongside some helper functions to get/set the metadata of the module. +//! +//! Please refer to the [`Call`] enum and its associated variants for a detailed list of dispatchable functions. +//! +//! ### Public +//! The staking module contains many public storage items and (im)mutable functions. Please refer to the [struct list](#structs) +//! below and the [`Module`](https://crates.parity.io/srml_staking/struct.Module.html) struct definition for more details. +//! +//! ## Usage +//! +//! +//! ### Snippet: Bonding and Accepting Roles +//! +//! An arbitrary account pair, given that the associated stash has the required funds, can become stakers via the following call: +//! +//! ```rust,ignore +//! // bond account 3 as stash +//! // account 4 as controller +//! // with stash value 1500 units +//! // while the rewards get transferred to the controller account. +//! Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller); +//! ``` +//! +//! To state desire to become a validator: +//! +//! ```rust,ignore +//! // controller account 4 states desire for validation with the given preferences. +//! Staking::validate(Origin::signed(4), ValidatorPrefs::default()); +//! ``` +//! +//! Note that, as mentioned, the stash account is transparent in such calls and only the controller initiates the function calls. +//! +//! Similarly, to state desire in nominating: +//! +//! ```rust,ignore +//! // controller account 4 nominates for account 10 and 20. +//! Staking::nominate(Origin::signed(4), vec![20, 10]); +//! ``` +//! +//! Finally, account 4 can withdraw from any of the above roles via +//! +//! ```rust,ignore +//! Staking::chill(Origin::signed(4)); +//! ``` +//! +//! ## Implementation Details +//! +//! ### Slot Stake +//! +//! The term `slot_stake` will be used throughout this section. It refers to a value calculated at the end of each era, +//! containing the _minimum value at stake among all validators._ +//! +//! ### Reward Calculation +//! +//! - Rewards are recorded **per-session** and paid **per-era**. The value of the reward for each session is calculated at +//! the end of the session based on the timeliness of the session, then accumulated to be paid later. The value of +//! the new _per-session-reward_ is calculated at the end of each era by multiplying `slot_stake` and a configuration +//! storage item named `SessionReward`. +//! - Once a new era is triggered, rewards are paid to the validators and the associated nominators. +//! - The validator can declare an amount, named `validator_payment`, that does not get shared with the nominators at +//! each reward payout through their `ValidatorPrefs`. This value gets deducted from the total reward that can be paid. +//! The remaining portion is split among the validator and all of the nominators who had a vote for this validator, +//! proportional to their staked value. +//! - All entities who receive a reward have the option to choose their reward destination, through the `Payee` storage item (see `set_payee()`), to be one of the following: +//! - Controller account. +//! - Stash account, not increasing the staked value. +//! - Stash account, also increasing the staked value. +//! +//! ### Slashing details +//! +//! - A validator can be _reported_ to be offline at any point via `on_offline_validator` public function. +//! - Each validator declares how many times it can be _reported_ before it actually gets slashed via the +//! `unstake_threshold` in `ValidatorPrefs`. On top of this, the module also introduces an `OfflineSlashGrace`, +//! which applies to all validators and prevents them from getting immediately slashed. +//! - Similar to the reward value, the slash value is updated at the end of each era by multiplying `slot_stake` and a +//! configuration storage item, `OfflineSlash`. +//! - Once a validator has been reported a sufficient number of times, the actual value that gets deducted from that +//! validator, and every single nominator that voted for it is calculated by multiplying the result of the above point +//! by `2.pow(unstake_threshold)`. +//! - If the previous overflows, then `slot_stake` is used. +//! - If the previous is more than what the validator/nominator has in stake, all of its stake is slashed (`.max(total_stake)`). +//! +//! ### Additional Fund Management Operations +//! +//! Any funds already placed into stash can be the target of the following operations: +//! +//! - The controller account can free a portion (or all) of the funds using the `unbond()` call. Note that the funds +//! are not immediately accessible, instead, a duration denoted by `BondingDuration` (in number of eras) must pass until the funds can actually be removed. +//! - To actually remove the funds, once the bonding duration is over, `withdraw_unbonded()` can be used. +//! - As opposed to the above, additional funds can be added to the stash account via the `bond_extra()` transaction call. +//! +//! ### Election algorithm details. +//! +//! The current election algorithm is implemented based on Phragmén. The reference implementation can be found [here](https://github.com/w3f/consensus/tree/master/NPoS). +//! +//! ## GenesisConfig +//! +//! See the [`GensisConfig`] for a list of attributes that can be provided. +//! +//! ## Related Modules +//! +//! - [**Balances**](https://crates.parity.io/srml_balances/index.html): Used to manage values at stake. +//! - [**Sessions**](https://crates.parity.io/srml_session/index.html): Used to manage sessions. Also, a list of new validators is also stored in the sessions module's `Validators` at the end of each era. +//! - [**System**](https://crates.parity.io/srml_system/index.html): Used to obtain block number and time, among other details. +//! +//! # References +//! +//! 1. This document is written as a more verbose version of the original [Staking.md](../Staking.md) file. Some sections, are taken directly from the aforementioned document. + #![cfg_attr(not(feature = "std"), no_std)] @@ -31,7 +229,7 @@ use srml_support::traits::{ LockIdentifier, LockableCurrency, WithdrawReasons }; use session::OnSessionChange; -use primitives::{Perbill}; +use primitives::Perbill; use primitives::traits::{Zero, One, As, StaticLookup, Saturating, Bounded}; #[cfg(feature = "std")] use primitives::{Serialize, Deserialize}; @@ -41,6 +239,8 @@ mod mock; mod tests; mod phragmen; +use phragmen::{elect, ElectionConfig}; + const RECENT_OFFLINE_COUNT: usize = 32; const DEFAULT_MINIMUM_VALIDATOR_COUNT: u32 = 4; const MAX_NOMINATIONS: usize = 16; @@ -48,7 +248,14 @@ const MAX_UNSTAKE_THRESHOLD: u32 = 10; // Indicates the initial status of the staker #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))] -pub enum StakerStatus { Idle, Validator, Nominator(Vec), } +pub enum StakerStatus { + // Chilling. + Idle, + // Declared state in validating or already participating in it. + Validator, + // Nominating for a group of other stakers. + Nominator(Vec), +} /// A destination account for payment. #[derive(PartialEq, Eq, Copy, Clone, Encode, Decode)] @@ -166,7 +373,7 @@ pub struct Exposure { pub others: Vec>, } -type BalanceOf = <::Currency as ArithmeticType>::Type; +type BalanceOf = <::Currency as ArithmeticType>::Type; pub trait Trait: system::Trait + session::Trait { /// The staking balance. @@ -277,6 +484,7 @@ decl_storage! { build(|storage: &mut primitives::StorageOverlay, _: &mut primitives::ChildrenStorageOverlay, config: &GenesisConfig| { with_storage(storage, || { for &(ref stash, ref controller, balance, ref status) in &config.stakers { + assert!(T::Currency::free_balance(&stash) >= balance); let _ = >::bond( T::Origin::from(Some(stash.clone()).into()), T::Lookup::unlookup(controller.clone()), @@ -310,6 +518,8 @@ decl_module! { /// Take the origin account as a stash and lock up `value` of its balance. `controller` will be the /// account that controls it. + /// + /// The dispatch origin for this call must be _Signed_. fn bond(origin, controller: ::Source, #[compact] value: BalanceOf, payee: RewardDestination) { let stash = ensure_signed(origin)?; @@ -335,7 +545,7 @@ decl_module! { /// /// Use this if there are additional funds in your stash account that you wish to bond. /// - /// NOTE: This call must be made by the controller, not the stash. + /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. fn bond_extra(origin, max_additional: BalanceOf) { let controller = ensure_signed(origin)?; let mut ledger = Self::ledger(&controller).ok_or("not a controller")?; @@ -356,7 +566,7 @@ decl_module! { /// Once the unlock period is done, you can call `withdraw_unbonded` to actually move /// the funds out of management ready for transfer. /// - /// NOTE: This call must be made by the controller, not the stash. + /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. /// /// See also [`Call::withdraw_unbonded`]. fn unbond(origin, #[compact] value: BalanceOf) { @@ -386,7 +596,7 @@ decl_module! { /// This essentially frees up that balance to be used by the stash account to do /// whatever it wants. /// - /// NOTE: This call must be made by the controller, not the stash. + /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. /// /// See also [`Call::unbond`]. fn withdraw_unbonded(origin) { @@ -400,7 +610,7 @@ decl_module! { /// /// Effects will be felt at the beginning of the next era. /// - /// NOTE: This call must be made by the controller, not the stash. + /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. fn validate(origin, prefs: ValidatorPrefs>) { let controller = ensure_signed(origin)?; let _ledger = Self::ledger(&controller).ok_or("not a controller")?; @@ -413,7 +623,7 @@ decl_module! { /// /// Effects will be felt at the beginning of the next era. /// - /// NOTE: This call must be made by the controller, not the stash. + /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. fn nominate(origin, targets: Vec<::Source>) { let controller = ensure_signed(origin)?; let _ledger = Self::ledger(&controller).ok_or("not a controller")?; @@ -431,7 +641,7 @@ decl_module! { /// /// Effects will be felt at the beginning of the next era. /// - /// NOTE: This call must be made by the controller, not the stash. + /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. fn chill(origin) { let controller = ensure_signed(origin)?; let _ledger = Self::ledger(&controller).ok_or("not a controller")?; @@ -443,7 +653,7 @@ decl_module! { /// /// Effects will be felt at the beginning of the next era. /// - /// NOTE: This call must be made by the controller, not the stash. + /// The dispatch origin for this call must be _Signed_ by the controller, not the stash. fn set_payee(origin, payee: RewardDestination) { let controller = ensure_signed(origin)?; let _ledger = Self::ledger(&controller).ok_or("not a controller")?; @@ -483,7 +693,7 @@ decl_module! { } } -/// An event in this module. +// An event in this module. decl_event!( pub enum Event where Balance = BalanceOf, ::AccountId { /// All validators have been rewarded by the given balance. @@ -660,31 +870,28 @@ impl Module { /// Select a new validator set from the assembled stakers and their role preferences. /// - /// @returns the new SlotStake value. + /// Returns the new SlotStake value. fn select_validators() -> BalanceOf { // Map of (would-be) validator account to amount of stake backing it. - + let rounds = || >::get() as usize; let validators = || >::enumerate(); let nominators = || >::enumerate(); - let stash_of = |w| Self::stash_balance(&w); + let stash_of = |w: &T::AccountId| -> BalanceOf { Self::stash_balance(w) }; let min_validator_count = Self::minimum_validator_count() as usize; - let elected_candidates = phragmen::elect::( + let elected_candidates = elect::( rounds, validators, nominators, stash_of, - min_validator_count + min_validator_count, + ElectionConfig::> { + equalise: true, + tolerance: >::sa(10 as u64), + iterations: 10, + } ); - // Figure out the minimum stake behind a slot. - let slot_stake = elected_candidates - .iter() - .min_by_key(|c| c.exposure.total) - .map(|c| c.exposure.total) - .unwrap_or_default(); - >::put(&slot_stake); - // Clear Stakers and reduce their slash_count. for v in >::validators().iter() { >::remove(v); @@ -694,16 +901,21 @@ impl Module { } } - // Populate Stakers. - for candidate in &elected_candidates { - >::insert(candidate.who.clone(), candidate.exposure.clone()); + // Populate Stakers and figure out the minimum stake behind a slot. + let mut slot_stake = elected_candidates[0].exposure.total; + for c in &elected_candidates { + if c.exposure.total < slot_stake { + slot_stake = c.exposure.total; + } + >::insert(c.who.clone(), c.exposure.clone()); } + >::put(&slot_stake); // Set the new validator set. >::set_validators( &elected_candidates.into_iter().map(|i| i.who).collect::>() ); - + slot_stake } diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index 6a76f350efbea..a639ffd5299b2 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -79,12 +79,12 @@ pub struct ExtBuilder { session_length: u64, sessions_per_era: u64, current_era: u64, - monied: bool, reward: u64, validator_pool: bool, nominate: bool, validator_count: u32, minimum_validator_count: u32, + fare: bool, } impl Default for ExtBuilder { @@ -94,12 +94,12 @@ impl Default for ExtBuilder { session_length: 1, sessions_per_era: 1, current_era: 0, - monied: true, reward: 10, validator_pool: false, nominate: true, validator_count: 2, minimum_validator_count: 0, + fare: true } } } @@ -121,16 +121,7 @@ impl ExtBuilder { self.current_era = current_era; self } - pub fn _monied(mut self, monied: bool) -> Self { - self.monied = monied; - self - } - pub fn reward(mut self, reward: u64) -> Self { - self.reward = reward; - self - } pub fn validator_pool(mut self, validator_pool: bool) -> Self { - // NOTE: this should only be set to true with monied = false. self.validator_pool = validator_pool; self } @@ -147,6 +138,10 @@ impl ExtBuilder { self.minimum_validator_count = count; self } + pub fn fare(mut self, is_fare: bool) -> Self { + self.fare = is_fare; + self + } pub fn build(self) -> runtime_io::TestExternalities { let (mut t, mut c) = system::GenesisConfig::::default().build_storage().unwrap(); let balance_factor = if self.existential_deposit > 0 { @@ -165,34 +160,22 @@ impl ExtBuilder { keys: vec![], }.assimilate_storage(&mut t, &mut c); let _ = balances::GenesisConfig::{ - balances: if self.monied { - if self.reward > 0 { - vec![ - (1, 10 * balance_factor), - (2, 20 * balance_factor), - (3, 300 * balance_factor), - (4, 400 * balance_factor), - (10, balance_factor), - (11, balance_factor * 1000), - (20, balance_factor), - (21, balance_factor * 2000), - (100, 2000 * balance_factor), - (101, 2000 * balance_factor), - ] - } else { - vec![ - (1, 10 * balance_factor), (2, 20 * balance_factor), - (3, 300 * balance_factor), (4, 400 * balance_factor) - ] - } - } else { - vec![ - (10, balance_factor), (11, balance_factor * 10), - (20, balance_factor), (21, balance_factor * 20), - (30, balance_factor), (31, balance_factor * 30), - (40, balance_factor), (41, balance_factor * 40) - ] - }, + balances: vec![ + (1, 10 * balance_factor), + (2, 20 * balance_factor), + (3, 300 * balance_factor), + (4, 400 * balance_factor), + (10, balance_factor), + (11, balance_factor * 1000), + (20, balance_factor), + (21, balance_factor * 2000), + (30, balance_factor), + (31, balance_factor * 2000), + (40, balance_factor), + (41, balance_factor * 2000), + (100, 2000 * balance_factor), + (101, 2000 * balance_factor), + ], existential_deposit: self.existential_deposit, transfer_fee: 0, creation_fee: 0, @@ -204,16 +187,16 @@ impl ExtBuilder { stakers: if self.validator_pool { vec![ (11, 10, balance_factor * 1000, StakerStatus::::Validator), - (21, 20, balance_factor * 2000, StakerStatus::::Validator), - (31, 30, balance_factor * 3000, if self.validator_pool { StakerStatus::::Validator } else { StakerStatus::::Idle }), - (41, 40, balance_factor * 4000, if self.validator_pool { StakerStatus::::Validator } else { StakerStatus::::Idle }), + (21, 20, balance_factor * if self.fare { 1000 } else { 2000 }, StakerStatus::::Validator), + (31, 30, balance_factor * 1000, if self.validator_pool { StakerStatus::::Validator } else { StakerStatus::::Idle }), + (41, 40, balance_factor * 1000, if self.validator_pool { StakerStatus::::Validator } else { StakerStatus::::Idle }), // nominator (101, 100, balance_factor * 500, if self.nominate { StakerStatus::::Nominator(vec![10, 20]) } else { StakerStatus::::Nominator(vec![]) }) ] } else { vec![ (11, 10, balance_factor * 1000, StakerStatus::::Validator), - (21, 20, balance_factor * 2000, StakerStatus::::Validator), + (21, 20, balance_factor * if self.fare { 1000 } else { 2000 }, StakerStatus::::Validator), // nominator (101, 100, balance_factor * 500, if self.nominate { StakerStatus::::Nominator(vec![10, 20]) } else { StakerStatus::::Nominator(vec![]) }) ] @@ -222,7 +205,7 @@ impl ExtBuilder { minimum_validator_count: self.minimum_validator_count, bonding_duration: self.sessions_per_era * self.session_length * 3, session_reward: Perbill::from_millionths((1000000 * self.reward / balance_factor) as u32), - offline_slash: if self.monied { Perbill::from_percent(40) } else { Perbill::zero() }, + offline_slash: Perbill::from_percent(40), current_session_reward: self.reward, current_offline_slash: 20, offline_slash_grace: 0, diff --git a/srml/staking/src/phragmen.rs b/srml/staking/src/phragmen.rs index bdaed1fee9760..48d019e48c071 100644 --- a/srml/staking/src/phragmen.rs +++ b/srml/staking/src/phragmen.rs @@ -1,4 +1,4 @@ -// Copyright 2017-2019 Parity Technologies (UK) Ltd. +// Copyright 2019 Parity Technologies (UK) Ltd. // This file is part of Substrate. // Substrate is free software: you can redistribute it and/or modify @@ -18,197 +18,248 @@ use rstd::{prelude::*}; use primitives::Perquintill; -use primitives::traits::{Zero, As}; +use primitives::traits::{Zero, As, Bounded, CheckedMul, CheckedSub}; use parity_codec::{HasCompact, Encode, Decode}; use crate::{Exposure, BalanceOf, Trait, ValidatorPrefs, IndividualExposure}; + +// Configure the behavior of the Phragmen election. +// Might be deprecated. +pub struct ElectionConfig { + // Perform equalise?. + pub equalise: bool, + // Number of equalise iterations. + pub iterations: usize, + // Tolerance of max change per equalise iteration. + pub tolerance: Balance, +} + // Wrapper around validation candidates some metadata. #[derive(Clone, Encode, Decode)] -#[cfg_attr(feature = "std", derive(Debug))] +#[cfg_attr(feature = "std", derive(Debug, Default))] pub struct Candidate { // The validator's account pub who: AccountId, // Exposure struct, holding info about the value that the validator has in stake. pub exposure: Exposure, - // Accumulator of the stake of this candidate based on received votes. - approval_stake: Balance, // Intermediary value used to sort candidates. - // See Phragmén reference implementation. pub score: Perquintill, + // Accumulator of the stake of this candidate based on received votes. + approval_stake: Balance, + // Flag for being elected. + elected: bool, + // This is most often equal to `Exposure.total` but not always. Needed for [`equalise`] + backing_stake: Balance } // Wrapper around the nomination info of a single nominator for a group of validators. #[derive(Clone, Encode, Decode)] #[cfg_attr(feature = "std", derive(Debug))] -pub struct Nominations { +pub struct Nominator { // The nominator's account. who: AccountId, // List of validators proposed by this nominator. - nominees: Vec>, + edges: Vec>, // the stake amount proposed by the nominator as a part of the vote. - // Same as `nom.budget` in Phragmén reference. - stake: Balance, + budget: Balance, // Incremented each time a nominee that this nominator voted for has been elected. load: Perquintill, } // Wrapper around a nominator vote and the load of that vote. -// Referred to as 'edge' in the Phragmén reference implementation. #[derive(Clone, Encode, Decode)] -#[cfg_attr(feature = "std", derive(Debug))] -pub struct Vote { +#[cfg_attr(feature = "std", derive(Debug, Default))] +pub struct Edge { // Account being voted for who: AccountId, // Load of this vote. load: Perquintill, // Final backing stake of this vote. - backing_stake: Balance + backing_stake: Balance, + // Index of the candidate stored in the 'candidates' vector + candidate_idx: usize, + // Index of the candidate stored in the 'elected_candidates' vector. Used only with equalise. + elected_idx: usize, + // Indicates if this edge is a vote for an elected candidate. Used only with equalise. + elected: bool, } /// Perform election based on Phragmén algorithm. /// /// Reference implementation: https://github.com/w3f/consensus /// -/// @returns a vector of elected candidates +/// Returns a vector of elected candidates pub fn elect( get_rounds: FR, get_validators: FV, get_nominators: FN, stash_of: FS, minimum_validator_count: usize, - ) -> Vec>> where - FR: Fn() -> usize, - FV: Fn() -> Box>) - >>, - FN: Fn() -> Box) - >>, - FS: Fn(T::AccountId) -> BalanceOf, + config: ElectionConfig>, +) -> Vec>> where + FR: Fn() -> usize, + FV: Fn() -> Box>) + >>, + FN: Fn() -> Box) + >>, + for <'r> FS: Fn(&'r T::AccountId) -> BalanceOf, { let rounds = get_rounds(); - let mut elected_candidates = vec![]; - + let mut elected_candidates; + // 1- Pre-process candidates and place them in a container let mut candidates = get_validators().map(|(who, _)| { - let stash_balance = stash_of(who.clone()); + let stash_balance = stash_of(&who); Candidate { who, - approval_stake: BalanceOf::::zero(), - score: Perquintill::zero(), exposure: Exposure { total: stash_balance, own: stash_balance, others: vec![] }, + ..Default::default() } }).collect::>>>(); // Just to be used when we are below minimum validator count let original_candidates = candidates.clone(); - + + // 1.1- Add phantom votes. + let mut nominators: Vec>> = Vec::with_capacity(candidates.len()); + candidates.iter_mut().enumerate().for_each(|(idx, c)| { + c.approval_stake += c.exposure.total; + nominators.push(Nominator { + who: c.who.clone(), + edges: vec![ Edge { who: c.who.clone(), candidate_idx: idx, ..Default::default() }], + budget: c.exposure.total, + load: Perquintill::zero(), + }) + }); + // 2- Collect the nominators with the associated votes. // Also collect approval stake along the way. - let mut nominations = get_nominators().map(|(who, nominees)| { - let nominator_stake = stash_of(who.clone()); + nominators.extend(get_nominators().map(|(who, nominees)| { + let nominator_stake = stash_of(&who); + let mut edges: Vec>> = Vec::with_capacity(nominees.len()); for n in &nominees { - candidates.iter_mut().filter(|i| i.who == *n).for_each(|c| { - c.approval_stake += nominator_stake; - }); + if let Some(idx) = candidates.iter_mut().position(|i| i.who == *n) { + candidates[idx].approval_stake += nominator_stake; + edges.push(Edge { who: n.clone(), candidate_idx: idx, ..Default::default() }); + } } - Nominations { + Nominator { who, - nominees: nominees.into_iter() - .map(|n| Vote {who: n, load: Perquintill::zero(), backing_stake: BalanceOf::::zero()}) - .collect::>>>(), - stake: nominator_stake, - load : Perquintill::zero(), + edges: edges, + budget: nominator_stake, + load: Perquintill::zero(), } - }).collect::>>>(); - + })); + // 3- optimization: // Candidates who have 0 stake => have no votes or all null-votes. Kick them out not. let mut candidates = candidates.into_iter().filter(|c| c.approval_stake > BalanceOf::::zero()) .collect::>>>(); // 4- If we have more candidates then needed, run Phragmén. - if candidates.len() > rounds { + if candidates.len() >= rounds { + elected_candidates = Vec::with_capacity(rounds); // Main election loop for _round in 0..rounds { // Loop 1: initialize score - for nominaotion in &nominations { - for vote in &nominaotion.nominees { - let candidate = &vote.who; - if let Some(c) = candidates.iter_mut().find(|i| i.who == *candidate) { - let approval_stake = c.approval_stake; - c.score = Perquintill::from_xth(approval_stake.as_()); - } + for c in &mut candidates { + if !c.elected { + c.score = Perquintill::from_xth(c.approval_stake.as_()); } } // Loop 2: increment score. - for nominaotion in &nominations { - for vote in &nominaotion.nominees { - let candidate = &vote.who; - if let Some(c) = candidates.iter_mut().find(|i| i.who == *candidate) { - let approval_stake = c.approval_stake; - let temp = - nominaotion.stake.as_() - * *nominaotion.load - / approval_stake.as_(); + for n in &nominators { + for e in &n.edges { + let c = &mut candidates[e.candidate_idx]; + if !c.elected { + let temp = n.budget.as_() * *n.load / c.approval_stake.as_(); c.score = Perquintill::from_quintillionths(*c.score + temp); } } } // Find the best - let (winner_index, _) = candidates.iter().enumerate().min_by_key(|&(_i, c)| *c.score) + let winner = candidates + .iter_mut() + .filter(|c| !c.elected) + .min_by_key(|c| *c.score) .expect("candidates length is checked to be >0; qed"); - // loop 3: update nominator and vote load - let winner = candidates.remove(winner_index); - for n in &mut nominations { - for v in &mut n.nominees { - if v.who == winner.who { - v.load = - Perquintill::from_quintillionths( - *winner.score - - *n.load - ); + // loop 3: update nominator and edge load + winner.elected = true; + for n in &mut nominators { + for e in &mut n.edges { + if e.who == winner.who { + e.load = Perquintill::from_quintillionths(*winner.score - *n.load); n.load = winner.score; } } } - elected_candidates.push(winner); - + elected_candidates.push(winner.clone()); } // end of all rounds // 4.1- Update backing stake of candidates and nominators - for n in &mut nominations { - for v in &mut n.nominees { + for n in &mut nominators { + for e in &mut n.edges { // if the target of this vote is among the winners, otherwise let go. - if let Some(c) = elected_candidates.iter_mut().find(|c| c.who == v.who) { - v.backing_stake = as As>::sa( - n.stake.as_() - * *v.load - / *n.load - ); - c.exposure.total += v.backing_stake; - // Update IndividualExposure of those who nominated and their vote won - c.exposure.others.push( - IndividualExposure {who: n.who.clone(), value: v.backing_stake } - ); + if let Some(c) = elected_candidates.iter_mut().find(|c| c.who == e.who) { + e.elected = true; + e.backing_stake = >::sa(n.budget.as_() * (*e.load / *n.load)); + c.backing_stake += e.backing_stake; + if c.who != n.who { + // Only update the exposure if this vote is from some other account. + c.exposure.total += e.backing_stake; + c.exposure.others.push( + IndividualExposure { who: n.who.clone(), value: e.backing_stake } + ); + } } } } + + // Optionally perform equalise post-processing. + if config.equalise { + let tolerance = config.tolerance; + let equalise_iterations = config.iterations; + + // Fix indexes + nominators.iter_mut().for_each(|n| { + n.edges.iter_mut().for_each(|e| { + if let Some(idx) = elected_candidates.iter().position(|c| c.who == e.who) { + e.elected_idx = idx; + } + }); + }); + + for _i in 0..equalise_iterations { + let mut max_diff = >::zero(); + nominators.iter_mut().for_each(|mut n| { + let diff = equalise::(&mut n, &mut elected_candidates, tolerance); + if diff > max_diff { + max_diff = diff; + } + }); + if max_diff < tolerance { + break; + } + } + } + } else { if candidates.len() > minimum_validator_count { // if we don't have enough candidates, just choose all that have some vote. elected_candidates = candidates; - // `Exposure.others` still needs an update - for n in &mut nominations { - for v in &mut n.nominees { - if let Some(c) = elected_candidates.iter_mut().find(|c| c.who == v.who) { - c.exposure.total += n.stake; + for n in &mut nominators { + let nominator = n.who.clone(); + for e in &mut n.edges { + if let Some(c) = elected_candidates.iter_mut().find(|c| c.who == e.who && c.who != nominator) { + c.exposure.total += n.budget; c.exposure.others.push( - IndividualExposure {who: n.who.clone(), value: n.stake } + IndividualExposure { who: n.who.clone(), value: n.budget } ); } } @@ -218,6 +269,91 @@ pub fn elect( elected_candidates = original_candidates; } } - elected_candidates +} + +pub fn equalise( + nominator: &mut Nominator>, + elected_candidates: &mut Vec>>, + tolerance: BalanceOf +) -> BalanceOf { + + let mut elected_edges = nominator.edges + .iter_mut() + .filter(|e| e.elected) + .collect::>>>(); + if elected_edges.len() == 0 { return >::zero(); } + let stake_used = elected_edges + .iter() + .fold(>::zero(), |s, e| s + e.backing_stake); + let backed_stakes = elected_edges + .iter() + .map(|e| elected_candidates[e.elected_idx].backing_stake) + .collect::>>(); + let backing_backed_stake = elected_edges + .iter() + .filter(|e| e.backing_stake > >::zero()) + .map(|e| elected_candidates[e.elected_idx].backing_stake) + .collect::>>(); + + let mut difference; + if backing_backed_stake.len() > 0 { + let max_stake = backing_backed_stake + .iter() + .max() + .expect("vector with positive length will have a max; qed") + .to_owned(); + let min_stake = backed_stakes + .iter() + .min() + .expect("vector with positive length will have a max; qed") + .to_owned(); + difference = max_stake - min_stake; + difference += nominator.budget - stake_used; + if difference < tolerance { + return difference; + } + } else { + difference = nominator.budget; + } + + // Undo updates to exposure + elected_edges.iter_mut().for_each(|e| { + assert_eq!(elected_candidates[e.elected_idx].who, e.who); + elected_candidates[e.elected_idx].backing_stake -= e.backing_stake; + elected_candidates[e.elected_idx].exposure.total -= e.backing_stake; + e.backing_stake = >::zero(); + }); + + elected_edges.sort_unstable_by_key(|e| elected_candidates[e.elected_idx].backing_stake); + + let mut cumulative_stake = >::zero(); + let mut last_index = elected_edges.len() - 1; + let budget = nominator.budget; + elected_edges.iter_mut().enumerate().for_each(|(idx, e)| { + let stake = elected_candidates[e.elected_idx].backing_stake; + + let stake_mul = stake.checked_mul(&>::sa(idx as u64)).unwrap_or(>::max_value()); + let stake_sub = stake_mul.checked_sub(&cumulative_stake).unwrap_or_default(); + if stake_sub > budget { + last_index = idx.clone().checked_sub(1).unwrap_or(0); + return + } + cumulative_stake += stake; + }); + + let last_stake = elected_candidates[elected_edges[last_index].elected_idx].backing_stake; + let split_ways = last_index + 1; + let excess = nominator.budget + cumulative_stake - last_stake * >::sa(split_ways as u64); + let nominator_address = nominator.who.clone(); + elected_edges.iter_mut().take(split_ways).for_each(|e| { + let c = &mut elected_candidates[e.elected_idx]; + e.backing_stake = excess / >::sa(split_ways as u64) + last_stake - c.backing_stake; + c.exposure.total += e.backing_stake; + c.backing_stake += e.backing_stake; + if let Some(i_expo) = c.exposure.others.iter_mut().find(|i| i.who == nominator_address) { + i_expo.value = e.backing_stake; + } + }); + difference } \ No newline at end of file diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 7921d7f313027..5d318df61b4f4 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -39,7 +39,7 @@ fn basic_setup_works() { // Account 10 controls the stash from account 11, which is 100 * balance_factor units assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![] })); // Account 20 controls the stash from account 21, which is 200 * balance_factor units - assert_eq!(Staking::ledger(&20), Some(StakingLedger { stash: 21, total: 2000, active: 2000, unlocking: vec![] })); + assert_eq!(Staking::ledger(&20), Some(StakingLedger { stash: 21, total: 1000, active: 1000, unlocking: vec![] })); // Account 1 does not control any stash assert_eq!(Staking::ledger(&1), None); @@ -53,9 +53,10 @@ fn basic_setup_works() { assert_eq!(Staking::ledger(100), Some(StakingLedger { stash: 101, total: 500, active: 500, unlocking: vec![] })); assert_eq!(Staking::nominators(100), vec![10, 20]); - // Account 10 is exposed by 100 * balance_factor from their own stash in account 11 - assert_eq!(Staking::stakers(10), Exposure { total: 1500, own: 1000, others: vec![ IndividualExposure { who: 100, value: 500 }] }); - assert_eq!(Staking::stakers(20), Exposure { total: 2500, own: 2000, others: vec![ IndividualExposure { who: 100, value: 500 }] }); + // Account 10 is exposed by 1000 * balance_factor from their own stash in account 11 + the default nominator vote + assert_eq!(Staking::stakers(10), Exposure { total: 1250, own: 1000, others: vec![ IndividualExposure { who: 100, value: 250 }] }); + // Account 20 is exposed by 1000 * balance_factor from their own stash in account 21 + the default nominator vote + assert_eq!(Staking::stakers(20), Exposure { total: 1250, own: 1000, others: vec![ IndividualExposure { who: 100, value: 250 }] }); // The number of validators required. assert_eq!(Staking::validator_count(), 2); @@ -68,7 +69,7 @@ fn basic_setup_works() { assert_eq!(Staking::current_session_reward(), 10); // initial slot_stake - assert_eq!(Staking::slot_stake(), 1500); + assert_eq!(Staking::slot_stake(), 1250); // initial slash_count of validators assert_eq!(Staking::slash_count(&10), 0); @@ -440,42 +441,35 @@ fn staking_should_work() { with_externalities(&mut ExtBuilder::default() .sessions_per_era(3) .nominate(false) + .fare(false) // to give 20 more staked value .build(), || { - assert_eq!(Staking::era_length(), 3); // remember + compare this along with the test. assert_eq!(Session::validators(), vec![20, 10]); + assert_ok!(Staking::set_bonding_duration(2)); assert_eq!(Staking::bonding_duration(), 2); // put some money in account that we'll use. - for i in 1..5 { Balances::set_free_balance(&i, 1000); } - - // bond one account pair and state interest in nomination. - // this is needed to keep 10 and 20 in the validator list with phragmen - assert_ok!(Staking::bond(Origin::signed(1), 2, 500, RewardDestination::default())); - assert_ok!(Staking::nominate(Origin::signed(2), vec![20, 4])); + for i in 1..5 { Balances::set_free_balance(&i, 2000); } // --- Block 1: System::set_block_number(1); + Session::check_rotate_session(System::block_number()); + assert_eq!(Staking::current_era(), 0); // add a new candidate for being a validator. account 3 controlled by 4. - assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); // balance of 3 = 3000, stashed = 1500 + assert_ok!(Staking::bond(Origin::signed(3), 4, 1500, RewardDestination::Controller)); + assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default())); - Session::check_rotate_session(System::block_number()); - assert_eq!(Staking::current_era(), 0); - // No effects will be seen so far.s + // No effects will be seen so far. assert_eq!(Session::validators(), vec![20, 10]); - // --- Block 2: System::set_block_number(2); - // Explicitly state the desire to validate - // note that the controller account will state interest as representative of the stash-controller pair. - assert_ok!(Staking::validate(Origin::signed(4), ValidatorPrefs::default())); - Session::check_rotate_session(System::block_number()); assert_eq!(Staking::current_era(), 0); + // No effects will be seen so far. Era has not been yet triggered. assert_eq!(Session::validators(), vec![20, 10]); @@ -486,32 +480,26 @@ fn staking_should_work() { // 2 only voted for 4 and 20 assert_eq!(Session::validators().len(), 2); - assert_eq!(Session::validators(), vec![4, 20]); + assert_eq!(Session::validators(), vec![20, 4]); assert_eq!(Staking::current_era(), 1); // --- Block 4: Unstake 4 as a validator, freeing up the balance stashed in 3 System::set_block_number(4); + Session::check_rotate_session(System::block_number()); - // unlock the entire stashed value. - // Note that this will ne be enough to remove 4 as a validator candidate! - Staking::unbond(Origin::signed(4), Staking::ledger(&4).unwrap().active).unwrap(); - // explicit chill indicated that 4 no longer wants to be a validator. + // 4 will chill Staking::chill(Origin::signed(4)).unwrap(); - // nominator votes for 10 - assert_ok!(Staking::nominate(Origin::signed(2), vec![20, 10])); - - Session::check_rotate_session(System::block_number()); // nothing should be changed so far. - assert_eq!(Session::validators(), vec![4, 20]); + assert_eq!(Session::validators(), vec![20, 4]); assert_eq!(Staking::current_era(), 1); // --- Block 5: nothing. 4 is still there. System::set_block_number(5); Session::check_rotate_session(System::block_number()); - assert_eq!(Session::validators(), vec![4, 20]); + assert_eq!(Session::validators(), vec![20, 4]); assert_eq!(Staking::current_era(), 1); @@ -521,6 +509,12 @@ fn staking_should_work() { assert_eq!(Staking::current_era(), 2); assert_eq!(Session::validators().contains(&4), false); assert_eq!(Session::validators(), vec![20, 10]); + + // Note: the stashed value of 4 is still lock + assert_eq!(Staking::ledger(&4), Some(StakingLedger { stash: 3, total: 1500, active: 1500, unlocking: vec![] })); + // e.g. it cannot spend more than 500 that it has free from the total 2000 + assert_noop!(Balances::reserve(&3, 501), "account liquidity restrictions prevent withdrawal"); + assert_ok!(Balances::reserve(&3, 409)); }); } @@ -532,21 +526,14 @@ fn less_than_needed_candidates_works() { .minimum_validator_count(1) .validator_count(3) .nominate(false) - .validator_pool(true) .build(), || { assert_eq!(Staking::era_length(), 1); assert_eq!(Staking::validator_count(), 3); - assert_eq!(Staking::minimum_validator_count(), 1); - assert_eq!(Staking::validator_count(), 3); // initial validators - assert_eq!(Session::validators(), vec![40, 30, 20, 10]); - - // only one nominator will exist and it will - assert_ok!(Staking::bond(Origin::signed(1), 2, 500, RewardDestination::default())); - assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 20])); + assert_eq!(Session::validators(), vec![20, 10]); // 10 and 20 are now valid candidates. // trigger era @@ -557,9 +544,9 @@ fn less_than_needed_candidates_works() { // both validators will be chosen again. NO election algorithm is even executed. assert_eq!(Session::validators(), vec![20, 10]); - // But the exposure is updated in a simple way. Each nominators vote is applied - assert_eq!(Staking::stakers(10).others.iter().map(|e| e.who).collect::>>(), vec![2]); - assert_eq!(Staking::stakers(20).others.iter().map(|e| e.who).collect::>>(), vec![2]); + // But the exposure is updated in a simple way. No external votes exists. This is purely self-vote. + assert_eq!(Staking::stakers(10).others.iter().map(|e| e.who).collect::>>(), vec![]); + assert_eq!(Staking::stakers(20).others.iter().map(|e| e.who).collect::>>(), vec![]); }); } @@ -568,17 +555,14 @@ fn no_candidate_emergency_condition() { // Test the situation where the number of validators are less than `ValidatorCount` and less than // The expected behavior is to choose all candidates from the previous era. with_externalities(&mut ExtBuilder::default() - .minimum_validator_count(1) - .validator_count(3) - .nominate(false) + .minimum_validator_count(10) + .validator_count(15) .validator_pool(true) + .nominate(false) .build(), || { assert_eq!(Staking::era_length(), 1); - assert_eq!(Staking::validator_count(), 3); - - assert_eq!(Staking::minimum_validator_count(), 1); - assert_eq!(Staking::validator_count(), 3); + assert_eq!(Staking::validator_count(), 15); // initial validators assert_eq!(Session::validators(), vec![40, 30, 20, 10]); @@ -600,27 +584,54 @@ fn nominating_and_rewards_should_work() { // // PHRAGMEN OUTPUT: running this test with the reference impl gives: // - // Votes [('2', 500, ['10', '20', '30']), ('4', 500, ['10', '20', '40'])] + // Votes [('10', 1000, ['10']), ('20', 1000, ['20']), ('30', 1000, ['30']), ('40', 1000, ['40']), ('2', 1000, ['10', '20', '30']), ('4', 1000, ['10', '20', '40'])] // Sequential Phragmén gives - // 10 is elected with stake 500.0 and score 0.001 - // 20 is elected with stake 500.0 and score 0.002 - // - // 2 has load 0.002 and supported - // 10 with stake 250.0 20 with stake 250.0 30 with stake 0.0 - // 4 has load 0.002 and supported - // 10 with stake 250.0 20 with stake 250.0 40 with stake 0.0 + // 10 is elected with stake 2200.0 and score 0.0003333333333333333 + // 20 is elected with stake 1800.0 and score 0.0005555555555555556 + + // 10 has load 0.0003333333333333333 and supported + // 10 with stake 1000.0 + // 20 has load 0.0005555555555555556 and supported + // 20 with stake 1000.0 + // 30 has load 0 and supported + // 30 with stake 0 + // 40 has load 0 and supported + // 40 with stake 0 + // 2 has load 0.0005555555555555556 and supported + // 10 with stake 600.0 20 with stake 400.0 30 with stake 0.0 + // 4 has load 0.0005555555555555556 and supported + // 10 with stake 600.0 20 with stake 400.0 40 with stake 0.0 + + // Sequential Phragmén with post processing gives + // 10 is elected with stake 2000.0 and score 0.0003333333333333333 + // 20 is elected with stake 2000.0 and score 0.0005555555555555556 + + // 10 has load 0.0003333333333333333 and supported + // 10 with stake 1000.0 + // 20 has load 0.0005555555555555556 and supported + // 20 with stake 1000.0 + // 30 has load 0 and supported + // 30 with stake 0 + // 40 has load 0 and supported + // 40 with stake 0 + // 2 has load 0.0005555555555555556 and supported + // 10 with stake 400.0 20 with stake 600.0 30 with stake 0 + // 4 has load 0.0005555555555555556 and supported + // 10 with stake 600.0 20 with stake 400.0 40 with stake 0.0 with_externalities(&mut ExtBuilder::default() .nominate(false) .validator_pool(true) .build(), || { - // initial validators - assert_eq!(Session::validators(), vec![40, 30, 20, 10]); + // initial validators -- everyone is actually even. + assert_eq!(Session::validators(), vec![40, 30]); // Set payee to controller assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); assert_ok!(Staking::set_payee(Origin::signed(20), RewardDestination::Controller)); + assert_ok!(Staking::set_payee(Origin::signed(30), RewardDestination::Controller)); + assert_ok!(Staking::set_payee(Origin::signed(40), RewardDestination::Controller)); // default reward for the first session. let session_reward = 10; @@ -637,53 +648,59 @@ fn nominating_and_rewards_should_work() { // bond two account pairs and state interest in nomination. // 2 will nominate for 10, 20, 30 - assert_ok!(Staking::bond(Origin::signed(1), 2, 500, RewardDestination::Controller)); + assert_ok!(Staking::bond(Origin::signed(1), 2, 1000, RewardDestination::Controller)); assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 20, 30])); // 4 will nominate for 10, 20, 40 - assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::Stash)); + assert_ok!(Staking::bond(Origin::signed(3), 4, 1000, RewardDestination::Controller)); assert_ok!(Staking::nominate(Origin::signed(4), vec![10, 20, 40])); - + System::set_block_number(1); Session::check_rotate_session(System::block_number()); assert_eq!(Staking::current_era(), 1); + // 10 and 20 have more votes, they will be chosen by phragmen. assert_eq!(Session::validators(), vec![20, 10]); - // validators must have already received some rewards. - assert_eq!(Balances::total_balance(&10), initial_balance + session_reward); - assert_eq!(Balances::total_balance(&20), initial_balance + session_reward); + + // OLD validators must have already received some rewards. + assert_eq!(Balances::total_balance(&40), 1 + session_reward); + assert_eq!(Balances::total_balance(&30), 1 + session_reward); // ------ check the staked value of all parties. - // total expo of 10, with 500 coming from nominators (externals), according to phragmen. + + // total expo of 10, with 1200 coming from nominators (externals), according to phragmen. assert_eq!(Staking::stakers(10).own, 1000); - assert_eq!(Staking::stakers(10).total, 1000 + 500); - // 2 and 4 supported 10, each with stake 250, according to phragmen. - assert_eq!(Staking::stakers(10).others.iter().map(|e| e.value).collect::>>(), vec![250, 250]); + assert_eq!(Staking::stakers(10).total, 1000 + 1000); + // 2 and 4 supported 10, each with stake 600, according to phragmen. + assert_eq!(Staking::stakers(10).others.iter().map(|e| e.value).collect::>>(), vec![500, 500]); assert_eq!(Staking::stakers(10).others.iter().map(|e| e.who).collect::>>(), vec![4, 2]); // total expo of 20, with 500 coming from nominators (externals), according to phragmen. - assert_eq!(Staking::stakers(20).own, 2000); - assert_eq!(Staking::stakers(20).total, 2000 + 500); + assert_eq!(Staking::stakers(20).own, 1000); + assert_eq!(Staking::stakers(20).total, 1000 + 1000); // 2 and 4 supported 20, each with stake 250, according to phragmen. - assert_eq!(Staking::stakers(20).others.iter().map(|e| e.value).collect::>>(), vec![250, 250]); + assert_eq!(Staking::stakers(20).others.iter().map(|e| e.value).collect::>>(), vec![500, 500]); assert_eq!(Staking::stakers(20).others.iter().map(|e| e.who).collect::>>(), vec![4, 2]); - + + // They are not chosen anymore + assert_eq!(Staking::stakers(30).total, 0); + assert_eq!(Staking::stakers(40).total, 0); + System::set_block_number(2); + Session::check_rotate_session(System::block_number()); // next session reward. let new_session_reward = Staking::session_reward() * Staking::slot_stake(); // nothing else will happen, era ends and rewards are paid again, // it is expected that nominators will also be paid. See below - Session::check_rotate_session(System::block_number()); - // Nominator 2: has [250/1500 ~ 1/6 from 10] + [250/2500 ~ 1/10 from 20]'s reward. ==> 1/6 + 1/10 - assert_eq!(Balances::total_balance(&2), initial_balance + (new_session_reward/6 + new_session_reward/10)); - // The Associated validator will get the other 4/6 --> 1500(total) minus 1/6(250) by each nominator -> 6/6 - 1/6 - 1/6 - assert_eq!(Balances::total_balance(&10), initial_balance + session_reward + 4*new_session_reward/6) ; + // Nominator 2: has [400/2000 ~ 1/5 from 10] + [600/2000 ~ 3/10 from 20]'s reward. + assert_eq!(Balances::total_balance(&2), initial_balance + (new_session_reward/5 + 3*new_session_reward/10)); + // Nominator 4: has [600/2000 ~ 3/10 from 10] + [400/2000 ~ 1/5 from 20]'s reward. + assert_eq!(Balances::total_balance(&4), initial_balance + (new_session_reward/5 + 3*new_session_reward/10)); - // Nominator 4: has [250/1500 ~ 1/6 from 10] + [250/2500 ~ 1/10 from 20]'s reward. ==> 1/6 + 1/10 - // This nominator chose stash as the reward destination. This means that the reward will go to 3, which is bonded as the stash of 4. - assert_eq!(Balances::total_balance(&3), initial_balance + (new_session_reward/6 + new_session_reward/10)); - // The Associated validator will get the other 8/10 --> 2500(total) minus 1/10(250) by each nominator -> 10/10 - 1/10 - 1/10 - assert_eq!(Balances::total_balance(&20), initial_balance + session_reward + 8*new_session_reward/10); + // 10 got 1000/2000 external stake => Validator's share = 1/2 + assert_eq!(Balances::total_balance(&10), initial_balance + new_session_reward/2); + // 20 got 1000/2000 external stake => Validator's share = 1/2 + assert_eq!(Balances::total_balance(&20), initial_balance + new_session_reward/2); }); } @@ -761,7 +778,6 @@ fn double_staking_should_fail() { fn session_and_eras_work() { with_externalities(&mut ExtBuilder::default() .sessions_per_era(2) - .reward(10) .build(), || { assert_eq!(Staking::era_length(), 2); @@ -832,13 +848,13 @@ fn session_and_eras_work() { #[test] fn cannot_transfer_staked_balance() { // Tests that a stash account cannot transfer funds - with_externalities(&mut ExtBuilder::default().build(), || { + with_externalities(&mut ExtBuilder::default().nominate(false).build(), || { // Confirm account 11 is stashed assert_eq!(Staking::bonded(&11), Some(10)); // Confirm account 11 has some free balance assert_eq!(Balances::free_balance(&11), 1000); // Confirm account 11 (via controller 10) is totally staked - assert_eq!(Staking::stakers(&10).total, 1000 + 500); + assert_eq!(Staking::stakers(&10).total, 1000); // Confirm account 11 cannot transfer as a result assert_noop!(Balances::transfer(Origin::signed(11), 20, 1), "account liquidity restrictions prevent withdrawal"); @@ -849,6 +865,30 @@ fn cannot_transfer_staked_balance() { }); } +#[test] +fn cannot_transfer_staked_balance_2() { + // Tests that a stash account cannot transfer funds + // Same test as above but with 20 + // 21 has 2000 free balance but 1000 at stake + with_externalities(&mut ExtBuilder::default() + .nominate(false) + .fare(true) + .build(), + || { + // Confirm account 21 is stashed + assert_eq!(Staking::bonded(&21), Some(20)); + // Confirm account 21 has some free balance + assert_eq!(Balances::free_balance(&21), 2000); + // Confirm account 21 (via controller 20) is totally staked + assert_eq!(Staking::stakers(&20).total, 1000); + // Confirm account 21 cannot transfer more than 1000 + assert_noop!(Balances::transfer(Origin::signed(21), 20, 1500), "account liquidity restrictions prevent withdrawal"); + + // Confirm that account 21 can transfer less than 1000 + assert_ok!(Balances::transfer(Origin::signed(21), 20, 500)); + }); +} + #[test] fn cannot_reserve_staked_balance() { // Checks that a bonded account cannot reserve balance from free balance @@ -858,7 +898,7 @@ fn cannot_reserve_staked_balance() { // Confirm account 11 has some free balance assert_eq!(Balances::free_balance(&11), 1000); // Confirm account 11 (via controller 10) is totally staked - assert_eq!(Staking::stakers(&10).total, 1000 + 500); + assert_eq!(Staking::stakers(&10).total, 1000 + 250); // Confirm account 11 cannot transfer as a result assert_noop!(Balances::reserve(&11, 1), "account liquidity restrictions prevent withdrawal"); @@ -872,7 +912,7 @@ fn cannot_reserve_staked_balance() { #[test] fn reward_destination_works() { // Rewards go to the correct destination as determined in Payee - with_externalities(&mut ExtBuilder::default().build(), || { + with_externalities(&mut ExtBuilder::default().nominate(false).build(), || { // Check that account 10 is a validator assert!(>::exists(10)); // Check the balance of the validator account @@ -896,13 +936,11 @@ fn reward_destination_works() { // Check current session reward is 10 assert_eq!(current_session_reward, 10); // Check that reward went to the stash account of validator - // 1/3 of the reward is for the nominator. - let validator_reward = (10. * (2./3.)) as u64; // = 6 - assert_eq!(Balances::free_balance(&11), 1000 + validator_reward); + assert_eq!(Balances::free_balance(&11), 1000 + current_session_reward); // Check that amount at stake increased accordingly - assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000 + 6, active: 1000 + 6, unlocking: vec![] })); + assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000 + 10, active: 1000 + 10, unlocking: vec![] })); // Update current session reward - current_session_reward = Staking::current_session_reward(); + current_session_reward = Staking::current_session_reward(); // 1010 (1* slot_stake) //Change RewardDestination to Stash >::insert(&10, RewardDestination::Stash); @@ -915,18 +953,19 @@ fn reward_destination_works() { // Check that RewardDestination is Stash assert_eq!(Staking::payee(&10), RewardDestination::Stash); // Check that reward went to the stash account - let new_validator_reward = ((1000 + 6) as f64 / ( (1000 + 6) + (500 + 4) ) as f64) * current_session_reward as f64; - assert_eq!(Balances::free_balance(&11), 1000 + validator_reward + new_validator_reward as u64); - // Check that amount at stake is not increased - assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1006, active: 1006, unlocking: vec![] })); + assert_eq!(Balances::free_balance(&11), 1000 + 10 + current_session_reward); + // Record this value + let recorded_stash_balance = 1000 + 10 + current_session_reward; + + // Check that amount at stake is NOT increased + assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000 + 10, active: 1000 + 10, unlocking: vec![] })); - //Change RewardDestination to Controller + // Change RewardDestination to Controller >::insert(&10, RewardDestination::Controller); // Check controller balance assert_eq!(Balances::free_balance(&10), 1); - // Move forward the system for payment System::set_block_number(3); Timestamp::set_timestamp(15); @@ -935,10 +974,11 @@ fn reward_destination_works() { // Check that RewardDestination is Controller assert_eq!(Staking::payee(&10), RewardDestination::Controller); // Check that reward went to the controller account - let reward_of = |w| Staking::stakers(w).own * Staking::current_session_reward() / Staking::stakers(w).total; - assert_eq!(Balances::free_balance(&10), 1 + reward_of(&10)); - // Check that amount at stake is not increased - assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1006, active: 1006, unlocking: vec![] })); + assert_eq!(Balances::free_balance(&10), 1 + 1010); + // Check that amount at stake is NOT increased + assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000 + 10, active: 1000 + 10, unlocking: vec![] })); + // Check that amount in staked account is NOT increased. + assert_eq!(Balances::free_balance(&11), recorded_stash_balance); }); } @@ -996,7 +1036,7 @@ fn validator_payment_prefs_work() { // session triggered: the reward value stashed should be 10 -- defined in ExtBuilder genesis. assert_eq!(Staking::current_session_reward(), session_reward); assert_eq!(Staking::current_era_reward(), session_reward); - + block = 6; // Block 6 => Session 2 => Era 0 System::set_block_number(block); Timestamp::set_timestamp(block*5); // a little late. @@ -1029,7 +1069,7 @@ fn validator_payment_prefs_work() { #[test] fn bond_extra_works() { // Tests that extra `free_balance` in the stash can be added to stake - // NOTE: this tests only verifies `StakingLedger` for correct updates. + // NOTE: this tests only verifies `StakingLedger` for correct updates // See `bond_extra_and_withdraw_unbonded_works` for more details and updates on `Exposure`. with_externalities(&mut ExtBuilder::default().build(), || { @@ -1066,7 +1106,6 @@ fn bond_extra_and_withdraw_unbonded_works() { // * it can unbond a portion of its funds from the stash account. // * Once the unbonding period is done, it can actually take the funds out of the stash. with_externalities(&mut ExtBuilder::default() - .reward(10) // it is the default, just for verbosity .nominate(false) .build(), || { @@ -1094,7 +1133,7 @@ fn bond_extra_and_withdraw_unbonded_works() { // confirm that 10 is a normal validator and gets paid at the end of the era. System::set_block_number(1); Timestamp::set_timestamp(5); - Session::check_rotate_session(System::block_number()); + Session::check_rotate_session(System::block_number()); assert_eq!(Staking::current_era(), 1); assert_eq!(Session::current_index(), 1); @@ -1107,7 +1146,8 @@ fn bond_extra_and_withdraw_unbonded_works() { assert_eq!(Staking::stakers(&10), Exposure { total: 1000, own: 1000, others: vec![] }); - // deposit the extra 100 units + + // deposit the extra 100 units Staking::bond_extra(Origin::signed(10), 100).unwrap(); assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000 + 100, active: 1000 + 100, unlocking: vec![] })); @@ -1164,13 +1204,19 @@ fn slot_stake_is_least_staked_validator_and_limits_maximum_punishment() { // Test that slot_stake is the maximum punishment that can happen to a validator // Note that rewardDestination is the stash account by default // Note that unlike reward slash will affect free_balance, not the stash account. - with_externalities(&mut ExtBuilder::default().nominate(false).build(), || { + with_externalities(&mut ExtBuilder::default() + .nominate(false) + .fare(false) + .build(), + || { + // Give the man some money. // Confirm validator count is 2 assert_eq!(Staking::validator_count(), 2); // Confirm account 10 and 20 are validators assert!(>::exists(&10) && >::exists(&20)); // Confirm 10 has less stake than 20 assert!(Staking::stakers(&10).total < Staking::stakers(&20).total); + assert_eq!(Staking::stakers(&10).total, 1000); assert_eq!(Staking::stakers(&20).total, 2000); @@ -1339,92 +1385,105 @@ fn on_free_balance_zero_stash_removes_nominator() { fn phragmen_poc_works() { // Tests the POC test of the phragmen, mentioned in the paper and reference implementation. // Initial votes: - // vote_list = [ - // ("A", 10.0, ["X", "Y"]), - // ("B", 20.0, ["X", "Z"]), - // ("C", 30.0, ["Y", "Z"]) - // ] + // Votes [ + // ('2', 500, ['10', '20', '30']), + // ('4', 500, ['10', '20', '40']), + // ('10', 1000, ['10']), + // ('20', 1000, ['20']), + // ('30', 1000, ['30']), + // ('40', 1000, ['40'])] // // Sequential Phragmén gives - // Z is elected with stake 35.0 and score 0.02 - // Y is elected with stake 25.0 and score 0.04 + // 10 is elected with stake 1666.6666666666665 and score 0.0005 + // 20 is elected with stake 1333.3333333333333 and score 0.00075 + + // 2 has load 0.00075 and supported + // 10 with stake 333.3333333333333 20 with stake 166.66666666666666 30 with stake 0.0 + // 4 has load 0.00075 and supported + // 10 with stake 333.3333333333333 20 with stake 166.66666666666666 40 with stake 0.0 + // 10 has load 0.0005 and supported + // 10 with stake 1000.0 + // 20 has load 0.00075 and supported + // 20 with stake 1000.0 + // 30 has load 0 and supported + // 30 with stake 0 + // 40 has load 0 and supported + // 40 with stake 0 + + // Sequential Phragmén with post processing gives + // 10 is elected with stake 1500.0 and score 0.0005 + // 20 is elected with stake 1500.0 and score 0.00075 // - // A has load 0.04 and supported - // X with stake 0.0 Y with stake 10.0 - // B has load 0.02 and supported - // X with stake 0.0 Z with stake 20.0 - // C has load 0.04 and supported - // Y with stake 15.0 Z with stake 15.0 - // - // NOTE: doesn't X/Y/Z's stash value make a difference here in phragmen? + // 10 has load 0.0005 and supported + // 10 with stake 1000.0 + // 20 has load 0.00075 and supported + // 20 with stake 1000.0 + // 30 has load 0 and supported + // 30 with stake 0 + // 40 has load 0 and supported + // 40 with stake 0 + // 2 has load 0.00075 and supported + // 10 with stake 166.66666666666674 20 with stake 333.33333333333326 30 with stake 0 + // 4 has load 0.00075 and supported + // 10 with stake 333.3333333333333 20 with stake 166.66666666666666 40 with stake 0.0 + + with_externalities(&mut ExtBuilder::default() .nominate(false) + .validator_pool(true) .build(), || { - // initial setup of 10 and 20, both validators. - assert_eq!(Session::validators(), vec![20, 10]); + // We don't really care about this. At this point everything is even. + // assert_eq!(Session::validators(), vec![40, 30]); assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![] })); - assert_eq!(Staking::ledger(&20), Some(StakingLedger { stash: 21, total: 2000, active: 2000, unlocking: vec![] })); + assert_eq!(Staking::ledger(&20), Some(StakingLedger { stash: 21, total: 1000, active: 1000, unlocking: vec![] })); + assert_eq!(Staking::ledger(&30), Some(StakingLedger { stash: 31, total: 1000, active: 1000, unlocking: vec![] })); + assert_eq!(Staking::ledger(&40), Some(StakingLedger { stash: 41, total: 1000, active: 1000, unlocking: vec![] })); - assert_eq!(Staking::validators(10), ValidatorPrefs::default()); - assert_eq!(Staking::validators(20), ValidatorPrefs::default()); + assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); + assert_ok!(Staking::set_payee(Origin::signed(20), RewardDestination::Controller)); + assert_ok!(Staking::set_payee(Origin::signed(30), RewardDestination::Controller)); + assert_ok!(Staking::set_payee(Origin::signed(40), RewardDestination::Controller)); - assert_eq!(Balances::free_balance(10), 1); - assert_eq!(Balances::free_balance(20), 1); - // no one is a nominator assert_eq!(>::enumerate().count(), 0 as usize); - // Bond [30, 31] as the third validator - assert_ok!(Staking::bond(Origin::signed(31), 30, 1000, RewardDestination::default())); - assert_ok!(Staking::validate(Origin::signed(30), ValidatorPrefs::default())); - - // bond [2,1](A), [4,3](B), [6,5](C) as the 3 nominators - // Give all of them some balance to be able to bond properly. - for i in &[1, 3, 5] { Balances::set_free_balance(i, 50); } - // Linking names to the above test: - // 10 => X - // 20 => Y - // 30 => Z - assert_ok!(Staking::bond(Origin::signed(1), 2, 10, RewardDestination::default())); - assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 20])); + // bond [2,1] / [4,3] a nominator + Balances::set_free_balance(&1, 1000); + Balances::set_free_balance(&3, 1000); - assert_ok!(Staking::bond(Origin::signed(3), 4, 20, RewardDestination::default())); - assert_ok!(Staking::nominate(Origin::signed(4), vec![10, 30])); + assert_ok!(Staking::bond(Origin::signed(1), 2, 500, RewardDestination::default())); + assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 20, 30])); - assert_ok!(Staking::bond(Origin::signed(5), 6, 30, RewardDestination::default())); - assert_ok!(Staking::nominate(Origin::signed(6), vec![20, 30])); + assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::default())); + assert_ok!(Staking::nominate(Origin::signed(4), vec![10, 20, 40])); // New era => election algorithm will trigger System::set_block_number(1); Session::check_rotate_session(System::block_number()); - // Z and Y are chosen - assert_eq!(Session::validators(), vec![30, 20]); + assert_eq!(Session::validators(), vec![20, 10]); - // with stake 35 and 25 respectively + // with stake 1666 and 1333 respectively + assert_eq!(Staking::stakers(10).own, 1000); + assert_eq!(Staking::stakers(10).total, 1000 + 500); + assert_eq!(Staking::stakers(20).own, 1000); + assert_eq!(Staking::stakers(20).total, 1000 + 500); - // This is only because 30 has been bonded on the fly, exposures are stored at the very end of the era. - // 35 is the point, not 'own' Exposure. - assert_eq!(Staking::stakers(30).own, 0); - assert_eq!(Staking::stakers(30).total, 0 + 35); - // same as above. +25 is the point - assert_eq!(Staking::stakers(20).own, 2010); - assert_eq!(Staking::stakers(20).total, 2010 + 25); + // Nominator's stake distribution. + assert_eq!(Staking::stakers(10).others.iter().map(|e| e.value).collect::>>(), vec![250, 250]); + assert_eq!(Staking::stakers(10).others.iter().map(|e| e.value).sum::>(), 500); + assert_eq!(Staking::stakers(10).others.iter().map(|e| e.who).collect::>>(), vec![4, 2]); - // 30(Z) was supported by B-4 and C-6 with stake 20 and 15 respectively. - assert_eq!(Staking::stakers(30).others.iter().map(|e| e.value).collect::>>(), vec![15, 20]); - assert_eq!(Staking::stakers(30).others.iter().map(|e| e.who).collect::>>(), vec![6, 4]); - - // 20(Y) was supported by A-2 and C-6 with stake 10 and 15 respectively. - assert_eq!(Staking::stakers(20).others.iter().map(|e| e.value).collect::>>(), vec![15, 10]); - assert_eq!(Staking::stakers(20).others.iter().map(|e| e.who).collect::>>(), vec![6, 2]); + assert_eq!(Staking::stakers(20).others.iter().map(|e| e.value).collect::>>(), vec![250, 250]); + assert_eq!(Staking::stakers(20).others.iter().map(|e| e.value).sum::>(), 500); + assert_eq!(Staking::stakers(20).others.iter().map(|e| e.who).collect::>>(), vec![4, 2]); }); } #[test] -fn phragmen_election_works() { +fn phragmen_election_works_example_2() { // tests the encapsulated phragmen::elect function. with_externalities(&mut ExtBuilder::default().nominate(false).build(), || { // initial setup of 10 and 20, both validators @@ -1439,17 +1498,17 @@ fn phragmen_election_works() { // bond [2,1](A), [4,3](B), as 2 nominators // Give all of them some balance to be able to bond properly. - for i in &[1, 3] { Balances::set_free_balance(i, 50); } - assert_ok!(Staking::bond(Origin::signed(1), 2, 5, RewardDestination::default())); + for i in &[1, 3] { Balances::set_free_balance(i, 2000); } + assert_ok!(Staking::bond(Origin::signed(1), 2, 50, RewardDestination::default())); assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 20])); - assert_ok!(Staking::bond(Origin::signed(3), 4, 45, RewardDestination::default())); + assert_ok!(Staking::bond(Origin::signed(3), 4, 1000, RewardDestination::default())); assert_ok!(Staking::nominate(Origin::signed(4), vec![10, 30])); let rounds = || 2 as usize; let validators = || >::enumerate(); let nominators = || >::enumerate(); - let stash_of = |w| Staking::stash_balance(&w); + let stash_of = |w: &u64| -> u64 { Staking::stash_balance(w) }; let min_validator_count = Staking::minimum_validator_count() as usize; let winners = phragmen::elect::( @@ -1457,7 +1516,12 @@ fn phragmen_election_works() { validators, nominators, stash_of, - min_validator_count + min_validator_count, + ElectionConfig::> { + equalise: true, + tolerance: >::sa(10 as u64), + iterations: 10, + } ); // 10 and 30 must be the winners @@ -1468,23 +1532,54 @@ fn phragmen_election_works() { // python implementation output: /* - 10 is elected with stake 26.31578947368421 and score 0.02 - 30 is elected with stake 23.684210526315788 and score 0.042222222222222223 + Votes [ + ('10', 1000, ['10']), + ('20', 1000, ['20']), + ('30', 1000, ['30']), + ('2', 50, ['10', '20']), + ('4', 1000, ['10', '30']) + ] + Sequential Phragmén gives + 10 is elected with stake 1705.7377049180327 and score 0.0004878048780487805 + 30 is elected with stake 1344.2622950819673 and score 0.0007439024390243903 + + 10 has load 0.0004878048780487805 and supported + 10 with stake 1000.0 + 20 has load 0 and supported + 20 with stake 0 + 30 has load 0.0007439024390243903 and supported + 30 with stake 1000.0 + 2 has load 0.0004878048780487805 and supported + 10 with stake 50.0 20 with stake 0.0 + 4 has load 0.0007439024390243903 and supported + 10 with stake 655.7377049180328 30 with stake 344.26229508196724 + + Sequential Phragmén with post processing gives + 10 is elected with stake 1525.0 and score 0.0004878048780487805 + 30 is elected with stake 1525.0 and score 0.0007439024390243903 + + 10 has load 0.0004878048780487805 and supported + 10 with stake 1000.0 + 20 has load 0 and supported + 20 with stake 0 + 30 has load 0.0007439024390243903 and supported + 30 with stake 1000.0 + 2 has load 0.0004878048780487805 and supported + 10 with stake 50.0 20 with stake 0.0 + 4 has load 0.0007439024390243903 and supported + 10 with stake 475.0 30 with stake 525.0 + - 2 has load 0.02 and supported - 10 with stake 5.0 20 with stake 0.0 - 4 has load 0.042222222222222223 and supported - 10 with stake 21.31578947368421 30 with stake 23.684210526315788 */ - assert_eq!(winner_10.exposure.total, 1000 + 26); - assert_eq!(winner_10.score, Perquintill::from_fraction(0.02)); - assert_eq!(winner_10.exposure.others[0].value, 21); - assert_eq!(winner_10.exposure.others[1].value, 5); + assert_eq!(winner_10.exposure.total, 1000 + 525); + assert_eq!(winner_10.score, Perquintill::from_quintillionths(487804878048780)); + assert_eq!(winner_10.exposure.others[0].value, 475); + assert_eq!(winner_10.exposure.others[1].value, 50); - assert_eq!(winner_30.exposure.total, 23); - assert_eq!(winner_30.score, Perquintill::from_quintillionths(42222222222222222)); - assert_eq!(winner_30.exposure.others[0].value, 23); + assert_eq!(winner_30.exposure.total, 1000 + 525); + assert_eq!(winner_30.score, Perquintill::from_quintillionths(743902439024390)); + assert_eq!(winner_30.exposure.others[0].value, 525); }) } @@ -1496,20 +1591,23 @@ fn switching_roles() { .sessions_per_era(3) .build(), || { + // Reset reward destination + for i in &[10, 20] { assert_ok!(Staking::set_payee(Origin::signed(*i), RewardDestination::Controller)); } + assert_eq!(Session::validators(), vec![20, 10]); // put some money in account that we'll use. for i in 1..7 { Balances::set_free_balance(&i, 5000); } // add 2 nominators - assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::default())); + assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::Controller)); assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 6])); - assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::default())); + assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::Controller)); assert_ok!(Staking::nominate(Origin::signed(4), vec![20, 2])); // add a new validator candidate - assert_ok!(Staking::bond(Origin::signed(5), 6, 1500, RewardDestination::Controller)); + assert_ok!(Staking::bond(Origin::signed(5), 6, 1000, RewardDestination::Controller)); assert_ok!(Staking::validate(Origin::signed(6), ValidatorPrefs::default())); // new block @@ -1530,13 +1628,15 @@ fn switching_roles() { System::set_block_number(3); Session::check_rotate_session(System::block_number()); - // with current nominators 10 and 4 have the most stake + // with current nominators 10 and 5 have the most stake assert_eq!(Session::validators(), vec![6, 10]); // 2 decides to be a validator. Consequences: - // 6 will not be chosen in the next round (no votes) - // 2 itself will be chosen + 20 who now has the higher votes - // 10 wil have no votes. + // new stakes: + // 10: 1000 self vote + // 6: 1000 self vote + // 20: 1000 self vote + 500 vote + // 2: 2000 self vote + 500 vote. assert_ok!(Staking::validate(Origin::signed(2), ValidatorPrefs::default())); System::set_block_number(4); @@ -1557,14 +1657,11 @@ fn switching_roles() { #[test] fn wrong_vote_is_null() { with_externalities(&mut ExtBuilder::default() - .session_length(1) - .sessions_per_era(1) .nominate(false) .validator_pool(true) .build(), || { - // from the first era onward, only two will be chosen - assert_eq!(Session::validators(), vec![40, 30, 20, 10]); + assert_eq!(Session::validators(), vec![40, 30]); // put some money in account that we'll use. for i in 1..3 { Balances::set_free_balance(&i, 5000); } @@ -1572,14 +1669,204 @@ fn wrong_vote_is_null() { // add 1 nominators assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::default())); assert_ok!(Staking::nominate(Origin::signed(2), vec![ - 10, 20, // good votes + 10, 20, // good votes 1, 2, 15, 1000, 25 // crap votes. No effect. ])); - // new block + // new block + System::set_block_number(1); + Session::check_rotate_session(System::block_number()); + + assert_eq!(Session::validators(), vec![20, 10]); + }); +} + +#[test] +fn bond_with_no_staked_value() { + // Behavior when someone bonds with no staked value. + // Particularly when she votes and the candidate is elected. + with_externalities(&mut ExtBuilder::default() + .validator_count(3) + .nominate(false) + .minimum_validator_count(1) + .build(), || { + // setup + assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); + assert_ok!(Staking::set_payee(Origin::signed(20), RewardDestination::Controller)); + Balances::set_free_balance(&3, 1000); + Balances::set_free_balance(&4, 1000); + Balances::set_free_balance(&2, 1000); + + // initial validators + assert_eq!(Session::validators(), vec![20, 10]); + + // Stingy validator. + assert_ok!(Staking::bond(Origin::signed(1), 2, 0, RewardDestination::Controller)); + assert_ok!(Staking::validate(Origin::signed(2), ValidatorPrefs::default())); + System::set_block_number(1); Session::check_rotate_session(System::block_number()); + // Not elected even though we want 3. assert_eq!(Session::validators(), vec![20, 10]); + + // min of 10 and 20. + assert_eq!(Staking::slot_stake(), 1000); + + // let's make the stingy one elected. + assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::Controller)); + assert_ok!(Staking::nominate(Origin::signed(4), vec![2])); + + assert_eq!(Staking::ledger(4), Some(StakingLedger { stash: 3, active: 500, total: 500, unlocking: vec![]})); + + System::set_block_number(1); + Session::check_rotate_session(System::block_number()); + + assert_eq!(Session::validators(), vec![20, 10, 2]); + assert_eq!(Staking::stakers(2), Exposure { own: 0, total: 500, others: vec![IndividualExposure { who: 4, value: 500}]}); + + assert_eq!(Staking::slot_stake(), 500); + + // no rewards paid to 2 and 4 yet + assert_eq!(Balances::free_balance(&2), 1000); + assert_eq!(Balances::free_balance(&4), 1000); + + System::set_block_number(1); + Session::check_rotate_session(System::block_number()); + + let reward = Staking::current_session_reward(); + // 2 will not get any reward + // 4 will get all the reward share + assert_eq!(Balances::free_balance(&2), 1000); + assert_eq!(Balances::free_balance(&4), 1000 + reward); }); } +#[test] +fn bond_with_little_staked_value() { + // Behavior when someone bonds with little staked value. + // Particularly when she votes and the candidate is elected. + with_externalities(&mut ExtBuilder::default() + .validator_count(3) + .nominate(false) + .minimum_validator_count(1) + .build(), + || { + // setup + assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); + assert_ok!(Staking::set_payee(Origin::signed(20), RewardDestination::Controller)); + Balances::set_free_balance(&2, 1000); + + // initial validators + assert_eq!(Session::validators(), vec![20, 10]); + + // Stingy validator. + assert_ok!(Staking::bond(Origin::signed(1), 2, 1, RewardDestination::Controller)); + assert_ok!(Staking::validate(Origin::signed(2), ValidatorPrefs::default())); + + System::set_block_number(1); + Session::check_rotate_session(System::block_number()); + + // 2 is elected. + // and fucks up the slot stake. + assert_eq!(Session::validators(), vec![20, 10, 2]); + assert_eq!(Staking::slot_stake(), 1); + + // Old ones are rewarded. + assert_eq!(Balances::free_balance(&10), 1 + 10); + assert_eq!(Balances::free_balance(&20), 1 + 10); + // no rewards paid to 2. This was initial election. + assert_eq!(Balances::free_balance(&2), 1000); + + System::set_block_number(1); + Session::check_rotate_session(System::block_number()); + + assert_eq!(Session::validators(), vec![20, 10, 2]); + assert_eq!(Staking::slot_stake(), 1); + + let reward = Staking::current_session_reward(); + // 2 will not get the full reward, practically 1 + assert_eq!(Balances::free_balance(&2), 1000 + reward.max(1)); + }); +} + + +#[test] +fn phragmen_linear_worse_case_equalise() { + with_externalities(&mut ExtBuilder::default() + .nominate(false) + .validator_pool(true) + .fare(true) + .build(), + || { + let bond_validator = |a, b| { + Balances::set_free_balance(&(a-1), b); + assert_ok!(Staking::bond(Origin::signed(a-1), a, b, RewardDestination::Controller)); + assert_ok!(Staking::validate(Origin::signed(a), ValidatorPrefs::default())); + }; + let bond_nominator = |a, b, v| { + Balances::set_free_balance(&(a-1), b); + assert_ok!(Staking::bond(Origin::signed(a-1), a, b, RewardDestination::Controller)); + assert_ok!(Staking::nominate(Origin::signed(a), v)); + }; + + for i in &[10, 20, 30, 40] { assert_ok!(Staking::set_payee(Origin::signed(*i), RewardDestination::Controller)); } + + bond_validator(50, 1000); + bond_validator(60, 1000); + bond_validator(70, 1000); + + bond_nominator(2, 2000, vec![10]); + bond_nominator(4, 1000, vec![10, 20]); + bond_nominator(6, 1000, vec![20, 30]); + bond_nominator(8, 1000, vec![30, 40]); + bond_nominator(110, 1000, vec![40, 50]); + bond_nominator(112, 1000, vec![50, 60]); + bond_nominator(114, 1000, vec![60, 70]); + + assert_eq!(Session::validators(), vec![40, 30]); + assert_ok!(Staking::set_validator_count(7)); + + System::set_block_number(1); + Session::check_rotate_session(System::block_number()); + + assert_eq!(Session::validators(), vec![10, 60, 40, 20, 50, 30, 70]); + + // Sequential Phragmén with post processing gives + // 10 is elected with stake 3000.0 and score 0.00025 + // 30 is elected with stake 2008.8712884829595 and score 0.0003333333333333333 + // 50 is elected with stake 2000.0001049958742 and score 0.0003333333333333333 + // 60 is elected with stake 1991.128921508789 and score 0.0004444444444444444 + // 20 is elected with stake 2017.7421569824219 and score 0.0005277777777777777 + // 40 is elected with stake 2000.0001049958742 and score 0.0005555555555555556 + // 70 is elected with stake 1982.2574230340813 and score 0.0007222222222222222 + + assert_eq!(Staking::stakers(10).total, 3000); + assert_eq!(Staking::stakers(30).total, 2035); + assert_eq!(Staking::stakers(50).total, 2000); + assert_eq!(Staking::stakers(60).total, 1968); + assert_eq!(Staking::stakers(20).total, 2035); + assert_eq!(Staking::stakers(40).total, 2024); + assert_eq!(Staking::stakers(70).total, 1936); + }) +} + +#[test] +fn phragmen_chooses_correct_validators() { + with_externalities(&mut ExtBuilder::default() + .nominate(true) + .validator_pool(true) + .fare(true) + .validator_count(1) + .build(), + || { + // 4 validator candidates + // self vote + default account 100 is nominator. + assert_eq!(Staking::validator_count(), 1); + assert_eq!(Session::validators().len(), 1); + + System::set_block_number(1); + Session::check_rotate_session(System::block_number()); + + assert_eq!(Session::validators().len(), 1); + }) +}