diff --git a/pallets/capacity/src/tests/change_staking_target_tests.rs b/pallets/capacity/src/tests/change_staking_target_tests.rs index c9ee0d7b12..8ca36cc494 100644 --- a/pallets/capacity/src/tests/change_staking_target_tests.rs +++ b/pallets/capacity/src/tests/change_staking_target_tests.rs @@ -1,4 +1,7 @@ -use super::{mock::*, testing_utils::*}; +use super::{ + mock::*, + testing_utils::{setup_provider, staking_events}, +}; use crate::{ BalanceOf, CapacityDetails, Config, CurrentEraInfo, Error, Event, RewardEraInfo, StakingAccountDetails, StakingAccountLedger, StakingTargetDetails, @@ -13,22 +16,6 @@ use common_primitives::{ use frame_support::{assert_noop, assert_ok, traits::Get}; // staker is unused unless amount > 0 -fn setup_provider(staker: &u64, target: &MessageSourceId, amount: &u64, staking_type: StakingType) { - let provider_name = String::from("Cst-") + target.to_string().as_str(); - register_provider(*target, provider_name); - if amount.gt(&0u64) { - assert_ok!(Capacity::stake( - RuntimeOrigin::signed(staker.clone()), - *target, - *amount, - staking_type.clone() - )); - let target = Capacity::get_target_for(staker, target).unwrap(); - assert_eq!(target.amount, *amount); - assert_eq!(target.staking_type, staking_type); - } -} - type TestCapacityDetails = CapacityDetails, u32>; type TestTargetDetails = StakingTargetDetails; @@ -60,6 +47,7 @@ fn assert_target_details( let from_target_details = Capacity::get_target_for(staker, msa_id).unwrap(); assert_eq!(from_target_details, expected_from_target_details); } + #[test] fn do_retarget_happy_path() { new_test_ext().execute_with(|| { @@ -160,6 +148,7 @@ fn assert_total_capacity(msas: Vec, total: u64) { .fold(0, |a, b| a + b); assert_eq!(total, sum); } + #[test] fn check_retarget_multiple_stakers() { new_test_ext().execute_with(|| { diff --git a/pallets/capacity/src/tests/eras_tests.rs b/pallets/capacity/src/tests/eras_tests.rs index 8f07875897..b6ec94b0e9 100644 --- a/pallets/capacity/src/tests/eras_tests.rs +++ b/pallets/capacity/src/tests/eras_tests.rs @@ -1,23 +1,74 @@ -use super::mock::*; -use crate::{ - tests::testing_utils::{run_to_block, system_run_to_block}, - CurrentEraInfo, RewardEraInfo, +use super::{ + mock::*, + testing_utils::{run_to_block, system_run_to_block}, }; +use crate::{Config, CurrentEraInfo, RewardEraInfo, RewardPoolInfo, StakingRewardPool}; +use sp_core::Get; #[test] -fn start_new_era_if_needed() { +fn start_new_era_if_needed_updates_era_info_and_limits_reward_pool_size() { new_test_ext().execute_with(|| { CurrentEraInfo::::set(RewardEraInfo { era_index: 1, started_at: 0 }); + StakingRewardPool::::insert( + 1, + RewardPoolInfo { + total_staked_token: 10_000, + total_reward_pool: 1_000, + unclaimed_balance: 1_000, + }, + ); system_run_to_block(9); - run_to_block(10); - let mut current_era_info = CurrentEraInfo::::get(); - assert_eq!(current_era_info.era_index, 2u32); - assert_eq!(current_era_info.started_at, 10u32); + for i in 1..4 { + let block_decade = i * 10; + run_to_block(block_decade); + + let current_era_info = CurrentEraInfo::::get(); - system_run_to_block(19); - run_to_block(20); - current_era_info = CurrentEraInfo::::get(); - assert_eq!(current_era_info.era_index, 3u32); - assert_eq!(current_era_info.started_at, 20u32); + let expected_era = i + 1; + assert_eq!(current_era_info.era_index, expected_era); + assert_eq!(current_era_info.started_at, block_decade); + let past_eras_max: u32 = ::StakingRewardsPastErasMax::get(); + assert!(StakingRewardPool::::count().le(&past_eras_max)); + system_run_to_block(block_decade + 9); + } }) } + +#[test] +fn start_new_era_if_needed_updates_reward_pool() { + new_test_ext().execute_with(|| { + CurrentEraInfo::::set(RewardEraInfo { era_index: 1, started_at: 0 }); + StakingRewardPool::::insert( + 1, + RewardPoolInfo { + total_staked_token: 10_000, + total_reward_pool: 1_000, + unclaimed_balance: 1_000, + }, + ); + system_run_to_block(8); + + // TODO: Provider boost, after staking updates reward pool info #1699 + // let staker = 10_000; + // let provider_msa: MessageSourceId = 1; + // let stake_amount = 600u64; + // setup_provider(&staker, &provider_msa, &stake_amount, ProviderBoost); + + system_run_to_block(9); + run_to_block(10); + assert_eq!(StakingRewardPool::::count(), 2); + let current_reward_pool_info = StakingRewardPool::::get(2).unwrap(); + assert_eq!( + current_reward_pool_info, + RewardPoolInfo { + total_staked_token: 10_000, + total_reward_pool: 1_000, + unclaimed_balance: 1_000, + } + ); + + // TODO: after staking updates reward pool info #1699 + // system_run_to_block(19); + // run_to_block(20); + }); +} diff --git a/pallets/capacity/src/tests/mock.rs b/pallets/capacity/src/tests/mock.rs index f2e15cdb70..6a5d3beb07 100644 --- a/pallets/capacity/src/tests/mock.rs +++ b/pallets/capacity/src/tests/mock.rs @@ -15,7 +15,7 @@ use sp_runtime::{ traits::{BlakeTwo256, Convert, IdentityLookup}, AccountId32, BuildStorage, DispatchError, Perbill, }; -use sp_std::ops::Mul; +use sp_std::ops::{Div, Mul}; type Block = frame_system::mocking::MockBlockU32; @@ -140,8 +140,8 @@ impl StakingRewardsProvider for TestStakingRewardsProvider { type RewardEra = TestRewardEra; type Hash = Hash; // use what's in common_primitives::node - fn reward_pool_size() -> Result, DispatchError> { - Ok(1000u64) + fn reward_pool_size(total_staked: BalanceOf) -> BalanceOf { + total_staked.div(10u64) } fn staking_reward_total( diff --git a/pallets/capacity/src/tests/rewards_provider_tests.rs b/pallets/capacity/src/tests/rewards_provider_tests.rs index 5f1fe82ff1..65fcd06840 100644 --- a/pallets/capacity/src/tests/rewards_provider_tests.rs +++ b/pallets/capacity/src/tests/rewards_provider_tests.rs @@ -6,6 +6,7 @@ use crate::{ use frame_support::assert_err; use sp_core::H256; +use sp_std::ops::Div; #[test] fn test_staking_reward_total_happy_path() { @@ -63,7 +64,7 @@ fn test_reward_pool_size_happy_path() { unclaimed_balance: 0u64, }, ); - assert_eq!(Ok(tc.expected_reward_pool), Capacity::reward_pool_size()); + assert_eq!(tc.expected_reward_pool, tc.total_staked.div(10u64)); } }) } diff --git a/pallets/capacity/src/tests/testing_utils.rs b/pallets/capacity/src/tests/testing_utils.rs index 3b37d5b654..117c95258c 100644 --- a/pallets/capacity/src/tests/testing_utils.rs +++ b/pallets/capacity/src/tests/testing_utils.rs @@ -1,6 +1,7 @@ use super::mock::*; use frame_support::{assert_ok, traits::Hooks}; +use common_primitives::capacity::StakingType; #[allow(unused)] use sp_runtime::traits::SignedExtension; @@ -63,3 +64,23 @@ pub fn create_capacity_account_and_fund( capacity_details } +pub fn setup_provider( + staker: &u64, + target: &MessageSourceId, + amount: &u64, + staking_type: StakingType, +) { + let provider_name = String::from("Cst-") + target.to_string().as_str(); + register_provider(*target, provider_name); + if amount.gt(&0u64) { + assert_ok!(Capacity::stake( + RuntimeOrigin::signed(staker.clone()), + *target, + *amount, + staking_type.clone() + )); + let target = Capacity::get_target_for(staker, target).unwrap(); + assert_eq!(target.amount, *amount); + assert_eq!(target.staking_type, staking_type); + } +} diff --git a/pallets/capacity/src/tests/unstaking_tests.rs b/pallets/capacity/src/tests/unstaking_tests.rs index 8b5d7214cd..7c00774e94 100644 --- a/pallets/capacity/src/tests/unstaking_tests.rs +++ b/pallets/capacity/src/tests/unstaking_tests.rs @@ -14,7 +14,7 @@ use sp_core::bounded::BoundedVec; #[test] fn unstake_happy_path() { new_test_ext().execute_with(|| { - // TODO: ProviderBoost + // TODO: ProviderBoost after unstake affects reward pool info #1699 let token_account = 200; let target: MessageSourceId = 1; let staking_amount = 100;