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

"OR gate" for EnsureOrigin #6237

Merged
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
Prev Previous commit
Next Next commit
Add docstring; Update 'Success' type.
  • Loading branch information
shaunxw committed Jun 8, 2020
commit 4518b5db6ad67dd4503752ab1c476ee44b40574f
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 14 additions & 12 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ use sp_std::marker::PhantomData;
use sp_std::fmt::Debug;
use sp_version::RuntimeVersion;
use sp_runtime::{
RuntimeDebug, Perbill, DispatchError, DispatchResult,
RuntimeDebug, Perbill, DispatchError, DispatchResult, Either,
generic::{self, Era},
transaction_validity::{
ValidTransaction, TransactionPriority, TransactionLongevity, TransactionValidityError,
Expand Down Expand Up @@ -847,18 +847,20 @@ impl<O, T> EnsureOrigin<O> for EnsureNever<T> {
}
}

pub struct EnsureOneOf<AccountId, A, B>(A, B, sp_std::marker::PhantomData<AccountId>);
/// The "OR gate" implementation of `EnsureOrigin`. Origin check would pass if any of `L`'s or
/// `R`'s origin check passes.
shaunxw marked this conversation as resolved.
Show resolved Hide resolved
pub struct EnsureOneOf<AccountId, L, R>(sp_std::marker::PhantomData<(AccountId, L, R)>);
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 = ();
L: EnsureOrigin<O>,
R: EnsureOrigin<O>,
> EnsureOrigin<O> for EnsureOneOf<AccountId, L, R> {
type Success = Either<L::Success, R::Success>;
fn try_origin(o: O) -> Result<Self::Success, O> {
A::try_origin(o).map_or_else(
|o| B::try_origin(o).map(|_| ()),
|_| Ok(()),
L::try_origin(o).map_or_else(
|o| R::try_origin(o).map(|o| Either::Right(o)),
|o| Ok(Either::Left(o)),
)
}

Expand Down Expand Up @@ -2725,12 +2727,12 @@ pub(crate) mod tests {

#[test]
fn ensure_one_of_works() {
fn ensure_root_or_signed(o: RawOrigin<u64>) -> Result<(), Origin> {
fn ensure_root_or_signed(o: RawOrigin<u64>) -> Result<Either<(), u64>, 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_ok!(ensure_root_or_signed(RawOrigin::Root), Either::Left(()));
assert_ok!(ensure_root_or_signed(RawOrigin::Signed(0)), Either::Right(0));
assert_err!(ensure_root_or_signed(RawOrigin::None), Origin::from(RawOrigin::None));
}
}
1 change: 1 addition & 0 deletions primitives/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl-trait-for-tuples = "0.1.3"
sp-inherents = { version = "2.0.0-rc2", default-features = false, path = "../inherents" }
parity-util-mem = { version = "0.6.1", default-features = false, features = ["primitive-types"] }
hash256-std-hasher = { version = "0.15.2", default-features = false }
either = { version = "1.5", default-features = false }
shaunxw marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
serde_json = "1.0.41"
Expand Down
2 changes: 2 additions & 0 deletions primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub use sp_arithmetic::biguint;

pub use random_number_generator::RandomNumberGenerator;

pub use either::Either;

/// An abstraction over justification for a block's validity under a consensus algorithm.
///
/// Essentially a finality proof. The exact formulation will vary between consensus
Expand Down