Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
'OR gate' for EnsureOrigin.
  • Loading branch information
shaunxw committed Jun 3, 2020
commit 5cc9917b518326fee7c05f24920b8e28a68befc7
22 changes: 12 additions & 10 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use frame_support::{
},
traits::{Currency, Imbalance, KeyOwnerProofSystem, OnUnbalanced, Randomness, LockIdentifier},
};
use frame_system::{EnsureRoot, EnsureOneOf};
use codec::{Encode, Decode};
use sp_core::{
crypto::KeyTypeId,
Expand Down Expand Up @@ -388,7 +389,7 @@ impl pallet_staking::Trait for Runtime {
type BondingDuration = BondingDuration;
type SlashDeferDuration = SlashDeferDuration;
/// A super-majority of the council can cancel the slash.
type SlashCancelOrigin = pallet_collective::EnsureProportionAtLeast<_3, _4, AccountId, CouncilCollective>;
type SlashCancelOrigin = EnsureOneOf<AccountId, EnsureRoot<AccountId>, pallet_collective::EnsureProportionAtLeast<_3, _4, AccountId, CouncilCollective>>;
type SessionInterface = Self;
type RewardCurve = RewardCurve;
type NextNewSession = Session;
Expand Down Expand Up @@ -506,13 +507,14 @@ impl pallet_collective::Trait<TechnicalCollective> for Runtime {
type MaxProposals = TechnicalMaxProposals;
}

type EnsureRootOrHalfCouncil = EnsureOneOf<AccountId, EnsureRoot<AccountId>, pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>>;
impl pallet_membership::Trait<pallet_membership::Instance1> for Runtime {
type Event = Event;
type AddOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
type RemoveOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
type SwapOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
type ResetOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
type PrimeOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
type AddOrigin = EnsureRootOrHalfCouncil;
type RemoveOrigin = EnsureRootOrHalfCouncil;
type SwapOrigin = EnsureRootOrHalfCouncil;
type ResetOrigin = EnsureRootOrHalfCouncil;
type PrimeOrigin = EnsureRootOrHalfCouncil;
type MembershipInitialized = TechnicalCommittee;
type MembershipChanged = TechnicalCommittee;
}
Expand All @@ -531,8 +533,8 @@ parameter_types! {

impl pallet_treasury::Trait for Runtime {
type Currency = Balances;
type ApproveOrigin = pallet_collective::EnsureMembers<_4, AccountId, CouncilCollective>;
type RejectOrigin = pallet_collective::EnsureMembers<_2, AccountId, CouncilCollective>;
type ApproveOrigin = EnsureOneOf<AccountId, EnsureRoot<AccountId>, pallet_collective::EnsureMembers<_4, AccountId, CouncilCollective>>;
type RejectOrigin = EnsureOneOf<AccountId, EnsureRoot<AccountId>, pallet_collective::EnsureMembers<_2, AccountId, CouncilCollective>>;
type Tippers = Elections;
type TipCountdown = TipCountdown;
type TipFindersFee = TipFindersFee;
Expand Down Expand Up @@ -712,8 +714,8 @@ impl pallet_identity::Trait for Runtime {
type MaxAdditionalFields = MaxAdditionalFields;
type MaxRegistrars = MaxRegistrars;
type Slashed = Treasury;
type ForceOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
type RegistrarOrigin = pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, CouncilCollective>;
type ForceOrigin = EnsureRootOrHalfCouncil;
type RegistrarOrigin = EnsureRootOrHalfCouncil;
}

parameter_types! {
Expand Down
12 changes: 4 additions & 8 deletions frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ use frame_support::{
traits::{Currency, ReservableCurrency, OnUnbalanced, Get, BalanceStatus, EnsureOrigin},
weights::Weight,
};
use frame_system::{self as system, ensure_signed, ensure_root};
use frame_system::{self as system, ensure_signed};

mod benchmarking;

Expand Down Expand Up @@ -635,9 +635,7 @@ decl_module! {
/// # </weight>
#[weight = weight_for::add_registrar::<T>(T::MaxRegistrars::get().into()) ]
fn add_registrar(origin, account: T::AccountId) -> DispatchResultWithPostInfo {
T::RegistrarOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::RegistrarOrigin::ensure_origin(origin)?;

let (i, registrar_count) = <Registrars<T>>::try_mutate(
|registrars| -> Result<(RegistrarIndex, usize), DispatchError> {
Expand Down Expand Up @@ -1108,9 +1106,7 @@ decl_module! {
T::MaxAdditionalFields::get().into(), // X
)]
fn kill_identity(origin, target: <T::Lookup as StaticLookup>::Source) -> DispatchResultWithPostInfo {
T::ForceOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::ForceOrigin::ensure_origin(origin)?;

// Figure out who we're meant to be clearing.
let target = T::Lookup::lookup(target)?;
Expand Down Expand Up @@ -1435,7 +1431,7 @@ mod tests {
new_test_ext().execute_with(|| {
assert_ok!(Identity::set_identity(Origin::signed(10), ten()));
assert_ok!(Identity::set_subs(Origin::signed(10), vec![(20, Data::Raw(vec![40; 1]))]));
assert_ok!(Identity::kill_identity(Origin::ROOT, 10));
assert_ok!(Identity::kill_identity(Origin::signed(2), 10));
assert_eq!(Balances::free_balance(10), 80);
assert!(Identity::super_of(20).is_none());
});
Expand Down
26 changes: 7 additions & 19 deletions frame/membership/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use frame_support::{
decl_module, decl_storage, decl_event, decl_error,
traits::{ChangeMembers, InitializeMembers, EnsureOrigin},
};
use frame_system::{self as system, ensure_root, ensure_signed};
use frame_system::{self as system, ensure_signed};

pub trait Trait<I=DefaultInstance>: frame_system::Trait {
/// The overarching event type.
Expand Down Expand Up @@ -120,9 +120,7 @@ decl_module! {
/// May only be called from `AddOrigin` or root.
#[weight = 50_000_000]
pub fn add_member(origin, who: T::AccountId) {
T::AddOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::AddOrigin::ensure_origin(origin)?;

let mut members = <Members<T, I>>::get();
let location = members.binary_search(&who).err().ok_or(Error::<T, I>::AlreadyMember)?;
Expand All @@ -139,9 +137,7 @@ decl_module! {
/// May only be called from `RemoveOrigin` or root.
#[weight = 50_000_000]
pub fn remove_member(origin, who: T::AccountId) {
T::RemoveOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::RemoveOrigin::ensure_origin(origin)?;

let mut members = <Members<T, I>>::get();
let location = members.binary_search(&who).ok().ok_or(Error::<T, I>::NotMember)?;
Expand All @@ -161,9 +157,7 @@ decl_module! {
/// Prime membership is *not* passed from `remove` to `add`, if extant.
#[weight = 50_000_000]
pub fn swap_member(origin, remove: T::AccountId, add: T::AccountId) {
T::SwapOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::SwapOrigin::ensure_origin(origin)?;

if remove == add { return Ok(()) }

Expand All @@ -190,9 +184,7 @@ decl_module! {
/// May only be called from `ResetOrigin` or root.
#[weight = 50_000_000]
pub fn reset_members(origin, members: Vec<T::AccountId>) {
T::ResetOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::ResetOrigin::ensure_origin(origin)?;

let mut members = members;
members.sort();
Expand Down Expand Up @@ -241,9 +233,7 @@ decl_module! {
/// Set the prime member. Must be a current member.
#[weight = 50_000_000]
pub fn set_prime(origin, who: T::AccountId) {
T::PrimeOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::PrimeOrigin::ensure_origin(origin)?;
Self::members().binary_search(&who).ok().ok_or(Error::<T, I>::NotMember)?;
Prime::<T, I>::put(&who);
T::MembershipChanged::set_prime(Some(who));
Expand All @@ -252,9 +242,7 @@ decl_module! {
/// Remove the prime member if it exists.
#[weight = 50_000_000]
pub fn clear_prime(origin) {
T::PrimeOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::PrimeOrigin::ensure_origin(origin)?;
Prime::<T, I>::kill();
T::MembershipChanged::set_prime(None);
}
Expand Down
10 changes: 3 additions & 7 deletions frame/nicks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use frame_support::{
decl_module, decl_event, decl_storage, ensure, decl_error,
traits::{Currency, EnsureOrigin, ReservableCurrency, OnUnbalanced, Get},
};
use frame_system::{self as system, ensure_signed, ensure_root};
use frame_system::{self as system, ensure_signed};

type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::NegativeImbalance;
Expand Down Expand Up @@ -197,9 +197,7 @@ decl_module! {
/// # </weight>
#[weight = 70_000_000]
fn kill_name(origin, target: <T::Lookup as StaticLookup>::Source) {
T::ForceOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::ForceOrigin::ensure_origin(origin)?;

// Figure out who we're meant to be clearing.
let target = T::Lookup::lookup(target)?;
Expand All @@ -225,9 +223,7 @@ decl_module! {
/// # </weight>
#[weight = 70_000_000]
fn force_name(origin, target: <T::Lookup as StaticLookup>::Source, name: Vec<u8>) {
T::ForceOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::ForceOrigin::ensure_origin(origin)?;

let target = T::Lookup::lookup(target)?;
let deposit = <NameOf<T>>::get(&target).map(|x| x.1).unwrap_or_else(Zero::zero);
Expand Down
8 changes: 2 additions & 6 deletions frame/scored-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@ decl_module! {
dest: <T::Lookup as StaticLookup>::Source,
index: u32
) {
T::KickOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::KickOrigin::ensure_origin(origin)?;

let who = T::Lookup::lookup(dest)?;

Expand All @@ -344,9 +342,7 @@ decl_module! {
index: u32,
score: T::Score
) {
T::ScoreOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::ScoreOrigin::ensure_origin(origin)?;

let who = T::Lookup::lookup(dest)?;

Expand Down
4 changes: 1 addition & 3 deletions frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1882,9 +1882,7 @@ decl_module! {
.saturating_add((35 * WEIGHT_PER_MICROS).saturating_mul(slash_indices.len() as Weight))
]
fn cancel_deferred_slash(origin, era: EraIndex, slash_indices: Vec<u32>) {
T::SlashCancelOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::SlashCancelOrigin::ensure_origin(origin)?;

ensure!(!slash_indices.is_empty(), Error::<T>::EmptyTargets);
ensure!(is_sorted_and_unique(&slash_indices), Error::<T>::NotSortedAndUnique);
Expand Down
34 changes: 33 additions & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,27 @@ impl<O, T> EnsureOrigin<O> for EnsureNever<T> {
}
}

pub struct EnsureOneOf<AccountId, A, B>(A, B, sp_std::marker::PhantomData<AccountId>);
impl<
AccountId,
O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
A: EnsureOrigin<O>,
B: EnsureOrigin<O>,
> EnsureOrigin<O> for EnsureOneOf<AccountId, A, B> {
type Success = ();
fn try_origin(o: O) -> Result<Self::Success, O> {
A::try_origin(o).map_or_else(
|o| B::try_origin(o).map(|_| ()),
|_| Ok(()),
)
}

#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> O {
A::successful_origin()
}
}

/// Ensure that the origin `o` represents a signed extrinsic (i.e. transaction).
/// Returns `Ok` with the account that signed the extrinsic or an `Err` otherwise.
pub fn ensure_signed<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<AccountId, BadOrigin>
Expand Down Expand Up @@ -1879,7 +1900,7 @@ pub(crate) mod tests {
use sp_core::H256;
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup, SignedExtension}, testing::Header, DispatchError};
use frame_support::{
impl_outer_origin, parameter_types, assert_ok, assert_noop,
impl_outer_origin, parameter_types, assert_ok, assert_noop, assert_err,
weights::WithPostDispatchInfo,
};

Expand Down Expand Up @@ -2701,4 +2722,15 @@ pub(crate) mod tests {
assert!(System::events().len() == 1);
});
}

#[test]
fn ensure_one_of_works() {
fn ensure_root_or_signed(o: RawOrigin<u64>) -> Result<(), Origin> {
EnsureOneOf::<u64, EnsureRoot<u64>, EnsureSigned<u64>>::try_origin(o.into())
}

assert_ok!(ensure_root_or_signed(RawOrigin::Root));
assert_ok!(ensure_root_or_signed(RawOrigin::Signed(0)));
assert_err!(ensure_root_or_signed(RawOrigin::None), Origin::from(RawOrigin::None));
}
}
12 changes: 4 additions & 8 deletions frame/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ use sp_runtime::{Permill, ModuleId, Percent, RuntimeDebug, traits::{
use frame_support::weights::{Weight, DispatchClass};
use frame_support::traits::{Contains, ContainsLengthBound, EnsureOrigin};
use codec::{Encode, Decode};
use frame_system::{self as system, ensure_signed, ensure_root};
use frame_system::{self as system, ensure_signed};

mod tests;
mod benchmarking;
Expand Down Expand Up @@ -316,7 +316,7 @@ decl_module! {

/// The amount held on deposit per byte within the tip report reason.
const TipReportDepositPerByte: BalanceOf<T> = T::TipReportDepositPerByte::get();

/// The treasury's module id, used for deriving its sovereign account ID.
const ModuleId: ModuleId = T::ModuleId::get();

Expand Down Expand Up @@ -362,9 +362,7 @@ decl_module! {
/// # </weight>
#[weight = (130_000_000 + T::DbWeight::get().reads_writes(2, 2), DispatchClass::Operational)]
fn reject_proposal(origin, #[compact] proposal_id: ProposalIndex) {
T::RejectOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::RejectOrigin::ensure_origin(origin)?;

let proposal = <Proposals<T>>::take(&proposal_id).ok_or(Error::<T>::InvalidProposalIndex)?;
let value = proposal.bond;
Expand All @@ -384,9 +382,7 @@ decl_module! {
/// # </weight>
#[weight = (34_000_000 + T::DbWeight::get().reads_writes(2, 1), DispatchClass::Operational)]
fn approve_proposal(origin, #[compact] proposal_id: ProposalIndex) {
T::ApproveOrigin::try_origin(origin)
.map(|_| ())
.or_else(ensure_root)?;
T::ApproveOrigin::ensure_origin(origin)?;

ensure!(<Proposals<T>>::contains_key(proposal_id), Error::<T>::InvalidProposalIndex);
Approvals::mutate(|v| v.push(proposal_id));
Expand Down