frame_support/traits/
dispatch.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Traits for dealing with dispatching calls and the origin from which they are dispatched.
19
20use crate::dispatch::{DispatchResultWithPostInfo, Parameter, RawOrigin};
21use codec::MaxEncodedLen;
22use core::{cmp::Ordering, marker::PhantomData};
23use sp_runtime::{
24	traits::{BadOrigin, Get, Member, Morph, TryMorph},
25	Either,
26};
27
28use super::misc;
29
30/// Some sort of check on the origin is performed by this object.
31pub trait EnsureOrigin<OuterOrigin> {
32	/// A return type.
33	type Success;
34
35	/// Perform the origin check.
36	fn ensure_origin(o: OuterOrigin) -> Result<Self::Success, BadOrigin> {
37		Self::try_origin(o).map_err(|_| BadOrigin)
38	}
39
40	/// The same as `ensure_origin` except that Root origin will always pass. This can only be
41	/// used if `Success` has a sensible impl of `Default` since that will be used in the result.
42	fn ensure_origin_or_root(o: OuterOrigin) -> Result<Option<Self::Success>, BadOrigin>
43	where
44		OuterOrigin: OriginTrait,
45	{
46		if o.caller().is_root() {
47			return Ok(None)
48		} else {
49			Self::ensure_origin(o).map(Some)
50		}
51	}
52
53	/// Perform the origin check.
54	fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin>;
55
56	/// The same as `try_origin` except that Root origin will always pass. This can only be
57	/// used if `Success` has a sensible impl of `Default` since that will be used in the result.
58	fn try_origin_or_root(o: OuterOrigin) -> Result<Option<Self::Success>, OuterOrigin>
59	where
60		OuterOrigin: OriginTrait,
61	{
62		if o.caller().is_root() {
63			return Ok(None)
64		} else {
65			Self::try_origin(o).map(Some)
66		}
67	}
68
69	/// Attempt to get an outer origin capable of passing `try_origin` check. May return `Err` if it
70	/// is impossible.
71	///
72	/// ** Should be used for benchmarking only!!! **
73	#[cfg(feature = "runtime-benchmarks")]
74	fn try_successful_origin() -> Result<OuterOrigin, ()>;
75}
76
77/// [`EnsureOrigin`] implementation that checks that an origin has equal or higher privilege
78/// compared to the expected `Origin`.
79///
80/// It will take the shortcut of comparing the incoming origin with the expected `Origin` and if
81/// both are the same the origin is accepted.
82///
83/// # Example
84///
85/// ```rust
86/// # use frame_support::traits::{EnsureOriginEqualOrHigherPrivilege, PrivilegeCmp, EnsureOrigin as _};
87/// # use sp_runtime::traits::{parameter_types, Get};
88/// # use core::cmp::Ordering;
89///
90/// #[derive(Eq, PartialEq, Debug)]
91/// pub enum Origin {
92///     Root,
93///     SomethingBelowRoot,
94///     NormalUser,
95/// }
96///
97/// struct OriginPrivilegeCmp;
98///
99/// impl PrivilegeCmp<Origin> for OriginPrivilegeCmp {
100///     fn cmp_privilege(left: &Origin, right: &Origin) -> Option<Ordering> {
101///         match (left, right) {
102///             (Origin::Root, Origin::Root) => Some(Ordering::Equal),
103///             (Origin::Root, _) => Some(Ordering::Greater),
104///             (Origin::SomethingBelowRoot, Origin::SomethingBelowRoot) => Some(Ordering::Equal),
105///             (Origin::SomethingBelowRoot, Origin::Root) => Some(Ordering::Less),
106///             (Origin::SomethingBelowRoot, Origin::NormalUser) => Some(Ordering::Greater),
107///             (Origin::NormalUser, Origin::NormalUser) => Some(Ordering::Equal),
108///             (Origin::NormalUser, _) => Some(Ordering::Less),
109///         }
110///     }
111/// }
112///
113/// parameter_types! {
114///     pub const ExpectedOrigin: Origin = Origin::SomethingBelowRoot;
115/// }
116///
117/// type EnsureOrigin = EnsureOriginEqualOrHigherPrivilege<ExpectedOrigin, OriginPrivilegeCmp>;
118///
119/// // `Root` has an higher privilege as our expected origin.
120/// assert!(EnsureOrigin::ensure_origin(Origin::Root).is_ok());
121/// // `SomethingBelowRoot` is exactly the expected origin.
122/// assert!(EnsureOrigin::ensure_origin(Origin::SomethingBelowRoot).is_ok());
123/// // The `NormalUser` origin is not allowed.
124/// assert!(EnsureOrigin::ensure_origin(Origin::NormalUser).is_err());
125/// ```
126pub struct EnsureOriginEqualOrHigherPrivilege<Origin, PrivilegeCmp>(
127	core::marker::PhantomData<(Origin, PrivilegeCmp)>,
128);
129
130impl<OuterOrigin, Origin, PrivilegeCmp> EnsureOrigin<OuterOrigin>
131	for EnsureOriginEqualOrHigherPrivilege<Origin, PrivilegeCmp>
132where
133	Origin: Get<OuterOrigin>,
134	OuterOrigin: Eq,
135	PrivilegeCmp: misc::PrivilegeCmp<OuterOrigin>,
136{
137	type Success = ();
138
139	fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
140		let expected_origin = Origin::get();
141
142		// If this is the expected origin, it has the same privilege.
143		if o == expected_origin {
144			return Ok(())
145		}
146
147		let cmp = PrivilegeCmp::cmp_privilege(&o, &expected_origin);
148
149		match cmp {
150			Some(Ordering::Equal) | Some(Ordering::Greater) => Ok(()),
151			None | Some(Ordering::Less) => Err(o),
152		}
153	}
154
155	#[cfg(feature = "runtime-benchmarks")]
156	fn try_successful_origin() -> Result<OuterOrigin, ()> {
157		Ok(Origin::get())
158	}
159}
160
161/// Some sort of check on the origin is performed by this object.
162pub trait EnsureOriginWithArg<OuterOrigin, Argument> {
163	/// A return type.
164	type Success;
165
166	/// Perform the origin check.
167	fn ensure_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, BadOrigin> {
168		Self::try_origin(o, a).map_err(|_| BadOrigin)
169	}
170
171	/// Perform the origin check, returning the origin value if unsuccessful. This allows chaining.
172	fn try_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, OuterOrigin>;
173
174	/// Attempt to get an outer origin capable of passing `try_origin` check. May return `Err` if it
175	/// is impossible.
176	///
177	/// ** Should be used for benchmarking only!!! **
178	#[cfg(feature = "runtime-benchmarks")]
179	fn try_successful_origin(a: &Argument) -> Result<OuterOrigin, ()>;
180}
181
182/// Simple macro to explicitly implement [EnsureOriginWithArg] to be used on any type which
183/// implements [EnsureOrigin]. This is quick and dirty, so you must use the type parameters `O`
184/// (the origin type), `T` (the argument type) and `AccountId` (if you are using the `O: ..` form).
185///
186/// The argument is ignored, much like in [AsEnsureOriginWithArg].
187#[macro_export]
188macro_rules! impl_ensure_origin_with_arg_ignoring_arg {
189	( impl < { O: .., I: 'static, $( $bound:tt )* }> EnsureOriginWithArg<O, $t_param:ty> for $name:ty {} ) => {
190		impl_ensure_origin_with_arg_ignoring_arg! {
191			impl <{
192				O: Into<Result<RawOrigin<AccountId, I>, O>> + From<RawOrigin<AccountId, I>>,
193				I: 'static,
194				$( $bound )*
195			}> EnsureOriginWithArg<O, $t_param> for $name {}
196		}
197	};
198	( impl < { O: .. , $( $bound:tt )* }> EnsureOriginWithArg<O, $t_param:ty> for $name:ty {} ) => {
199		impl_ensure_origin_with_arg_ignoring_arg! {
200			impl <{
201				O: Into<Result<RawOrigin<AccountId>, O>> + From<RawOrigin<AccountId>>,
202				$( $bound )*
203			}> EnsureOriginWithArg<O, $t_param> for $name {}
204		}
205	};
206	( impl < { $( $bound:tt )* } > EnsureOriginWithArg<$o_param:ty, $t_param:ty> for $name:ty {} ) => {
207		impl < $( $bound )* > EnsureOriginWithArg<$o_param, $t_param> for $name {
208			type Success = <Self as EnsureOrigin<$o_param>>::Success;
209			fn try_origin(o: $o_param, _: &$t_param) -> Result<Self::Success, $o_param> {
210				<Self as EnsureOrigin<$o_param>>::try_origin(o)
211			}
212			#[cfg(feature = "runtime-benchmarks")]
213			fn try_successful_origin(_: &$t_param) -> Result<$o_param, ()> {
214				<Self as EnsureOrigin<$o_param>>::try_successful_origin()
215			}
216		}
217	}
218}
219
220/// [`EnsureOrigin`] implementation that always fails.
221pub struct NeverEnsureOrigin<Success>(core::marker::PhantomData<Success>);
222impl<OO, Success> EnsureOrigin<OO> for NeverEnsureOrigin<Success> {
223	type Success = Success;
224	fn try_origin(o: OO) -> Result<Success, OO> {
225		Err(o)
226	}
227	#[cfg(feature = "runtime-benchmarks")]
228	fn try_successful_origin() -> Result<OO, ()> {
229		Err(())
230	}
231}
232impl_ensure_origin_with_arg_ignoring_arg! {
233	impl<{ OO, Success, A }>
234		EnsureOriginWithArg<OO, A> for NeverEnsureOrigin<Success>
235	{}
236}
237
238pub struct AsEnsureOriginWithArg<EO>(core::marker::PhantomData<EO>);
239impl<OuterOrigin, Argument, EO: EnsureOrigin<OuterOrigin>>
240	EnsureOriginWithArg<OuterOrigin, Argument> for AsEnsureOriginWithArg<EO>
241{
242	/// A return type.
243	type Success = EO::Success;
244
245	/// Perform the origin check.
246	fn ensure_origin(o: OuterOrigin, _: &Argument) -> Result<Self::Success, BadOrigin> {
247		EO::ensure_origin(o)
248	}
249
250	/// Perform the origin check, returning the origin value if unsuccessful. This allows chaining.
251	fn try_origin(o: OuterOrigin, _: &Argument) -> Result<Self::Success, OuterOrigin> {
252		EO::try_origin(o)
253	}
254
255	/// Returns an outer origin capable of passing `try_origin` check.
256	///
257	/// ** Should be used for benchmarking only!!! **
258	#[cfg(feature = "runtime-benchmarks")]
259	fn try_successful_origin(_: &Argument) -> Result<OuterOrigin, ()> {
260		EO::try_successful_origin()
261	}
262}
263
264/// A derivative `EnsureOrigin` implementation. It mutates the `Success` result of an `Original`
265/// implementation with a given `Mutator`.
266pub struct MapSuccess<Original, Mutator>(PhantomData<(Original, Mutator)>);
267impl<O, Original: EnsureOrigin<O>, Mutator: Morph<Original::Success>> EnsureOrigin<O>
268	for MapSuccess<Original, Mutator>
269{
270	type Success = Mutator::Outcome;
271	fn try_origin(o: O) -> Result<Mutator::Outcome, O> {
272		Ok(Mutator::morph(Original::try_origin(o)?))
273	}
274	#[cfg(feature = "runtime-benchmarks")]
275	fn try_successful_origin() -> Result<O, ()> {
276		Original::try_successful_origin()
277	}
278}
279impl<O, Original: EnsureOriginWithArg<O, A>, Mutator: Morph<Original::Success>, A>
280	EnsureOriginWithArg<O, A> for MapSuccess<Original, Mutator>
281{
282	type Success = Mutator::Outcome;
283	fn try_origin(o: O, a: &A) -> Result<Mutator::Outcome, O> {
284		Ok(Mutator::morph(Original::try_origin(o, a)?))
285	}
286	#[cfg(feature = "runtime-benchmarks")]
287	fn try_successful_origin(a: &A) -> Result<O, ()> {
288		Original::try_successful_origin(a)
289	}
290}
291
292/// A derivative `EnsureOrigin` implementation. It mutates the `Success` result of an `Original`
293/// implementation with a given `Mutator`, allowing the possibility of an error to be returned
294/// from the mutator.
295///
296/// NOTE: This is strictly worse performance than `MapSuccess` since it clones the original origin
297/// value. If possible, use `MapSuccess` instead.
298pub struct TryMapSuccess<Orig, Mutator>(PhantomData<(Orig, Mutator)>);
299impl<O: Clone, Original: EnsureOrigin<O>, Mutator: TryMorph<Original::Success>> EnsureOrigin<O>
300	for TryMapSuccess<Original, Mutator>
301{
302	type Success = Mutator::Outcome;
303	fn try_origin(o: O) -> Result<Mutator::Outcome, O> {
304		let orig = o.clone();
305		Mutator::try_morph(Original::try_origin(o)?).map_err(|()| orig)
306	}
307	#[cfg(feature = "runtime-benchmarks")]
308	fn try_successful_origin() -> Result<O, ()> {
309		Original::try_successful_origin()
310	}
311}
312impl<O: Clone, Original: EnsureOriginWithArg<O, A>, Mutator: TryMorph<Original::Success>, A>
313	EnsureOriginWithArg<O, A> for TryMapSuccess<Original, Mutator>
314{
315	type Success = Mutator::Outcome;
316	fn try_origin(o: O, a: &A) -> Result<Mutator::Outcome, O> {
317		let orig = o.clone();
318		Mutator::try_morph(Original::try_origin(o, a)?).map_err(|()| orig)
319	}
320	#[cfg(feature = "runtime-benchmarks")]
321	fn try_successful_origin(a: &A) -> Result<O, ()> {
322		Original::try_successful_origin(a)
323	}
324}
325
326pub struct TryWithMorphedArg<O, A, Morph, Inner, Success>(
327	PhantomData<(O, A, Morph, Inner, Success)>,
328);
329impl<
330		O,
331		A,
332		Morph: for<'a> TryMorph<&'a A>,
333		Inner: for<'a> EnsureOriginWithArg<O, <Morph as TryMorph<&'a A>>::Outcome, Success = Success>,
334		Success,
335	> EnsureOriginWithArg<O, A> for TryWithMorphedArg<O, A, Morph, Inner, Success>
336{
337	type Success = Success;
338	fn try_origin(o: O, a: &A) -> Result<Success, O> {
339		match Morph::try_morph(a) {
340			Ok(x) => Inner::try_origin(o, &x),
341			_ => return Err(o),
342		}
343	}
344	#[cfg(feature = "runtime-benchmarks")]
345	fn try_successful_origin(a: &A) -> Result<O, ()> {
346		Inner::try_successful_origin(&Morph::try_morph(a).map_err(|_| ())?)
347	}
348}
349
350/// "OR gate" implementation of `EnsureOrigin` allowing for different `Success` types for `L`
351/// and `R`, with them combined using an `Either` type.
352///
353/// Origin check will pass if `L` or `R` origin check passes. `L` is tested first.
354///
355/// Successful origin is derived from the left side.
356pub struct EitherOfDiverse<L, R>(core::marker::PhantomData<(L, R)>);
357impl<OuterOrigin, L: EnsureOrigin<OuterOrigin>, R: EnsureOrigin<OuterOrigin>>
358	EnsureOrigin<OuterOrigin> for EitherOfDiverse<L, R>
359{
360	type Success = Either<L::Success, R::Success>;
361	fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
362		L::try_origin(o)
363			.map_or_else(|o| R::try_origin(o).map(Either::Right), |o| Ok(Either::Left(o)))
364	}
365
366	#[cfg(feature = "runtime-benchmarks")]
367	fn try_successful_origin() -> Result<OuterOrigin, ()> {
368		L::try_successful_origin().or_else(|()| R::try_successful_origin())
369	}
370}
371impl<
372		OuterOrigin,
373		L: EnsureOriginWithArg<OuterOrigin, Argument>,
374		R: EnsureOriginWithArg<OuterOrigin, Argument>,
375		Argument,
376	> EnsureOriginWithArg<OuterOrigin, Argument> for EitherOfDiverse<L, R>
377{
378	type Success = Either<L::Success, R::Success>;
379	fn try_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, OuterOrigin> {
380		L::try_origin(o, a)
381			.map_or_else(|o| R::try_origin(o, a).map(Either::Right), |o| Ok(Either::Left(o)))
382	}
383
384	#[cfg(feature = "runtime-benchmarks")]
385	fn try_successful_origin(a: &Argument) -> Result<OuterOrigin, ()> {
386		L::try_successful_origin(a).or_else(|()| R::try_successful_origin(a))
387	}
388}
389
390/// "OR gate" implementation of `EnsureOrigin` allowing for different `Success` types for `L`
391/// and `R`, with them combined using an `Either` type.
392///
393/// Origin check will pass if `L` or `R` origin check passes. `L` is tested first.
394///
395/// Successful origin is derived from the left side.
396#[deprecated = "Use `EitherOfDiverse` instead"]
397pub type EnsureOneOf<L, R> = EitherOfDiverse<L, R>;
398
399/// "OR gate" implementation of `EnsureOrigin`, `Success` type for both `L` and `R` must
400/// be equal.
401///
402/// Origin check will pass if `L` or `R` origin check passes. `L` is tested first.
403///
404/// Successful origin is derived from the left side.
405pub struct EitherOf<L, R>(core::marker::PhantomData<(L, R)>);
406impl<
407		OuterOrigin,
408		L: EnsureOrigin<OuterOrigin>,
409		R: EnsureOrigin<OuterOrigin, Success = L::Success>,
410	> EnsureOrigin<OuterOrigin> for EitherOf<L, R>
411{
412	type Success = L::Success;
413	fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin> {
414		L::try_origin(o).or_else(|o| R::try_origin(o))
415	}
416
417	#[cfg(feature = "runtime-benchmarks")]
418	fn try_successful_origin() -> Result<OuterOrigin, ()> {
419		L::try_successful_origin().or_else(|()| R::try_successful_origin())
420	}
421}
422impl<
423		OuterOrigin,
424		L: EnsureOriginWithArg<OuterOrigin, Argument>,
425		R: EnsureOriginWithArg<OuterOrigin, Argument, Success = L::Success>,
426		Argument,
427	> EnsureOriginWithArg<OuterOrigin, Argument> for EitherOf<L, R>
428{
429	type Success = L::Success;
430	fn try_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, OuterOrigin> {
431		L::try_origin(o, a).or_else(|o| R::try_origin(o, a))
432	}
433
434	#[cfg(feature = "runtime-benchmarks")]
435	fn try_successful_origin(a: &Argument) -> Result<OuterOrigin, ()> {
436		L::try_successful_origin(a).or_else(|()| R::try_successful_origin(a))
437	}
438}
439
440/// Type that can be dispatched with an origin but without checking the origin filter.
441///
442/// Implemented for pallet dispatchable type by `decl_module` and for runtime dispatchable by
443/// `construct_runtime`.
444pub trait UnfilteredDispatchable {
445	/// The origin type of the runtime, (i.e. `frame_system::Config::RuntimeOrigin`).
446	type RuntimeOrigin;
447
448	/// Dispatch this call but do not check the filter in origin.
449	fn dispatch_bypass_filter(self, origin: Self::RuntimeOrigin) -> DispatchResultWithPostInfo;
450}
451
452/// The trait implemented by the overarching enumeration of the different pallets' origins.
453/// Unlike `OriginTrait` impls, this does not include any kind of dispatch/call filter. Also, this
454/// trait is more flexible in terms of how it can be used: it is a `Parameter` and `Member`, so it
455/// can be used as dispatchable parameters as well as in storage items.
456pub trait CallerTrait<AccountId>: Parameter + Member + From<RawOrigin<AccountId>> {
457	/// Extract the signer from the message if it is a `Signed` origin.
458	fn into_system(self) -> Option<RawOrigin<AccountId>>;
459
460	/// Extract a reference to the system-level `RawOrigin` if it is that.
461	fn as_system_ref(&self) -> Option<&RawOrigin<AccountId>>;
462
463	/// Extract the signer from it if a system `Signed` origin, `None` otherwise.
464	fn as_signed(&self) -> Option<&AccountId> {
465		self.as_system_ref().and_then(RawOrigin::as_signed)
466	}
467
468	/// Returns `true` if `self` is a system `Root` origin, `None` otherwise.
469	fn is_root(&self) -> bool {
470		self.as_system_ref().map_or(false, RawOrigin::is_root)
471	}
472
473	/// Returns `true` if `self` is a system `None` origin, `None` otherwise.
474	fn is_none(&self) -> bool {
475		self.as_system_ref().map_or(false, RawOrigin::is_none)
476	}
477}
478
479/// Methods available on `frame_system::Config::RuntimeOrigin`.
480pub trait OriginTrait: Sized {
481	/// Runtime call type, as in `frame_system::Config::Call`
482	type Call;
483
484	/// The caller origin, overarching type of all pallets origins.
485	type PalletsOrigin: Send + Sync + Into<Self> + CallerTrait<Self::AccountId> + MaxEncodedLen;
486
487	/// The AccountId used across the system.
488	type AccountId;
489
490	/// Add a filter to the origin.
491	fn add_filter(&mut self, filter: impl Fn(&Self::Call) -> bool + 'static);
492
493	/// Reset origin filters to default one, i.e `frame_system::1fig::BaseCallFilter`.
494	fn reset_filter(&mut self);
495
496	/// Replace the caller with caller from the other origin
497	fn set_caller_from(&mut self, other: impl Into<Self>);
498
499	/// Replace the caller with caller from the other origin
500	fn set_caller(&mut self, caller: Self::PalletsOrigin);
501
502	/// Replace the caller with caller from the other origin
503	fn set_caller_from_signed(&mut self, caller_account: Self::AccountId) {
504		self.set_caller(Self::PalletsOrigin::from(RawOrigin::Signed(caller_account)))
505	}
506
507	/// Filter the call if caller is not root, if false is returned then the call must be filtered
508	/// out.
509	///
510	/// For root origin caller, the filters are bypassed and true is returned.
511	fn filter_call(&self, call: &Self::Call) -> bool;
512
513	/// Get a reference to the caller (`CallerTrait` impl).
514	fn caller(&self) -> &Self::PalletsOrigin;
515
516	/// Consume `self` and return the caller.
517	fn into_caller(self) -> Self::PalletsOrigin;
518
519	/// Do something with the caller, consuming self but returning it if the caller was unused.
520	fn try_with_caller<R>(
521		self,
522		f: impl FnOnce(Self::PalletsOrigin) -> Result<R, Self::PalletsOrigin>,
523	) -> Result<R, Self>;
524
525	/// Create with system none origin and `frame_system::Config::BaseCallFilter`.
526	fn none() -> Self;
527
528	/// Create with system root origin and `frame_system::Config::BaseCallFilter`.
529	fn root() -> Self;
530
531	/// Create with system signed origin and `frame_system::Config::BaseCallFilter`.
532	fn signed(by: Self::AccountId) -> Self;
533
534	/// Extract the signer from the message if it is a `Signed` origin.
535	#[deprecated = "Use `into_signer` instead"]
536	fn as_signed(self) -> Option<Self::AccountId> {
537		self.into_signer()
538	}
539
540	/// Extract the signer from the message if it is a `Signed` origin.
541	fn into_signer(self) -> Option<Self::AccountId> {
542		self.into_caller().into_system().and_then(|s| {
543			if let RawOrigin::Signed(who) = s {
544				Some(who)
545			} else {
546				None
547			}
548		})
549	}
550
551	/// Extract a reference to the system origin, if that's what the caller is.
552	fn as_system_ref(&self) -> Option<&RawOrigin<Self::AccountId>> {
553		self.caller().as_system_ref()
554	}
555
556	/// Extract a reference to the signer, if that's what the caller is.
557	fn as_signer(&self) -> Option<&Self::AccountId> {
558		self.caller().as_system_ref().and_then(|s| {
559			if let RawOrigin::Signed(ref who) = s {
560				Some(who)
561			} else {
562				None
563			}
564		})
565	}
566}
567
568#[cfg(test)]
569mod tests {
570	use super::*;
571	use crate::traits::{ConstBool, ConstU8, TypedGet};
572	use std::marker::PhantomData;
573
574	struct EnsureSuccess<V>(PhantomData<V>);
575	struct EnsureFail<T>(PhantomData<T>);
576
577	impl<V: TypedGet> EnsureOrigin<()> for EnsureSuccess<V> {
578		type Success = V::Type;
579		fn try_origin(_: ()) -> Result<Self::Success, ()> {
580			Ok(V::get())
581		}
582		#[cfg(feature = "runtime-benchmarks")]
583		fn try_successful_origin() -> Result<(), ()> {
584			Ok(())
585		}
586	}
587
588	impl<T> EnsureOrigin<()> for EnsureFail<T> {
589		type Success = T;
590		fn try_origin(_: ()) -> Result<Self::Success, ()> {
591			Err(())
592		}
593		#[cfg(feature = "runtime-benchmarks")]
594		fn try_successful_origin() -> Result<(), ()> {
595			Err(())
596		}
597	}
598
599	#[test]
600	fn either_of_diverse_works() {
601		assert_eq!(
602			EitherOfDiverse::<
603				EnsureSuccess<ConstBool<true>>,
604				EnsureSuccess<ConstU8<0>>,
605			>::try_origin(()).unwrap().left(),
606			Some(true)
607		);
608		assert_eq!(
609			EitherOfDiverse::<EnsureSuccess<ConstBool<true>>, EnsureFail<u8>>::try_origin(())
610				.unwrap()
611				.left(),
612			Some(true)
613		);
614		assert_eq!(
615			EitherOfDiverse::<EnsureFail<bool>, EnsureSuccess<ConstU8<0>>>::try_origin(())
616				.unwrap()
617				.right(),
618			Some(0u8)
619		);
620		assert!(EitherOfDiverse::<EnsureFail<bool>, EnsureFail<u8>>::try_origin(()).is_err());
621	}
622
623	#[test]
624	fn either_of_works() {
625		assert_eq!(
626			EitherOf::<
627				EnsureSuccess<ConstBool<true>>,
628				EnsureSuccess<ConstBool<false>>,
629			>::try_origin(()).unwrap(),
630			true
631		);
632		assert_eq!(
633			EitherOf::<EnsureSuccess<ConstBool<true>>, EnsureFail<bool>>::try_origin(()).unwrap(),
634			true
635		);
636		assert_eq!(
637			EitherOf::<EnsureFail<bool>, EnsureSuccess<ConstBool<false>>>::try_origin(()).unwrap(),
638			false
639		);
640		assert!(EitherOf::<EnsureFail<bool>, EnsureFail<bool>>::try_origin(()).is_err());
641	}
642}