Skip to content

Commit

Permalink
fix: move from unregistered
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 11, 2025
1 parent 4bf6ec9 commit 9ded185
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
19 changes: 12 additions & 7 deletions pallets/dapp-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,21 +1520,26 @@ pub mod pallet {
let current_era = protocol_state.era;

// Extract total staked amount on the specified unregistered contract
let (amount, stake_amount_iter, preserved_bonus_status) =
let (amount, unstake_amount_iter, preserved_bonus_status) =
match StakerInfo::<T>::get(&account, &smart_contract) {
Some(mut staking_info) => {
ensure!(
staking_info.period_number() == protocol_state.period_number(),
Error::<T>::UnstakeFromPastPeriod
);

let amount = staking_info.total_staked_amount();
let preserved_bonus_status = staking_info.bonus_status;
// This need to be built before 'unstake', otherwise voting amount is converted into B&E amount
let unstake_amount_iter: Vec<StakeAmount> =
vec![staking_info.previous_staked, staking_info.staked]
.into_iter()
.filter(|stake_amount| !stake_amount.is_empty())
.collect();
let amount = staking_info.staked.total();

let (stake_amount_iter, _) =
staking_info.unstake(amount, current_era, protocol_state.subperiod());
staking_info.unstake(amount, current_era, protocol_state.subperiod());

(amount, stake_amount_iter, preserved_bonus_status)
(amount, unstake_amount_iter, preserved_bonus_status)
}
None => {
return Err(Error::<T>::NoStakingInfo.into());
Expand All @@ -1558,14 +1563,14 @@ pub mod pallet {
// This means 'fake' stake total amount has been kept until now, even though contract was unregistered.
// Although strange, it's been requested to keep it like this from the team.
CurrentEraInfo::<T>::mutate(|era_info| {
era_info.unstake_amount(stake_amount_iter.clone());
era_info.unstake_amount(unstake_amount_iter.clone());
});

// Update remaining storage entries
Self::update_ledger(&account, ledger)?;
StakerInfo::<T>::remove(&account, &smart_contract);

let unstake_amount = stake_amount_iter
let unstake_amount = unstake_amount_iter
.iter()
.max_by(|a, b| a.total().cmp(&b.total()))
.expect("At least one value exists, otherwise we wouldn't be here.");
Expand Down
16 changes: 15 additions & 1 deletion pallets/dapp-staking/src/test/testing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ pub(crate) fn assert_move_stake(
// =====================
// =====================

let (unstake_amount_entries, bonus_status) = assert_staker_info_and_unstake(
let (amount_entries, bonus_status) = assert_staker_info_and_unstake(
&pre_snapshot,
&post_snapshot,
account,
Expand All @@ -837,6 +837,20 @@ pub(crate) fn assert_move_stake(
is_full_move_from_source,
);

let pre_staker_info = pre_snapshot
.staker_info
.get(&(account, source_contract.clone()))
.expect("Entry must exist since 'move' is being called.");

let unstake_amount_entries = if is_source_unregistered {
vec![pre_staker_info.previous_staked, pre_staker_info.staked]
.into_iter()
.filter(|stake_amount| !stake_amount.is_empty())
.collect::<Vec<StakeAmount>>()
} else {
amount_entries
};

let unstake_amount_entries_clone = unstake_amount_entries.clone();
let stake_amount = unstake_amount_entries_clone
.iter()
Expand Down
16 changes: 16 additions & 0 deletions pallets/dapp-staking/src/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3782,6 +3782,22 @@ fn move_stake_from_unregistered_contract_is_ok() {
&dest_contract,
1, // the amount is not important for an unregistered contract, everything is moved
);

let default_bonus_status = BonusStatusWrapperFor::<Test>::default().0;
assert!(StakerInfo::<Test>::get(&account, &source_contract).is_none());
let expected_dest_staking_info = SingularStakingInfo {
previous_staked: StakeAmount::default(),
staked: StakeAmount {
voting: partial_stake_1,
build_and_earn: partial_stake_2,
era: 3,
period: 1,
},
bonus_status: default_bonus_status,
};
let dest_staking_info = StakerInfo::<Test>::get(&account, &dest_contract)
.expect("Should exist after a successful move operation");
assert_eq!(dest_staking_info, expected_dest_staking_info);
})
}

Expand Down

0 comments on commit 9ded185

Please sign in to comment.