Skip to content

Commit f9fac6c

Browse files
Optimize origin checks (#8000)
Optimize origin checks, avoid cloning and conversion when not needed. --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 6a3bcf8 commit f9fac6c

File tree

16 files changed

+330
-225
lines changed

16 files changed

+330
-225
lines changed

bridges/snowbridge/pallets/system/src/mock.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ mod pallet_xcm_origin {
7373
pub struct EnsureXcm<F>(PhantomData<F>);
7474
impl<O: OriginTrait + From<Origin>, F: Contains<Location>> EnsureOrigin<O> for EnsureXcm<F>
7575
where
76-
O::PalletsOrigin: From<Origin> + TryInto<Origin, Error = O::PalletsOrigin>,
76+
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
7777
{
7878
type Success = Location;
7979

8080
fn try_origin(outer: O) -> Result<Self::Success, O> {
81-
outer.try_with_caller(|caller| {
82-
caller.try_into().and_then(|o| match o {
83-
Origin(location) if F::contains(&location) => Ok(location),
84-
o => Err(o.into()),
85-
})
86-
})
81+
match outer.caller().try_into() {
82+
Ok(Origin(ref location)) if F::contains(location) => return Ok(location.clone()),
83+
_ => (),
84+
}
85+
86+
Err(outer)
8787
}
8888

8989
#[cfg(feature = "runtime-benchmarks")]

bridges/snowbridge/runtime/runtime-common/src/v2/register_token.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ impl<
2727
> EnsureOriginWithArg<RuntimeOrigin, L>
2828
for ForeignAssetOwner<IsForeign, AssetInspect, AccountId, LocationToAccountId, L>
2929
where
30-
RuntimeOrigin::PalletsOrigin:
31-
From<XcmOrigin> + TryInto<XcmOrigin, Error = RuntimeOrigin::PalletsOrigin>,
30+
for<'a> &'a RuntimeOrigin::PalletsOrigin: TryInto<&'a XcmOrigin>,
3231
<AssetInspect as frame_support::traits::fungibles::Inspect<AccountId>>::AssetId: From<Location>,
3332
{
3433
type Success = L;

cumulus/parachains/runtimes/assets/common/src/foreign_creators.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ impl<
3434
L: TryFrom<Location> + TryInto<Location> + Clone + Debug,
3535
> EnsureOriginWithArg<RuntimeOrigin, L> for ForeignCreators<IsForeign, AccountOf, AccountId, L>
3636
where
37-
RuntimeOrigin::PalletsOrigin:
38-
From<XcmOrigin> + TryInto<XcmOrigin, Error = RuntimeOrigin::PalletsOrigin>,
37+
for<'a> &'a RuntimeOrigin::PalletsOrigin: TryInto<&'a XcmOrigin>,
3938
{
4039
type Success = AccountId;
4140

cumulus/parachains/runtimes/collectives/collectives-westend/src/ambassador/origins.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,18 @@ pub mod pallet_origins {
9090

9191
/// Implementation of the [EnsureOrigin] trait for the [Origin::HeadAmbassadors] origin.
9292
pub struct EnsureHeadAmbassadorsVoice;
93-
impl<O: Into<Result<Origin, O>> + From<Origin>> EnsureOrigin<O> for EnsureHeadAmbassadorsVoice {
93+
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for EnsureHeadAmbassadorsVoice
94+
where
95+
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
96+
{
9497
type Success = ();
9598
fn try_origin(o: O) -> Result<Self::Success, O> {
96-
o.into().and_then(|o| match o {
97-
Origin::HeadAmbassadors => Ok(()),
98-
r => Err(O::from(r)),
99-
})
99+
match o.caller().try_into() {
100+
Ok(Origin::HeadAmbassadors) => return Ok(()),
101+
_ => (),
102+
}
103+
104+
Err(o)
100105
}
101106

102107
#[cfg(feature = "runtime-benchmarks")]
@@ -108,15 +113,18 @@ pub mod pallet_origins {
108113
/// Implementation of the [EnsureOrigin] trait for the plurality voice [Origin]s
109114
/// from a given rank `R` with the success result of the corresponding [Rank].
110115
pub struct EnsureAmbassadorsVoiceFrom<R>(PhantomData<R>);
111-
impl<R: Get<Rank>, O: Into<Result<Origin, O>> + From<Origin>> EnsureOrigin<O>
112-
for EnsureAmbassadorsVoiceFrom<R>
116+
impl<R: Get<Rank>, O: OriginTrait + From<Origin>> EnsureOrigin<O> for EnsureAmbassadorsVoiceFrom<R>
117+
where
118+
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
113119
{
114120
type Success = Rank;
115121
fn try_origin(o: O) -> Result<Self::Success, O> {
116-
o.into().and_then(|o| match Origin::as_voice(&o) {
117-
Some(r) if r >= R::get() => Ok(r),
118-
_ => Err(O::from(o)),
119-
})
122+
match o.caller().try_into().map(|o| Origin::as_voice(o)) {
123+
Ok(Some(r)) if r >= R::get() => return Ok(r),
124+
_ => (),
125+
}
126+
127+
Err(o)
120128
}
121129

122130
#[cfg(feature = "runtime-benchmarks")]
@@ -131,10 +139,18 @@ pub mod pallet_origins {
131139
/// Implementation of the [EnsureOrigin] trait for the plurality voice [Origin]s with the
132140
/// success result of the corresponding [Rank].
133141
pub struct EnsureAmbassadorsVoice;
134-
impl<O: Into<Result<Origin, O>> + From<Origin>> EnsureOrigin<O> for EnsureAmbassadorsVoice {
142+
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for EnsureAmbassadorsVoice
143+
where
144+
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
145+
{
135146
type Success = Rank;
136147
fn try_origin(o: O) -> Result<Self::Success, O> {
137-
o.into().and_then(|o| Origin::as_voice(&o).ok_or(O::from(o)))
148+
match o.caller().try_into().map(|o| Origin::as_voice(o)) {
149+
Ok(Some(r)) => return Ok(r),
150+
_ => (),
151+
}
152+
153+
Err(o)
138154
}
139155

140156
#[cfg(feature = "runtime-benchmarks")]

cumulus/parachains/runtimes/collectives/collectives-westend/src/fellowship/origins.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,18 @@ pub mod pallet_origins {
146146
macro_rules! decl_unit_ensures {
147147
( $name:ident: $success_type:ty = $success:expr ) => {
148148
pub struct $name;
149-
impl<O: Into<Result<Origin, O>> + From<Origin>>
150-
EnsureOrigin<O> for $name
149+
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
150+
where
151+
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
151152
{
152153
type Success = $success_type;
153154
fn try_origin(o: O) -> Result<Self::Success, O> {
154-
o.into().and_then(|o| match o {
155-
Origin::$name => Ok($success),
156-
r => Err(O::from(r)),
157-
})
155+
match o.caller().try_into() {
156+
Ok(Origin::$name) => return Ok($success),
157+
_ => (),
158+
}
159+
160+
Err(o)
158161
}
159162
#[cfg(feature = "runtime-benchmarks")]
160163
fn try_successful_origin() -> Result<O, ()> {
@@ -187,17 +190,20 @@ pub mod pallet_origins {
187190
}
188191
) => {
189192
$vis struct $name;
190-
impl<O: Into<Result<Origin, O>> + From<Origin>>
191-
EnsureOrigin<O> for $name
193+
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
194+
where
195+
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
192196
{
193197
type Success = $success_type;
194198
fn try_origin(o: O) -> Result<Self::Success, O> {
195-
o.into().and_then(|o| match o {
199+
match o.caller().try_into() {
196200
$(
197-
Origin::$item => Ok($success),
201+
Ok(Origin::$item) => return Ok($success),
198202
)*
199-
r => Err(O::from(r)),
200-
})
203+
_ => (),
204+
}
205+
206+
Err(o)
201207
}
202208
#[cfg(feature = "runtime-benchmarks")]
203209
fn try_successful_origin() -> Result<O, ()> {

polkadot/runtime/rococo/src/governance/origins.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,18 @@ pub mod pallet_custom_origins {
101101
macro_rules! decl_unit_ensures {
102102
( $name:ident: $success_type:ty = $success:expr ) => {
103103
pub struct $name;
104-
impl<O: Into<Result<Origin, O>> + From<Origin>>
105-
EnsureOrigin<O> for $name
104+
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
105+
where
106+
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
106107
{
107108
type Success = $success_type;
108109
fn try_origin(o: O) -> Result<Self::Success, O> {
109-
o.into().and_then(|o| match o {
110-
Origin::$name => Ok($success),
111-
r => Err(O::from(r)),
112-
})
110+
match o.caller().try_into() {
111+
Ok(Origin::$name) => return Ok($success),
112+
_ => (),
113+
}
114+
115+
Err(o)
113116
}
114117
#[cfg(feature = "runtime-benchmarks")]
115118
fn try_successful_origin() -> Result<O, ()> {
@@ -151,17 +154,20 @@ pub mod pallet_custom_origins {
151154
}
152155
) => {
153156
$vis struct $name;
154-
impl<O: Into<Result<Origin, O>> + From<Origin>>
155-
EnsureOrigin<O> for $name
157+
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
158+
where
159+
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
156160
{
157161
type Success = $success_type;
158162
fn try_origin(o: O) -> Result<Self::Success, O> {
159-
o.into().and_then(|o| match o {
163+
match o.caller().try_into() {
160164
$(
161-
Origin::$item => Ok($success),
165+
Ok(Origin::$item) => return Ok($success),
162166
)*
163-
r => Err(O::from(r)),
164-
})
167+
_ => (),
168+
}
169+
170+
Err(o)
165171
}
166172
#[cfg(feature = "runtime-benchmarks")]
167173
fn try_successful_origin() -> Result<O, ()> {

polkadot/runtime/westend/src/governance/origins.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,18 @@ pub mod pallet_custom_origins {
101101
macro_rules! decl_unit_ensures {
102102
( $name:ident: $success_type:ty = $success:expr ) => {
103103
pub struct $name;
104-
impl<O: Into<Result<Origin, O>> + From<Origin>>
105-
EnsureOrigin<O> for $name
104+
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
105+
where
106+
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
106107
{
107108
type Success = $success_type;
108109
fn try_origin(o: O) -> Result<Self::Success, O> {
109-
o.into().and_then(|o| match o {
110-
Origin::$name => Ok($success),
111-
r => Err(O::from(r)),
112-
})
110+
match o.caller().try_into() {
111+
Ok(Origin::$name) => return Ok($success),
112+
_ => (),
113+
}
114+
115+
Err(o)
113116
}
114117
#[cfg(feature = "runtime-benchmarks")]
115118
fn try_successful_origin() -> Result<O, ()> {
@@ -151,17 +154,20 @@ pub mod pallet_custom_origins {
151154
}
152155
) => {
153156
$vis struct $name;
154-
impl<O: Into<Result<Origin, O>> + From<Origin>>
155-
EnsureOrigin<O> for $name
157+
impl<O: OriginTrait + From<Origin>> EnsureOrigin<O> for $name
158+
where
159+
for <'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
156160
{
157161
type Success = $success_type;
158162
fn try_origin(o: O) -> Result<Self::Success, O> {
159-
o.into().and_then(|o| match o {
163+
match o.caller().try_into() {
160164
$(
161-
Origin::$item => Ok($success),
165+
Ok(Origin::$item) => return Ok($success),
162166
)*
163-
r => Err(O::from(r)),
164-
})
167+
_ => (),
168+
}
169+
170+
Err(o)
165171
}
166172
#[cfg(feature = "runtime-benchmarks")]
167173
fn try_successful_origin() -> Result<O, ()> {

polkadot/xcm/pallet-xcm/src/lib.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,20 +3590,22 @@ impl<
35903590
L: TryFrom<Location> + TryInto<Location> + Clone,
35913591
> EnsureOrigin<O> for EnsureXcm<F, L>
35923592
where
3593-
O::PalletsOrigin: From<Origin> + TryInto<Origin, Error = O::PalletsOrigin>,
3593+
for<'a> &'a O::PalletsOrigin: TryInto<&'a Origin>,
35943594
{
35953595
type Success = L;
35963596

35973597
fn try_origin(outer: O) -> Result<Self::Success, O> {
3598-
outer.try_with_caller(|caller| {
3599-
caller.try_into().and_then(|o| match o {
3600-
Origin::Xcm(ref location)
3601-
if F::contains(&location.clone().try_into().map_err(|_| o.clone().into())?) =>
3602-
Ok(location.clone().try_into().map_err(|_| o.clone().into())?),
3603-
Origin::Xcm(location) => Err(Origin::Xcm(location).into()),
3604-
o => Err(o.into()),
3605-
})
3606-
})
3598+
match outer.caller().try_into() {
3599+
Ok(Origin::Xcm(ref location)) =>
3600+
if let Ok(location) = location.clone().try_into() {
3601+
if F::contains(&location) {
3602+
return Ok(location);
3603+
}
3604+
},
3605+
_ => (),
3606+
}
3607+
3608+
Err(outer)
36073609
}
36083610

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

36243626
fn try_origin(outer: O) -> Result<Self::Success, O> {
3625-
outer.try_with_caller(|caller| {
3626-
caller.try_into().and_then(|o| match o {
3627-
Origin::Response(responder) => Ok(responder),
3628-
o => Err(o.into()),
3629-
})
3630-
})
3627+
match outer.caller().try_into() {
3628+
Ok(Origin::Response(responder)) => return Ok(responder.clone()),
3629+
_ => (),
3630+
}
3631+
3632+
Err(outer)
36313633
}
36323634

36333635
#[cfg(feature = "runtime-benchmarks")]

prdoc/pr_8000.prdoc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
title: Optimize origin checks
2+
doc:
3+
- audience: Runtime Dev
4+
description: Optimize origin checks, avoid cloning and conversion when not needed.
5+
crates:
6+
- name: snowbridge-pallet-system
7+
bump: patch
8+
- name: collectives-westend-runtime
9+
bump: major
10+
- name: rococo-runtime
11+
bump: major
12+
- name: westend-runtime
13+
bump: major
14+
- name: pallet-xcm
15+
bump: major
16+
- name: pallet-collective
17+
bump: major
18+
- name: pallet-ranked-collective
19+
bump: patch
20+
- name: pallet-society
21+
bump: patch
22+
- name: frame-support
23+
bump: major
24+
- name: frame-system
25+
bump: major
26+
- name: pallet-treasury
27+
bump: patch
28+
- name: snowbridge-runtime-common
29+
bump: major

0 commit comments

Comments
 (0)