From 82925e4091eb76a819eac4899ebfee3d6e0da1d6 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 3 Dec 2019 17:36:29 +0800 Subject: [PATCH] fix: https://github.com/darwinia-network/darwinia/issues/103, https://github.com/darwinia-network/darwinia/issues/129 --- srml/staking/src/inflation.rs | 32 ++++++++++++++++++++------------ srml/staking/src/lib.rs | 16 ++++++---------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/srml/staking/src/inflation.rs b/srml/staking/src/inflation.rs index b18a82f..8a17b79 100644 --- a/srml/staking/src/inflation.rs +++ b/srml/staking/src/inflation.rs @@ -1,29 +1,29 @@ -use sr_primitives::Perbill; - -use sr_primitives::traits::{IntegerSquareRoot, SaturatedConversion}; - -/// utility in staking -use crate::{KtonBalanceOf, RingBalanceOf, Trait}; use rstd::convert::TryInto; +use sr_primitives::{ + traits::{IntegerSquareRoot, SaturatedConversion}, + Perbill, +}; use substrate_primitives::U256; -// 1 - (99 /100)^sqrt(year) +use super::{KtonBalanceOf, RingBalanceOf, TimeStamp, Trait}; + +// 1 - (99 / 100) ^ sqrt(year) // () -> RingBalanceOf pub fn compute_total_payout( - era_duration: u64, - living_time: u64, + era_duration: TimeStamp, + living_time: TimeStamp, total_left: u128, ) -> (RingBalanceOf, RingBalanceOf) { // Milliseconds per year for the Julian year (365.25 days). - const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100; + const MILLISECONDS_PER_YEAR: TimeStamp = 1000 * 3600 * 24 * 36525 / 100; let year: u32 = (living_time / MILLISECONDS_PER_YEAR + 1).saturated_into::(); - let portion = Perbill::from_rational_approximation(era_duration as u64, MILLISECONDS_PER_YEAR); + let portion = Perbill::from_rational_approximation(era_duration, MILLISECONDS_PER_YEAR); let maximum = portion * total_left; - // let maximum = maximum - maximum * 99_u128.pow(year.integer_sqrt()) / 100_u128.pow(year.integer_sqrt()); + let maximum = maximum - maximum * 99_u128.pow(year.integer_sqrt()) / 100_u128.pow(year.integer_sqrt()); let payout = maximum * 1; // TODO: add treasury ratio: Perbill::from_rational_approximation(npos_token_staked, total_tokens); @@ -59,3 +59,11 @@ pub fn compute_kton_return(value: RingBalanceOf, months: u32) -> Kt / U256::from(1_970_000); res.as_u128().try_into().unwrap_or_default() } + +#[cfg(any(feature = "std", test))] +mod test { + // use super::*; + + #[test] + fn _test() {} +} diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 661a046..d71ed44 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -47,16 +47,11 @@ use darwinia_support::{ }; use phragmen::{build_support_map, elect, equalize, ExtendedBalance, PhragmenStakedAssignment}; -#[allow(unused)] -#[cfg(any(feature = "bench", test))] +#[cfg(test)] mod mock; -// #[cfg(test)] mod tests; -//#[cfg(all(feature = "bench", test))] -//mod benches; - const DEFAULT_MINIMUM_VALIDATOR_COUNT: u32 = 4; const MAX_NOMINATIONS: usize = 16; const MAX_UNSTAKE_THRESHOLD: u32 = 10; @@ -999,8 +994,9 @@ impl Module { // from the nearest expire time if !value_left.is_zero() { // sorted by expire_time from far to near - deposit_items - .sort_unstable_by_key(|item| u64::max_value() - item.expire_time.saturated_into::()); + deposit_items.sort_unstable_by_key(|item| { + TimeStamp::max_value() - item.expire_time.saturated_into::() + }); deposit_items.drain_filter(|item| { if value_left.is_zero() { return false; @@ -1084,8 +1080,8 @@ impl Module { let total_left: u128 = (T::Cap::get() - T::Ring::total_issuance()).saturated_into::(); let (total_payout, max_payout) = inflation::compute_total_payout::( - era_duration.saturated_into::(), - (T::GenesisTime::get() - T::Time::now()).saturated_into::(), + era_duration.saturated_into::(), + (T::Time::now() - T::GenesisTime::get()).saturated_into::(), total_left, );