Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions pallets/capacity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,11 +809,18 @@ impl<T: Config> Pallet<T> {
current_epoch.saturating_add(T::EpochNumber::from(T::UnstakingThawPeriod::get()));
let mut unlocks = UnstakeUnlocks::<T>::get(unstaker).unwrap_or_default();

let unlock_chunk: UnlockChunk<BalanceOf<T>, T::EpochNumber> =
UnlockChunk { value: actual_unstaked_amount, thaw_at };
unlocks
.try_push(unlock_chunk)
.map_err(|_| Error::<T>::MaxUnlockingChunksExceeded)?;
match unlocks.iter_mut().find(|chunk| chunk.thaw_at == thaw_at) {
Some(chunk) => {
chunk.value += actual_unstaked_amount;
},
None => {
let unlock_chunk: UnlockChunk<BalanceOf<T>, T::EpochNumber> =
UnlockChunk { value: actual_unstaked_amount, thaw_at };
unlocks
.try_push(unlock_chunk)
.map_err(|_| Error::<T>::MaxUnlockingChunksExceeded)?;
},
}

UnstakeUnlocks::<T>::set(unstaker, Some(unlocks));
Ok(())
Expand Down
110 changes: 100 additions & 10 deletions pallets/capacity/src/tests/unstaking_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{mock::*, testing_utils::*};
use crate as pallet_capacity;
use crate::{
CapacityDetails, CapacityLedger, CurrentEraProviderBoostTotal, FreezeReason,
CapacityDetails, CapacityLedger, CurrentEpoch, CurrentEraProviderBoostTotal, FreezeReason,
ProviderBoostHistories, ProviderBoostHistory, StakingAccountLedger, StakingDetails,
StakingTargetDetails, StakingTargetLedger, StakingType, StakingType::ProviderBoost,
UnlockChunk, UnstakeUnlocks,
Expand Down Expand Up @@ -155,17 +155,26 @@ fn unstake_errors_unstaking_amount_is_zero() {
});
}

