Skip to content

Commit

Permalink
passing tests with unstake failing if unclaimed rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
shannonwells committed May 16, 2024
1 parent 39afae6 commit 75cc0ff
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
16 changes: 16 additions & 0 deletions pallets/capacity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ pub mod pallet {

ensure!(requested_amount > Zero::zero(), Error::<T>::UnstakedAmountIsZero);

ensure!(!Self::has_unclaimed_rewards(&unstaker), Error::<T>::MustFirstClaimRewards);

let (actual_amount, staking_type) =
Self::decrease_active_staking_balance(&unstaker, requested_amount)?;
Self::add_unlock_chunk(&unstaker, actual_amount)?;
Expand Down Expand Up @@ -1068,6 +1070,20 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub(crate) fn has_unclaimed_rewards(account: &T::AccountId) -> bool {
let current_era = Self::get_current_era().era_index;
match Self::get_staking_history_for(account) {
Some(provider_boost_history) => {
match provider_boost_history.count() {
0usize => false,
// they staked before the current era, so they have unclaimed rewards.
1usize => provider_boost_history.get_entry_for_era(&current_era).is_none(),
_ => true,
}
},
None => false,
} // 1r
}
#[allow(unused)]
pub(crate) fn check_for_unclaimed_rewards(
account: &T::AccountId,
Expand Down
27 changes: 27 additions & 0 deletions pallets/capacity/src/tests/rewards_provider_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,30 @@ fn check_for_unclaimed_rewards_has_eligible_rewards() {
}
})
}

#[test]
fn has_unclaimed_rewards_works() {
new_test_ext().execute_with(|| {
let account = 10_000u64;
let target: MessageSourceId = 10;
let amount = 1_000u64;

assert!(!Capacity::has_unclaimed_rewards(&account));

// staking 1k as of block 1, era 1
setup_provider(&account, &target, &amount, ProviderBoost);
assert!(!Capacity::has_unclaimed_rewards(&account));

// staking 2k as of block 11, era 2
run_to_block(11);
assert_ok!(Capacity::provider_boost(RuntimeOrigin::signed(account), target, amount));
assert!(Capacity::has_unclaimed_rewards(&account));

// staking 3k as of era 4, block 31
run_to_block(31);
assert!(Capacity::has_unclaimed_rewards(&account));

run_to_block(61);
assert!(Capacity::has_unclaimed_rewards(&account));
})
}
70 changes: 61 additions & 9 deletions pallets/capacity/src/tests/unstaking_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{mock::*, testing_utils::*};
use crate as pallet_capacity;
use crate::{
CapacityDetails, FreezeReason, ProviderBoostHistory, RewardPoolInfo, StakingDetails,
StakingTargetDetails, StakingType, UnlockChunk,
StakingTargetDetails, StakingType, StakingType::ProviderBoost, UnlockChunk,
};
use common_primitives::msa::MessageSourceId;
use frame_support::{
Expand Down Expand Up @@ -158,8 +158,17 @@ fn unstake_errors_unstaking_amount_is_zero() {
});
}

fn fill_unstake_unlock_chunks(token_account: u64, target: MessageSourceId, unstaking_amount: u64) {
for _n in 0..<Test as Config>::MaxUnlockingChunks::get() {
assert_ok!(Capacity::unstake(
RuntimeOrigin::signed(token_account),
target,
unstaking_amount
));
}
}
#[test]
fn unstake_errors_max_unlocking_chunks_exceeded() {
fn unstake_errors_max_unlocking_chunks_exceeded_stake() {
new_test_ext().execute_with(|| {
let token_account = 200;
let target: MessageSourceId = 1;
Expand All @@ -170,13 +179,31 @@ fn unstake_errors_max_unlocking_chunks_exceeded() {

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

for _n in 0..<Test as Config>::MaxUnlockingChunks::get() {
assert_ok!(Capacity::unstake(
RuntimeOrigin::signed(token_account),
target,
unstaking_amount
));
}
fill_unstake_unlock_chunks(token_account, target, unstaking_amount);

assert_noop!(
Capacity::unstake(RuntimeOrigin::signed(token_account), target, unstaking_amount),
Error::<Test>::MaxUnlockingChunksExceeded
);
});
}
#[test]
fn unstake_errors_max_unlocking_chunks_exceeded_provider_boost() {
new_test_ext().execute_with(|| {
let token_account = 200;
let target: MessageSourceId = 1;
let staking_amount = 60;
let unstaking_amount = 10;

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

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

fill_unstake_unlock_chunks(token_account, target, unstaking_amount);

assert_noop!(
Capacity::unstake(RuntimeOrigin::signed(token_account), target, unstaking_amount),
Expand Down Expand Up @@ -337,6 +364,7 @@ fn unstake_maximum_does_not_change_provider_boost_history() {
register_provider(target1, String::from("Test Target"));

assert_ok!(Capacity::stake(RuntimeOrigin::signed(staker), target1, 1_000));
assert_ok!(Capacity::unstake(RuntimeOrigin::signed(staker), target1, 500));
assert!(Capacity::get_staking_history_for(staker).is_none());
})
}
Expand Down Expand Up @@ -381,3 +409,27 @@ fn get_amount_staked_for_era_works() {
// (StakingRewardsPastErasMax = 5 in mock) returns zero.
assert_eq!(staking_history.get_amount_staked_for_era(&9u32), 0u64);
}

#[test]
fn unstake_fails_if_provider_boosted_and_have_unclaimed_rewards() {
new_test_ext().execute_with(|| {
let account = 10_000u64;
let target: MessageSourceId = 10;
let amount = 1_000u64;

// staking 1k as of block 1, era 1
setup_provider(&account, &target, &amount, ProviderBoost);

// staking 2k as of block 11, era 2
run_to_block(11);
assert_ok!(Capacity::provider_boost(RuntimeOrigin::signed(account), target, amount));

// staking 3k as of era 4, block 31
run_to_block(31);

assert_noop!(
Capacity::unstake(RuntimeOrigin::signed(account), target, amount),
Error::<Test>::MustFirstClaimRewards
);
})
}

0 comments on commit 75cc0ff

Please sign in to comment.