mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
Self-Vote for Staking (among others.) (#2078)
* initial doc for the staking module * Remove md style links. * Remove todos. * Add rust code types * Rename and fix review notes. * Add new md file * Final touches. * Migrate compleatly to rustdoc * Update link * Fix heading * Final touches wrt the new template. * Remove empty prereq. * Fix more reviews * Some final nits. * Fix some side issues. * Fix another set of reviews * Fix + stabilize leftover reivews. * Remove unused test parameters * Fix typo. * Merge redundant loops * Adds phantom self-vote * Fix broken tests. * Refactor some names to match the reference. * Remove redundant inner loops from election round. * Introduce phragmen post-processing. * Some fixes and todos. * Fix some tests with new phragmen params * Fix test * Bump spec * Fix wasm build * Fix tests and phragmen fallback. Avoid double-controlling * Fix and rebuild wasm * Whitespaces, whitespaces everywhere. * Rebuild * Disable post-processing. * Identify by stash, not controller account. * Couple of fixes * Fix first test * Fix invulnerability_should_work * Fix a couple more tests * Fix more tests * Fix more tests * Fix more tests * Fix some tests * Fix update-ledger. * Fix update-ledger. * Fix another test * Fix another test * Fix rest of staking tests * Remove printlns * Rebuild wasm * Fix & tests for auth/val syncing * Fix up threading for tests * Remove superfluous asserts
This commit is contained in:
Generated
+1
@@ -3300,6 +3300,7 @@ name = "srml-session"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"hex-literal 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parity-codec-derive 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
||||
BIN
Binary file not shown.
@@ -107,7 +107,6 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
|
||||
current_era: 0,
|
||||
offline_slash: Perbill::from_billionths(1_000_000),
|
||||
session_reward: Perbill::from_billionths(2_065),
|
||||
current_offline_slash: 0,
|
||||
current_session_reward: 0,
|
||||
validator_count: 7,
|
||||
sessions_per_era: 12,
|
||||
@@ -263,7 +262,6 @@ pub fn testnet_genesis(
|
||||
bonding_duration: 2 * 60 * 12,
|
||||
offline_slash: Perbill::zero(),
|
||||
session_reward: Perbill::zero(),
|
||||
current_offline_slash: 0,
|
||||
current_session_reward: 0,
|
||||
offline_slash_grace: 0,
|
||||
stakers: initial_authorities.iter().map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator)).collect(),
|
||||
|
||||
@@ -308,7 +308,6 @@ mod tests {
|
||||
bonding_duration: 0,
|
||||
offline_slash: Perbill::zero(),
|
||||
session_reward: Perbill::zero(),
|
||||
current_offline_slash: 0,
|
||||
current_session_reward: 0,
|
||||
offline_slash_grace: 0,
|
||||
invulnerables: vec![alice(), bob(), charlie()],
|
||||
@@ -443,13 +442,7 @@ mod tests {
|
||||
]
|
||||
);
|
||||
|
||||
// let mut digest = generic::Digest::<Log>::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::<Log>::default(); // TODO test this
|
||||
let digest = generic::Digest::<Log>::default();
|
||||
assert_eq!(Header::decode(&mut &block2.0[..]).unwrap().digest, digest);
|
||||
|
||||
(block1, block2)
|
||||
@@ -578,14 +571,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))
|
||||
|
||||
@@ -58,8 +58,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
spec_name: create_runtime_str!("node"),
|
||||
impl_name: create_runtime_str!("substrate-node"),
|
||||
authoring_version: 10,
|
||||
spec_version: 38,
|
||||
impl_version: 40,
|
||||
spec_version: 39,
|
||||
impl_version: 39,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
};
|
||||
|
||||
|
||||
BIN
Binary file not shown.
@@ -244,6 +244,12 @@ impl<T: Trait> Module<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a single authority by index.
|
||||
pub fn set_authority_count(count: u32) {
|
||||
Self::save_original_authorities(None);
|
||||
AuthorityStorageVec::<T::SessionKey>::set_count(count);
|
||||
}
|
||||
|
||||
/// Set a single authority by index.
|
||||
pub fn set_authority(index: u32, key: &T::SessionKey) {
|
||||
let current_authority = AuthorityStorageVec::<T::SessionKey>::item(index);
|
||||
|
||||
@@ -20,6 +20,7 @@ timestamp = { package = "srml-timestamp", path = "../timestamp", default-feature
|
||||
[dev-dependencies]
|
||||
substrate-primitives = { path = "../../core/primitives" }
|
||||
runtime_io = { package = "sr-io", path = "../../core/sr-io" }
|
||||
lazy_static = "1.0"
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
|
||||
@@ -184,8 +184,12 @@ impl<T: Trait> Module<T> {
|
||||
<LastLengthChange<T>>::put(block_number);
|
||||
}
|
||||
|
||||
T::OnSessionChange::on_session_change(time_elapsed, apply_rewards);
|
||||
|
||||
// Update any changes in session keys.
|
||||
for (i, v) in Self::validators().into_iter().enumerate() {
|
||||
let v = Self::validators();
|
||||
<consensus::Module<T>>::set_authority_count(v.len() as u32);
|
||||
for (i, v) in v.into_iter().enumerate() {
|
||||
<consensus::Module<T>>::set_authority(
|
||||
i as u32,
|
||||
&<NextKeyFor<T>>::get(&v)
|
||||
@@ -193,8 +197,6 @@ impl<T: Trait> Module<T> {
|
||||
.unwrap_or_default()
|
||||
);
|
||||
};
|
||||
|
||||
T::OnSessionChange::on_session_change(time_elapsed, apply_rewards);
|
||||
}
|
||||
|
||||
/// Get the time that should have elapsed over a session if everything was working perfectly.
|
||||
@@ -224,6 +226,7 @@ impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::cell::RefCell;
|
||||
use srml_support::{impl_outer_origin, assert_ok};
|
||||
use runtime_io::with_externalities;
|
||||
use substrate_primitives::{H256, Blake2Hasher};
|
||||
@@ -235,6 +238,17 @@ mod tests {
|
||||
pub enum Origin for Test {}
|
||||
}
|
||||
|
||||
thread_local!{
|
||||
static NEXT_VALIDATORS: RefCell<Vec<u64>> = RefCell::new(vec![1, 2, 3]);
|
||||
}
|
||||
|
||||
pub struct TestOnSessionChange;
|
||||
impl OnSessionChange<u64> for TestOnSessionChange {
|
||||
fn on_session_change(_elapsed: u64, _should_reward: bool) {
|
||||
NEXT_VALIDATORS.with(|v| Session::set_validators(&*v.borrow()));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct Test;
|
||||
impl consensus::Trait for Test {
|
||||
@@ -261,7 +275,7 @@ mod tests {
|
||||
}
|
||||
impl Trait for Test {
|
||||
type ConvertAccountIdToSessionKey = ConvertUintAuthorityId;
|
||||
type OnSessionChange = ();
|
||||
type OnSessionChange = TestOnSessionChange;
|
||||
type Event = ();
|
||||
}
|
||||
|
||||
@@ -273,14 +287,14 @@ mod tests {
|
||||
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0;
|
||||
t.extend(consensus::GenesisConfig::<Test>{
|
||||
code: vec![],
|
||||
authorities: vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)],
|
||||
authorities: NEXT_VALIDATORS.with(|l| l.borrow().iter().cloned().map(UintAuthorityId).collect()),
|
||||
}.build_storage().unwrap().0);
|
||||
t.extend(timestamp::GenesisConfig::<Test>{
|
||||
period: 5,
|
||||
}.build_storage().unwrap().0);
|
||||
t.extend(GenesisConfig::<Test>{
|
||||
session_length: 2,
|
||||
validators: vec![1, 2, 3],
|
||||
validators: NEXT_VALIDATORS.with(|l| l.borrow().clone()),
|
||||
keys: vec![],
|
||||
}.build_storage().unwrap().0);
|
||||
runtime_io::TestExternalities::new(t)
|
||||
@@ -289,12 +303,35 @@ mod tests {
|
||||
#[test]
|
||||
fn simple_setup_should_work() {
|
||||
with_externalities(&mut new_test_ext(), || {
|
||||
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1).into(), UintAuthorityId(2).into(), UintAuthorityId(3).into()]);
|
||||
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]);
|
||||
assert_eq!(Session::length(), 2);
|
||||
assert_eq!(Session::validators(), vec![1, 2, 3]);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn authorities_should_track_validators() {
|
||||
with_externalities(&mut new_test_ext(), || {
|
||||
NEXT_VALIDATORS.with(|v| *v.borrow_mut() = vec![1, 2]);
|
||||
assert_ok!(Session::force_new_session(false));
|
||||
Session::check_rotate_session(1);
|
||||
assert_eq!(Session::validators(), vec![1, 2]);
|
||||
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1), UintAuthorityId(2)]);
|
||||
|
||||
NEXT_VALIDATORS.with(|v| *v.borrow_mut() = vec![1, 2, 4]);
|
||||
assert_ok!(Session::force_new_session(false));
|
||||
Session::check_rotate_session(2);
|
||||
assert_eq!(Session::validators(), vec![1, 2, 4]);
|
||||
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(4)]);
|
||||
|
||||
NEXT_VALIDATORS.with(|v| *v.borrow_mut() = vec![1, 2, 3]);
|
||||
assert_ok!(Session::force_new_session(false));
|
||||
Session::check_rotate_session(3);
|
||||
assert_eq!(Session::validators(), vec![1, 2, 3]);
|
||||
assert_eq!(Consensus::authorities(), vec![UintAuthorityId(1), UintAuthorityId(2), UintAuthorityId(3)]);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_work_with_early_exit() {
|
||||
with_externalities(&mut new_test_ext(), || {
|
||||
|
||||
+359
-119
@@ -14,7 +14,205 @@
|
||||
// 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.
|
||||
//! # Staking Module
|
||||
//!
|
||||
//! <!-- Original author of paragraph: @gavofyork -->
|
||||
//! 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
|
||||
//! <!-- Original author of paragraph: @gavofyork -->
|
||||
//!
|
||||
//! - 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
|
||||
//! <!-- Original author of paragraph: @gavofyork -->
|
||||
//!
|
||||
//! 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)]
|
||||
|
||||
@@ -39,14 +237,23 @@ 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;
|
||||
const MAX_UNSTAKE_THRESHOLD: u32 = 10;
|
||||
|
||||
// Indicates the initial status of the staker
|
||||
/// Indicates the initial status of the staker.
|
||||
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
|
||||
pub enum StakerStatus<AccountId> { Idle, Validator, Nominator(Vec<AccountId>), }
|
||||
pub enum StakerStatus<AccountId> {
|
||||
/// Chilling.
|
||||
Idle,
|
||||
/// Declared state in validating or already participating in it.
|
||||
Validator,
|
||||
/// Nominating for a group of other stakers.
|
||||
Nominator(Vec<AccountId>),
|
||||
}
|
||||
|
||||
/// A destination account for payment.
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode)]
|
||||
@@ -143,7 +350,7 @@ impl<
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub struct IndividualExposure<AccountId, Balance: HasCompact> {
|
||||
/// Which nominator.
|
||||
/// The stash account of the nominator in question.
|
||||
who: AccountId,
|
||||
/// Amount of funds exposed.
|
||||
#[codec(compact)]
|
||||
@@ -207,9 +414,6 @@ decl_storage! {
|
||||
/// The length of the bonding duration in blocks.
|
||||
pub BondingDuration get(bonding_duration) config(): T::BlockNumber = T::BlockNumber::sa(1000);
|
||||
|
||||
// TODO: remove once Alex/CC updated #1785
|
||||
pub Invulerables get(invulerables): Vec<T::AccountId>;
|
||||
|
||||
/// Any validators that may never be slashed or forcibly kicked. It's a Vec since they're easy to initialise
|
||||
/// and the performance hit is minimal (we expect no more than four invulnerables) and restricted to testnets.
|
||||
pub Invulnerables get(invulnerables) config(): Vec<T::AccountId>;
|
||||
@@ -219,21 +423,19 @@ decl_storage! {
|
||||
/// Map from all (unlocked) "controller" accounts to the info regarding the staking.
|
||||
pub Ledger get(ledger): map T::AccountId => Option<StakingLedger<T::AccountId, BalanceOf<T>, T::BlockNumber>>;
|
||||
|
||||
/// Where the reward payment should be made.
|
||||
/// Where the reward payment should be made. Keyed by stash.
|
||||
pub Payee get(payee): map T::AccountId => RewardDestination;
|
||||
|
||||
/// The set of keys are all controllers that want to validate.
|
||||
///
|
||||
/// The values are the preferences that a validator has.
|
||||
/// The map from (wannabe) validator stash key to the preferences of that validator.
|
||||
pub Validators get(validators): linked_map T::AccountId => ValidatorPrefs<BalanceOf<T>>;
|
||||
|
||||
/// The set of keys are all controllers that want to nominate.
|
||||
///
|
||||
/// The value are the nominations.
|
||||
/// The map from nominator stash key to the set of stash keys of all validators to nominate.
|
||||
pub Nominators get(nominators): linked_map T::AccountId => Vec<T::AccountId>;
|
||||
|
||||
/// Nominators for a particular account that is in action right now. You can't iterate through validators here,
|
||||
/// but you can find them in the `sessions` module.
|
||||
///
|
||||
/// This is keyed by the stash account.
|
||||
pub Stakers get(stakers): map T::AccountId => Exposure<T::AccountId, BalanceOf<T>>;
|
||||
|
||||
// The historical validators and their nominations for a given era. Stored as a trie root of the mapping
|
||||
@@ -244,13 +446,14 @@ decl_storage! {
|
||||
// entry removed down to a specific number of entries (probably around 90 for a 3 month history).
|
||||
// pub HistoricalStakers get(historical_stakers): map T::BlockNumber => Option<H256>;
|
||||
|
||||
/// The currently elected validator set keyed by stash account ID.
|
||||
pub CurrentElected get(current_elected): Vec<T::AccountId>;
|
||||
|
||||
/// The current era index.
|
||||
pub CurrentEra get(current_era) config(): T::BlockNumber;
|
||||
|
||||
/// Maximum reward, per validator, that is provided per acceptable session.
|
||||
pub CurrentSessionReward get(current_session_reward) config(): BalanceOf<T>;
|
||||
/// Slash, per validator that is taken for the first time they are found to be offline.
|
||||
pub CurrentOfflineSlash get(current_offline_slash) config(): BalanceOf<T>;
|
||||
|
||||
/// The accumulated reward for the current era. Reset to zero at the beginning of the era and
|
||||
/// increased for every successfully finished session.
|
||||
@@ -282,6 +485,7 @@ decl_storage! {
|
||||
build(|storage: &mut primitives::StorageOverlay, _: &mut primitives::ChildrenStorageOverlay, config: &GenesisConfig<T>| {
|
||||
with_storage(storage, || {
|
||||
for &(ref stash, ref controller, balance, ref status) in &config.stakers {
|
||||
assert!(T::Currency::free_balance(&stash) >= balance);
|
||||
let _ = <Module<T>>::bond(
|
||||
T::Origin::from(Some(stash.clone()).into()),
|
||||
T::Lookup::unlookup(controller.clone()),
|
||||
@@ -315,6 +519,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: <T::Lookup as StaticLookup>::Source, #[compact] value: BalanceOf<T>, payee: RewardDestination) {
|
||||
let stash = ensure_signed(origin)?;
|
||||
|
||||
@@ -324,15 +530,18 @@ decl_module! {
|
||||
|
||||
let controller = T::Lookup::lookup(controller)?;
|
||||
|
||||
if <Ledger<T>>::exists(&controller) {
|
||||
return Err("controller already paired")
|
||||
}
|
||||
|
||||
// You're auto-bonded forever, here. We might improve this by only bonding when
|
||||
// you actually validate/nominate.
|
||||
<Bonded<T>>::insert(&stash, controller.clone());
|
||||
<Payee<T>>::insert(&stash, payee);
|
||||
|
||||
let stash_balance = T::Currency::free_balance(&stash);
|
||||
let value = value.min(stash_balance);
|
||||
|
||||
Self::update_ledger(&controller, StakingLedger { stash, total: value, active: value, unlocking: vec![] });
|
||||
<Payee<T>>::insert(&controller, payee);
|
||||
Self::update_ledger(&controller, &StakingLedger { stash, total: value, active: value, unlocking: vec![] });
|
||||
}
|
||||
|
||||
/// Add some extra amount that have appeared in the stash `free_balance` into the balance up for
|
||||
@@ -340,7 +549,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<T>) {
|
||||
let controller = ensure_signed(origin)?;
|
||||
let mut ledger = Self::ledger(&controller).ok_or("not a controller")?;
|
||||
@@ -350,7 +559,7 @@ decl_module! {
|
||||
let extra = (stash_balance - ledger.total).min(max_additional);
|
||||
ledger.total += extra;
|
||||
ledger.active += extra;
|
||||
Self::update_ledger(&controller, ledger);
|
||||
Self::update_ledger(&controller, &ledger);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -361,7 +570,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<T>) {
|
||||
@@ -382,7 +591,7 @@ decl_module! {
|
||||
|
||||
let era = Self::current_era() + Self::bonding_duration();
|
||||
ledger.unlocking.push(UnlockChunk { value, era });
|
||||
Self::update_ledger(&controller, ledger);
|
||||
Self::update_ledger(&controller, &ledger);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -391,68 +600,90 @@ 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) {
|
||||
let controller = ensure_signed(origin)?;
|
||||
let ledger = Self::ledger(&controller).ok_or("not a controller")?;
|
||||
let ledger = ledger.consolidate_unlocked(Self::current_era());
|
||||
Self::update_ledger(&controller, ledger);
|
||||
Self::update_ledger(&controller, &ledger);
|
||||
}
|
||||
|
||||
/// Declare the desire to validate for the origin controller.
|
||||
///
|
||||
/// 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<BalanceOf<T>>) {
|
||||
let controller = ensure_signed(origin)?;
|
||||
let _ledger = Self::ledger(&controller).ok_or("not a controller")?;
|
||||
let ledger = Self::ledger(&controller).ok_or("not a controller")?;
|
||||
let stash = &ledger.stash;
|
||||
ensure!(prefs.unstake_threshold <= MAX_UNSTAKE_THRESHOLD, "unstake threshold too large");
|
||||
<Nominators<T>>::remove(&controller);
|
||||
<Validators<T>>::insert(controller, prefs);
|
||||
<Nominators<T>>::remove(stash);
|
||||
<Validators<T>>::insert(stash, prefs);
|
||||
}
|
||||
|
||||
/// Declare the desire to nominate `targets` for the origin controller.
|
||||
///
|
||||
/// 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<<T::Lookup as StaticLookup>::Source>) {
|
||||
let controller = ensure_signed(origin)?;
|
||||
let _ledger = Self::ledger(&controller).ok_or("not a controller")?;
|
||||
let ledger = Self::ledger(&controller).ok_or("not a controller")?;
|
||||
let stash = &ledger.stash;
|
||||
ensure!(!targets.is_empty(), "targets cannot be empty");
|
||||
let targets = targets.into_iter()
|
||||
.take(MAX_NOMINATIONS)
|
||||
.map(T::Lookup::lookup)
|
||||
.collect::<result::Result<Vec<T::AccountId>, &'static str>>()?;
|
||||
|
||||
<Validators<T>>::remove(&controller);
|
||||
<Nominators<T>>::insert(controller, targets);
|
||||
<Validators<T>>::remove(stash);
|
||||
<Nominators<T>>::insert(stash, targets);
|
||||
}
|
||||
|
||||
/// Declare no desire to either validate or nominate.
|
||||
///
|
||||
/// 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")?;
|
||||
<Validators<T>>::remove(&controller);
|
||||
<Nominators<T>>::remove(&controller);
|
||||
let ledger = Self::ledger(&controller).ok_or("not a controller")?;
|
||||
let stash = &ledger.stash;
|
||||
<Validators<T>>::remove(stash);
|
||||
<Nominators<T>>::remove(stash);
|
||||
}
|
||||
|
||||
/// (Re-)set the payment target for a controller.
|
||||
///
|
||||
/// 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")?;
|
||||
<Payee<T>>::insert(&controller, payee);
|
||||
let ledger = Self::ledger(&controller).ok_or("not a controller")?;
|
||||
let stash = &ledger.stash;
|
||||
<Payee<T>>::insert(stash, payee);
|
||||
}
|
||||
|
||||
/// (Re-)set the payment target for a controller.
|
||||
///
|
||||
/// Effects will be felt at the beginning of the next era.
|
||||
///
|
||||
/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
|
||||
fn set_controller(origin, controller: <T::Lookup as StaticLookup>::Source) {
|
||||
let stash = ensure_signed(origin)?;
|
||||
let old_controller = Self::bonded(&stash).ok_or("not a stash")?;
|
||||
let controller = T::Lookup::lookup(controller)?;
|
||||
if <Ledger<T>>::exists(&controller) {
|
||||
return Err("controller already paired")
|
||||
}
|
||||
if controller != old_controller {
|
||||
<Bonded<T>>::insert(&stash, &controller);
|
||||
if let Some(l) = <Ledger<T>>::take(&old_controller) { <Ledger<T>>::insert(&controller, l) };
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the number of sessions in an era.
|
||||
@@ -488,7 +719,7 @@ decl_module! {
|
||||
}
|
||||
}
|
||||
|
||||
/// An event in this module.
|
||||
// An event in this module.
|
||||
decl_event!(
|
||||
pub enum Event<T> where Balance = BalanceOf<T>, <T as system::Trait>::AccountId {
|
||||
/// All validators have been rewarded by the given balance.
|
||||
@@ -515,13 +746,6 @@ impl<T: Trait> Module<T> {
|
||||
Self::sessions_per_era() * <session::Module<T>>::length()
|
||||
}
|
||||
|
||||
/// The stashed funds whose staking activities are controlled by `controller` and
|
||||
/// which are actively in stake right now.
|
||||
pub fn stash_balance(controller: &T::AccountId) -> BalanceOf<T> {
|
||||
Self::ledger(controller)
|
||||
.map_or_else(Zero::zero, |l| l.active)
|
||||
}
|
||||
|
||||
/// The total balance that can be slashed from a validator controller account as of
|
||||
/// right now.
|
||||
pub fn slashable_balance(who: &T::AccountId) -> BalanceOf<T> {
|
||||
@@ -531,21 +755,21 @@ impl<T: Trait> Module<T> {
|
||||
// MUTABLES (DANGEROUS)
|
||||
|
||||
/// Update the ledger for a controller. This will also update the stash lock.
|
||||
fn update_ledger(controller: &T::AccountId, ledger: StakingLedger<T::AccountId, BalanceOf<T>, T::BlockNumber>) {
|
||||
fn update_ledger(controller: &T::AccountId, ledger: &StakingLedger<T::AccountId, BalanceOf<T>, T::BlockNumber>) {
|
||||
T::Currency::set_lock(STAKING_ID, &ledger.stash, ledger.total, T::BlockNumber::max_value(), WithdrawReasons::all());
|
||||
<Ledger<T>>::insert(controller, ledger);
|
||||
}
|
||||
|
||||
/// 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: BalanceOf<T>) {
|
||||
fn slash_validator(stash: &T::AccountId, slash: BalanceOf<T>) {
|
||||
// The exposure (backing stake) information of the validator to be slashed.
|
||||
let exposure = Self::stakers(v);
|
||||
let exposure = Self::stakers(stash);
|
||||
// The amount we are actually going to slash (can't be bigger than their total exposure)
|
||||
let slash = slash.min(exposure.total);
|
||||
// The amount we'll slash from the validator's stash directly.
|
||||
let own_slash = exposure.own.min(slash);
|
||||
let (mut imbalance, missing) = T::Currency::slash(v, own_slash);
|
||||
let (mut imbalance, missing) = T::Currency::slash(stash, own_slash);
|
||||
let own_slash = own_slash - missing;
|
||||
// The amount remaining that we can't slash from the validator, that must be taken from the nominators.
|
||||
let rest_slash = slash - own_slash;
|
||||
@@ -565,17 +789,22 @@ impl<T: Trait> Module<T> {
|
||||
|
||||
/// Actually make a payment to a staker. This uses the currency's reward function
|
||||
/// to pay the right payee for the given staker account.
|
||||
fn make_payout(who: &T::AccountId, amount: BalanceOf<T>) -> Option<PositiveImbalanceOf<T>> {
|
||||
match Self::payee(who) {
|
||||
RewardDestination::Controller => T::Currency::deposit_into_existing(&who, amount).ok(),
|
||||
RewardDestination::Stash => Self::ledger(who)
|
||||
.and_then(|l| T::Currency::deposit_into_existing(&l.stash, amount).ok()),
|
||||
RewardDestination::Staked =>
|
||||
Self::ledger(who).and_then(|mut l| {
|
||||
fn make_payout(stash: &T::AccountId, amount: BalanceOf<T>) -> Option<PositiveImbalanceOf<T>> {
|
||||
let dest = Self::payee(stash);
|
||||
match dest {
|
||||
RewardDestination::Controller => Self::bonded(stash)
|
||||
.and_then(|controller|
|
||||
T::Currency::deposit_into_existing(&controller, amount).ok()
|
||||
),
|
||||
RewardDestination::Stash =>
|
||||
T::Currency::deposit_into_existing(stash, amount).ok(),
|
||||
RewardDestination::Staked => Self::bonded(stash)
|
||||
.and_then(|c| Self::ledger(&c).map(|l| (c, l)))
|
||||
.and_then(|(controller, mut l)| {
|
||||
l.active += amount;
|
||||
l.total += amount;
|
||||
let r = T::Currency::deposit_into_existing(&l.stash, amount).ok();
|
||||
Self::update_ledger(who, l);
|
||||
let r = T::Currency::deposit_into_existing(stash, amount).ok();
|
||||
Self::update_ledger(&controller, &l);
|
||||
r
|
||||
}),
|
||||
}
|
||||
@@ -583,22 +812,23 @@ impl<T: Trait> Module<T> {
|
||||
|
||||
/// Reward a given validator by a specific amount. Add the reward to their, and their nominators'
|
||||
/// balance, pro-rata based on their exposure, after having removed the validator's pre-payout cut.
|
||||
fn reward_validator(who: &T::AccountId, reward: BalanceOf<T>) {
|
||||
let off_the_table = reward.min(Self::validators(who).validator_payment);
|
||||
fn reward_validator(stash: &T::AccountId, reward: BalanceOf<T>) {
|
||||
let off_the_table = reward.min(Self::validators(stash).validator_payment);
|
||||
let reward = reward - off_the_table;
|
||||
let mut imbalance = <PositiveImbalanceOf<T>>::zero();
|
||||
let validator_cut = if reward.is_zero() {
|
||||
Zero::zero()
|
||||
} else {
|
||||
let exposure = Self::stakers(who);
|
||||
let exposure = Self::stakers(stash);
|
||||
let total = exposure.total.max(One::one());
|
||||
let safe_mul_rational = |b| b * reward / total;// FIXME #1572: avoid overflow
|
||||
for i in &exposure.others {
|
||||
imbalance.maybe_subsume(Self::make_payout(&i.who, safe_mul_rational(i.value)));
|
||||
let nom_payout = safe_mul_rational(i.value);
|
||||
imbalance.maybe_subsume(Self::make_payout(&i.who, nom_payout));
|
||||
}
|
||||
safe_mul_rational(exposure.own)
|
||||
};
|
||||
imbalance.maybe_subsume(Self::make_payout(who, validator_cut + off_the_table));
|
||||
imbalance.maybe_subsume(Self::make_payout(stash, validator_cut + off_the_table));
|
||||
T::Reward::on_unbalanced(imbalance);
|
||||
}
|
||||
|
||||
@@ -637,7 +867,7 @@ impl<T: Trait> Module<T> {
|
||||
// Payout
|
||||
let reward = <CurrentEraReward<T>>::take();
|
||||
if !reward.is_zero() {
|
||||
let validators = <session::Module<T>>::validators();
|
||||
let validators = Self::current_elected();
|
||||
for v in validators.iter() {
|
||||
Self::reward_validator(v, reward);
|
||||
}
|
||||
@@ -661,58 +891,71 @@ impl<T: Trait> Module<T> {
|
||||
// Reassign all Stakers.
|
||||
let slot_stake = Self::select_validators();
|
||||
|
||||
// Update the balances for slashing/rewarding according to the stakes.
|
||||
<CurrentOfflineSlash<T>>::put(Self::offline_slash() * slot_stake);
|
||||
// Update the balances for rewarding according to the stakes.
|
||||
<CurrentSessionReward<T>>::put(Self::session_reward() * slot_stake);
|
||||
}
|
||||
|
||||
fn slashable_balance_of(stash: &T::AccountId) -> BalanceOf<T> {
|
||||
Self::bonded(stash).and_then(Self::ledger).map(|l| l.total).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// 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<T> {
|
||||
// Map of (would-be) validator account to amount of stake backing it.
|
||||
|
||||
let rounds = || <ValidatorCount<T>>::get() as usize;
|
||||
let validators = || <Validators<T>>::enumerate();
|
||||
let nominators = || <Nominators<T>>::enumerate();
|
||||
let stash_of = |w| Self::stash_balance(&w);
|
||||
let min_validator_count = Self::minimum_validator_count() as usize;
|
||||
let elected_candidates = phragmen::elect::<T, _, _, _, _>(
|
||||
let maybe_elected_candidates = elect::<T, _, _, _, _>(
|
||||
rounds,
|
||||
validators,
|
||||
nominators,
|
||||
stash_of,
|
||||
min_validator_count
|
||||
);
|
||||
|
||||
// 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();
|
||||
<SlotStake<T>>::put(&slot_stake);
|
||||
|
||||
// Clear Stakers and reduce their slash_count.
|
||||
for v in <session::Module<T>>::validators().iter() {
|
||||
<Stakers<T>>::remove(v);
|
||||
let slash_count = <SlashCount<T>>::take(v);
|
||||
if slash_count > 1 {
|
||||
<SlashCount<T>>::insert(v, slash_count - 1);
|
||||
Self::slashable_balance_of,
|
||||
min_validator_count,
|
||||
ElectionConfig::<BalanceOf<T>> {
|
||||
equalise: false,
|
||||
tolerance: <BalanceOf<T>>::sa(10 as u64),
|
||||
iterations: 10,
|
||||
}
|
||||
}
|
||||
|
||||
// Populate Stakers.
|
||||
for candidate in &elected_candidates {
|
||||
<Stakers<T>>::insert(candidate.who.clone(), candidate.exposure.clone());
|
||||
}
|
||||
|
||||
// Set the new validator set.
|
||||
<session::Module<T>>::set_validators(
|
||||
&elected_candidates.into_iter().map(|i| i.who).collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
slot_stake
|
||||
|
||||
if let Some(elected_candidates) = maybe_elected_candidates {
|
||||
// Clear Stakers and reduce their slash_count.
|
||||
for v in Self::current_elected().iter() {
|
||||
<Stakers<T>>::remove(v);
|
||||
let slash_count = <SlashCount<T>>::take(v);
|
||||
if slash_count > 1 {
|
||||
<SlashCount<T>>::insert(v, slash_count - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
<Stakers<T>>::insert(c.who.clone(), c.exposure.clone());
|
||||
}
|
||||
<SlotStake<T>>::put(&slot_stake);
|
||||
|
||||
// Set the new validator set.
|
||||
let elected_stashes = elected_candidates.into_iter().map(|i| i.who).collect::<Vec<_>>();
|
||||
<CurrentElected<T>>::put(&elected_stashes);
|
||||
<session::Module<T>>::set_validators(
|
||||
&elected_stashes.into_iter().map(|s| Self::bonded(s).unwrap_or_default()).collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
slot_stake
|
||||
} else {
|
||||
// There were not enough candidates for even our minimal level of functionality.
|
||||
// This is bad.
|
||||
// We should probably disable all functionality except for block production
|
||||
// and let the chain keep producing blocks until we can decide on a sufficiently
|
||||
// substantial set.
|
||||
Self::slot_stake()
|
||||
}
|
||||
}
|
||||
|
||||
/// Call when a validator is determined to be offline. `count` is the
|
||||
@@ -724,10 +967,6 @@ impl<T: Trait> Module<T> {
|
||||
if Self::invulnerables().contains(&v) {
|
||||
return
|
||||
}
|
||||
// TODO: remove once Alex/CC updated #1785
|
||||
if Self::invulerables().contains(&v) {
|
||||
return
|
||||
}
|
||||
|
||||
let slash_count = Self::slash_count(&v);
|
||||
let new_slash_count = slash_count + count as u32;
|
||||
@@ -753,13 +992,14 @@ impl<T: Trait> Module<T> {
|
||||
let max_slashes = grace + unstake_threshold;
|
||||
|
||||
let event = if new_slash_count > max_slashes {
|
||||
let slot_stake = Self::slot_stake();
|
||||
let slash_exposure = Self::stakers(&v).total;
|
||||
let offline_slash_base = Self::offline_slash() * slash_exposure;
|
||||
// They're bailing.
|
||||
let slash = Self::current_offline_slash()
|
||||
// Multiply current_offline_slash by 2^(unstake_threshold with upper bound)
|
||||
let slash = offline_slash_base
|
||||
// Multiply slash_mantissa by 2^(unstake_threshold with upper bound)
|
||||
.checked_shl(unstake_threshold)
|
||||
.map(|x| x.min(slot_stake))
|
||||
.unwrap_or(slot_stake);
|
||||
.map(|x| x.min(slash_exposure))
|
||||
.unwrap_or(slash_exposure);
|
||||
let _ = Self::slash_validator(&v, slash);
|
||||
<Validators<T>>::remove(&v);
|
||||
let _ = Self::apply_force_new_era(false);
|
||||
@@ -780,14 +1020,14 @@ impl<T: Trait> OnSessionChange<T::Moment> for Module<T> {
|
||||
}
|
||||
|
||||
impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> {
|
||||
fn on_free_balance_zero(who: &T::AccountId) {
|
||||
if let Some(controller) = <Bonded<T>>::take(who) {
|
||||
fn on_free_balance_zero(stash: &T::AccountId) {
|
||||
if let Some(controller) = <Bonded<T>>::take(stash) {
|
||||
<Ledger<T>>::remove(&controller);
|
||||
<Payee<T>>::remove(&controller);
|
||||
<SlashCount<T>>::remove(&controller);
|
||||
<Validators<T>>::remove(&controller);
|
||||
<Nominators<T>>::remove(&controller);
|
||||
}
|
||||
<Payee<T>>::remove(stash);
|
||||
<SlashCount<T>>::remove(stash);
|
||||
<Validators<T>>::remove(stash);
|
||||
<Nominators<T>>::remove(stash);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -84,12 +84,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 {
|
||||
@@ -99,12 +99,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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,16 +126,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.
|
||||
pub fn validator_pool(mut self, validator_pool: bool) -> Self {
|
||||
self.validator_pool = validator_pool;
|
||||
self
|
||||
}
|
||||
@@ -152,6 +143,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<Blake2Hasher> {
|
||||
let (mut t, mut c) = system::GenesisConfig::<Test>::default().build_storage().unwrap();
|
||||
let balance_factor = if self.existential_deposit > 0 {
|
||||
@@ -170,34 +165,22 @@ impl ExtBuilder {
|
||||
keys: vec![],
|
||||
}.assimilate_storage(&mut t, &mut c);
|
||||
let _ = balances::GenesisConfig::<Test>{
|
||||
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),
|
||||
],
|
||||
transaction_base_fee: 0,
|
||||
transaction_byte_fee: 0,
|
||||
existential_deposit: self.existential_deposit,
|
||||
@@ -211,27 +194,26 @@ impl ExtBuilder {
|
||||
stakers: if self.validator_pool {
|
||||
vec![
|
||||
(11, 10, balance_factor * 1000, StakerStatus::<AccountIdType>::Validator),
|
||||
(21, 20, balance_factor * 2000, StakerStatus::<AccountIdType>::Validator),
|
||||
(31, 30, balance_factor * 3000, if self.validator_pool { StakerStatus::<AccountIdType>::Validator } else { StakerStatus::<AccountIdType>::Idle }),
|
||||
(41, 40, balance_factor * 4000, if self.validator_pool { StakerStatus::<AccountIdType>::Validator } else { StakerStatus::<AccountIdType>::Idle }),
|
||||
(21, 20, balance_factor * if self.fare { 1000 } else { 2000 }, StakerStatus::<AccountIdType>::Validator),
|
||||
(31, 30, balance_factor * 1000, if self.validator_pool { StakerStatus::<AccountIdType>::Validator } else { StakerStatus::<AccountIdType>::Idle }),
|
||||
(41, 40, balance_factor * 1000, if self.validator_pool { StakerStatus::<AccountIdType>::Validator } else { StakerStatus::<AccountIdType>::Idle }),
|
||||
// nominator
|
||||
(101, 100, balance_factor * 500, if self.nominate { StakerStatus::<AccountIdType>::Nominator(vec![10, 20]) } else { StakerStatus::<AccountIdType>::Nominator(vec![]) })
|
||||
(101, 100, balance_factor * 500, if self.nominate { StakerStatus::<AccountIdType>::Nominator(vec![11, 21]) } else { StakerStatus::<AccountIdType>::Nominator(vec![]) })
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
(11, 10, balance_factor * 1000, StakerStatus::<AccountIdType>::Validator),
|
||||
(21, 20, balance_factor * 2000, StakerStatus::<AccountIdType>::Validator),
|
||||
(21, 20, balance_factor * if self.fare { 1000 } else { 2000 }, StakerStatus::<AccountIdType>::Validator),
|
||||
// nominator
|
||||
(101, 100, balance_factor * 500, if self.nominate { StakerStatus::<AccountIdType>::Nominator(vec![10, 20]) } else { StakerStatus::<AccountIdType>::Nominator(vec![]) })
|
||||
(101, 100, balance_factor * 500, if self.nominate { StakerStatus::<AccountIdType>::Nominator(vec![11, 21]) } else { StakerStatus::<AccountIdType>::Nominator(vec![]) })
|
||||
]
|
||||
},
|
||||
validator_count: self.validator_count,
|
||||
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(5),
|
||||
current_session_reward: self.reward,
|
||||
current_offline_slash: 20,
|
||||
offline_slash_grace: 0,
|
||||
invulnerables: vec![],
|
||||
}.assimilate_storage(&mut t, &mut c);
|
||||
|
||||
@@ -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
|
||||
@@ -16,208 +16,342 @@
|
||||
|
||||
//! Rust implementation of the Phragmén election algorithm.
|
||||
|
||||
use rstd::{prelude::*};
|
||||
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<Balance: HasCompact> {
|
||||
// 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)]
|
||||
#[derive(Clone, Encode, Decode, Default)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub struct Candidate<AccountId, Balance: HasCompact> {
|
||||
// The validator's account
|
||||
pub who: AccountId,
|
||||
// Exposure struct, holding info about the value that the validator has in stake.
|
||||
pub exposure: Exposure<AccountId, Balance>,
|
||||
// Intermediary value used to sort candidates.
|
||||
pub score: Perquintill,
|
||||
// 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,
|
||||
// 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)]
|
||||
#[derive(Clone, Encode, Decode, Default)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub struct Nominations<AccountId, Balance: HasCompact> {
|
||||
pub struct Nominator<AccountId, Balance: HasCompact> {
|
||||
// The nominator's account.
|
||||
who: AccountId,
|
||||
// List of validators proposed by this nominator.
|
||||
nominees: Vec<Vote<AccountId, Balance>>,
|
||||
edges: Vec<Edge<AccountId, Balance>>,
|
||||
// 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)]
|
||||
#[derive(Clone, Encode, Decode, Default)]
|
||||
#[cfg_attr(feature = "std", derive(Debug))]
|
||||
pub struct Vote<AccountId, Balance: HasCompact> {
|
||||
pub struct Edge<AccountId, Balance: HasCompact> {
|
||||
// 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_index: 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<T: Trait + 'static, FR, FN, FV, FS>(
|
||||
get_rounds: FR,
|
||||
get_validators: FV,
|
||||
get_nominators: FN,
|
||||
stash_of: FS,
|
||||
minimum_validator_count: usize,
|
||||
) -> Vec<Candidate<T::AccountId, BalanceOf<T>>> where
|
||||
FR: Fn() -> usize,
|
||||
FV: Fn() -> Box<dyn Iterator<
|
||||
Item =(T::AccountId, ValidatorPrefs<BalanceOf<T>>)
|
||||
>>,
|
||||
FN: Fn() -> Box<dyn Iterator<
|
||||
Item =(T::AccountId, Vec<T::AccountId>)
|
||||
>>,
|
||||
FS: Fn(T::AccountId) -> BalanceOf<T>,
|
||||
config: ElectionConfig<BalanceOf<T>>,
|
||||
) -> Option<Vec<Candidate<T::AccountId, BalanceOf<T>>>> where
|
||||
FR: Fn() -> usize,
|
||||
FV: Fn() -> Box<dyn Iterator<
|
||||
Item =(T::AccountId, ValidatorPrefs<BalanceOf<T>>)
|
||||
>>,
|
||||
FN: Fn() -> Box<dyn Iterator<
|
||||
Item =(T::AccountId, Vec<T::AccountId>)
|
||||
>>,
|
||||
for <'r> FS: Fn(&'r T::AccountId) -> BalanceOf<T>,
|
||||
{
|
||||
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::<T>::zero(),
|
||||
score: Perquintill::zero(),
|
||||
exposure: Exposure { total: stash_balance, own: stash_balance, others: vec![] },
|
||||
..Default::default()
|
||||
}
|
||||
}).collect::<Vec<Candidate<T::AccountId, BalanceOf<T>>>>();
|
||||
|
||||
// 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<Nominator<T::AccountId, BalanceOf<T>>> = 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_index: 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<Edge<T::AccountId, BalanceOf<T>>> = 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_index: idx, ..Default::default() });
|
||||
}
|
||||
}
|
||||
|
||||
Nominations {
|
||||
Nominator {
|
||||
who,
|
||||
nominees: nominees.into_iter()
|
||||
.map(|n| Vote {who: n, load: Perquintill::zero(), backing_stake: BalanceOf::<T>::zero()})
|
||||
.collect::<Vec<Vote<T::AccountId, BalanceOf<T>>>>(),
|
||||
stake: nominator_stake,
|
||||
load : Perquintill::zero(),
|
||||
edges: edges,
|
||||
budget: nominator_stake,
|
||||
load: Perquintill::zero(),
|
||||
}
|
||||
}).collect::<Vec<Nominations<T::AccountId, BalanceOf<T>>>>();
|
||||
|
||||
}));
|
||||
|
||||
|
||||
// 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::<T>::zero())
|
||||
.collect::<Vec<Candidate<T::AccountId, BalanceOf<T>>>>();
|
||||
|
||||
// 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_index];
|
||||
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 = <BalanceOf<T> as As<u64>>::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;
|
||||
// NOTE: for now, always divide last to avoid collapse to zero.
|
||||
e.backing_stake = <BalanceOf<T>>::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 = <BalanceOf<T>>::zero();
|
||||
nominators.iter_mut().for_each(|mut n| {
|
||||
let diff = equalise::<T>(&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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// if we have less than minimum, use the previous validator set.
|
||||
elected_candidates = original_candidates;
|
||||
return None
|
||||
}
|
||||
}
|
||||
Some(elected_candidates)
|
||||
}
|
||||
|
||||
elected_candidates
|
||||
}
|
||||
pub fn equalise<T: Trait + 'static>(
|
||||
nominator: &mut Nominator<T::AccountId, BalanceOf<T>>,
|
||||
elected_candidates: &mut Vec<Candidate<T::AccountId, BalanceOf<T>>>,
|
||||
tolerance: BalanceOf<T>
|
||||
) -> BalanceOf<T> {
|
||||
|
||||
let mut elected_edges = nominator.edges
|
||||
.iter_mut()
|
||||
.filter(|e| e.elected)
|
||||
.collect::<Vec<&mut Edge<T::AccountId, BalanceOf<T>>>>();
|
||||
if elected_edges.len() == 0 { return <BalanceOf<T>>::zero(); }
|
||||
let stake_used = elected_edges
|
||||
.iter()
|
||||
.fold(<BalanceOf<T>>::zero(), |s, e| s + e.backing_stake);
|
||||
let backed_stakes = elected_edges
|
||||
.iter()
|
||||
.map(|e| elected_candidates[e.elected_idx].backing_stake)
|
||||
.collect::<Vec<BalanceOf<T>>>();
|
||||
let backing_backed_stake = elected_edges
|
||||
.iter()
|
||||
.filter(|e| e.backing_stake > <BalanceOf<T>>::zero())
|
||||
.map(|e| elected_candidates[e.elected_idx].backing_stake)
|
||||
.collect::<Vec<BalanceOf<T>>>();
|
||||
|
||||
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");
|
||||
let min_stake = *backed_stakes
|
||||
.iter()
|
||||
.min()
|
||||
.expect("vector with positive length will have a max; qed");
|
||||
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| {
|
||||
// NOTE: no assertions in the runtime, but this should nonetheless be indicative.
|
||||
//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 = <BalanceOf<T>>::zero();
|
||||
});
|
||||
|
||||
elected_edges.sort_unstable_by_key(|e| elected_candidates[e.elected_idx].backing_stake);
|
||||
|
||||
let mut cumulative_stake = <BalanceOf<T>>::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(&<BalanceOf<T>>::sa(idx as u64)).unwrap_or(<BalanceOf<T>>::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 * <BalanceOf<T>>::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 / <BalanceOf<T>>::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
|
||||
}
|
||||
|
||||
+721
-417
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user