From b4891cdbc7e1c9008e17582fdf1cbce38c440bf4 Mon Sep 17 00:00:00 2001 From: readlnh Date: Tue, 24 Dec 2019 13:49:27 +0800 Subject: [PATCH 1/7] fix current_total_payout_for_duration --- srml/staking/src/mock.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index c2b72efe6..0d890348f 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -480,15 +480,13 @@ pub fn start_era(era_index: EraIndex) { assert_eq!(Staking::current_era(), era_index); } -// TODO + pub fn current_total_payout_for_duration(duration: u64) -> Balance { - // inflation::compute_total_payout( - // era_duration.saturated_into::(), - // (>::Time::now() - >::GenesisTime::get()).saturated_into::(), - // (>::Cap::get() - Ring::total_issuance()).saturated_into::(), - // ) - // .0 - unimplemented!() + inflation::compute_total_payout::( + duration.saturated_into::(), + (Timestamp::now() - ::GenesisTime::get()).saturated_into::(), + (::Cap::get() - Ring::total_issuance()).saturated_into::(), + ).0 } pub fn reward_all_elected() { From ef7a3223f638f1255ede4410dbf26065a379b7b3 Mon Sep 17 00:00:00 2001 From: readlnh Date: Tue, 24 Dec 2019 13:53:33 +0800 Subject: [PATCH 2/7] add total_payout in rewards_should_work, tests still failed --- srml/staking/src/tests.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 734f5c445..6e67fed68 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -1,4 +1,4 @@ -use sr_primitives::traits::OnInitialize; +use sr_primitives::{traits::OnInitialize, assert_eq_error_rate}; use srml_support::{ assert_eq_uvec, assert_err, assert_noop, assert_ok, traits::{Currency, ReservableCurrency}, @@ -362,8 +362,8 @@ 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 * 1000); - // assert!(total_payout > 10); // Test is meaningful if reward something + let total_payout = current_total_payout_for_duration(9 * 5 * 1000); + assert!(total_payout > 10); // Test is meaningful if reward something // No reward yet assert_eq!(Ring::total_balance(&2), init_balance_2); @@ -384,9 +384,9 @@ fn rewards_should_work() { 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_error_rate!(Balances::total_balance(&2), init_balance_2 + total_payout / 3, 1); - // assert_eq_error_rate!(Balances::total_balance(&10), init_balance_10 + total_payout / 3, 1); + // 11 validator has 2/3 of the total rewards and half half for it and its nominator. (should fix) + assert_eq_error_rate!(Ring::total_balance(&2), init_balance_2 + total_payout / 3, 1); + assert_eq_error_rate!(Ring::total_balance(&10), init_balance_10 + total_payout / 3, 1); assert_eq!(Ring::total_balance(&11), init_balance_11); }); } @@ -2644,7 +2644,7 @@ fn validator_payment_ratio_should_work() { vec![validator_stash], )); - assert_eq!(Staking::reward_validator(&validator_stash, COIN).peek(), 0); + //assert_eq!(Staking::reward_validator(&validator_stash, COIN).peek(), 0); assert_ok!(Staking::chill(Origin::signed(validator_controller))); assert_ok!(Staking::chill(Origin::signed(nominator_controller))); @@ -2661,7 +2661,7 @@ fn validator_payment_ratio_should_work() { vec![validator_stash], )); - assert_eq!(Staking::reward_validator(&validator_stash, COIN).peek(), COIN); + //assert_eq!(Staking::reward_validator(&validator_stash, COIN).peek(), COIN); }); } From 194b18180aaef76e12ddedac56807fea584c19e0 Mon Sep 17 00:00:00 2001 From: readlnh Date: Tue, 24 Dec 2019 13:59:28 +0800 Subject: [PATCH 3/7] fix multi_era_reward_should_work --- srml/staking/src/tests.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 6e67fed68..8ca50101a 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -398,14 +398,14 @@ fn multi_era_reward_should_work() { // The value of current_session_reward is set at the end of each era, based on // slot_stake and session_reward. ExtBuilder::default().nominate(false).build().execute_with(|| { - let _init_balance_10 = Ring::total_balance(&10); + let init_balance_10 = Ring::total_balance(&10); // Set payee to controller. 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(3000); - // assert!(total_payout_0 > 10); // Test is meaningfull if reward something + let total_payout_0 = current_total_payout_for_duration(3000); + assert!(total_payout_0 > 10); // Test is meaningfull if reward something >::reward_by_ids(vec![(11, 1)]); start_session(0); @@ -414,22 +414,22 @@ fn multi_era_reward_should_work() { start_session(3); assert_eq!(Staking::current_era(), 1); - // assert_eq!(Ring::total_balance(&10), init_balance_10 + total_payout_0); + assert_eq!(Ring::total_balance(&10), init_balance_10 + total_payout_0); start_session(4); - // let total_payout_1 = current_total_payout_for_duration(3000); - // assert!(total_payout_1 > 10); // Test is meaningfull if reward something + 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)]); // New era is triggered here. start_session(5); // Pay time. - // assert_eq!( - // Ring::total_balance(&10), - // init_balance_10 + total_payout_0 + total_payout_1 - // ); + assert_eq!( + Ring::total_balance(&10), + init_balance_10 + total_payout_0 + total_payout_1 + ); }); } From 4935ad13240e39c6f40b12a9ad43f2fa8d811109 Mon Sep 17 00:00:00 2001 From: readlnh Date: Fri, 27 Dec 2019 16:13:13 +0800 Subject: [PATCH 4/7] add test_payout --- srml/staking/src/tests.rs | 45 +++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index a734d2f78..69134f5e5 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -3964,16 +3964,39 @@ fn xavier_q3() { } #[test] -fn test_current_total_payout_for_duration() { +fn test_payout() { ExtBuilder::default().build().execute_with(|| { - let duration = 9 * 5 * 1000; - assert_eq!( - inflation::compute_total_payout::( - duration.saturated_into::(), - (Timestamp::now() - ::GenesisTime::get()).saturated_into::(), - (::Cap::get() - Ring::total_issuance()).saturated_into::(), - ), - (0, 0) - ); + // Set payee to controller + assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); + + assert_eq!(Ring::total_issuance(), + Ring::total_balance(&1) + Ring::total_balance(&2) + Ring::total_balance(&3) + Ring::total_balance(&4) + + Ring::total_balance(&10) + Ring::total_balance(&11) + + Ring::total_balance(&20) + Ring::total_balance(&21) + + Ring::total_balance(&30) + Ring::total_balance(&31) + + Ring::total_balance(&40) + Ring::total_balance(&41) + + Ring::total_balance(&100) + Ring::total_balance(&101) + + Ring::total_balance(&999) + ); + let left = 2000_000_000 * COIN - (Ring::total_balance(&1) + Ring::total_balance(&2) + Ring::total_balance(&3) + Ring::total_balance(&4) + + Ring::total_balance(&10) + Ring::total_balance(&11) + + Ring::total_balance(&20) + Ring::total_balance(&21) + + Ring::total_balance(&30) + Ring::total_balance(&31) + + Ring::total_balance(&40) + Ring::total_balance(&41) + + Ring::total_balance(&100) + Ring::total_balance(&101) + + Ring::total_balance(&999)); + let _ = Ring::deposit_creating(&9999, left); + assert_eq!(Ring::total_issuance(), 2000_000_000 * COIN); + + assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); + + let total_pay_out_now = current_total_payout_for_duration(180 * 1000); + assert_eq!(total_pay_out_now, 456240000000); + + // // for one year, Note: this test will take over 60s + // for i in 0..175320 { + // start_session(i); + // } + // assert_eq!(current_total_payout_for_duration(1000 * 3600 * 24 * 36525 / 100), 8 * 10_000_000 * COIN); }); -} +} \ No newline at end of file From cb20bdf7add57d7f9fbd7a8db45e91ffff5a06a7 Mon Sep 17 00:00:00 2001 From: readlnh Date: Fri, 27 Dec 2019 17:13:42 +0800 Subject: [PATCH 5/7] update mock:current_total_payout_for_duration() payout_fraction to 50% --- srml/staking/src/mock.rs | 1 + srml/staking/src/tests.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index 94d0862d8..4144844a1 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -486,6 +486,7 @@ pub fn current_total_payout_for_duration(duration: u64) -> Balance { duration.saturated_into::(), (Timestamp::now() - ::GenesisTime::get()).saturated_into::(), (::Cap::get() - Ring::total_issuance()).saturated_into::(), + Perbill::from_percent(50) ).0 } diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index bb2c5c3c4..ac0aff6b6 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -4066,13 +4066,13 @@ fn test_payout() { assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); let total_pay_out_now = current_total_payout_for_duration(180 * 1000); - assert_eq!(total_pay_out_now, 456240000000); + assert_eq!(total_pay_out_now, 456240000000 / 2); - // // for one year, Note: this test will take over 60s - // for i in 0..175320 { - // start_session(i); - // } - // assert_eq!(current_total_payout_for_duration(1000 * 3600 * 24 * 36525 / 100), 8 * 10_000_000 * COIN); + // for one year, Note: this test will take over 60s + for i in 0..175320 { + start_session(i); + } + assert_eq!(current_total_payout_for_duration(1000 * 3600 * 24 * 36525 / 100), 8 * 10_000_000 * COIN / 2); }); } // breakpoint test From 8c72b066d28e5804a72a7416a7257873e847ad8b Mon Sep 17 00:00:00 2001 From: readlnh Date: Fri, 27 Dec 2019 17:35:48 +0800 Subject: [PATCH 6/7] change Perbill to Perquintill --- srml/staking/src/inflation.rs | 4 ++-- srml/staking/src/tests.rs | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/srml/staking/src/inflation.rs b/srml/staking/src/inflation.rs index 379d4a881..64a08f2c9 100644 --- a/srml/staking/src/inflation.rs +++ b/srml/staking/src/inflation.rs @@ -1,7 +1,7 @@ use rstd::convert::TryInto; use sr_primitives::{ traits::{IntegerSquareRoot, SaturatedConversion}, - Perbill, + Perbill, Perquintill, }; use substrate_primitives::U256; @@ -20,7 +20,7 @@ pub fn compute_total_payout( let year: u32 = (living_time / MILLISECONDS_PER_YEAR + 1).saturated_into::(); - let portion = Perbill::from_rational_approximation(era_duration, MILLISECONDS_PER_YEAR); + let portion = Perquintill::from_rational_approximation(era_duration, MILLISECONDS_PER_YEAR); let maximum = portion * total_left; diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index ac0aff6b6..b901e27ed 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -355,7 +355,7 @@ fn rewards_should_work() { assert_eq!(Staking::current_era(), 0); assert_eq!(Session::current_index(), 1); >::reward_by_ids(vec![(11, 50)]); - //>::reward_by_ids(vec![(11, 50)]); + >::reward_by_ids(vec![(11, 50)]); // This is the second validator of the current elected set. >::reward_by_ids(vec![(21, 50)]); // This must be no-op as it is not an elected validator. @@ -385,8 +385,8 @@ fn rewards_should_work() { assert_eq!(Session::current_index(), 3); // 11 validator has 2/3 of the total rewards and half half for it and its nominator. (should fix) - assert_eq_error_rate!(Ring::total_balance(&2), init_balance_2 + total_payout / 4, 1); - assert_eq_error_rate!(Ring::total_balance(&10), init_balance_10 + total_payout / 4, 1); + assert_eq_error_rate!(Ring::total_balance(&2), init_balance_2 + total_payout / 3, 100); + assert_eq_error_rate!(Ring::total_balance(&10), init_balance_10 + total_payout / 3, 100); assert_eq!(Ring::total_balance(&11), init_balance_11); }); } @@ -4066,13 +4066,13 @@ fn test_payout() { assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller)); let total_pay_out_now = current_total_payout_for_duration(180 * 1000); - assert_eq!(total_pay_out_now, 456240000000 / 2); + assert_eq!(total_pay_out_now, 456308464522 / 2); - // for one year, Note: this test will take over 60s - for i in 0..175320 { - start_session(i); - } - assert_eq!(current_total_payout_for_duration(1000 * 3600 * 24 * 36525 / 100), 8 * 10_000_000 * COIN / 2); + // // for one year, Note: this test will take over 60s + // for i in 0..175320 { + // start_session(i); + // } + // assert_eq!(current_total_payout_for_duration(1000 * 3600 * 24 * 36525 / 100), 79601332265494830 / 2); }); } // breakpoint test From 5b549ba78135f23f15fedaccbc2fa9977782b41e Mon Sep 17 00:00:00 2001 From: readlnh Date: Mon, 30 Dec 2019 16:08:12 +0800 Subject: [PATCH 7/7] fix validator_payment_ratio_should_work --- srml/staking/src/tests.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index b901e27ed..473fcedb8 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -2647,7 +2647,7 @@ fn validator_payment_ratio_should_work() { vec![validator_stash], )); - //assert_eq!(Staking::reward_validator(&validator_stash, COIN).peek(), 0); + assert_eq!(Staking::reward_validator(&validator_stash, COIN).0.peek(), 0); assert_ok!(Staking::chill(Origin::signed(validator_controller))); assert_ok!(Staking::chill(Origin::signed(nominator_controller))); @@ -2664,7 +2664,7 @@ fn validator_payment_ratio_should_work() { vec![validator_stash], )); - //assert_eq!(Staking::reward_validator(&validator_stash, COIN).peek(), COIN); + assert_eq!(Staking::reward_validator(&validator_stash, COIN).0.peek(), COIN); }); } @@ -4069,8 +4069,9 @@ fn test_payout() { assert_eq!(total_pay_out_now, 456308464522 / 2); // // for one year, Note: this test will take over 60s - // for i in 0..175320 { + // for i in 0..175319 { // start_session(i); + // >::reward_by_ids(vec![(11, 101)]); // } // assert_eq!(current_total_payout_for_duration(1000 * 3600 * 24 * 36525 / 100), 79601332265494830 / 2); });