Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Custom origin for staking pallet calls #12943

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ impl pallet_staking::Config for Runtime {
type BondingDuration = BondingDuration;
type SlashDeferDuration = SlashDeferDuration;
/// A super-majority of the council can cancel the slash.
type SlashCancelOrigin = EitherOfDiverse<
type AdminOrigin = EitherOfDiverse<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 3, 4>,
>;
Expand Down
2 changes: 1 addition & 1 deletion frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl pallet_staking::Config for Test {
type SessionsPerEra = SessionsPerEra;
type BondingDuration = BondingDuration;
type SlashDeferDuration = SlashDeferDuration;
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type SessionInterface = Self;
type UnixTime = pallet_timestamp::Pallet<Test>;
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
Expand Down
2 changes: 1 addition & 1 deletion frame/fast-unstake/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl pallet_staking::Config for Runtime {
type Reward = ();
type SessionsPerEra = ();
type SlashDeferDuration = ();
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = BondingDuration;
type SessionInterface = ();
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
Expand Down
2 changes: 1 addition & 1 deletion frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl pallet_staking::Config for Test {
type SessionsPerEra = SessionsPerEra;
type BondingDuration = BondingDuration;
type SlashDeferDuration = ();
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type SessionInterface = Self;
type UnixTime = pallet_timestamp::Pallet<Test>;
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
Expand Down
2 changes: 1 addition & 1 deletion frame/nomination-pools/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl pallet_staking::Config for Runtime {
type Reward = ();
type SessionsPerEra = ();
type SlashDeferDuration = ();
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = ConstU32<3>;
type SessionInterface = ();
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
Expand Down
2 changes: 1 addition & 1 deletion frame/nomination-pools/test-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl pallet_staking::Config for Runtime {
type Reward = ();
type SessionsPerEra = ();
type SlashDeferDuration = ();
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = BondingDuration;
type SessionInterface = ();
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
Expand Down
2 changes: 1 addition & 1 deletion frame/offences/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl pallet_staking::Config for Test {
type Reward = ();
type SessionsPerEra = ();
type SlashDeferDuration = ();
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = ();
type SessionInterface = Self;
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
Expand Down
2 changes: 1 addition & 1 deletion frame/root-offences/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl pallet_staking::Config for Test {
type Reward = ();
type SessionsPerEra = SessionsPerEra;
type SlashDeferDuration = SlashDeferDuration;
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = BondingDuration;
type SessionInterface = Self;
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
Expand Down
2 changes: 1 addition & 1 deletion frame/session/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl pallet_staking::Config for Test {
type Reward = ();
type SessionsPerEra = ();
type SlashDeferDuration = ();
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = ();
type SessionInterface = Self;
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
Expand Down
2 changes: 1 addition & 1 deletion frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl crate::pallet::pallet::Config for Test {
type Reward = MockReward;
type SessionsPerEra = SessionsPerEra;
type SlashDeferDuration = SlashDeferDuration;
type SlashCancelOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = BondingDuration;
type SessionInterface = Self;
type EraPayout = ConvertCurve<RewardCurve>;
Expand Down
43 changes: 23 additions & 20 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,11 @@ pub mod pallet {
#[pallet::constant]
type SlashDeferDuration: Get<EraIndex>;

/// The origin which can cancel a deferred slash. Root can always do this.
type SlashCancelOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// The origin which can manage critical staking operations.
///
/// Set of operations that needs AdminOrigin: `set_config`, changing validator count,
/// force era changes, `force_unstake`, `cancel_deferred_slash`.
type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// Interface for interacting with a session pallet.
type SessionInterface: SessionInterface<Self::AccountId>;
Expand Down Expand Up @@ -1278,7 +1281,7 @@ pub mod pallet {

/// Sets the ideal number of validators.
///
/// The dispatch origin must be Root.
/// The dispatch origin must be `T::AdminOrigin`.
///
/// # <weight>
/// Weight: O(1)
Expand All @@ -1290,7 +1293,7 @@ pub mod pallet {
origin: OriginFor<T>,
#[pallet::compact] new: u32,
) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::ensure_origin(origin)?;
// ensure new validator count does not exceed maximum winners
// support by election provider.
ensure!(
Expand All @@ -1304,7 +1307,7 @@ pub mod pallet {
/// Increments the ideal number of validators upto maximum of
/// `ElectionProviderBase::MaxWinners`.
///
/// The dispatch origin must be Root.
/// The dispatch origin must be `T::AdminOrigin`.
///
/// # <weight>
/// Same as [`Self::set_validator_count`].
Expand All @@ -1315,7 +1318,7 @@ pub mod pallet {
origin: OriginFor<T>,
#[pallet::compact] additional: u32,
) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::ensure_origin(origin)?;
let old = ValidatorCount::<T>::get();
let new = old.checked_add(additional).ok_or(ArithmeticError::Overflow)?;
ensure!(
Expand All @@ -1330,15 +1333,15 @@ pub mod pallet {
/// Scale up the ideal number of validators by a factor upto maximum of
/// `ElectionProviderBase::MaxWinners`.
///
/// The dispatch origin must be Root.
/// The dispatch origin must be `T::AdminOrigin`.
///
/// # <weight>
/// Same as [`Self::set_validator_count`].
/// # </weight>
#[pallet::call_index(11)]
#[pallet::weight(T::WeightInfo::set_validator_count())]
pub fn scale_validator_count(origin: OriginFor<T>, factor: Percent) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::ensure_origin(origin)?;
let old = ValidatorCount::<T>::get();
let new = old.checked_add(factor.mul_floor(old)).ok_or(ArithmeticError::Overflow)?;

Expand All @@ -1353,7 +1356,7 @@ pub mod pallet {

/// Force there to be no new eras indefinitely.
///
/// The dispatch origin must be Root.
/// The dispatch origin must be `T::AdminOrigin`.
///
/// # Warning
///
Expand All @@ -1369,15 +1372,15 @@ pub mod pallet {
#[pallet::call_index(12)]
#[pallet::weight(T::WeightInfo::force_no_eras())]
pub fn force_no_eras(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::ensure_origin(origin)?;
ForceEra::<T>::put(Forcing::ForceNone);
Ok(())
}

/// Force there to be a new era at the end of the next session. After this, it will be
/// reset to normal (non-forced) behaviour.
///
/// The dispatch origin must be Root.
/// The dispatch origin must be `T::AdminOrigin`.
///
/// # Warning
///
Expand All @@ -1393,7 +1396,7 @@ pub mod pallet {
#[pallet::call_index(13)]
#[pallet::weight(T::WeightInfo::force_new_era())]
pub fn force_new_era(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::ensure_origin(origin)?;
ForceEra::<T>::put(Forcing::ForceNew);
Ok(())
}
Expand All @@ -1414,15 +1417,15 @@ pub mod pallet {

/// Force a current staker to become completely unstaked, immediately.
///
/// The dispatch origin must be Root.
/// The dispatch origin must be `T::AdminOrigin`.
#[pallet::call_index(15)]
#[pallet::weight(T::WeightInfo::force_unstake(*num_slashing_spans))]
pub fn force_unstake(
origin: OriginFor<T>,
stash: T::AccountId,
num_slashing_spans: u32,
) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::ensure_origin(origin)?;

// Remove all staking-related information.
Self::kill_stash(&stash, num_slashing_spans)?;
Expand All @@ -1434,7 +1437,7 @@ pub mod pallet {

/// Force there to be a new era at the end of sessions indefinitely.
///
/// The dispatch origin must be Root.
/// The dispatch origin must be `T::AdminOrigin`.
///
/// # Warning
///
Expand All @@ -1444,14 +1447,14 @@ pub mod pallet {
#[pallet::call_index(16)]
#[pallet::weight(T::WeightInfo::force_new_era_always())]
pub fn force_new_era_always(origin: OriginFor<T>) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::ensure_origin(origin)?;
ForceEra::<T>::put(Forcing::ForceAlways);
Ok(())
}

/// Cancel enactment of a deferred slash.
///
/// Can be called by the `T::SlashCancelOrigin`.
/// Can be called by the `T::AdminOrigin`.
///
/// Parameters: era and indices of the slashes for that era to kill.
#[pallet::call_index(17)]
Expand All @@ -1461,7 +1464,7 @@ pub mod pallet {
era: EraIndex,
slash_indices: Vec<u32>,
) -> DispatchResult {
T::SlashCancelOrigin::ensure_origin(origin)?;
T::AdminOrigin::ensure_origin(origin)?;

ensure!(!slash_indices.is_empty(), Error::<T>::EmptyTargets);
ensure!(is_sorted_and_unique(&slash_indices), Error::<T>::NotSortedAndUnique);
Expand Down Expand Up @@ -1642,7 +1645,7 @@ pub mod pallet {
/// * `min_commission`: The minimum amount of commission that each validators must maintain.
/// This is checked only upon calling `validate`. Existing validators are not affected.
///
/// RuntimeOrigin must be Root to call this function.
/// RuntimeOrigin must be `T::AdminOrigin` to call this function.
///
/// NOTE: Existing nominators and validators will not be affected by this update.
/// to kick people under the new limits, `chill_other` should be called.
Expand All @@ -1662,7 +1665,7 @@ pub mod pallet {
chill_threshold: ConfigOp<Percent>,
min_commission: ConfigOp<Perbill>,
) -> DispatchResult {
ensure_root(origin)?;
T::AdminOrigin::ensure_origin(origin)?;

macro_rules! config_op_exp {
($storage:ty, $op:ident) => {
Expand Down