Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force unstaking #1352

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading