Skip to content

Commit b5c1c81

Browse files
gavofyorkkianenigmabkchr
authored
Allow lossless matching for Origin (paritytech#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>
1 parent 3297b9a commit b5c1c81

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

frame/support/src/origin.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ macro_rules! impl_outer_origin {
246246
&self.caller
247247
}
248248

249+
fn try_with_caller<R>(
250+
mut self,
251+
f: impl FnOnce(Self::PalletsOrigin) -> Result<R, Self::PalletsOrigin>,
252+
) -> Result<R, Self> {
253+
match f(self.caller) {
254+
Ok(r) => Ok(r),
255+
Err(caller) => { self.caller = caller; Err(self) }
256+
}
257+
}
258+
249259
/// Create with system none origin and `frame-system::Config::BaseCallFilter`.
250260
fn none() -> Self {
251261
$system::RawOrigin::None.into()
@@ -299,6 +309,20 @@ macro_rules! impl_outer_origin {
299309
$caller_name::system(x)
300310
}
301311
}
312+
313+
impl $crate::sp_std::convert::TryFrom<$caller_name> for $system::Origin<$runtime> {
314+
type Error = $caller_name;
315+
fn try_from(x: $caller_name)
316+
-> $crate::sp_std::result::Result<$system::Origin<$runtime>, $caller_name>
317+
{
318+
if let $caller_name::system(l) = x {
319+
Ok(l)
320+
} else {
321+
Err(x)
322+
}
323+
}
324+
}
325+
302326
impl From<$system::Origin<$runtime>> for $name {
303327
/// Convert to runtime origin:
304328
/// * root origin is built with no filter
@@ -376,6 +400,22 @@ macro_rules! impl_outer_origin {
376400
}
377401
}
378402
}
403+
404+
impl $crate::sp_std::convert::TryFrom<
405+
$caller_name
406+
> for $module::Origin < $( $generic )? $(, $module::$generic_instance )? > {
407+
type Error = $caller_name;
408+
fn try_from(x: $caller_name) -> $crate::sp_std::result::Result<
409+
$module::Origin < $( $generic )? $(, $module::$generic_instance )? >,
410+
$caller_name,
411+
> {
412+
if let $caller_name::[< $module $( _ $generic_instance )? >](l) = x {
413+
Ok(l)
414+
} else {
415+
Err(x)
416+
}
417+
}
418+
}
379419
}
380420
)*
381421
}

frame/support/src/traits/dispatch.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ pub trait OriginTrait: Sized {
7676
/// Get the caller.
7777
fn caller(&self) -> &Self::PalletsOrigin;
7878

79+
/// Do something with the caller, consuming self but returning it if the caller was unused.
80+
fn try_with_caller<R>(
81+
self,
82+
f: impl FnOnce(Self::PalletsOrigin) -> Result<R, Self::PalletsOrigin>,
83+
) -> Result<R, Self>;
84+
7985
/// Create with system none origin and `frame-system::Config::BaseCallFilter`.
8086
fn none() -> Self;
8187

0 commit comments

Comments
 (0)