use crate::dispatch::{DispatchResultWithPostInfo, Parameter, RawOrigin};
use codec::MaxEncodedLen;
use core::{cmp::Ordering, marker::PhantomData};
use sp_runtime::{
traits::{BadOrigin, Get, Member, Morph, TryMorph},
Either,
};
use super::misc;
pub trait EnsureOrigin<OuterOrigin> {
type Success;
fn ensure_origin(o: OuterOrigin) -> Result<Self::Success, BadOrigin> {
Self::try_origin(o).map_err(|_| BadOrigin)
}
fn ensure_origin_or_root(o: OuterOrigin) -> Result<Option<Self::Success>, BadOrigin>
where
OuterOrigin: OriginTrait,
{
if o.caller().is_root() {
return Ok(None)
} else {
Self::ensure_origin(o).map(Some)
}
}
fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin>;
fn try_origin_or_root(o: OuterOrigin) -> Result<Option<Self::Success>, OuterOrigin>
where
OuterOrigin: OriginTrait,
{
if o.caller().is_root() {
return Ok(None)
} else {
Self::try_origin(o).map(Some)
}
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<OuterOrigin, ()>;
}
pub struct EnsureOriginEqualOrHigherPrivilege<Origin, PrivilegeCmp>(
core::marker::PhantomData<(Origin, PrivilegeCmp)>,
);
impl<OuterOrigin, Origin, PrivilegeCmp> EnsureOrigin<OuterOrigin>
for EnsureOriginEqualOrHigherPrivilege<Origin, PrivilegeCmp>
where
Origin: Get<OuterOrigin>,
OuterOrigin: Eq,
PrivilegeCmp: misc::PrivilegeCmp<OuterOrigin>,
{
type Success = ();
fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
let expected_origin = Origin::get();
if o == expected_origin {
return Ok(())
}
let cmp = PrivilegeCmp::cmp_privilege(&o, &expected_origin);
match cmp {
Some(Ordering::Equal) | Some(Ordering::Greater) => Ok(()),
None | Some(Ordering::Less) => Err(o),
}
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<OuterOrigin, ()> {
Ok(Origin::get())
}
}
pub trait EnsureOriginWithArg<OuterOrigin, Argument> {
type Success;
fn ensure_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, BadOrigin> {
Self::try_origin(o, a).map_err(|_| BadOrigin)
}
fn try_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, OuterOrigin>;
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &Argument) -> Result<OuterOrigin, ()>;
}
#[macro_export]
macro_rules! impl_ensure_origin_with_arg_ignoring_arg {
( impl < { O: .., I: 'static, $( $bound:tt )* }> EnsureOriginWithArg<O, $t_param:ty> for $name:ty {} ) => {
impl_ensure_origin_with_arg_ignoring_arg! {
impl <{
O: Into<Result<RawOrigin<AccountId, I>, O>> + From<RawOrigin<AccountId, I>>,
I: 'static,
$( $bound )*
}> EnsureOriginWithArg<O, $t_param> for $name {}
}
};
( impl < { O: .. , $( $bound:tt )* }> EnsureOriginWithArg<O, $t_param:ty> for $name:ty {} ) => {
impl_ensure_origin_with_arg_ignoring_arg! {
impl <{
O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
$( $bound )*
}> EnsureOriginWithArg<O, $t_param> for $name {}
}
};
( impl < { $( $bound:tt )* } > EnsureOriginWithArg<$o_param:ty, $t_param:ty> for $name:ty {} ) => {
impl < $( $bound )* > EnsureOriginWithArg<$o_param, $t_param> for $name {
type Success = <Self as EnsureOrigin<$o_param>>::Success;
fn try_origin(o: $o_param, _: &$t_param) -> Result<Self::Success, $o_param> {
<Self as EnsureOrigin<$o_param>>::try_origin(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(_: &$t_param) -> Result<$o_param, ()> {
<Self as EnsureOrigin<$o_param>>::try_successful_origin()
}
}
}
}
pub struct NeverEnsureOrigin<Success>(core::marker::PhantomData<Success>);
impl<OO, Success> EnsureOrigin<OO> for NeverEnsureOrigin<Success> {
type Success = Success;
fn try_origin(o: OO) -> Result<Success, OO> {
Err(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<OO, ()> {
Err(())
}
}
impl_ensure_origin_with_arg_ignoring_arg! {
impl<{ OO, Success, A }>
EnsureOriginWithArg<OO, A> for NeverEnsureOrigin<Success>
{}
}
pub struct AsEnsureOriginWithArg<EO>(core::marker::PhantomData<EO>);
impl<OuterOrigin, Argument, EO: EnsureOrigin<OuterOrigin>>
EnsureOriginWithArg<OuterOrigin, Argument> for AsEnsureOriginWithArg<EO>
{
type Success = EO::Success;
fn ensure_origin(o: OuterOrigin, _: &Argument) -> Result<Self::Success, BadOrigin> {
EO::ensure_origin(o)
}
fn try_origin(o: OuterOrigin, _: &Argument) -> Result<Self::Success, OuterOrigin> {
EO::try_origin(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(_: &Argument) -> Result<OuterOrigin, ()> {
EO::try_successful_origin()
}
}
pub struct MapSuccess<Original, Mutator>(PhantomData<(Original, Mutator)>);
impl<O, Original: EnsureOrigin<O>, Mutator: Morph<Original::Success>> EnsureOrigin<O>
for MapSuccess<Original, Mutator>
{
type Success = Mutator::Outcome;
fn try_origin(o: O) -> Result<Mutator::Outcome, O> {
Ok(Mutator::morph(Original::try_origin(o)?))
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Original::try_successful_origin()
}
}
impl<O, Original: EnsureOriginWithArg<O, A>, Mutator: Morph<Original::Success>, A>
EnsureOriginWithArg<O, A> for MapSuccess<Original, Mutator>
{
type Success = Mutator::Outcome;
fn try_origin(o: O, a: &A) -> Result<Mutator::Outcome, O> {
Ok(Mutator::morph(Original::try_origin(o, a)?))
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &A) -> Result<O, ()> {
Original::try_successful_origin(a)
}
}
pub struct TryMapSuccess<Orig, Mutator>(PhantomData<(Orig, Mutator)>);
impl<O: Clone, Original: EnsureOrigin<O>, Mutator: TryMorph<Original::Success>> EnsureOrigin<O>
for TryMapSuccess<Original, Mutator>
{
type Success = Mutator::Outcome;
fn try_origin(o: O) -> Result<Mutator::Outcome, O> {
let orig = o.clone();
Mutator::try_morph(Original::try_origin(o)?).map_err(|()| orig)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Original::try_successful_origin()
}
}
impl<O: Clone, Original: EnsureOriginWithArg<O, A>, Mutator: TryMorph<Original::Success>, A>
EnsureOriginWithArg<O, A> for TryMapSuccess<Original, Mutator>
{
type Success = Mutator::Outcome;
fn try_origin(o: O, a: &A) -> Result<Mutator::Outcome, O> {
let orig = o.clone();
Mutator::try_morph(Original::try_origin(o, a)?).map_err(|()| orig)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &A) -> Result<O, ()> {
Original::try_successful_origin(a)
}
}
pub struct TryWithMorphedArg<O, A, Morph, Inner, Success>(
PhantomData<(O, A, Morph, Inner, Success)>,
);
impl<
O,
A,
Morph: for<'a> TryMorph<&'a A>,
Inner: for<'a> EnsureOriginWithArg<O, <Morph as TryMorph<&'a A>>::Outcome, Success = Success>,
Success,
> EnsureOriginWithArg<O, A> for TryWithMorphedArg<O, A, Morph, Inner, Success>
{
type Success = Success;
fn try_origin(o: O, a: &A) -> Result<Success, O> {
match Morph::try_morph(a) {
Ok(x) => Inner::try_origin(o, &x),
_ => return Err(o),
}
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &A) -> Result<O, ()> {
Inner::try_successful_origin(&Morph::try_morph(a).map_err(|_| ())?)
}
}
pub struct EitherOfDiverse<L, R>(core::marker::PhantomData<(L, R)>);
impl<OuterOrigin, L: EnsureOrigin<OuterOrigin>, R: EnsureOrigin<OuterOrigin>>
EnsureOrigin<OuterOrigin> for EitherOfDiverse<L, R>
{
type Success = Either<L::Success, R::Success>;
fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
L::try_origin(o)
.map_or_else(|o| R::try_origin(o).map(Either::Right), |o| Ok(Either::Left(o)))
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<OuterOrigin, ()> {
L::try_successful_origin().or_else(|()| R::try_successful_origin())
}
}
impl<
OuterOrigin,
L: EnsureOriginWithArg<OuterOrigin, Argument>,
R: EnsureOriginWithArg<OuterOrigin, Argument>,
Argument,
> EnsureOriginWithArg<OuterOrigin, Argument> for EitherOfDiverse<L, R>
{
type Success = Either<L::Success, R::Success>;
fn try_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, OuterOrigin> {
L::try_origin(o, a)
.map_or_else(|o| R::try_origin(o, a).map(Either::Right), |o| Ok(Either::Left(o)))
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &Argument) -> Result<OuterOrigin, ()> {
L::try_successful_origin(a).or_else(|()| R::try_successful_origin(a))
}
}
#[deprecated = "Use `EitherOfDiverse` instead"]
pub type EnsureOneOf<L, R> = EitherOfDiverse<L, R>;
pub struct EitherOf<L, R>(core::marker::PhantomData<(L, R)>);
impl<
OuterOrigin,
L: EnsureOrigin<OuterOrigin>,
R: EnsureOrigin<OuterOrigin, Success = L::Success>,
> EnsureOrigin<OuterOrigin> for EitherOf<L, R>
{
type Success = L::Success;
fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
L::try_origin(o).or_else(|o| R::try_origin(o))
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<OuterOrigin, ()> {
L::try_successful_origin().or_else(|()| R::try_successful_origin())
}
}
impl<
OuterOrigin,
L: EnsureOriginWithArg<OuterOrigin, Argument>,
R: EnsureOriginWithArg<OuterOrigin, Argument, Success = L::Success>,
Argument,
> EnsureOriginWithArg<OuterOrigin, Argument> for EitherOf<L, R>
{
type Success = L::Success;
fn try_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, OuterOrigin> {
L::try_origin(o, a).or_else(|o| R::try_origin(o, a))
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin(a: &Argument) -> Result<OuterOrigin, ()> {
L::try_successful_origin(a).or_else(|()| R::try_successful_origin(a))
}
}
pub trait UnfilteredDispatchable {
type RuntimeOrigin;
fn dispatch_bypass_filter(self, origin: Self::RuntimeOrigin) -> DispatchResultWithPostInfo;
}
pub trait CallerTrait<AccountId>: Parameter + Member + From<RawOrigin<AccountId>> {
fn into_system(self) -> Option<RawOrigin<AccountId>>;
fn as_system_ref(&self) -> Option<&RawOrigin<AccountId>>;
fn as_signed(&self) -> Option<&AccountId> {
self.as_system_ref().and_then(RawOrigin::as_signed)
}
fn is_root(&self) -> bool {
self.as_system_ref().map_or(false, RawOrigin::is_root)
}
fn is_none(&self) -> bool {
self.as_system_ref().map_or(false, RawOrigin::is_none)
}
}
pub trait OriginTrait: Sized {
type Call;
type PalletsOrigin: Into<Self> + CallerTrait<Self::AccountId> + MaxEncodedLen;
type AccountId;
fn add_filter(&mut self, filter: impl Fn(&Self::Call) -> bool + 'static);
fn reset_filter(&mut self);
fn set_caller_from(&mut self, other: impl Into<Self>);
fn filter_call(&self, call: &Self::Call) -> bool;
fn caller(&self) -> &Self::PalletsOrigin;
fn into_caller(self) -> Self::PalletsOrigin;
fn try_with_caller<R>(
self,
f: impl FnOnce(Self::PalletsOrigin) -> Result<R, Self::PalletsOrigin>,
) -> Result<R, Self>;
fn none() -> Self;
fn root() -> Self;
fn signed(by: Self::AccountId) -> Self;
#[deprecated = "Use `into_signer` instead"]
fn as_signed(self) -> Option<Self::AccountId> {
self.into_signer()
}
fn into_signer(self) -> Option<Self::AccountId> {
self.into_caller().into_system().and_then(|s| {
if let RawOrigin::Signed(who) = s {
Some(who)
} else {
None
}
})
}
fn as_system_ref(&self) -> Option<&RawOrigin<Self::AccountId>> {
self.caller().as_system_ref()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::{ConstBool, ConstU8, TypedGet};
use std::marker::PhantomData;
struct EnsureSuccess<V>(PhantomData<V>);
struct EnsureFail<T>(PhantomData<T>);
impl<V: TypedGet> EnsureOrigin<()> for EnsureSuccess<V> {
type Success = V::Type;
fn try_origin(_: ()) -> Result<Self::Success, ()> {
Ok(V::get())
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<(), ()> {
Ok(())
}
}
impl<T> EnsureOrigin<()> for EnsureFail<T> {
type Success = T;
fn try_origin(_: ()) -> Result<Self::Success, ()> {
Err(())
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<(), ()> {
Err(())
}
}
#[test]
fn either_of_diverse_works() {
assert_eq!(
EitherOfDiverse::<
EnsureSuccess<ConstBool<true>>,
EnsureSuccess<ConstU8<0>>,
>::try_origin(()).unwrap().left(),
Some(true)
);
assert_eq!(
EitherOfDiverse::<EnsureSuccess<ConstBool<true>>, EnsureFail<u8>>::try_origin(())
.unwrap()
.left(),
Some(true)
);
assert_eq!(
EitherOfDiverse::<EnsureFail<bool>, EnsureSuccess<ConstU8<0>>>::try_origin(())
.unwrap()
.right(),
Some(0u8)
);
assert!(EitherOfDiverse::<EnsureFail<bool>, EnsureFail<u8>>::try_origin(()).is_err());
}
#[test]
fn either_of_works() {
assert_eq!(
EitherOf::<
EnsureSuccess<ConstBool<true>>,
EnsureSuccess<ConstBool<false>>,
>::try_origin(()).unwrap(),
true
);
assert_eq!(
EitherOf::<EnsureSuccess<ConstBool<true>>, EnsureFail<bool>>::try_origin(()).unwrap(),
true
);
assert_eq!(
EitherOf::<EnsureFail<bool>, EnsureSuccess<ConstBool<false>>>::try_origin(()).unwrap(),
false
);
assert!(EitherOf::<EnsureFail<bool>, EnsureFail<bool>>::try_origin(()).is_err());
}
}