Skip to content

Commit

Permalink
Stake test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Oct 5, 2023
1 parent 42a7075 commit 6bf7604
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
10 changes: 10 additions & 0 deletions pallets/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,20 @@ pub(crate) fn advance_to_era(era: EraNumber) {
ActiveProtocolState::<Test>::mutate(|state| state.era = era);
}

/// Advance blocks until next era has been reached.
pub(crate) fn advance_to_next_era() {
advance_to_era(ActiveProtocolState::<Test>::get().era + 1);
}

/// Advance blocks until the specified period has been reached.
///
/// Function has no effect if period is already passed.
pub(crate) fn advance_to_period(period: PeriodNumber) {
// TODO: Properly implement this later when additional logic has been implemented
ActiveProtocolState::<Test>::mutate(|state| state.period_info.number = period);
}

/// Advance blocks until next period has been reached.
pub(crate) fn advance_to_next_period() {
advance_to_period(ActiveProtocolState::<Test>::get().period_info.number + 1);
}
3 changes: 2 additions & 1 deletion pallets/dapp-staking-v3/src/test/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,6 @@ pub(crate) fn assert_stake(
// TODO: since default value is all zeros, maybe we can just skip the branching code and do it once?
match pre_contract_stake.last_stake_period() {
Some(last_stake_period) if last_stake_period == stake_period => {
assert_eq!(post_contract_stake.len(), pre_contract_stake.len());
assert_eq!(
post_contract_stake.total_staked_amount(stake_period),
pre_contract_stake.total_staked_amount(stake_period) + amount,
Expand Down Expand Up @@ -558,6 +557,8 @@ pub(crate) fn assert_stake(
assert_eq!(post_contract_stake.last_stake_period(), Some(stake_period));
assert_eq!(post_contract_stake.last_stake_era(), Some(stake_era));

// TODO: expand this check to compare inner slices as well!

// 4. verify era info
// =========================
// =========================
Expand Down
39 changes: 28 additions & 11 deletions pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ fn unlock_basic_example_is_ok() {
assert_unlock(account, first_unlock_amount);

// Advance era and unlock additional amount
advance_to_era(ActiveProtocolState::<Test>::get().era + 1);
advance_to_next_era();
assert_unlock(account, first_unlock_amount);

// Lock a bit more, and unlock again
Expand All @@ -384,7 +384,7 @@ fn unlock_with_remaining_amount_below_threshold_is_ok() {
let account = 2;
let lock_amount = 101;
assert_lock(account, lock_amount);
advance_to_era(ActiveProtocolState::<Test>::get().era + 1);
advance_to_next_era();
assert_lock(account, lock_amount);
advance_to_era(ActiveProtocolState::<Test>::get().era + 3);

Expand All @@ -406,7 +406,7 @@ fn unlock_with_amount_higher_than_avaiable_is_ok() {
let account = 2;
let lock_amount = 101;
assert_lock(account, lock_amount);
advance_to_era(ActiveProtocolState::<Test>::get().era + 1);
advance_to_next_era();
assert_lock(account, lock_amount);

// TODO: Hacky, maybe improve later when staking is implemented?
Expand Down Expand Up @@ -445,7 +445,7 @@ fn unlock_advanced_examples_are_ok() {
assert_unlock(account, unlock_amount);

// Advance era and unlock additional amount
advance_to_era(ActiveProtocolState::<Test>::get().era + 1);
advance_to_next_era();
assert_unlock(account, unlock_amount * 2);

// Advance few more eras, and unlock everything
Expand All @@ -456,7 +456,7 @@ fn unlock_advanced_examples_are_ok() {
.is_zero());

// Advance one more era and ensure we can still lock & unlock
advance_to_era(ActiveProtocolState::<Test>::get().era + 1);
advance_to_next_era();
assert_lock(account, lock_amount);
assert_unlock(account, unlock_amount);
})
Expand All @@ -468,7 +468,7 @@ fn unlock_everything_with_active_stake_fails() {
let account = 2;
let lock_amount = 101;
assert_lock(account, lock_amount);
advance_to_era(ActiveProtocolState::<Test>::get().era + 1);
advance_to_next_era();

// We stake so the amount is just below the minimum locked amount, causing full unlock impossible.
let minimum_locked_amount: Balance =
Expand Down Expand Up @@ -500,7 +500,7 @@ fn unlock_with_zero_amount_fails() {
let account = 2;
let lock_amount = 101;
assert_lock(account, lock_amount);
advance_to_era(ActiveProtocolState::<Test>::get().era + 1);
advance_to_next_era();

// Unlock with zero fails
assert_noop!(
Expand Down Expand Up @@ -711,10 +711,27 @@ fn stake_basic_example_is_ok() {
let lock_amount = 300;
assert_lock(account, lock_amount);

// Stake some amount
let first_stake_amount = 100;
assert_stake(account, &smart_contract, first_stake_amount);
// 1st scenario - stake some amount, and then some more
let (stake_amount_1, stake_amount_2) = (31, 29);
assert_stake(account, &smart_contract, stake_amount_1);
assert_stake(account, &smart_contract, stake_amount_2);

// continue here
// 2nd scenario - stake in the next era
advance_to_next_era();
let stake_amount_3 = 23;
assert_stake(account, &smart_contract, stake_amount_3);

// 3rd scenario - advance era again but create a gap, and then stake
advance_to_era(ActiveProtocolState::<Test>::get().era + 3);
let stake_amount_4 = 19;
assert_stake(account, &smart_contract, stake_amount_4);

// 4th scenario - advance period, and stake
// TODO: Hacky! Improve this when proper era/period transition handling is implemented!
// advance_to_next_era();
// advance_to_next_period();
// let stake_amount_5 = 17;
// assert_stake(account, &smart_contract, stake_amount_5);
// TODO: this can only be tested after reward claiming has been implemented!!!
})
}
4 changes: 2 additions & 2 deletions pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,8 +962,8 @@ impl ContractStakingInfoSeries {

/// TODO
#[cfg(test)]
pub fn into_inner(self) -> Vec<ContractStakingInfo> {
self.0.into_inner()
pub fn inner(&self) -> Vec<ContractStakingInfo> {
self.0.clone().into_inner()
}

/// Length of the series.
Expand Down

0 comments on commit 6bf7604

Please sign in to comment.