Skip to content

Commit

Permalink
Force unstaking (#1352)
Browse files Browse the repository at this point in the history
Signed-off-by: Jamie <djptux@gmail.com>
  • Loading branch information
Dengjianping authored Oct 28, 2024
1 parent 2cf703b commit 692b5c8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
25 changes: 25 additions & 0 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,31 @@ pub mod pallet {
let delegator = ensure_signed(origin)?;
Self::delegation_cancel_request(candidate, delegator)
}

#[pallet::call_index(27)]
#[pallet::weight(<T as Config>::WeightInfo::schedule_revoke_delegation())]
/// Root only. Request to revoke an existing delegation. If successful, the delegation is scheduled
/// to be allowed to be revoked via the `execute_delegation_request` extrinsic.
pub fn force_schedule_revoke_delegation(
origin: OriginFor<T>,
delegator: T::AccountId,
collator: T::AccountId,
) -> DispatchResultWithPostInfo {
frame_system::ensure_root(origin)?;
Self::delegation_schedule_revoke(collator, delegator)
}

#[pallet::call_index(28)]
#[pallet::weight(<T as Config>::WeightInfo::execute_delegator_bond_less())]
/// Root only. Execute pending request to change an existing delegation
pub fn force_execute_delegation_request(
origin: OriginFor<T>,
delegator: T::AccountId,
candidate: T::AccountId,
) -> DispatchResultWithPostInfo {
frame_system::ensure_root(origin)?;
Self::delegation_execute_scheduled_request(candidate, delegator)
}
}

impl<T: Config> Pallet<T> {
Expand Down
71 changes: 70 additions & 1 deletion pallets/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use crate::{
DelegatorStatus, Error, Event, Range, DELEGATOR_LOCK_ID,
};
use frame_support::{assert_noop, assert_ok};
use sp_runtime::{traits::Zero, DispatchError, ModuleError, Perbill, Percent};
use sp_runtime::{
traits::{BadOrigin, Zero},
DispatchError, ModuleError, Perbill, Percent,
};

// ~~ ROOT ~~

Expand Down Expand Up @@ -2852,6 +2855,72 @@ fn cannot_cancel_leave_delegators_if_single_delegation_revoke_manually_cancelled

// SCHEDULE REVOKE DELEGATION

#[test]
fn force_revoke_delegation_event_emits_correctly() {
ExtBuilder::default()
.with_balances(vec![(1, 30), (2, 20), (3, 30)])
.with_candidates(vec![(1, 30), (3, 30)])
.with_delegations(vec![(2, 1, 10), (2, 3, 10)])
.build()
.execute_with(|| {
// non root signer cannot trigger this extrinsic
assert_noop!(
ParachainStaking::force_schedule_revoke_delegation(
RuntimeOrigin::signed(2),
2, // delegator
1 // collator
),
BadOrigin
);
// invalid delegatot will fail as well
assert_noop!(
ParachainStaking::force_schedule_revoke_delegation(
RuntimeOrigin::root(),
5, // invalid delegator
1 // collator
),
Error::<Test>::DelegatorDNE
);
// only root can trigger this extrinsic
assert_ok!(ParachainStaking::force_schedule_revoke_delegation(
RuntimeOrigin::root(),
2, // delegator
1 // collator
));
assert_last_event!(MetaEvent::ParachainStaking(
Event::DelegationRevocationScheduled {
round: 1,
delegator: 2,
candidate: 1,
scheduled_exit: 3,
}
));
roll_to(10);
// non root signer cannot trigger this extrinsic
assert_noop!(
ParachainStaking::force_execute_delegation_request(RuntimeOrigin::signed(2), 2, 1),
BadOrigin
);
// invalid delegatot will fail as well
assert_noop!(
ParachainStaking::force_execute_delegation_request(RuntimeOrigin::root(), 5, 1),
Error::<Test>::DelegatorDNE
);
// only root can trigger this extrinsic
assert_ok!(ParachainStaking::force_execute_delegation_request(
RuntimeOrigin::root(),
2,
1
));
assert_event_emitted!(Event::DelegatorLeftCandidate {
delegator: 2,
candidate: 1,
unstaked_amount: 10,
total_candidate_staked: 30
});
});
}

#[test]
fn revoke_delegation_event_emits_correctly() {
ExtBuilder::default()
Expand Down

0 comments on commit 692b5c8

Please sign in to comment.