fn fill_unstake_unlock_chunks(token_account: u64, target: MessageSourceId, unstaking_amount: u64) {
fn fill_unstake_unlock_chunks(
token_account: u64,
target: MessageSourceId,
unstaking_amount: u64,
use_new_epoch: bool,
) {
for _n in 0..<Test as Config>::MaxUnlockingChunks::get() {
assert_ok!(Capacity::unstake(
RuntimeOrigin::signed(token_account),
target,
unstaking_amount
));
if use_new_epoch {
// this would force the thaw_at to be different
CurrentEpoch::<Test>::set(CurrentEpoch::<Test>::get() + 1);
}
}
}
#[test]
fn unstake_errors_max_unlocking_chunks_exceeded_stake() {
fn unstake_errors_max_unlocking_chunks_exceeded_stake_with_different_epochs() {
new_test_ext().execute_with(|| {
let token_account = 200;
let target: MessageSourceId = 1;
Expand All @@ -176,14 +185,50 @@ fn unstake_errors_max_unlocking_chunks_exceeded_stake() {

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

fill_unstake_unlock_chunks(token_account, target, unstaking_amount);
fill_unstake_unlock_chunks(token_account, target, unstaking_amount, true);

assert_noop!(
Capacity::unstake(RuntimeOrigin::signed(token_account), target, unstaking_amount),
Error::<Test>::MaxUnlockingChunksExceeded
);
});
}

#[test]
fn unstake_should_work_with_more_than_max_unlocking_chunks_exceeded_in_the_same_epochs() {
new_test_ext().execute_with(|| {
let token_account = 200;
let target: MessageSourceId = 1;
let staking_amount = 70;
let unstaking_amount = 10;

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

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

fill_unstake_unlock_chunks(token_account, target, unstaking_amount, false);

assert_ok!(Capacity::unstake(
RuntimeOrigin::signed(token_account),
target,
unstaking_amount
));

let max_unlocking: u32 = <Test as Config>::MaxUnlockingChunks::get();
let expected_unlocking_chunks: BoundedVec<
UnlockChunk<BalanceOf<Test>, <Test as Config>::EpochNumber>,
<Test as Config>::MaxUnlockingChunks,
> = BoundedVec::try_from(vec![UnlockChunk {
value: unstaking_amount * (max_unlocking as u64 + 1),
thaw_at: 2u32,
}])
.unwrap();

let unlocking = UnstakeUnlocks::<Test>::get(token_account).unwrap();
assert_eq!(unlocking, expected_unlocking_chunks);
});
}

#[test]
fn unstake_errors_max_unlocking_chunks_exceeded_provider_boost() {
new_test_ext().execute_with(|| {
Expand All @@ -200,7 +245,7 @@ fn unstake_errors_max_unlocking_chunks_exceeded_provider_boost() {
staking_amount
));

fill_unstake_unlock_chunks(token_account, target, unstaking_amount);
fill_unstake_unlock_chunks(token_account, target, unstaking_amount, true);

assert_noop!(
Capacity::unstake(RuntimeOrigin::signed(token_account), target, unstaking_amount),
Expand All @@ -209,6 +254,46 @@ fn unstake_errors_max_unlocking_chunks_exceeded_provider_boost() {
});
}

#[test]
fn unstake_should_work_with_more_than_max_unlocking_chunks_exceeded_in_the_same_epochs_provider_boost(
) {
new_test_ext().execute_with(|| {
let token_account = 200;
let target: MessageSourceId = 1;
let staking_amount = 70;
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, false);

assert_ok!(Capacity::unstake(
RuntimeOrigin::signed(token_account),
target,
unstaking_amount
));

let max_unlocking: u32 = <Test as Config>::MaxUnlockingChunks::get();
let expected_unlocking_chunks: BoundedVec<
UnlockChunk<BalanceOf<Test>, <Test as Config>::EpochNumber>,
<Test as Config>::MaxUnlockingChunks,
> = BoundedVec::try_from(vec![UnlockChunk {
value: unstaking_amount * (max_unlocking as u64 + 1),
thaw_at: 2u32,
}])
.unwrap();

let unlocking = UnstakeUnlocks::<Test>::get(token_account).unwrap();
assert_eq!(unlocking, expected_unlocking_chunks);
});
}

#[test]
fn unstake_errors_amount_to_unstake_exceeds_amount_staked() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -328,7 +413,7 @@ fn unstake_maximum_does_not_change_reward_pool() {
}

#[test]
fn unstake_fills_up_common_unlock_for_any_target() {
fn unstake_should_increase_the_unlock_chunk_for_any_target() {
new_test_ext().execute_with(|| {
let staker = 10_000;

Expand All @@ -345,10 +430,15 @@ fn unstake_fills_up_common_unlock_for_any_target() {
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
);
assert_ok!(Capacity::unstake(RuntimeOrigin::signed(staker), target1, 50),);

let expected_unlocking_chunks: BoundedVec<
UnlockChunk<BalanceOf<Test>, <Test as Config>::EpochNumber>,
<Test as Config>::MaxUnlockingChunks,
> = BoundedVec::try_from(vec![UnlockChunk { value: 250, thaw_at: 2u32 }]).unwrap();

let unlocking = UnstakeUnlocks::<Test>::get(staker).unwrap();
assert_eq!(unlocking, expected_unlocking_chunks);
})
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/frequency/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: Cow::Borrowed("frequency"),
impl_name: Cow::Borrowed("frequency"),
authoring_version: 1,
spec_version: 162,
spec_version: 163,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand All @@ -468,7 +468,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: Cow::Borrowed("frequency-testnet"),
impl_name: Cow::Borrowed("frequency"),
authoring_version: 1,
spec_version: 162,
spec_version: 163,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down