Skip to content

Commit

Permalink
Additional scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Oct 9, 2023
1 parent b079603 commit 3207039
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 30 deletions.
8 changes: 6 additions & 2 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,6 @@ pub mod pallet {
_ => Error::<T>::InternalUnstakeError,
})?;

// TODO: if it's full unstake, entry must be removed!
// 2.
// Update `StakerInfo` storage with the reduced stake amount on the specified contract.
let new_staking_info = match StakerInfo::<T>::get(&account, &smart_contract) {
Expand Down Expand Up @@ -897,9 +896,14 @@ pub mod pallet {
// 5.
// Update remaining storage entries
Self::update_ledger(&account, ledger);
StakerInfo::<T>::insert(&account, &smart_contract, new_staking_info);
ContractStake::<T>::insert(&smart_contract, contract_stake_info);

if new_staking_info.is_empty() {
StakerInfo::<T>::remove(&account, &smart_contract);
} else {
StakerInfo::<T>::insert(&account, &smart_contract, new_staking_info);
}

Self::deposit_event(Event::<T>::Unstake {
account,
smart_contract,
Expand Down
60 changes: 33 additions & 27 deletions pallets/dapp-staking-v3/src/test/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ pub(crate) fn assert_unstake(
let _unstake_era = pre_snapshot.active_protocol_state.era;
let unstake_period = pre_snapshot.active_protocol_state.period_number();
let unstake_period_type = pre_snapshot.active_protocol_state.period_type();
let is_full_unstake = pre_staker_info.total_staked_amount() == amount;

// Unstake from smart contract & verify event
assert_ok!(DappStaking::unstake(
Expand All @@ -614,10 +615,6 @@ pub(crate) fn assert_unstake(
// Verify post-state
let post_snapshot = MemorySnapshot::new();
let post_ledger = post_snapshot.ledger.get(&account).unwrap();
let post_staker_info = post_snapshot
.staker_info
.get(&(account, *smart_contract))
.expect("Entry must exist since 'stake' operation was successfull.");
let post_contract_stake = post_snapshot
.contract_stake
.get(&smart_contract)
Expand All @@ -643,28 +640,39 @@ pub(crate) fn assert_unstake(
// 2. verify staker info
// =====================
// =====================
assert_eq!(post_staker_info.period_number(), unstake_period);
assert_eq!(
post_staker_info.total_staked_amount(),
pre_staker_info.total_staked_amount() - amount,
"Total staked amount must decrease by the 'amount'"
);
assert_eq!(
post_staker_info.staked_amount(unstake_period_type),
pre_staker_info
.staked_amount(unstake_period_type)
.saturating_sub(amount),
"Staked amount must decrease by the 'amount'"
);
if is_full_unstake {
assert!(
!StakerInfo::<Test>::contains_key(&account, smart_contract),
"Entry must be deleted since it was a full unstake."
);
} else {
let post_staker_info = post_snapshot
.staker_info
.get(&(account, *smart_contract))
.expect("Entry must exist since 'stake' operation was successfull and it wasn't a full unstake.");
assert_eq!(post_staker_info.period_number(), unstake_period);
assert_eq!(
post_staker_info.total_staked_amount(),
pre_staker_info.total_staked_amount() - amount,
"Total staked amount must decrease by the 'amount'"
);
assert_eq!(
post_staker_info.staked_amount(unstake_period_type),
pre_staker_info
.staked_amount(unstake_period_type)
.saturating_sub(amount),
"Staked amount must decrease by the 'amount'"
);

let is_loyal = !(unstake_period_type == PeriodType::BuildAndEarn
&& post_staker_info.staked_amount(PeriodType::Voting)
< pre_staker_info.staked_amount(PeriodType::Voting));
assert_eq!(
post_staker_info.is_loyal(),
is_loyal,
"If 'Voting' stake amount is reduced in B&E period, loyalty flag must be set to false."
);
let is_loyal = !(unstake_period_type == PeriodType::BuildAndEarn
&& post_staker_info.staked_amount(PeriodType::Voting)
< pre_staker_info.staked_amount(PeriodType::Voting));
assert_eq!(
post_staker_info.is_loyal(),
is_loyal,
"If 'Voting' stake amount is reduced in B&E period, loyalty flag must be set to false."
);
}

// 3. verify contract stake
// =========================
Expand All @@ -691,8 +699,6 @@ pub(crate) fn assert_unstake(
if pre_era_info.total_staked_amount() < amount {
assert!(post_era_info.total_staked_amount().is_zero());
} else {
println!("pre_era_info: {:?}", pre_era_info);
println!("post_era_info: {:?}", post_era_info);
assert_eq!(
post_era_info.total_staked_amount(),
pre_era_info.total_staked_amount() - amount,
Expand Down
9 changes: 8 additions & 1 deletion pallets/dapp-staking-v3/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::test::mock::*;
use crate::test::testing_utils::*;
use crate::{
pallet as pallet_dapp_staking, ActiveProtocolState, DAppId, EraNumber, Error, IntegratedDApps,
Ledger, NextDAppId, PeriodType,
Ledger, NextDAppId, PeriodType, StakerInfo,
};

use frame_support::{assert_noop, assert_ok, error::BadOrigin, traits::Get};
Expand Down Expand Up @@ -993,6 +993,13 @@ fn unstake_basic_example_is_ok() {
advance_to_era(ActiveProtocolState::<Test>::get().era + 3);
assert_unstake(account, &smart_contract, unstake_amount_3);
assert_unstake(account, &smart_contract, unstake_amount_2);

// 4th scenario - perform a full unstake
advance_to_next_era();
let full_unstake_amount = StakerInfo::<Test>::get(&account, &smart_contract)
.unwrap()
.total_staked_amount();
assert_unstake(account, &smart_contract, full_unstake_amount);
})
}

Expand Down
5 changes: 5 additions & 0 deletions pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,11 @@ impl SingularStakingInfo {
pub fn period_number(&self) -> PeriodNumber {
self.period
}

/// `true` if no stake exists, `false` otherwise.
pub fn is_empty(&self) -> bool {
self.vp_staked_amount.is_zero() && self.bep_staked_amount.is_zero()
}
}

/// Information about how much was staked on a contract during a specific era or period.
Expand Down

0 comments on commit 3207039

Please sign in to comment.