Skip to content

Commit 3448934

Browse files
committed
Feat/reward pool history (#1710)
The goal of this PR is to implement storage of reward pool history. Closes #1710
1 parent 68593fb commit 3448934

File tree

6 files changed

+98
-36
lines changed

6 files changed

+98
-36
lines changed

pallets/capacity/src/tests/change_staking_target_tests.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use super::{mock::*, testing_utils::*};
1+
use super::{
2+
mock::*,
3+
testing_utils::{setup_provider, staking_events},
4+
};
25
use crate::{
36
BalanceOf, CapacityDetails, Config, CurrentEraInfo, Error, Event, RewardEraInfo,
47
StakingAccountDetails, StakingAccountLedger, StakingTargetDetails,
@@ -13,22 +16,6 @@ use common_primitives::{
1316
use frame_support::{assert_noop, assert_ok, traits::Get};
1417

1518
// staker is unused unless amount > 0
16-
fn setup_provider(staker: &u64, target: &MessageSourceId, amount: &u64, staking_type: StakingType) {
17-
let provider_name = String::from("Cst-") + target.to_string().as_str();
18-
register_provider(*target, provider_name);
19-
if amount.gt(&0u64) {
20-
assert_ok!(Capacity::stake(
21-
RuntimeOrigin::signed(staker.clone()),
22-
*target,
23-
*amount,
24-
staking_type.clone()
25-
));
26-
let target = Capacity::get_target_for(staker, target).unwrap();
27-
assert_eq!(target.amount, *amount);
28-
assert_eq!(target.staking_type, staking_type);
29-
}
30-
}
31-
3219
type TestCapacityDetails = CapacityDetails<BalanceOf<Test>, u32>;
3320
type TestTargetDetails = StakingTargetDetails<Test>;
3421

@@ -60,6 +47,7 @@ fn assert_target_details(
6047
let from_target_details = Capacity::get_target_for(staker, msa_id).unwrap();
6148
assert_eq!(from_target_details, expected_from_target_details);
6249
}
50+
6351
#[test]
6452
fn do_retarget_happy_path() {
6553
new_test_ext().execute_with(|| {
@@ -160,6 +148,7 @@ fn assert_total_capacity(msas: Vec<MessageSourceId>, total: u64) {
160148
.fold(0, |a, b| a + b);
161149
assert_eq!(total, sum);
162150
}
151+
163152
#[test]
164153
fn check_retarget_multiple_stakers() {
165154
new_test_ext().execute_with(|| {
Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,74 @@
1-
use super::mock::*;
2-
use crate::{
3-
tests::testing_utils::{run_to_block, system_run_to_block},
4-
CurrentEraInfo, RewardEraInfo,
1+
use super::{
2+
mock::*,
3+
testing_utils::{run_to_block, system_run_to_block},
54
};
5+
use crate::{Config, CurrentEraInfo, RewardEraInfo, RewardPoolInfo, StakingRewardPool};
6+
use sp_core::Get;
67

78
#[test]
8-
fn start_new_era_if_needed() {
9+
fn start_new_era_if_needed_updates_era_info_and_limits_reward_pool_size() {
910
new_test_ext().execute_with(|| {
1011
CurrentEraInfo::<Test>::set(RewardEraInfo { era_index: 1, started_at: 0 });
12+
StakingRewardPool::<Test>::insert(
13+
1,
14+
RewardPoolInfo {
15+
total_staked_token: 10_000,
16+
total_reward_pool: 1_000,
17+
unclaimed_balance: 1_000,
18+
},
19+
);
1120
system_run_to_block(9);
12-
run_to_block(10);
13-
let mut current_era_info = CurrentEraInfo::<Test>::get();
14-
assert_eq!(current_era_info.era_index, 2u32);
15-
assert_eq!(current_era_info.started_at, 10u32);
21+
for i in 1..4 {
22+
let block_decade = i * 10;
23+
run_to_block(block_decade);
24+
25+
let current_era_info = CurrentEraInfo::<Test>::get();
1626

17-
system_run_to_block(19);
18-
run_to_block(20);
19-
current_era_info = CurrentEraInfo::<Test>::get();
20-
assert_eq!(current_era_info.era_index, 3u32);
21-
assert_eq!(current_era_info.started_at, 20u32);
27+
let expected_era = i + 1;
28+
assert_eq!(current_era_info.era_index, expected_era);
29+
assert_eq!(current_era_info.started_at, block_decade);
30+
let past_eras_max: u32 = <Test as Config>::StakingRewardsPastErasMax::get();
31+
assert!(StakingRewardPool::<Test>::count().le(&past_eras_max));
32+
system_run_to_block(block_decade + 9);
33+
}
2234
})
2335
}
36+
37+
#[test]
38+
fn start_new_era_if_needed_updates_reward_pool() {
39+
new_test_ext().execute_with(|| {
40+
CurrentEraInfo::<Test>::set(RewardEraInfo { era_index: 1, started_at: 0 });
41+
StakingRewardPool::<Test>::insert(
42+
1,
43+
RewardPoolInfo {
44+
total_staked_token: 10_000,
45+
total_reward_pool: 1_000,
46+
unclaimed_balance: 1_000,
47+
},
48+
);
49+
system_run_to_block(8);
50+
51+
// TODO: Provider boost, after staking updates reward pool info #1699
52+
// let staker = 10_000;
53+
// let provider_msa: MessageSourceId = 1;
54+
// let stake_amount = 600u64;
55+
// setup_provider(&staker, &provider_msa, &stake_amount, ProviderBoost);
56+
57+
system_run_to_block(9);
58+
run_to_block(10);
59+
assert_eq!(StakingRewardPool::<Test>::count(), 2);
60+
let current_reward_pool_info = StakingRewardPool::<Test>::get(2).unwrap();
61+
assert_eq!(
62+
current_reward_pool_info,
63+
RewardPoolInfo {
64+
total_staked_token: 10_000,
65+
total_reward_pool: 1_000,
66+
unclaimed_balance: 1_000,
67+
}
68+
);
69+
70+
// TODO: after staking updates reward pool info #1699
71+
// system_run_to_block(19);
72+
// run_to_block(20);
73+
});
74+
}

pallets/capacity/src/tests/mock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use sp_runtime::{
1515
traits::{BlakeTwo256, Convert, IdentityLookup},
1616
AccountId32, BuildStorage, DispatchError, Perbill,
1717
};
18-
use sp_std::ops::Mul;
18+
use sp_std::ops::{Div, Mul};
1919

2020
type Block = frame_system::mocking::MockBlockU32<Test>;
2121

@@ -141,8 +141,8 @@ impl StakingRewardsProvider<Test> for TestStakingRewardsProvider {
141141
type RewardEra = TestRewardEra;
142142
type Hash = Hash; // use what's in common_primitives::node
143143

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

148148
fn staking_reward_total(

pallets/capacity/src/tests/rewards_provider_tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
use frame_support::assert_err;
77

88
use sp_core::H256;
9+
use sp_std::ops::Div;
910

1011
#[test]
1112
fn test_staking_reward_total_happy_path() {
@@ -63,7 +64,7 @@ fn test_reward_pool_size_happy_path() {
6364
unclaimed_balance: 0u64,
6465
},
6566
);
66-
assert_eq!(Ok(tc.expected_reward_pool), Capacity::reward_pool_size());
67+
assert_eq!(tc.expected_reward_pool, tc.total_staked.div(10u64));
6768
}
6869
})
6970
}

pallets/capacity/src/tests/testing_utils.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::mock::*;
22
use frame_support::{assert_ok, traits::Hooks};
33

4+
use common_primitives::capacity::StakingType;
45
#[allow(unused)]
56
use sp_runtime::traits::SignedExtension;
67

@@ -63,3 +64,23 @@ pub fn create_capacity_account_and_fund(
6364

6465
capacity_details
6566
}
67+
pub fn setup_provider(
68+
staker: &u64,
69+
target: &MessageSourceId,
70+
amount: &u64,
71+
staking_type: StakingType,
72+
) {
73+
let provider_name = String::from("Cst-") + target.to_string().as_str();
74+
register_provider(*target, provider_name);
75+
if amount.gt(&0u64) {
76+
assert_ok!(Capacity::stake(
77+
RuntimeOrigin::signed(staker.clone()),
78+
*target,
79+
*amount,
80+
staking_type.clone()
81+
));
82+
let target = Capacity::get_target_for(staker, target).unwrap();
83+
assert_eq!(target.amount, *amount);
84+
assert_eq!(target.staking_type, staking_type);
85+
}
86+
}

pallets/capacity/src/tests/unstaking_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use sp_core::bounded::BoundedVec;
1414
#[test]
1515
fn unstake_happy_path() {
1616
new_test_ext().execute_with(|| {
17-
// TODO: ProviderBoost
17+
// TODO: ProviderBoost after unstake affects reward pool info #1699
1818
let token_account = 200;
1919
let target: MessageSourceId = 1;
2020
let staking_amount = 100;

0 commit comments

Comments
 (0)