Allow lossless matching for Origin (#8576)

* Allow lossless matching for Origin

Without these changes, it's difficult/impossible to not lose any filters
when making fine-grained matches against origin.

* whilespace

* Apply suggestions from code review

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Gavin Wood
2021-04-08 20:06:09 +02:00
committed by GitHub
parent d440ef322b
commit f935dfc1a1
2 changed files with 46 additions and 0 deletions
+40
View File
@@ -246,6 +246,16 @@ macro_rules! impl_outer_origin {
&self.caller
}
fn try_with_caller<R>(
mut self,
f: impl FnOnce(Self::PalletsOrigin) -> Result<R, Self::PalletsOrigin>,
) -> Result<R, Self> {
match f(self.caller) {
Ok(r) => Ok(r),
Err(caller) => { self.caller = caller; Err(self) }
}
}
/// Create with system none origin and `frame-system::Config::BaseCallFilter`.
fn none() -> Self {
$system::RawOrigin::None.into()
@@ -299,6 +309,20 @@ macro_rules! impl_outer_origin {
$caller_name::system(x)
}
}
impl $crate::sp_std::convert::TryFrom<$caller_name> for $system::Origin<$runtime> {
type Error = $caller_name;
fn try_from(x: $caller_name)
-> $crate::sp_std::result::Result<$system::Origin<$runtime>, $caller_name>
{
if let $caller_name::system(l) = x {
Ok(l)
} else {
Err(x)
}
}
}
impl From<$system::Origin<$runtime>> for $name {
/// Convert to runtime origin:
/// * root origin is built with no filter
@@ -376,6 +400,22 @@ macro_rules! impl_outer_origin {
}
}
}
impl $crate::sp_std::convert::TryFrom<
$caller_name
> for $module::Origin < $( $generic )? $(, $module::$generic_instance )? > {
type Error = $caller_name;
fn try_from(x: $caller_name) -> $crate::sp_std::result::Result<
$module::Origin < $( $generic )? $(, $module::$generic_instance )? >,
$caller_name,
> {
if let $caller_name::[< $module $( _ $generic_instance )? >](l) = x {
Ok(l)
} else {
Err(x)
}
}
}
}
)*
}
@@ -76,6 +76,12 @@ pub trait OriginTrait: Sized {
/// Get the caller.
fn caller(&self) -> &Self::PalletsOrigin;
/// Do something with the caller, consuming self but returning it if the caller was unused.
fn try_with_caller<R>(
self,
f: impl FnOnce(Self::PalletsOrigin) -> Result<R, Self::PalletsOrigin>,
) -> Result<R, Self>;
/// Create with system none origin and `frame-system::Config::BaseCallFilter`.
fn none() -> Self;