diff --git a/substrate/node/runtime/Cargo.toml b/substrate/node/runtime/Cargo.toml index 9424788e2f..755c4afe84 100644 --- a/substrate/node/runtime/Cargo.toml +++ b/substrate/node/runtime/Cargo.toml @@ -6,45 +6,47 @@ edition = "2018" build = "build.rs" [dependencies] -integer-sqrt = { version = "0.1.2" } -safe-mix = { version = "1.0", default-features = false } codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } -primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false } +integer-sqrt = { version = "0.1.2" } +rustc-hex = { version = "2.0", optional = true } +safe-mix = { version = "1.0", default-features = false } +serde = { version = "1.0", optional = true } + +authority-discovery-primitives = { package = "substrate-authority-discovery-primitives", path = "../../core/authority-discovery/primitives", default-features = false } +babe-primitives = { package = "substrate-consensus-babe-primitives", path = "../../core/consensus/babe/primitives", default-features = false } client = { package = "substrate-client", path = "../../core/client", default-features = false } +node-primitives = { path = "../primitives", default-features = false } +offchain-primitives = { package = "substrate-offchain-primitives", path = "../../core/offchain/primitives", default-features = false } +primitives = { package = "substrate-primitives", path = "../../core/primitives", default-features = false } rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false } sr-primitives = { path = "../../core/sr-primitives", default-features = false } sr-staking-primitives = { path = "../../core/sr-staking-primitives", default-features = false } -offchain-primitives = { package = "substrate-offchain-primitives", path = "../../core/offchain/primitives", default-features = false } -authority-discovery-primitives = { package = "substrate-authority-discovery-primitives", path = "../../core/authority-discovery/primitives", default-features = false } +substrate-keyring = { path = "../../core/keyring", optional = true } +substrate-session = { path = "../../core/session", default-features = false } version = { package = "sr-version", path = "../../core/sr-version", default-features = false } -support = { package = "srml-support", path = "../../srml/support", default-features = false } + +authority-discovery = { package = "srml-authority-discovery", path = "../../srml/authority-discovery", default-features = false } authorship = { package = "srml-authorship", path = "../../srml/authorship", default-features = false } babe = { package = "srml-babe", path = "../../srml/babe", default-features = false } -babe-primitives = { package = "substrate-consensus-babe-primitives", path = "../../core/consensus/babe/primitives", default-features = false } balances = { package = "srml-balances", path = "../../srml/balances", default-features = false } -contracts = { package = "srml-contracts", path = "../../srml/contracts", default-features = false } collective = { package = "srml-collective", path = "../../srml/collective", default-features = false } +contracts = { package = "srml-contracts", path = "../../srml/contracts", default-features = false } democracy = { package = "srml-democracy", path = "../../srml/democracy", default-features = false } elections = { package = "srml-elections", path = "../../srml/elections", default-features = false } executive = { package = "srml-executive", path = "../../srml/executive", default-features = false } finality-tracker = { package = "srml-finality-tracker", path = "../../srml/finality-tracker", default-features = false } grandpa = { package = "srml-grandpa", path = "../../srml/grandpa", default-features = false } +im-online = { package = "srml-im-online", path = "../../srml/im-online", default-features = false } indices = { package = "srml-indices", path = "../../srml/indices", default-features = false } membership = { package = "srml-membership", path = "../../srml/membership", default-features = false } +offences = { package = "srml-offences", path = "../../srml/offences", default-features = false } session = { package = "srml-session", path = "../../srml/session", default-features = false, features = ["historical"] } staking = { package = "srml-staking", path = "../../srml/staking", default-features = false } +sudo = { package = "srml-sudo", path = "../../srml/sudo", default-features = false } +support = { package = "srml-support", path = "../../srml/support", default-features = false } system = { package = "srml-system", path = "../../srml/system", default-features = false } timestamp = { package = "srml-timestamp", path = "../../srml/timestamp", default-features = false } treasury = { package = "srml-treasury", path = "../../srml/treasury", default-features = false } -sudo = { package = "srml-sudo", path = "../../srml/sudo", default-features = false } -im-online = { package = "srml-im-online", path = "../../srml/im-online", default-features = false } -authority-discovery = { package = "srml-authority-discovery", path = "../../srml/authority-discovery", default-features = false } -offences = { package = "srml-offences", path = "../../srml/offences", default-features = false } -node-primitives = { path = "../primitives", default-features = false } -rustc-hex = { version = "2.0", optional = true } -serde = { version = "1.0", optional = true } -substrate-keyring = { path = "../../core/keyring", optional = true } -substrate-session = { path = "../../core/session", default-features = false } [build-dependencies] wasm-builder-runner = { package = "substrate-wasm-builder-runner", version = "1.0.2", path = "../../core/utils/wasm-builder-runner" } diff --git a/substrate/node/runtime/src/lib.rs b/substrate/node/runtime/src/lib.rs index 16a4b551cb..4a1df077c4 100644 --- a/substrate/node/runtime/src/lib.rs +++ b/substrate/node/runtime/src/lib.rs @@ -82,7 +82,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to equal spec_version. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 155, + spec_version: 156, impl_version: 156, apis: RUNTIME_API_VERSIONS, }; diff --git a/substrate/srml/staking/src/inflation.rs b/substrate/srml/staking/src/inflation.rs index 80065886d7..d39e16471b 100644 --- a/substrate/srml/staking/src/inflation.rs +++ b/substrate/srml/staking/src/inflation.rs @@ -97,8 +97,7 @@ struct PiecewiseLinear { impl PiecewiseLinear { /// Compute `f(n/d)*d`. This is useful to avoid loss of precision. - fn calculate_for_fraction_times_denominator(&self, n: N, d: N) -> N - where + fn calculate_for_fraction_times_denominator(&self, n: N, d: N) -> N where N: SimpleArithmetic + Clone { let part = self.pieces.iter() @@ -145,11 +144,10 @@ const I_NPOS: PiecewiseLinear = PiecewiseLinear { ] }; -/// Second per year for the Julian year (365.25 days). -const SECOND_PER_YEAR: u32 = 3600*24*36525/100; - /// The total payout to all validators (and their nominators) per era. /// +/// `era_duration` is expressed in millisecond. +/// /// Named P_NPoS in the [paper](http://research.web3.foundation/en/latest/polkadot/Token%20Ec /// onomics/#inflation-model). /// @@ -157,13 +155,14 @@ const SECOND_PER_YEAR: u32 = 3600*24*36525/100; /// i.e. `P_NPoS(x) = I_NPoS(x) * current_total_token * era_duration / year_duration` /// /// I_NPoS is the desired yearly inflation rate for nominated proof of stake. -pub fn compute_total_payout(npos_token_staked: N, total_tokens: N, era_duration: N) -> N -where +pub fn compute_total_payout(npos_token_staked: N, total_tokens: N, era_duration: u64) -> N where N: SimpleArithmetic + Clone { - let year_duration: N = SECOND_PER_YEAR.into(); - I_NPOS.calculate_for_fraction_times_denominator(npos_token_staked, total_tokens) - * era_duration / year_duration + // Milliseconds per year for the Julian year (365.25 days). + const MILLISECONDS_PER_YEAR: u64 = 1000 * 3600 * 24 * 36525 / 100; + + Perbill::from_rational_approximation(era_duration as u64, MILLISECONDS_PER_YEAR) + * I_NPOS.calculate_for_fraction_times_denominator(npos_token_staked, total_tokens) } #[allow(non_upper_case_globals, non_snake_case)] // To stick with paper notations @@ -182,12 +181,12 @@ mod test_inflation { fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Self { LinearFloat { a: (y1 - y0) / (x1 - x0), - b: (x0*y1 - x1*y0) / (x0 - x1), + b: (x0 * y1 - x1 * y0) / (x0 - x1), } } fn compute(&self, x: f64) -> f64 { - self.a*x + self.b + self.a * x + self.b } } @@ -204,12 +203,12 @@ mod test_inflation { // Left part from `x_ideal` fn I_left(x: f64) -> f64 { - I_0 + x * (i_ideal - I_0/x_ideal) + I_0 + x * (i_ideal - I_0 / x_ideal) } // Right part from `x_ideal` fn I_right(x: f64) -> f64 { - I_0 + (i_ideal*x_ideal - I_0) * 2_f64.powf((x_ideal-x)/d) + I_0 + (i_ideal * x_ideal - I_0) * 2_f64.powf((x_ideal - x) / d) } // Definition of I_NPoS in float @@ -221,6 +220,41 @@ mod test_inflation { } } + #[test] + fn npos_curve_is_sensible() { + const YEAR: u64 = 365 * 24 * 60 * 60 * 1000; + //super::I_NPOS.calculate_for_fraction_times_denominator(25, 100) + assert_eq!(super::compute_total_payout(0, 100_000u64, YEAR), 2_498); + assert_eq!(super::compute_total_payout(5_000, 100_000u64, YEAR), 3_247); + assert_eq!(super::compute_total_payout(25_000, 100_000u64, YEAR), 6_245); + assert_eq!(super::compute_total_payout(40_000, 100_000u64, YEAR), 8_494); + assert_eq!(super::compute_total_payout(50_000, 100_000u64, YEAR), 9_993); + assert_eq!(super::compute_total_payout(60_000, 100_000u64, YEAR), 4_380); + assert_eq!(super::compute_total_payout(75_000, 100_000u64, YEAR), 2_735); + assert_eq!(super::compute_total_payout(95_000, 100_000u64, YEAR), 2_518); + assert_eq!(super::compute_total_payout(100_000, 100_000u64, YEAR), 2_505); + + const DAY: u64 = 24 * 60 * 60 * 1000; + assert_eq!(super::compute_total_payout(25_000, 100_000u64, DAY), 17); + assert_eq!(super::compute_total_payout(50_000, 100_000u64, DAY), 27); + assert_eq!(super::compute_total_payout(75_000, 100_000u64, DAY), 7); + + const SIX_HOURS: u64 = 6 * 60 * 60 * 1000; + assert_eq!(super::compute_total_payout(25_000, 100_000u64, SIX_HOURS), 4); + assert_eq!(super::compute_total_payout(50_000, 100_000u64, SIX_HOURS), 6); + assert_eq!(super::compute_total_payout(75_000, 100_000u64, SIX_HOURS), 1); + + const HOUR: u64 = 60 * 60 * 1000; + assert_eq!( + super::compute_total_payout( + 2_500_000_000_000_000_000_000_000_000u128, + 5_000_000_000_000_000_000_000_000_000u128, + HOUR + ), + 57_038_500_000_000_000_000_000 + ); + } + // Compute approximation of I_NPoS into piecewise linear function fn I_NPoS_points() -> super::PiecewiseLinear { let mut points = vec![]; @@ -265,7 +299,7 @@ mod test_inflation { // Test error is not overflowed // Quick test on one point - if (I_right((x0 + next_x1)/2.0) - (y0 + next_y1)/2.0).abs() > GEN_ERROR { + if (I_right((x0 + next_x1) / 2.0) - (y0 + next_y1) / 2.0).abs() > GEN_ERROR { error_overflowed = true; } @@ -298,7 +332,7 @@ mod test_inflation { let pieces: Vec<(u32, super::Linear)> = (0..points.len()-1) .map(|i| { let p0 = points[i]; - let p1 = points[i+1]; + let p1 = points[i + 1]; let linear = LinearFloat::new(p0.0, p0.1, p1.0, p1.1); @@ -333,7 +367,7 @@ mod test_inflation { } /// This test ensure that i_npos piecewise linear approximation is close to the actual function. - /// It does compare the result from a computation in integer of different capcity and in f64. + /// It does compare the result from a computation in integer of different capacity and in f64. #[test] fn i_npos_precision() { const STEP_PRECISION: f64 = 0.000_001; diff --git a/substrate/srml/staking/src/lib.rs b/substrate/srml/staking/src/lib.rs index 837d12960d..3a77e89223 100644 --- a/substrate/srml/staking/src/lib.rs +++ b/substrate/srml/staking/src/lib.rs @@ -292,24 +292,27 @@ const STAKING_ID: LockIdentifier = *b"staking "; /// Counter for the number of eras that have passed. pub type EraIndex = u32; +/// Counter for the number of "reward" points earned by a given validator. +pub type Points = u32; + /// Reward points of an era. Used to split era total payout between validators. #[derive(Encode, Decode, Default)] -pub struct EraRewards { +pub struct EraPoints { /// Total number of points. Equals the sum of reward points for each validator. - total: u32, - /// Reward at one index correspond to reward for validator in current_elected of this index. - /// Thus this reward vec is only valid for one elected set. - rewards: Vec, + total: Points, + /// The reward points earned by a given validator. The index of this vec corresponds to the + /// index into the current validator set. + individual: Vec, } -impl EraRewards { +impl EraPoints { /// Add the reward to the validator at the given index. Index must be valid /// (i.e. `index < current_elected.len()`). fn add_points_to_index(&mut self, index: u32, points: u32) { if let Some(new_total) = self.total.checked_add(points) { self.total = new_total; - self.rewards.resize((index as usize + 1).max(self.rewards.len()), 0); - self.rewards[index as usize] += points; // Addition is less than total + self.individual.resize((index as usize + 1).max(self.individual.len()), 0); + self.individual[index as usize] += points; // Addition is less than total } } } @@ -590,7 +593,7 @@ decl_storage! { pub CurrentEraStartSessionIndex get(current_era_start_session_index): SessionIndex; /// Rewards for the current era. Using indices of current elected set. - CurrentEraRewards get(current_era_reward): EraRewards; + CurrentEraPointsEarned get(current_era_reward): EraPoints; /// The amount of balance actively at stake for each validator slot, currently. /// @@ -1161,7 +1164,7 @@ impl Module { /// get a chance to set their session keys. fn new_era(start_session_index: SessionIndex) -> Option> { // Payout - let rewards = CurrentEraRewards::take(); + let points = CurrentEraPointsEarned::take(); let now = T::Time::now(); let previous_era_start = >::mutate(|v| { rstd::mem::replace(v, now) @@ -1176,21 +1179,22 @@ impl Module { let total_payout = inflation::compute_total_payout( total_rewarded_stake.clone(), T::Currency::total_issuance(), - // Era of duration more than u32::MAX is rewarded as u32::MAX. - >::from(era_duration.saturated_into::()), + // Duration of era; more than u64::MAX is rewarded as u64::MAX. + era_duration.saturated_into::(), ); let mut total_imbalance = >::zero(); - let total_points = rewards.total; - for (v, points) in validators.iter().zip(rewards.rewards.into_iter()) { - if points != 0 { - let reward = multiply_by_rational(total_payout, points, total_points); + for (v, p) in validators.iter().zip(points.individual.into_iter()) { + if p != 0 { + let reward = multiply_by_rational(total_payout, p, points.total); total_imbalance.subsume(Self::reward_validator(v, reward)); } } let total_reward = total_imbalance.peek(); + // assert!(total_reward <= total_payout) + Self::deposit_event(RawEvent::Reward(total_reward)); T::Reward::on_unbalanced(total_imbalance); T::OnRewardMinted::on_dilution(total_reward, total_rewarded_stake); @@ -1376,7 +1380,7 @@ impl Module { /// COMPLEXITY: Complexity is `number_of_validator_to_reward x current_elected_len`. /// If you need to reward lots of validator consider using `reward_by_indices`. pub fn reward_by_ids(validators_points: impl IntoIterator) { - CurrentEraRewards::mutate(|rewards| { + CurrentEraPointsEarned::mutate(|rewards| { let current_elected = >::current_elected(); for (validator, points) in validators_points.into_iter() { if let Some(index) = current_elected.iter() @@ -1396,7 +1400,7 @@ impl Module { // TODO: This can be optimised once #3302 is implemented. let current_elected_len = >::current_elected().len() as u32; - CurrentEraRewards::mutate(|rewards| { + CurrentEraPointsEarned::mutate(|rewards| { for (validator_index, points) in validators_points.into_iter() { if validator_index < current_elected_len { rewards.add_points_to_index(validator_index, points); diff --git a/substrate/srml/staking/src/mock.rs b/substrate/srml/staking/src/mock.rs index 94e2663b57..ace06ed771 100644 --- a/substrate/srml/staking/src/mock.rs +++ b/substrate/srml/staking/src/mock.rs @@ -391,16 +391,16 @@ pub fn assert_is_stash(acc: u64) { pub fn bond_validator(acc: u64, val: u64) { // a = controller // a + 1 = stash - let _ = Balances::make_free_balance_be(&(acc+1), val); - assert_ok!(Staking::bond(Origin::signed(acc+1), acc, val, RewardDestination::Controller)); + let _ = Balances::make_free_balance_be(&(acc + 1), val); + assert_ok!(Staking::bond(Origin::signed(acc + 1), acc, val, RewardDestination::Controller)); assert_ok!(Staking::validate(Origin::signed(acc), ValidatorPrefs::default())); } pub fn bond_nominator(acc: u64, val: u64, target: Vec) { // a = controller // a + 1 = stash - let _ = Balances::make_free_balance_be(&(acc+1), val); - assert_ok!(Staking::bond(Origin::signed(acc+1), acc, val, RewardDestination::Controller)); + let _ = Balances::make_free_balance_be(&(acc + 1), val); + assert_ok!(Staking::bond(Origin::signed(acc + 1), acc, val, RewardDestination::Controller)); assert_ok!(Staking::nominate(Origin::signed(acc), target)); } @@ -414,7 +414,7 @@ pub fn start_session(session_index: SessionIndex) { let session_index = session_index + 1; for i in Session::current_index()..session_index { System::set_block_number((i + 1).into()); - Timestamp::set_timestamp(System::block_number()); + Timestamp::set_timestamp(System::block_number() * 1000); Session::on_initialize(System::block_number()); } @@ -428,7 +428,7 @@ pub fn start_era(era_index: EraIndex) { pub fn current_total_payout_for_duration(duration: u64) -> u64 { let res = inflation::compute_total_payout( - >::slot_stake()*2, + >::slot_stake() * 2, Balances::total_issuance(), duration, ); diff --git a/substrate/srml/staking/src/tests.rs b/substrate/srml/staking/src/tests.rs index 3a42ead68a..2f3f94bfbf 100644 --- a/substrate/srml/staking/src/tests.rs +++ b/substrate/srml/staking/src/tests.rs @@ -132,7 +132,7 @@ fn rewards_should_work() { // Init some balances let _ = Balances::make_free_balance_be(&2, 500); - let delay = 1; + let delay = 1000; let init_balance_2 = Balances::total_balance(&2); let init_balance_10 = Balances::total_balance(&10); let init_balance_11 = Balances::total_balance(&11); @@ -160,7 +160,7 @@ fn rewards_should_work() { let mut block = 3; // Block 3 => Session 1 => Era 0 System::set_block_number(block); - Timestamp::set_timestamp(block*5); // on time. + Timestamp::set_timestamp(block * 5000); // on time. Session::on_initialize(System::block_number()); assert_eq!(Staking::current_era(), 0); assert_eq!(Session::current_index(), 1); @@ -172,7 +172,7 @@ fn rewards_should_work() { >::reward_by_ids(vec![(1001, 10_000)]); // Compute total payout now for whole duration as other parameter won't change - let total_payout = current_total_payout_for_duration(9 * 5); + let total_payout = current_total_payout_for_duration(9 * 5 * 1000); assert!(total_payout > 10); // Test is meaningful if reward something // No reward yet @@ -182,21 +182,21 @@ fn rewards_should_work() { block = 6; // Block 6 => Session 2 => Era 0 System::set_block_number(block); - Timestamp::set_timestamp(block*5 + delay); // a little late. + Timestamp::set_timestamp(block * 5000 + delay); // a little late. Session::on_initialize(System::block_number()); assert_eq!(Staking::current_era(), 0); assert_eq!(Session::current_index(), 2); block = 9; // Block 9 => Session 3 => Era 1 System::set_block_number(block); - Timestamp::set_timestamp(block*5); // back to being on time. no delays + Timestamp::set_timestamp(block * 5000); // back to being on time. no delays Session::on_initialize(System::block_number()); assert_eq!(Staking::current_era(), 1); assert_eq!(Session::current_index(), 3); - // 11 validator has 2/3 of the total rewards and half half for it and its nominator - assert_eq!(Balances::total_balance(&2), init_balance_2 + total_payout/3); - assert_eq!(Balances::total_balance(&10), init_balance_10 + total_payout/3); + // 11 validator has 2 / 3 of the total rewards and half half for it and its nominator + assert_eq!(Balances::total_balance(&2), init_balance_2 + total_payout / 3); + assert_eq!(Balances::total_balance(&10), init_balance_10 + total_payout / 3); assert_eq!(Balances::total_balance(&11), init_balance_11); }); } @@ -216,7 +216,7 @@ fn multi_era_reward_should_work() { assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); // Compute now as other parameter won't change - let total_payout_0 = current_total_payout_for_duration(3); + let total_payout_0 = current_total_payout_for_duration(3000); assert!(total_payout_0 > 10); // Test is meaningfull if reward something dbg!(>::slot_stake()); >::reward_by_ids(vec![(11, 1)]); @@ -231,7 +231,7 @@ fn multi_era_reward_should_work() { start_session(4); - let total_payout_1 = current_total_payout_for_duration(3); + let total_payout_1 = current_total_payout_for_duration(3000); assert!(total_payout_1 > 10); // Test is meaningfull if reward something >::reward_by_ids(vec![(11, 101)]); @@ -436,7 +436,7 @@ fn nominating_and_rewards_should_work() { assert_ok!(Staking::nominate(Origin::signed(4), vec![11, 21, 41])); // the total reward for era 0 - let total_payout_0 = current_total_payout_for_duration(3); + let total_payout_0 = current_total_payout_for_duration(3000); assert!(total_payout_0 > 100); // Test is meaningfull if reward something >::reward_by_ids(vec![(41, 1)]); >::reward_by_ids(vec![(31, 1)]); @@ -449,8 +449,8 @@ fn nominating_and_rewards_should_work() { assert_eq_uvec!(validator_controllers(), vec![20, 10]); // OLD validators must have already received some rewards. - assert_eq!(Balances::total_balance(&40), 1 + total_payout_0/2); - assert_eq!(Balances::total_balance(&30), 1 + total_payout_0/2); + assert_eq!(Balances::total_balance(&40), 1 + total_payout_0 / 2); + assert_eq!(Balances::total_balance(&30), 1 + total_payout_0 / 2); // ------ check the staked value of all parties. @@ -511,7 +511,7 @@ fn nominating_and_rewards_should_work() { assert_eq!(Staking::stakers(41).total, 0); // the total reward for era 1 - let total_payout_1 = current_total_payout_for_duration(3); + let total_payout_1 = current_total_payout_for_duration(3000); assert!(total_payout_1 > 100); // Test is meaningfull if reward something >::reward_by_ids(vec![(41, 10)]); // must be no-op >::reward_by_ids(vec![(31, 10)]); // must be no-op @@ -523,28 +523,34 @@ fn nominating_and_rewards_should_work() { // nothing else will happen, era ends and rewards are paid again, // it is expected that nominators will also be paid. See below - let payout_for_10 = total_payout_1/3; - let payout_for_20 = 2*total_payout_1/3; + let payout_for_10 = total_payout_1 / 3; + let payout_for_20 = 2 * total_payout_1 / 3; if cfg!(feature = "equalize") { - // Nominator 2: has [400/2000 ~ 1/5 from 10] + [600/2000 ~ 3/10 from 20]'s reward. - assert_eq!(Balances::total_balance(&2), initial_balance + payout_for_10/5 + payout_for_20*3/10 - 1); - // Nominator 4: has [400/2000 ~ 1/5 from 20] + [600/2000 ~ 3/10 from 10]'s reward. - assert_eq!(Balances::total_balance(&4), initial_balance + payout_for_20/5 + payout_for_10*3/10); + // Nominator 2: has [400 / 2000 ~ 1 / 5 from 10] + [600 / 2000 ~ 3 / 10 from 20]'s reward. + assert_eq!(Balances::total_balance(&2), initial_balance + payout_for_10 / 5 + payout_for_20 * 3 / 10); + // Nominator 4: has [400 / 2000 ~ 1 / 5 from 20] + [600 / 2000 ~ 3 / 10 from 10]'s reward. + assert_eq!(Balances::total_balance(&4), initial_balance + payout_for_20 / 5 + payout_for_10 * 3 / 10); // Validator 10: got 1000 / 2000 external stake. - assert_eq!(Balances::total_balance(&10), initial_balance + payout_for_10/2); + assert_eq!(Balances::total_balance(&10), initial_balance + payout_for_10 / 2); // Validator 20: got 1000 / 2000 external stake. - assert_eq!(Balances::total_balance(&20), initial_balance + payout_for_20/2); + assert_eq!(Balances::total_balance(&20), initial_balance + payout_for_20 / 2); } else { - // Nominator 2: has [400/1800 ~ 2/9 from 10] + [600/2200 ~ 3/11 from 20]'s reward. ==> 2/9 + 3/11 - assert_eq!(Balances::total_balance(&2), initial_balance + (2*payout_for_10/9 + 3*payout_for_20/11) - 2); - // Nominator 4: has [400/1800 ~ 2/9 from 10] + [600/2200 ~ 3/11 from 20]'s reward. ==> 2/9 + 3/11 - assert_eq!(Balances::total_balance(&4), initial_balance + (2*payout_for_10/9 + 3*payout_for_20/11) - 2); + // Nominator 2: has [400 / 1800 ~ 2 / 9 from 10] + [600 / 2200 ~ 3 / 11 from 20]'s reward. ==> 2 / 9 + 3 / 11 + assert_eq!( + Balances::total_balance(&2), + initial_balance + (2 * payout_for_10 / 9 + 3 * payout_for_20 / 11) - 2 + ); + // Nominator 4: has [400 / 1800 ~ 2 / 9 from 10] + [600 / 2200 ~ 3 / 11 from 20]'s reward. ==> 2 / 9 + 3 / 11 + assert_eq!( + Balances::total_balance(&4), + initial_balance + (2 * payout_for_10 / 9 + 3 * payout_for_20 / 11) - 2 + ); - // Validator 10: got 800 / 1800 external stake => 8/18 =? 4/9 => Validator's share = 5/9 - assert_eq!(Balances::total_balance(&10), initial_balance + 5*payout_for_10/9 - 1); - // Validator 20: got 1200 / 2200 external stake => 12/22 =? 6/11 => Validator's share = 5/11 - assert_eq!(Balances::total_balance(&20), initial_balance + 5*payout_for_20/11); + // Validator 10: got 800 / 1800 external stake => 8 / 18 =? 4 / 9 => Validator's share = 5 / 9 + assert_eq!(Balances::total_balance(&10), initial_balance + 5*payout_for_10 / 9 - 1); + // Validator 20: got 1200 / 2200 external stake => 12 / 22 =? 6 / 11 => Validator's share = 5 / 11 + assert_eq!(Balances::total_balance(&20), initial_balance + 5*payout_for_20 / 11 + 1); } check_exposure_all(); @@ -576,7 +582,7 @@ fn nominators_also_get_slashed() { assert_ok!(Staking::bond(Origin::signed(1), 2, nominator_stake, RewardDestination::default())); assert_ok!(Staking::nominate(Origin::signed(2), vec![20, 10])); - let total_payout = current_total_payout_for_duration(3); + let total_payout = current_total_payout_for_duration(3000); assert!(total_payout > 100); // Test is meaningfull if reward something >::reward_by_ids(vec![(11, 1)]); @@ -824,7 +830,7 @@ fn reward_destination_works() { })); // Compute total payout now for whole duration as other parameter won't change - let total_payout_0 = current_total_payout_for_duration(3); + let total_payout_0 = current_total_payout_for_duration(3000); assert!(total_payout_0 > 100); // Test is meaningfull if reward something >::reward_by_ids(vec![(11, 1)]); @@ -846,7 +852,7 @@ fn reward_destination_works() { >::insert(&11, RewardDestination::Stash); // Compute total payout now for whole duration as other parameter won't change - let total_payout_1 = current_total_payout_for_duration(3); + let total_payout_1 = current_total_payout_for_duration(3000); assert!(total_payout_1 > 100); // Test is meaningfull if reward something >::reward_by_ids(vec![(11, 1)]); @@ -873,7 +879,7 @@ fn reward_destination_works() { assert_eq!(Balances::free_balance(&10), 1); // Compute total payout now for whole duration as other parameter won't change - let total_payout_2 = current_total_payout_for_duration(3); + let total_payout_2 = current_total_payout_for_duration(3000); assert!(total_payout_2 > 100); // Test is meaningfull if reward something >::reward_by_ids(vec![(11, 1)]); @@ -926,7 +932,7 @@ fn validator_payment_prefs_work() { }); // Compute total payout now for whole duration as other parameter won't change - let total_payout_0 = current_total_payout_for_duration(3); + let total_payout_0 = current_total_payout_for_duration(3000); assert!(total_payout_0 > 100); // Test is meaningfull if reward something >::reward_by_ids(vec![(11, 1)]); @@ -935,11 +941,11 @@ fn validator_payment_prefs_work() { // whats left to be shared is the sum of 3 rounds minus the validator's cut. let shared_cut = total_payout_0 - validator_cut; // Validator's payee is Staked account, 11, reward will be paid here. - assert_eq!(Balances::total_balance(&11), stash_initial_balance + shared_cut/2 + validator_cut); + assert_eq!(Balances::total_balance(&11), stash_initial_balance + shared_cut / 2 + validator_cut); // Controller account will not get any reward. assert_eq!(Balances::total_balance(&10), 1); // Rest of the reward will be shared and paid to the nominator in stake. - assert_eq!(Balances::total_balance(&2), 500 + shared_cut/2); + assert_eq!(Balances::total_balance(&2), 500 + shared_cut / 2); check_exposure_all(); check_nominator_all(); @@ -1140,7 +1146,7 @@ fn slot_stake_is_least_staked_validator_and_exposure_defines_maximum_punishment( >::insert(&20, StakingLedger { stash: 22, total: 69, active: 69, unlocking: vec![] }); // Compute total payout now for whole duration as other parameter won't change - let total_payout_0 = current_total_payout_for_duration(3); + let total_payout_0 = current_total_payout_for_duration(3000); assert!(total_payout_0 > 100); // Test is meaningfull if reward something >::reward_by_ids(vec![(11, 1)]); >::reward_by_ids(vec![(21, 1)]); @@ -1149,14 +1155,14 @@ fn slot_stake_is_least_staked_validator_and_exposure_defines_maximum_punishment( start_era(1); // -- new balances + reward - assert_eq!(Staking::stakers(&11).total, 1000 + total_payout_0/2); - assert_eq!(Staking::stakers(&21).total, 69 + total_payout_0/2); + assert_eq!(Staking::stakers(&11).total, 1000 + total_payout_0 / 2); + assert_eq!(Staking::stakers(&21).total, 69 + total_payout_0 / 2); let _11_balance = Balances::free_balance(&11); - assert_eq!(_11_balance, 1000 + total_payout_0/2); + assert_eq!(_11_balance, 1000 + total_payout_0 / 2); // -- slot stake should also be updated. - assert_eq!(Staking::slot_stake(), 69 + total_payout_0/2); + assert_eq!(Staking::slot_stake(), 69 + total_payout_0 / 2); check_exposure_all(); check_nominator_all(); @@ -1621,7 +1627,7 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() { assert_ok!(Staking::bond(Origin::signed(1), 2, 1, RewardDestination::Controller)); assert_ok!(Staking::validate(Origin::signed(2), ValidatorPrefs::default())); - let total_payout_0 = current_total_payout_for_duration(3); + let total_payout_0 = current_total_payout_for_duration(3000); assert!(total_payout_0 > 100); // Test is meaningfull if reward something reward_all_elected(); start_era(1); @@ -1632,11 +1638,11 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() { assert_eq!(Staking::slot_stake(), 1); // Old ones are rewarded. - assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0/3); + assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0 / 3); // no rewards paid to 2. This was initial election. assert_eq!(Balances::free_balance(&2), init_balance_2); - let total_payout_1 = current_total_payout_for_duration(3); + let total_payout_1 = current_total_payout_for_duration(3000); assert!(total_payout_1 > 100); // Test is meaningfull if reward something reward_all_elected(); start_era(2); @@ -1644,8 +1650,8 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() { assert_eq_uvec!(validator_controllers(), vec![20, 10, 2]); assert_eq!(Staking::slot_stake(), 1); - assert_eq!(Balances::free_balance(&2), init_balance_2 + total_payout_1/3); - assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0/3 + total_payout_1/3); + assert_eq!(Balances::free_balance(&2), init_balance_2 + total_payout_1 / 3); + assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0 / 3 + total_payout_1 / 3); check_exposure_all(); check_nominator_all(); }); @@ -1750,8 +1756,8 @@ fn phragmen_should_not_overflow_validators() { 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]); + bond_nominator(6, u64::max_value() / 2, vec![3, 5]); + bond_nominator(8, u64::max_value() / 2, vec![3, 5]); start_era(1); @@ -1773,8 +1779,8 @@ fn phragmen_should_not_overflow_nominators() { let _ = Staking::chill(Origin::signed(10)); let _ = Staking::chill(Origin::signed(20)); - bond_validator(2, u64::max_value()/2); - bond_validator(4, u64::max_value()/2); + bond_validator(2, u64::max_value() / 2); + bond_validator(4, u64::max_value() / 2); bond_nominator(6, u64::max_value(), vec![3, 5]); bond_nominator(8, u64::max_value(), vec![3, 5]); @@ -1943,9 +1949,9 @@ fn reward_from_authorship_event_handler_works() { assert_eq!(>::get(), vec![21, 11]); // 21 is rewarded as an uncle producer - // 11 is rewarded as a block procuder and uncle referencer and uncle producer - assert_eq!(CurrentEraRewards::get().rewards, vec![1, 20+2*3 + 1]); - assert_eq!(CurrentEraRewards::get().total, 28); + // 11 is rewarded as a block producer and uncle referencer and uncle producer + assert_eq!(CurrentEraPointsEarned::get().individual, vec![1, 20 + 2 * 3 + 1]); + assert_eq!(CurrentEraPointsEarned::get().total, 28); }) } @@ -1972,8 +1978,8 @@ fn add_reward_points_fns_works() { (11, 1), ]); - assert_eq!(CurrentEraRewards::get().rewards, vec![2, 4]); - assert_eq!(CurrentEraRewards::get().total, 6); + assert_eq!(CurrentEraPointsEarned::get().individual, vec![2, 4]); + assert_eq!(CurrentEraPointsEarned::get().total, 6); }) }