Skip to content

Commit

Permalink
block chill and withdraw-unbond calls
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp committed Nov 14, 2023
1 parent 9fd4dee commit 09de121
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
52 changes: 50 additions & 2 deletions pallets/roles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub mod pallet {
pub enum Error<T> {
/// Not a validator.
NotValidator,
/// Role has already been assigned to provided validator.
/// Validator has active role assigned
HasRoleAssigned,
/// No role assigned to provided validator.
NoRoleAssigned,
Expand Down Expand Up @@ -251,6 +251,30 @@ pub mod pallet {
Ok(())
}

/// Declare no desire to either validate or nominate.
///
/// If you have opted for any of the roles, please submit `clear_role` extrinsic to opt out
/// of all the services. Once your role is cleared, your request will be processed.
///
/// # Parameters
///
/// - `origin`: Origin of the transaction.
///
/// This function will return error if
/// - Account is not a validator account.
/// - Role is assigned to the validator.
#[pallet::weight({0})]
#[pallet::call_index(2)]
pub fn chill(origin: OriginFor<T>) -> DispatchResult {
let account = ensure_signed(origin.clone())?;
// Ensure no role is assigned to the account before chilling.
ensure!(!AccountRolesMapping::<T>::contains_key(&account), Error::<T>::HasRoleAssigned);

// chill
let _ = pallet_staking::Pallet::<T>::chill(origin);
Ok(())
}

/// Unbound funds from the stash account.
/// This will allow user to unbound and later withdraw funds.
/// If you have opted for any of the roles, please submit `clear_role` extrinsic to opt out
Expand All @@ -266,7 +290,7 @@ pub mod pallet {
/// - If there is any active role assigned to the user.
///
#[pallet::weight({0})]
#[pallet::call_index(2)]
#[pallet::call_index(3)]
pub fn unbound_funds(
origin: OriginFor<T>,
#[pallet::compact] amount: BalanceOf<T>,
Expand All @@ -280,5 +304,29 @@ pub mod pallet {

Ok(())
}

/// Withdraw unbound funds after un-bonding period has passed.
///
/// # Parameters
///
/// - `origin`: Origin of the transaction.
///
/// This function will return error if
/// - If there is any active role assigned to the user.
#[pallet::weight({0})]
#[pallet::call_index(4)]
pub fn withdraw_unbonded(origin: OriginFor<T>) -> DispatchResult {
let stash_account = ensure_signed(origin.clone())?;
// Ensure no role is assigned to the account and is eligible to withdraw.
ensure!(
!AccountRolesMapping::<T>::contains_key(&stash_account),
Error::<T>::HasRoleAssigned
);

// Withdraw unbound funds.
let _ = pallet_staking::Pallet::<T>::withdraw_unbonded(origin, 0);

Ok(())
}
}
}
13 changes: 13 additions & 0 deletions pallets/roles/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ impl Contains<RuntimeCall> for BaseFilter {
return false
}

// no chill call
if matches!(call, RuntimeCall::Staking(pallet_staking::Call::chill { .. })) {
return false
}

// no withdraw_unbonded call
let is_stake_withdraw_call =
matches!(call, RuntimeCall::Staking(pallet_staking::Call::withdraw_unbonded { .. }));

if is_stake_withdraw_call {
return false
}

true
}
}
Expand Down
18 changes: 17 additions & 1 deletion standalone/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,8 @@ impl Contains<RuntimeCall> for BaseFilter {
// no paused call
return false
}

// Following staking pallet calls will be blocked and will be allowed to execute
// through role pallet.
let is_stake_unbound_call =
matches!(call, RuntimeCall::Staking(pallet_staking::Call::unbond { .. }));

Expand All @@ -1106,6 +1107,21 @@ impl Contains<RuntimeCall> for BaseFilter {
return false
}

// no chill call
if matches!(call, RuntimeCall::Staking(pallet_staking::Call::chill { .. })) {
return false
}

// no withdraw_unbonded call
let is_stake_withdraw_call = matches!(
call,
RuntimeCall::Staking(pallet_staking::Call::withdraw_unbonded { .. })
);

if is_stake_withdraw_call{
return false
}

let democracy_related = matches!(
call,
// Filter democracy proposals creation
Expand Down

0 comments on commit 09de121

Please sign in to comment.