Skip to content

Commit

Permalink
Merge pull request #299 from AurevoirXavier/canary
Browse files Browse the repository at this point in the history
Check Overflow
  • Loading branch information
hackfisher authored Feb 27, 2020
2 parents 2e5992c + 5b85020 commit 5c87576
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
4 changes: 2 additions & 2 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1887,8 +1887,8 @@ impl<T: Trait> Module<T> {
let validators = Self::current_elected();
let (total_payout, max_payout) = inflation::compute_total_payout::<T>(
era_duration,
T::Time::now() - T::GenesisTime::get(),
T::Cap::get() - T::RingCurrency::total_issuance(),
now - T::GenesisTime::get(),
T::Cap::get().saturating_sub(T::RingCurrency::total_issuance()),
PayoutFraction::get(),
);
let mut total_imbalance = <RingPositiveImbalance<T>>::zero();
Expand Down
13 changes: 5 additions & 8 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//! Test utilities

use std::{
cell::RefCell,
collections::HashSet,
time::{SystemTime, UNIX_EPOCH},
};
use std::{cell::RefCell, collections::HashSet};

use frame_support::{
assert_ok, impl_outer_origin, parameter_types,
Expand All @@ -28,7 +24,7 @@ use crate::*;

pub type AccountId = u64;
pub type BlockNumber = u64;
pub type Balance = u64;
pub type Balance = u128;

pub type System = system::Module<Test>;
pub type Session = pallet_session::Module<Test>;
Expand All @@ -43,7 +39,8 @@ pub const MICRO: Balance = 1_000 * NANO;
pub const MILLI: Balance = 1_000 * MICRO;
pub const COIN: Balance = 1_000 * MILLI;

pub const CAP: Balance = 1_000_000_000 * COIN;
pub const CAP: Balance = 10_000_000_000 * COIN;
pub const GENESIS_TIME: Moment = 0;
pub const TOTAL_POWER: Power = 1_000_000_000;

thread_local! {
Expand Down Expand Up @@ -203,7 +200,7 @@ parameter_types! {

pub const Cap: Balance = CAP;
pub const TotalPower: Power = TOTAL_POWER;
pub const GenesisTime: Moment = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as _;
pub const GenesisTime: Moment = GENESIS_TIME;
}
impl Trait for Test {
type Time = pallet_timestamp::Module<Self>;
Expand Down
50 changes: 26 additions & 24 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Tests for the module.

use substrate_test_utils::assert_eq_uvec;

use crate::{mock::*, *};

// #[test]
Expand Down Expand Up @@ -1771,30 +1773,30 @@ use crate::{mock::*, *};
// check_nominator_all();
// })
// }
//
// #[test]
// fn phragmen_should_not_overflow_validators() {
// ExtBuilder::default().nominate(false).build().execute_with(|| {
// let _ = Staking::chill(Origin::signed(10));
// let _ = Staking::chill(Origin::signed(20));
//
// bond_validator(2, u64::max_value());
// bond_validator(4, u64::max_value());
//
// bond_nominator(6, u64::max_value() / 2, vec![3, 5]);
// bond_nominator(8, u64::max_value() / 2, vec![3, 5]);
//
// start_era(1);
//
// assert_eq_uvec!(validator_controllers(), vec![4, 2]);
//
// // This test will fail this. Will saturate.
// // check_exposure_all();
// assert_eq!(Staking::stakers(3).total, u64::max_value());
// assert_eq!(Staking::stakers(5).total, u64::max_value());
// })
// }
//

#[test]
fn phragmen_should_not_overflow_validators() {
ExtBuilder::default().nominate(false).build().execute_with(|| {
let _ = Staking::chill(Origin::signed(10));
let _ = Staking::chill(Origin::signed(20));

bond_validator(2, StakingBalance::RingBalance(CAP - 1));
bond_validator(4, StakingBalance::KtonBalance(CAP - 1));

bond_nominator(6, StakingBalance::RingBalance(1), vec![3, 5]);
bond_nominator(8, StakingBalance::KtonBalance(1), vec![3, 5]);

start_era(1);

assert_eq_uvec!(validator_controllers(), vec![4, 2]);

// This test will fail this. Will saturate.
// check_exposure_all();
assert_eq!(Staking::stakers(3).total_power, TOTAL_POWER / 2);
assert_eq!(Staking::stakers(5).total_power, TOTAL_POWER / 2);
})
}

// #[test]
// fn phragmen_should_not_overflow_nominators() {
// ExtBuilder::default().nominate(false).build().execute_with(|| {
Expand Down
8 changes: 4 additions & 4 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ pub mod structs {
Ordering::Less
} else {
// Don't even compute gcd.
let self_n = self.0 as u64 * other.1 as u64;
let other_n = other.0 as u64 * self.1 as u64;
let self_n = self.0 as u128 * other.1 as u128;
let other_n = other.0 as u128 * self.1 as u128;
self_n.cmp(&other_n)
}
}
Expand All @@ -206,8 +206,8 @@ pub mod structs {
if self.1 == other.1 {
self.0.eq(&other.0)
} else {
let self_n = self.0 as u64 * other.1 as u64;
let other_n = other.0 as u64 * self.1 as u64;
let self_n = self.0 as u128 * other.1 as u128;
let other_n = other.0 as u128 * self.1 as u128;
self_n.eq(&other_n)
}
}
Expand Down

0 comments on commit 5c87576

Please sign in to comment.