Skip to content

Commit

Permalink
Fix for incorrect loyalty flag setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Jan 16, 2024
1 parent b27118b commit 307d45a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
13 changes: 9 additions & 4 deletions pallets/dapp-staking-v3/src/test/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,9 +703,14 @@ pub(crate) fn assert_unstake(
);

let is_loyal = pre_staker_info.is_loyal()
&& !(unstake_subperiod == Subperiod::BuildAndEarn
&& post_staker_info.staked_amount(Subperiod::Voting)
< pre_staker_info.staked_amount(Subperiod::Voting));
&& match unstake_subperiod {
Subperiod::Voting => !post_staker_info.staked_amount(Subperiod::Voting).is_zero(),
Subperiod::BuildAndEarn => {
post_staker_info.staked_amount(Subperiod::Voting)
== pre_staker_info.staked_amount(Subperiod::Voting)
}
};

assert_eq!(
post_staker_info.is_loyal(),
is_loyal,
Expand Down Expand Up @@ -782,7 +787,7 @@ pub(crate) fn assert_claim_staker_rewards(account: AccountId) {
.earliest_staked_era()
.expect("Entry must exist, otherwise 'claim' is invalid.");

// Get the apprropriate era rewards span for the 'first era'
// Get the appropriate era rewards span for the 'first era'
let era_span_length: EraNumber = <Test as Config>::EraRewardSpanLength::get();
let era_span_index = first_claim_era - (first_claim_era % era_span_length);
let era_rewards_span = pre_snapshot
Expand Down
13 changes: 8 additions & 5 deletions pallets/dapp-staking-v3/src/test/tests_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ fn account_ledger_staked_amount_for_type_works() {
build_and_earn_1
);

// Inocrrect period should simply return 0
// Incorrect period should simply return 0
assert!(acc_ledger
.staked_amount_for_type(Subperiod::Voting, period - 1)
.is_zero());
Expand Down Expand Up @@ -816,7 +816,7 @@ fn account_ledger_add_stake_amount_too_large_amount_fails() {
Err(AccountLedgerError::UnavailableStakeFunds)
);

// Additional check - have some active stake, and then try to overstake
// Additional check - have some active stake, and then try to stake more than available
assert!(acc_ledger
.add_stake_amount(lock_amount - 2, era_1, period_info_1)
.is_ok());
Expand Down Expand Up @@ -2113,15 +2113,18 @@ fn singular_staking_info_unstake_during_voting_is_ok() {
"Stake era should remain valid."
);

// Fully unstake, attempting to undersaturate, and ensure loyalty flag is still true.
// Fully unstake, attempting to underflow, and ensure loyalty flag has been removed.
let era_2 = era_1 + 2;
let remaining_stake = staking_info.total_staked_amount();
assert_eq!(
staking_info.unstake(remaining_stake + 1, era_2, Subperiod::Voting),
(remaining_stake, Balance::zero())
);
assert!(staking_info.total_staked_amount().is_zero());
assert!(staking_info.is_loyal());
assert!(
!staking_info.is_loyal(),
"Loyalty flag should have been removed since it was full unstake."
);
assert_eq!(staking_info.era(), era_2);
}

Expand Down Expand Up @@ -2507,7 +2510,7 @@ fn contract_stake_amount_unstake_is_ok() {
);
assert!(
contract_stake.staked_future.is_none(),
"future enry should remain 'None'"
"future entry should remain 'None'"
);

// 4th scenario - do a full unstake with existing future entry, expect a cleanup
Expand Down
6 changes: 4 additions & 2 deletions pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,10 @@ impl SingularStakingInfo {
self.staked.era = self.staked.era.max(current_era);

self.loyal_staker = self.loyal_staker
&& (subperiod == Subperiod::Voting
|| subperiod == Subperiod::BuildAndEarn && self.staked.voting == snapshot.voting);
&& match subperiod {
Subperiod::Voting => !self.staked.voting.is_zero(),
Subperiod::BuildAndEarn => self.staked.voting == snapshot.voting,
};

// Amount that was unstaked
(
Expand Down

0 comments on commit 307d45a

Please sign in to comment.