Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions bridges/snowbridge/pallets/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ mod pallet_xcm_origin {
pub struct EnsureXcm<F>(PhantomData<F>);
impl<O: OriginTrait + From<Origin>, F: Contains<Location>> EnsureOrigin<O> for EnsureXcm<F>
where
O::PalletsOrigin: From<Origin> + TryInto<Origin, Error = O::PalletsOrigin>,
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = Location;

fn try_origin(outer: O) -> Result<Self::Success, O> {
outer.try_with_caller(|caller| {
caller.try_into().and_then(|o| match o {
Origin(location) if F::contains(&location) => Ok(location),
o => Err(o.into()),
})
})
match outer.caller().try_into() {
Ok(Origin(ref location)) if F::contains(location) => return Ok(location.clone()),
_ => (),
}

Err(outer)
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ impl<
> EnsureOriginWithArg<RuntimeOrigin, L>
for ForeignAssetOwner<IsForeign, AssetInspect, AccountId, LocationToAccountId, L>
where
RuntimeOrigin::PalletsOrigin:
From<XcmOrigin> + TryInto<XcmOrigin, Error = RuntimeOrigin::PalletsOrigin>,
for<'a> &'a RuntimeOrigin::PalletsOrigin: TryInto<&'a XcmOrigin>,
<AssetInspect as frame_support::traits::fungibles::Inspect<AccountId>>::AssetId: From<Location>,
{
type Success = L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ impl<
L: TryFrom<Location> + TryInto<Location> + Clone + Debug,
> EnsureOriginWithArg<RuntimeOrigin, L> for ForeignCreators<IsForeign, AccountOf, AccountId, L>
where
RuntimeOrigin::PalletsOrigin:
From<XcmOrigin> + TryInto<XcmOrigin, Error = RuntimeOrigin::PalletsOrigin>,
for<'a> &'a RuntimeOrigin::PalletsOrigin: TryInto<&'a XcmOrigin>,
{
type Success = AccountId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ pub mod pallet_origins {

/// Implementation of the [EnsureOrigin] trait for the [Origin::HeadAmbassadors] origin.
pub struct EnsureHeadAmbassadorsVoice;
impl<O: Into<Result<Origin, O>> + From<Origin>> EnsureOrigin<O> for EnsureHeadAmbassadorsVoice {
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for EnsureHeadAmbassadorsVoice
where
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = ();
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
Origin::HeadAmbassadors => Ok(()),
r => Err(O::from(r)),
})
match o.caller().try_into() {
Ok(Origin::HeadAmbassadors) => return Ok(()),
_ => (),
}

Err(o)
}

#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -108,15 +113,18 @@ pub mod pallet_origins {
/// Implementation of the [EnsureOrigin] trait for the plurality voice [Origin]s
/// from a given rank `R` with the success result of the corresponding [Rank].
pub struct EnsureAmbassadorsVoiceFrom<R>(PhantomData<R>);
impl<R: Get<Rank>, O: Into<Result<Origin, O>> + From<Origin>> EnsureOrigin<O>
for EnsureAmbassadorsVoiceFrom<R>
impl<R: Get<Rank>, O: OriginTrait + From<Origin>> EnsureOrigin<O> for EnsureAmbassadorsVoiceFrom<R>
where
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = Rank;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match Origin::as_voice(&o) {
Some(r) if r >= R::get() => Ok(r),
_ => Err(O::from(o)),
})
match o.caller().try_into().map(|o| Origin::as_voice(o)) {
Ok(Some(r)) if r >= R::get() => return Ok(r),
_ => (),
}

Err(o)
}

#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -131,10 +139,18 @@ pub mod pallet_origins {
/// Implementation of the [EnsureOrigin] trait for the plurality voice [Origin]s with the
/// success result of the corresponding [Rank].
pub struct EnsureAmbassadorsVoice;
impl<O: Into<Result<Origin, O>> + From<Origin>> EnsureOrigin<O> for EnsureAmbassadorsVoice {
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for EnsureAmbassadorsVoice
where
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = Rank;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| Origin::as_voice(&o).ok_or(O::from(o)))
match o.caller().try_into().map(|o| Origin::as_voice(o)) {
Ok(Some(r)) => return Ok(r),
_ => (),
}

Err(o)
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,18 @@ pub mod pallet_origins {
macro_rules! decl_unit_ensures {
( $name:ident: $success_type:ty = $success:expr ) => {
pub struct $name;
impl<O: Into<Result<Origin, O>> + From<Origin>>
EnsureOrigin<O> for $name
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
where
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = $success_type;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
Origin::$name => Ok($success),
r => Err(O::from(r)),
})
match o.caller().try_into() {
Ok(Origin::$name) => return Ok($success),
_ => (),
}

Err(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Expand Down Expand Up @@ -187,17 +190,20 @@ pub mod pallet_origins {
}
) => {
$vis struct $name;
impl<O: Into<Result<Origin, O>> + From<Origin>>
EnsureOrigin<O> for $name
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
where
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = $success_type;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
match o.caller().try_into() {
$(
Origin::$item => Ok($success),
Ok(Origin::$item) => return Ok($success),
)*
r => Err(O::from(r)),
})
_ => (),
}

Err(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Expand Down
30 changes: 18 additions & 12 deletions polkadot/runtime/rococo/src/governance/origins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,18 @@ pub mod pallet_custom_origins {
macro_rules! decl_unit_ensures {
( $name:ident: $success_type:ty = $success:expr ) => {
pub struct $name;
impl<O: Into<Result<Origin, O>> + From<Origin>>
EnsureOrigin<O> for $name
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
where
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = $success_type;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
Origin::$name => Ok($success),
r => Err(O::from(r)),
})
match o.caller().try_into() {
Ok(Origin::$name) => return Ok($success),
_ => (),
}

Err(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Expand Down Expand Up @@ -151,17 +154,20 @@ pub mod pallet_custom_origins {
}
) => {
$vis struct $name;
impl<O: Into<Result<Origin, O>> + From<Origin>>
EnsureOrigin<O> for $name
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
where
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = $success_type;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
match o.caller().try_into() {
$(
Origin::$item => Ok($success),
Ok(Origin::$item) => return Ok($success),
)*
r => Err(O::from(r)),
})
_ => (),
}

Err(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Expand Down
30 changes: 18 additions & 12 deletions polkadot/runtime/westend/src/governance/origins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,18 @@ pub mod pallet_custom_origins {
macro_rules! decl_unit_ensures {
( $name:ident: $success_type:ty = $success:expr ) => {
pub struct $name;
impl<O: Into<Result<Origin, O>> + From<Origin>>
EnsureOrigin<O> for $name
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
where
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = $success_type;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
Origin::$name => Ok($success),
r => Err(O::from(r)),
})
match o.caller().try_into() {
Ok(Origin::$name) => return Ok($success),
_ => (),
}

Err(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Expand Down Expand Up @@ -151,17 +154,20 @@ pub mod pallet_custom_origins {
}
) => {
$vis struct $name;
impl<O: Into<Result<Origin, O>> + From<Origin>>
EnsureOrigin<O> for $name
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
where
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = $success_type;
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
match o.caller().try_into() {
$(
Origin::$item => Ok($success),
Ok(Origin::$item) => return Ok($success),
)*
r => Err(O::from(r)),
})
_ => (),
}

Err(o)
}
#[cfg(feature = "runtime-benchmarks")]
fn try_successful_origin() -> Result<O, ()> {
Expand Down
36 changes: 19 additions & 17 deletions polkadot/xcm/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3590,20 +3590,22 @@ impl<
L: TryFrom<Location> + TryInto<Location> + Clone,
> EnsureOrigin<O> for EnsureXcm<F, L>
where
O::PalletsOrigin: From<Origin> + TryInto<Origin, Error = O::PalletsOrigin>,
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = L;

fn try_origin(outer: O) -> Result<Self::Success, O> {
outer.try_with_caller(|caller| {
caller.try_into().and_then(|o| match o {
Origin::Xcm(ref location)
if F::contains(&location.clone().try_into().map_err(|_| o.clone().into())?) =>
Ok(location.clone().try_into().map_err(|_| o.clone().into())?),
Origin::Xcm(location) => Err(Origin::Xcm(location).into()),
o => Err(o.into()),
})
})
match outer.caller().try_into() {
Ok(Origin::Xcm(ref location)) =>
if let Ok(location) = location.clone().try_into() {
if F::contains(&location) {
return Ok(location);
}
},
_ => (),
}

Err(outer)
}

#[cfg(feature = "runtime-benchmarks")]
Expand All @@ -3617,17 +3619,17 @@ where
pub struct EnsureResponse<F>(PhantomData<F>);
impl<O: OriginTrait + From<Origin>, F: Contains<Location>> EnsureOrigin<O> for EnsureResponse<F>
where
O::PalletsOrigin: From<Origin> + TryInto<Origin, Error = O::PalletsOrigin>,
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
{
type Success = Location;

fn try_origin(outer: O) -> Result<Self::Success, O> {
outer.try_with_caller(|caller| {
caller.try_into().and_then(|o| match o {
Origin::Response(responder) => Ok(responder),
o => Err(o.into()),
})
})
match outer.caller().try_into() {
Ok(Origin::Response(responder)) => return Ok(responder.clone()),
_ => (),
}

Err(outer)
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
29 changes: 29 additions & 0 deletions prdoc/pr_8000.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
title: Optimize origin checks
doc:
- audience: Runtime Dev
description: Optimize origin checks, avoid cloning and conversion when not needed.
crates:
- name: snowbridge-pallet-system
bump: patch
- name: collectives-westend-runtime
bump: major
- name: rococo-runtime
bump: major
- name: westend-runtime
bump: major
- name: pallet-xcm
bump: major
- name: pallet-collective
bump: major
- name: pallet-ranked-collective
bump: patch
- name: pallet-society
bump: patch
- name: frame-support
bump: major
- name: frame-system
bump: major
- name: pallet-treasury
bump: patch
- name: snowbridge-runtime-common
bump: major
Loading
Loading