Skip to content

Commit

Permalink
Feat/reward pool history (#1710)
Browse files Browse the repository at this point in the history
The goal of this PR is to implement storage of reward pool history.

Closes #1710
  • Loading branch information
shannonwells committed May 21, 2024
1 parent 19bea84 commit 6dc96b4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 36 deletions.
23 changes: 6 additions & 17 deletions pallets/capacity/src/tests/change_staking_target_tests.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<BalanceOf<Test>, u32>;
type TestTargetDetails = StakingTargetDetails<Test>;

Expand Down Expand Up @@ -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(|| {
Expand Down Expand Up @@ -160,6 +148,7 @@ fn assert_total_capacity(msas: Vec<MessageSourceId>, total: u64) {
.fold(0, |a, b| a + b);
assert_eq!(total, sum);
}

#[test]
fn check_retarget_multiple_stakers() {
new_test_ext().execute_with(|| {
Expand Down
79 changes: 65 additions & 14 deletions pallets/capacity/src/tests/eras_tests.rs
Original file line number Diff line number Diff line change
@@ -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::<Test>::set(RewardEraInfo { era_index: 1, started_at: 0 });
StakingRewardPool::<Test>::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::<Test>::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::<Test>::get();

system_run_to_block(19);
run_to_block(20);
current_era_info = CurrentEraInfo::<Test>::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 = <Test as Config>::StakingRewardsPastErasMax::get();
assert!(StakingRewardPool::<Test>::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::<Test>::set(RewardEraInfo { era_index: 1, started_at: 0 });
StakingRewardPool::<Test>::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::<Test>::count(), 2);
let current_reward_pool_info = StakingRewardPool::<Test>::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);
});
}
6 changes: 3 additions & 3 deletions pallets/capacity/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Test>;

Expand Down Expand Up @@ -140,8 +140,8 @@ impl StakingRewardsProvider<Test> for TestStakingRewardsProvider {
type RewardEra = TestRewardEra;
type Hash = Hash; // use what's in common_primitives::node

fn reward_pool_size() -> Result<BalanceOf<Test>, DispatchError> {
Ok(1000u64)
fn reward_pool_size(total_staked: BalanceOf<Test>) -> BalanceOf<Test> {
total_staked.div(10u64)
}

fn staking_reward_total(
Expand Down
3 changes: 2 additions & 1 deletion pallets/capacity/src/tests/rewards_provider_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
}
})
}
Expand Down
21 changes: 21 additions & 0 deletions pallets/capacity/src/tests/testing_utils.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion pallets/capacity/src/tests/unstaking_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6dc96b4

Please sign in to comment.