Fix Staking Accuracy issues. (#2180)

* Nasty patch for election

* cleanup

* a few more comments.

* minor fixes

* Improve comment
This commit is contained in:
Kian Peymani
2019-04-02 17:57:57 +02:00
committed by Gav Wood
parent f0c21815b3
commit b7eeb28de9
3 changed files with 111 additions and 4 deletions
+7 -2
View File
@@ -24,6 +24,7 @@ use crate::{Exposure, BalanceOf, Trait, ValidatorPrefs, IndividualExposure};
type Fraction = PerU128;
type ExtendedBalance = u128;
const SCALE_FACTOR_64: u128 = u64::max_value() as u128 + 1;
/// Configure the behavior of the Phragmen election.
/// Might be deprecated.
@@ -178,7 +179,12 @@ pub fn elect<T: Trait + 'static, FV, FN, FS>(
for e in &n.edges {
let c = &mut candidates[e.candidate_index];
if !c.elected && !c.approval_stake.is_zero() {
let temp = n.budget.saturating_mul(*n.load) / c.approval_stake;
let temp =
// Basic fixed-point shifting by 64.
// This will never saturate since n.budget cannot exceed u64,despite being stored in u128.
// Note that left-associativity in operators precedence is crucially important here.
n.budget.saturating_mul(SCALE_FACTOR_64) / c.approval_stake
* (*n.load / SCALE_FACTOR_64);
c.score = Fraction::from_max_value((*c.score).saturating_add(temp));
}
}
@@ -214,7 +220,6 @@ pub fn elect<T: Trait + 'static, FV, FN, FS>(
// 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 == e.who) {
e.elected = true;
// NOTE: for now, always divide last to avoid collapse to zero.
e.backing_stake = n.budget.saturating_mul(*e.load) / n.load.max(1);
c.backing_stake = c.backing_stake.saturating_add(e.backing_stake);
if c.who != n.who {