Skip to content

Commit

Permalink
unstake unlocks common storage, some tests, small refactor in test ut…
Browse files Browse the repository at this point in the history
…ility
  • Loading branch information
shannonwells committed Nov 1, 2023
1 parent 97cd585 commit 1cad250
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 37 deletions.
17 changes: 13 additions & 4 deletions pallets/capacity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ pub mod pallet {
#[pallet::getter(fn get_retargets_for)]
pub type Retargets<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, RetargetInfo<T>>;

#[pallet::storage]
#[pallet::getter(fn get_unstake_unlocking_for)]
pub type UnstakeUnlocks<T:Config> =
StorageMap<_, Twox64Concat, T::AccountId, BoundedVec<UnlockChunk<BalanceOf<T>, T::EpochNumber>, T::MaxUnlockingChunks>>;

#[pallet::pallet]
pub struct Pallet<T>(_);

Expand Down Expand Up @@ -813,12 +818,16 @@ impl<T: Config> Pallet<T> {
current_epoch.saturating_add(thaw_period.into())
}

// TODO: alter for checking provider boost or make another function
// Cannot unstake if they don't have an account of either type
// Cannot unstake if their unlock chunks list is full; they must wait until the oldest thaw period has ended.
fn ensure_can_unstake(unstaker: &T::AccountId) -> Result<(), DispatchError> {
let staking_account: StakingAccountDetails<T> =
Self::get_staking_account_for(unstaker).ok_or(Error::<T>::NotAStakingAccount)?;
ensure!(
staking_account.unlocking.len().lt(&(T::MaxUnlockingChunks::get() as usize)),
Self::get_staking_account_for(unstaker).is_some() ||
Self::get_boost_details_for(unstaker).is_some(),
Error::<T>::NotAStakingAccount
);
ensure!(
!Self::get_unstake_unlocking_for(unstaker).unwrap_or_default().is_full(),
Error::<T>::MaxUnlockingChunksExceeded
);
Ok(())
Expand Down
16 changes: 8 additions & 8 deletions pallets/capacity/src/tests/change_staking_target_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn do_retarget_happy_path() {
let to_amount = 300u64;
let to_msa: MessageSourceId = 2;
let staking_type = ProviderBoost;
set_era_and_reward_pool_at_block(10, 500, 500, 0);
set_era_and_reward_pool_at_block(10,500,0);
setup_provider(&staker, &from_msa, &from_amount, staking_type.clone());
setup_provider(&staker, &to_msa, &to_amount, staking_type.clone());

Expand All @@ -84,7 +84,7 @@ fn do_retarget_flip_flop() {
let from_amount = 600u64;
let to_amount = 300u64;
let to_msa: MessageSourceId = 2;
set_era_and_reward_pool_at_block(10, 500, 500, 0);
set_era_and_reward_pool_at_block(10,500,0);
setup_provider(&staker, &from_msa, &from_amount, ProviderBoost);
setup_provider(&staker, &to_msa, &to_amount, ProviderBoost);

Expand Down Expand Up @@ -121,7 +121,7 @@ fn check_retarget_rounding_errors() {
let from_amount = 666u64;
let to_amount = 301u64;
let to_msa: MessageSourceId = 2;
set_era_and_reward_pool_at_block(10, 500, 500, 0);
set_era_and_reward_pool_at_block(10,500,0);

setup_provider(&staker, &from_msa, &from_amount, ProviderBoost);
setup_provider(&staker, &to_msa, &to_amount, ProviderBoost);
Expand Down Expand Up @@ -165,7 +165,7 @@ fn check_retarget_multiple_stakers() {
let amt1 = 192u64;
let amt2 = 313u64;

set_era_and_reward_pool_at_block(10, 500, 500, 0);
set_era_and_reward_pool_at_block(10,500,0);
setup_provider(&staker_10k, &from_msa, &647u64, ProviderBoost);
setup_provider(&staker_500, &to_msa, &293u64, ProviderBoost);
assert_ok!(Capacity::stake(RuntimeOrigin::signed(staker_600.clone()), from_msa, 479u64,));
Expand Down Expand Up @@ -244,7 +244,7 @@ fn change_staking_starget_emits_event_on_success() {
let from_amount = 20u64;
let to_amount = from_amount / 2;
let to_msa: MessageSourceId = 2;
set_era_and_reward_pool_at_block(10, 500, 500, 0);
set_era_and_reward_pool_at_block(10, 500, 0);
setup_provider(&staker, &from_msa, &from_amount, ProviderBoost);
setup_provider(&staker, &to_msa, &to_amount, ProviderBoost);

Expand All @@ -269,7 +269,7 @@ fn change_staking_target_errors_if_too_many_changes_before_thaw() {
let staker = 200u64;
let from_msa: MessageSourceId = 1;
let to_msa: MessageSourceId = 2;
set_era_and_reward_pool_at_block(0, 0, 0, 0);
set_era_and_reward_pool_at_block(0, 0, 0);

let max_chunks: u32 = <Test as Config>::MaxRetargetsPerRewardEra::get();
let staking_amount = ((max_chunks + 2u32) * 10u32) as u64;
Expand Down Expand Up @@ -305,7 +305,7 @@ fn change_staking_target_garbage_collects_thawed_chunks() {
let staking_account = 200u64;
let from_target: MessageSourceId = 3;
let to_target: MessageSourceId = 4;
set_era_and_reward_pool_at_block(0, 0, 0, 0);
set_era_and_reward_pool_at_block(0, 0, 0);

setup_provider(&staking_account, &from_target, &staked_amount, ProviderBoost);
setup_provider(&staking_account, &to_target, &staked_amount, ProviderBoost);
Expand Down Expand Up @@ -335,7 +335,7 @@ fn change_staking_target_test_parametric_validity() {
new_test_ext().execute_with(|| {
let staked_amount = 10u64;
let from_account = 200u64;
set_era_and_reward_pool_at_block(0, 0, 0, 0);
set_era_and_reward_pool_at_block(0, 0, 0);

StakingAccountLedger::<Test>::insert(
from_account,
Expand Down
4 changes: 2 additions & 2 deletions pallets/capacity/src/tests/eras_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::tests::testing_utils::setup_provider;
#[test]
fn start_new_era_if_needed_updates_era_info_and_limits_reward_pool_size() {
new_test_ext().execute_with(|| {
set_era_and_reward_pool_at_block(1, 0, 1, 10_000);
set_era_and_reward_pool_at_block(1, 0, 10_000);
system_run_to_block(9);
for i in 1..4 {
let block_decade = i * 10;
Expand All @@ -32,7 +32,7 @@ fn start_new_era_if_needed_updates_era_info_and_limits_reward_pool_size() {
#[test]
fn start_new_era_if_needed_updates_reward_pool() {
new_test_ext().execute_with(|| {
set_era_and_reward_pool_at_block(1, 0, 1, 10_000);
set_era_and_reward_pool_at_block(1, 0, 10_000);
system_run_to_block(8);
let staker = 10_000;
let provider_msa: MessageSourceId = 1;
Expand Down
12 changes: 6 additions & 6 deletions pallets/capacity/src/tests/provider_boost_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn provider_boost_extrinsic_works() {
let amount = 200;
let capacity = 1;
register_provider(target, String::from("Foo"));
set_era_and_reward_pool_at_block(10, 100, 100, 0);
set_era_and_reward_pool_at_block(10,100,0);
assert_ok!(Capacity::provider_boost(RuntimeOrigin::signed(account), target, amount));

// Check that StakingAccountLedger is updated.
Expand Down Expand Up @@ -52,7 +52,7 @@ fn assert_successful_increase_stake_and_issue_boost(
expected_target_token: u64,
expected_capacity: u64,
) {
set_era_and_reward_pool_at_block(10, 100, 100, 0);
set_era_and_reward_pool_at_block(10, 100, 0);
let staker = 10_000; // has 10_000 token
let mut boost_account = BoostingAccountDetails::<Test>::default();

Expand Down Expand Up @@ -86,7 +86,7 @@ fn provider_boost_adjusts_reward_pool_total() {
let amount = 500;
let current_era = 10;
register_provider(target, String::from("Foo"));
set_era_and_reward_pool_at_block(current_era, 100, 100, 0);
set_era_and_reward_pool_at_block(current_era, 100, 0);
assert_ok!(Capacity::provider_boost(RuntimeOrigin::signed(account), target, amount));

let reward_pool_info = Capacity::get_reward_pool_for_era(current_era).unwrap();
Expand Down Expand Up @@ -132,7 +132,7 @@ fn reward_pool_boost_and_rollover() {
let current_era = 99u32;
let current_block = 500u32;
let initial_total_staked= 500u64;
set_era_and_reward_pool_at_block(current_era, current_era, current_block, initial_total_staked);
set_era_and_reward_pool_at_block(current_era, current_block, initial_total_staked);


system_run_to_block(504);
Expand Down Expand Up @@ -172,7 +172,7 @@ fn calling_stake_on_provider_boost_target_errors() {
let target: MessageSourceId = 1;
let amount = 200;
register_provider(target, String::from("Bear"));
set_era_and_reward_pool_at_block(10, 100, 100, 0);
set_era_and_reward_pool_at_block(10, 100, 0);

assert_ok!(Capacity::provider_boost(RuntimeOrigin::signed(account), target, amount));
assert_noop!(
Expand All @@ -188,7 +188,7 @@ fn calling_provider_boost_on_staked_target_errors() {
let target: MessageSourceId = 1;
let amount = 200;
register_provider(target, String::from("Foobear"));
set_era_and_reward_pool_at_block(10, 100, 100, 0);
set_era_and_reward_pool_at_block(10, 100, 0);
assert_ok!(Capacity::stake(RuntimeOrigin::signed(account), target, amount));
assert_noop!(
Capacity::provider_boost(RuntimeOrigin::signed(account), target, 50),
Expand Down
14 changes: 7 additions & 7 deletions pallets/capacity/src/tests/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn setup_provider(

pub fn assert_capacity_and_target_details(
target: &MessageSourceId,
expected_target_token: u64,
expected_target_amount: u64,
expected_capacity: u64,
staker: &u64,
) {
Expand All @@ -105,17 +105,17 @@ pub fn assert_capacity_and_target_details(

let target_details = Capacity::get_target_for(&staker, &target).unwrap();

assert_eq!(target_details.amount, expected_target_token);
assert_eq!(target_details.amount, expected_target_amount);
assert_eq!(target_details.capacity, expected_capacity);
}

pub fn set_era_and_reward_pool_at_block(current_era: u32, era_start: u32, current_block: u32, amount: u64) {
let initial_rpi: RewardPoolInfo<BalanceOf<Test>> = RewardPoolInfo {
pub fn set_era_and_reward_pool_at_block(era: u32, block: u32, amount: u64) {
let rpi: RewardPoolInfo<BalanceOf<Test>> = RewardPoolInfo {
total_staked_token: amount,
total_reward_pool: 0,
unclaimed_balance: 0,
};
StakingRewardPool::<Test>::insert(current_era, initial_rpi);
CurrentEraInfo::<Test>::set(RewardEraInfo { era_index: current_era, started_at: era_start });
System::set_block_number(current_block);
StakingRewardPool::<Test>::insert(era, rpi);
CurrentEraInfo::<Test>::set(RewardEraInfo { era_index: era, started_at: block });
System::set_block_number(block);
}
46 changes: 36 additions & 10 deletions pallets/capacity/src/tests/unstaking_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use sp_core::bounded::BoundedVec;
fn unstake_happy_path() {
new_test_ext().execute_with(|| {
// TODO: ProviderBoost after unstake affects reward pool info #1699
// TODO: Unstake maximized adds to common unlock chunks
let token_account = 200;
let target: MessageSourceId = 1;
let staking_amount = 100;
Expand Down Expand Up @@ -111,9 +112,9 @@ fn unstake_errors_max_unlocking_chunks_exceeded() {

register_provider(target, String::from("Test Target"));

assert_ok!(Capacity::stake(RuntimeOrigin::signed(token_account), target, staking_amount,));
assert_ok!(Capacity::stake(RuntimeOrigin::signed(token_account), target, staking_amount));

for _n in 0..<Test as pallet_capacity::Config>::MaxUnlockingChunks::get() {
for _n in 0..<Test as Config>::MaxUnlockingChunks::get() {
assert_ok!(Capacity::unstake(
RuntimeOrigin::signed(token_account),
target,
Expand Down Expand Up @@ -192,24 +193,49 @@ fn unstake_provider_boosted_target_adjusts_reward_pool_total() {
});
}

// TODO: when resuming reward pool branch
#[test]
fn unstake_provider_boosted_target_updates_staking_account_history() {
fn unstake_provider_boosted_target_updates_boost_account_history() {
new_test_ext().execute_with(|| {
assert!(false);
let initial_block = 10_000;
let initial_pool = 4_000;
let initial_era = 199;
});
}

#[test]
fn unstake_when_both_types_of_staking_correctly_updates_lock_values() {
fn unstake_when_both_types_of_staking_correctly_updates_locked_balance() {
new_test_ext().execute_with(|| {
assert!(false);
})
}

#[test]
fn unstake_provider_boosted_target_adds_to_common_unlock_chunks() {
new_test_ext().execute_with(|| {
assert!(false);
})
fn unstake_both_types_fills_up_common_unlock() {
new_test_ext().execute_with((|| {
let initial_block = 20_000;
let initial_pool = 4_000;
let initial_era = 199;
let staker = 10_000;

set_era_and_reward_pool_at_block(initial_era, initial_block, initial_pool);

let target1 = 1;
let target2 = 2;
register_provider(target1, String::from("Test Target"));
register_provider(target2, String::from("Test Target"));

assert_ok!(Capacity::stake(RuntimeOrigin::signed(staker), target1, 1_000));
assert_ok!(Capacity::provider_boost(RuntimeOrigin::signed(staker), target2, 2_000));

// max unlock chunks in mock is 4
for _i in 0..2 {
assert_ok!(Capacity::unstake(RuntimeOrigin::signed(staker), target1, 50));
assert_ok!(Capacity::unstake(RuntimeOrigin::signed(staker), target2, 50));
}
assert_noop!(
Capacity::unstake(RuntimeOrigin::signed(staker), target1, 50),
Error::<Test>::MaxUnlockingChunksExceeded
);
}))
}

0 comments on commit 1cad250

Please sign in to comment.