diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 28e1ab042..c2bcc0faa 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -1887,8 +1887,8 @@ impl Module { let validators = Self::current_elected(); let (total_payout, max_payout) = inflation::compute_total_payout::( 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 = >::zero(); diff --git a/frame/staking/src/mock.rs b/frame/staking/src/mock.rs index 94a56d2d9..4e88b3061 100644 --- a/frame/staking/src/mock.rs +++ b/frame/staking/src/mock.rs @@ -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, @@ -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; pub type Session = pallet_session::Module; @@ -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! { @@ -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; diff --git a/frame/staking/src/tests.rs b/frame/staking/src/tests.rs index 8c3c380a8..a4bb3b4f7 100644 --- a/frame/staking/src/tests.rs +++ b/frame/staking/src/tests.rs @@ -1,5 +1,7 @@ //! Tests for the module. +use substrate_test_utils::assert_eq_uvec; + use crate::{mock::*, *}; // #[test] @@ -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(|| { diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index 8fec80ee9..4a000204f 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -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) } } @@ -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) } }