From 6bf7604f948297440ac0c8b42c46a41e16eeceb3 Mon Sep 17 00:00:00 2001 From: Dino Pacandi Date: Thu, 5 Oct 2023 19:52:37 +0200 Subject: [PATCH] Stake test --- pallets/dapp-staking-v3/src/test/mock.rs | 10 +++++ .../dapp-staking-v3/src/test/testing_utils.rs | 3 +- pallets/dapp-staking-v3/src/test/tests.rs | 39 +++++++++++++------ pallets/dapp-staking-v3/src/types.rs | 4 +- 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/pallets/dapp-staking-v3/src/test/mock.rs b/pallets/dapp-staking-v3/src/test/mock.rs index 4ea25996df..f0cfb42ce1 100644 --- a/pallets/dapp-staking-v3/src/test/mock.rs +++ b/pallets/dapp-staking-v3/src/test/mock.rs @@ -195,6 +195,11 @@ pub(crate) fn advance_to_era(era: EraNumber) { ActiveProtocolState::::mutate(|state| state.era = era); } +/// Advance blocks until next era has been reached. +pub(crate) fn advance_to_next_era() { + advance_to_era(ActiveProtocolState::::get().era + 1); +} + /// Advance blocks until the specified period has been reached. /// /// Function has no effect if period is already passed. @@ -202,3 +207,8 @@ pub(crate) fn advance_to_period(period: PeriodNumber) { // TODO: Properly implement this later when additional logic has been implemented ActiveProtocolState::::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::::get().period_info.number + 1); +} diff --git a/pallets/dapp-staking-v3/src/test/testing_utils.rs b/pallets/dapp-staking-v3/src/test/testing_utils.rs index db9df7ed26..0629508411 100644 --- a/pallets/dapp-staking-v3/src/test/testing_utils.rs +++ b/pallets/dapp-staking-v3/src/test/testing_utils.rs @@ -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, @@ -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 // ========================= // ========================= diff --git a/pallets/dapp-staking-v3/src/test/tests.rs b/pallets/dapp-staking-v3/src/test/tests.rs index f185173c90..e9a329bb6a 100644 --- a/pallets/dapp-staking-v3/src/test/tests.rs +++ b/pallets/dapp-staking-v3/src/test/tests.rs @@ -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::::get().era + 1); + advance_to_next_era(); assert_unlock(account, first_unlock_amount); // Lock a bit more, and unlock again @@ -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::::get().era + 1); + advance_to_next_era(); assert_lock(account, lock_amount); advance_to_era(ActiveProtocolState::::get().era + 3); @@ -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::::get().era + 1); + advance_to_next_era(); assert_lock(account, lock_amount); // TODO: Hacky, maybe improve later when staking is implemented? @@ -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::::get().era + 1); + advance_to_next_era(); assert_unlock(account, unlock_amount * 2); // Advance few more eras, and unlock everything @@ -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::::get().era + 1); + advance_to_next_era(); assert_lock(account, lock_amount); assert_unlock(account, unlock_amount); }) @@ -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::::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 = @@ -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::::get().era + 1); + advance_to_next_era(); // Unlock with zero fails assert_noop!( @@ -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::::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!!! }) } diff --git a/pallets/dapp-staking-v3/src/types.rs b/pallets/dapp-staking-v3/src/types.rs index 977cf961b3..413d667c1f 100644 --- a/pallets/dapp-staking-v3/src/types.rs +++ b/pallets/dapp-staking-v3/src/types.rs @@ -962,8 +962,8 @@ impl ContractStakingInfoSeries { /// TODO #[cfg(test)] - pub fn into_inner(self) -> Vec { - self.0.into_inner() + pub fn inner(&self) -> Vec { + self.0.clone().into_inner() } /// Length of the series.