-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
The goal of this PR is to implement a really basic version of the StakingRewardsProvider in the Capacity pallet and in the test mock, neither of which is actively used. Closes #1572 Does not include anything to do with setting and storing RewardPoolInfo when each new Era starts. - [x] Design doc(s) updated - [x] Tests added
- Loading branch information
1 parent
756ffe0
commit a3fa8e3
Showing
6 changed files
with
128 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
use super::mock::*; | ||
use crate::{ | ||
tests::testing_utils::{run_to_block, system_run_to_block}, | ||
Config, CurrentEraInfo, Error, Event, RewardEraInfo, | ||
CurrentEraInfo, RewardEraInfo, | ||
}; | ||
|
||
use frame_support::traits::Get; | ||
|
||
#[test] | ||
fn start_new_era_if_needed() { | ||
new_test_ext().execute_with(|| { | ||
CurrentEraInfo::<Test>::set(RewardEraInfo { current_era: 1, era_start: 0 }); | ||
CurrentEraInfo::<Test>::set(RewardEraInfo { era_index: 1, started_at: 0 }); | ||
system_run_to_block(9); | ||
run_to_block(10); | ||
let mut current_era_info = CurrentEraInfo::<Test>::get(); | ||
assert_eq!(current_era_info.current_era, 2u32); | ||
assert_eq!(current_era_info.era_start, 10u32); | ||
assert_eq!(current_era_info.era_index, 2u32); | ||
assert_eq!(current_era_info.started_at, 10u32); | ||
|
||
system_run_to_block(19); | ||
run_to_block(20); | ||
current_era_info = CurrentEraInfo::<Test>::get(); | ||
assert_eq!(current_era_info.current_era, 3u32); | ||
assert_eq!(current_era_info.era_start, 20u32); | ||
assert_eq!(current_era_info.era_index, 3u32); | ||
assert_eq!(current_era_info.started_at, 20u32); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use super::mock::*; | ||
use crate::{ | ||
tests::testing_utils::{run_to_block, system_run_to_block}, | ||
Config, CurrentEraInfo, Error, Event, RewardEraInfo, RewardPoolInfo, StakingAccountDetails, | ||
StakingRewardClaim, StakingRewardPool, StakingRewardsProvider, | ||
}; | ||
use frame_support::assert_err; | ||
|
||
use common_primitives::capacity::StakingType::MaximumCapacity; | ||
use sp_core::H256; | ||
|
||
#[test] | ||
fn test_staking_reward_total_happy_path() { | ||
new_test_ext().execute_with(|| { | ||
CurrentEraInfo::<Test>::set(RewardEraInfo { era_index: 11, started_at: 100 }); | ||
// this calls the implementation in the pallet | ||
assert_eq!(Ok(5u64), Capacity::staking_reward_total(1, 5u32, 10u32)); | ||
let proof = H256::random(); | ||
let payload: StakingRewardClaim<Test> = StakingRewardClaim { | ||
claimed_reward: 1, | ||
staking_account_end_state: StakingAccountDetails { | ||
active: 1, | ||
total: 1, | ||
unlocking: Default::default(), | ||
staking_type: MaximumCapacity, | ||
last_rewards_claimed_at: None, | ||
stake_change_unlocking: Default::default(), | ||
}, | ||
from_era: 1, | ||
to_era: 5, | ||
}; | ||
assert!(Capacity::validate_staking_reward_claim(4, proof, payload)); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn test_payout_eligible() { | ||
new_test_ext().execute_with(|| { | ||
let is_eligible = Capacity::payout_eligible(1); | ||
assert!(!is_eligible); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn test_reward_pool_size_happy_path() { | ||
struct TestCase { | ||
total_staked: u64, | ||
expected_reward_pool: u64, | ||
} | ||
new_test_ext().execute_with(|| { | ||
let test_cases: Vec<TestCase> = vec![ | ||
TestCase { total_staked: 0, expected_reward_pool: 0 }, | ||
TestCase { total_staked: 4, expected_reward_pool: 0 }, | ||
TestCase { total_staked: 10, expected_reward_pool: 1 }, | ||
TestCase { total_staked: 333, expected_reward_pool: 33 }, | ||
]; | ||
let era = 20u32; | ||
CurrentEraInfo::<Test>::set(RewardEraInfo { era_index: era, started_at: 200u32 }); | ||
for tc in test_cases { | ||
StakingRewardPool::<Test>::insert( | ||
era, | ||
RewardPoolInfo { | ||
total_staked_token: tc.total_staked, | ||
total_reward_pool: 0u64, | ||
unclaimed_balance: 0u64, | ||
}, | ||
); | ||
assert_eq!(Ok(tc.expected_reward_pool), Capacity::reward_pool_size()); | ||
} | ||
}) | ||
} | ||
|
||
#[test] | ||
fn test_staking_reward_total_era_out_of_range() { | ||
new_test_ext().execute_with(|| { | ||
CurrentEraInfo::<Test>::set(RewardEraInfo { era_index: 10, started_at: 100 }); | ||
assert_err!(Capacity::staking_reward_total(1, 5u32, 4u32), Error::<Test>::EraOutOfRange); | ||
assert_err!(Capacity::staking_reward_total(1, 15u32, 5u32), Error::<Test>::EraOutOfRange); | ||
assert_err!(Capacity::staking_reward_total(1, 5u32, 13u32), Error::<Test>::EraOutOfRange); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters