Skip to content

Commit f6679dd

Browse files
Minor Uniques pallet improvements and XCM v3 preparations (paritytech#10896)
* Introduce Helper to Uniques for benchmark stuff * Fixes * Formatting * Featuregate the Helper, include ContainsPair * Introduce & use EnsureOriginWithArg * Benchmarking * Docs * More ContainsBoth helpers * Formatting * Formatting * Fixes Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
1 parent 32a4fe0 commit f6679dd

12 files changed

Lines changed: 417 additions & 113 deletions

File tree

bin/node/runtime/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use frame_election_provider_support::onchain;
2727
use frame_support::{
2828
construct_runtime, parameter_types,
2929
traits::{
30-
ConstU128, ConstU16, ConstU32, Currency, EnsureOneOf, EqualPrivilegeOnly, Everything,
31-
Imbalance, InstanceFilter, KeyOwnerProofSystem, LockIdentifier, Nothing, OnUnbalanced,
32-
U128CurrencyToVote,
30+
AsEnsureOriginWithArg, ConstU128, ConstU16, ConstU32, Currency, EnsureOneOf,
31+
EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, KeyOwnerProofSystem,
32+
LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote,
3333
},
3434
weights::{
3535
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
@@ -1348,6 +1348,9 @@ impl pallet_uniques::Config for Runtime {
13481348
type KeyLimit = KeyLimit;
13491349
type ValueLimit = ValueLimit;
13501350
type WeightInfo = pallet_uniques::weights::SubstrateWeight<Runtime>;
1351+
#[cfg(feature = "runtime-benchmarks")]
1352+
type Helper = ();
1353+
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
13511354
}
13521355

13531356
impl pallet_transaction_storage::Config for Runtime {

frame/support/src/traits.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ mod members;
3434
#[allow(deprecated)]
3535
pub use members::{AllowAll, DenyAll, Filter};
3636
pub use members::{
37-
AsContains, ChangeMembers, Contains, ContainsLengthBound, Everything, InitializeMembers,
38-
IsInVec, Nothing, SortedMembers,
37+
AsContains, ChangeMembers, Contains, ContainsLengthBound, ContainsPair, Everything,
38+
EverythingBut, FromContainsPair, InitializeMembers, InsideBoth, IsInVec, Nothing,
39+
SortedMembers, TheseExcept,
3940
};
4041

4142
mod validation;
@@ -89,7 +90,10 @@ pub use storage::{
8990
};
9091

9192
mod dispatch;
92-
pub use dispatch::{EnsureOneOf, EnsureOrigin, OriginTrait, UnfilteredDispatchable};
93+
pub use dispatch::{
94+
AsEnsureOriginWithArg, EnsureOneOf, EnsureOrigin, EnsureOriginWithArg, OriginTrait,
95+
UnfilteredDispatchable,
96+
};
9397

9498
mod voting;
9599
pub use voting::{

frame/support/src/traits/dispatch.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ use sp_runtime::{
2727
pub trait EnsureOrigin<OuterOrigin> {
2828
/// A return type.
2929
type Success;
30+
3031
/// Perform the origin check.
3132
fn ensure_origin(o: OuterOrigin) -> Result<Self::Success, BadOrigin> {
3233
Self::try_origin(o).map_err(|_| BadOrigin)
3334
}
35+
3436
/// Perform the origin check.
3537
fn try_origin(o: OuterOrigin) -> Result<Self::Success, OuterOrigin>;
3638

@@ -41,6 +43,52 @@ pub trait EnsureOrigin<OuterOrigin> {
4143
fn successful_origin() -> OuterOrigin;
4244
}
4345

46+
/// Some sort of check on the origin is performed by this object.
47+
pub trait EnsureOriginWithArg<OuterOrigin, Argument> {
48+
/// A return type.
49+
type Success;
50+
51+
/// Perform the origin check.
52+
fn ensure_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, BadOrigin> {
53+
Self::try_origin(o, a).map_err(|_| BadOrigin)
54+
}
55+
56+
/// Perform the origin check, returning the origin value if unsuccessful. This allows chaining.
57+
fn try_origin(o: OuterOrigin, a: &Argument) -> Result<Self::Success, OuterOrigin>;
58+
59+
/// Returns an outer origin capable of passing `try_origin` check.
60+
///
61+
/// ** Should be used for benchmarking only!!! **
62+
#[cfg(feature = "runtime-benchmarks")]
63+
fn successful_origin(a: &Argument) -> OuterOrigin;
64+
}
65+
66+
pub struct AsEnsureOriginWithArg<EO>(sp_std::marker::PhantomData<EO>);
67+
impl<OuterOrigin, Argument, EO: EnsureOrigin<OuterOrigin>>
68+
EnsureOriginWithArg<OuterOrigin, Argument> for AsEnsureOriginWithArg<EO>
69+
{
70+
/// A return type.
71+
type Success = EO::Success;
72+
73+
/// Perform the origin check.
74+
fn ensure_origin(o: OuterOrigin, _: &Argument) -> Result<Self::Success, BadOrigin> {
75+
EO::ensure_origin(o)
76+
}
77+
78+
/// Perform the origin check, returning the origin value if unsuccessful. This allows chaining.
79+
fn try_origin(o: OuterOrigin, _: &Argument) -> Result<Self::Success, OuterOrigin> {
80+
EO::try_origin(o)
81+
}
82+
83+
/// Returns an outer origin capable of passing `try_origin` check.
84+
///
85+
/// ** Should be used for benchmarking only!!! **
86+
#[cfg(feature = "runtime-benchmarks")]
87+
fn successful_origin(_: &Argument) -> OuterOrigin {
88+
EO::successful_origin()
89+
}
90+
}
91+
4492
/// Type that can be dispatched with an origin but without checking the origin filter.
4593
///
4694
/// Implemented for pallet dispatchable type by `decl_module` and for runtime dispatchable by

frame/support/src/traits/members.rs

Lines changed: 129 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,52 @@ pub trait Contains<T> {
2525
fn contains(t: &T) -> bool;
2626
}
2727

28+
#[impl_trait_for_tuples::impl_for_tuples(1, 30)]
29+
impl<T> Contains<T> for Tuple {
30+
fn contains(t: &T) -> bool {
31+
for_tuples!( #(
32+
if Tuple::contains(t) { return true }
33+
)* );
34+
false
35+
}
36+
}
37+
38+
/// A trait for querying whether a type can be said to "contain" a pair-value.
39+
pub trait ContainsPair<A, B> {
40+
/// Return `true` if this "contains" the pair-value `(a, b)`.
41+
fn contains(a: &A, b: &B) -> bool;
42+
}
43+
44+
#[impl_trait_for_tuples::impl_for_tuples(0, 30)]
45+
impl<A, B> ContainsPair<A, B> for Tuple {
46+
fn contains(a: &A, b: &B) -> bool {
47+
for_tuples!( #(
48+
if Tuple::contains(a, b) { return true }
49+
)* );
50+
false
51+
}
52+
}
53+
54+
/// Converter `struct` to use a `ContainsPair` implementation for a `Contains` bound.
55+
pub struct FromContainsPair<CP>(PhantomData<CP>);
56+
impl<A, B, CP: ContainsPair<A, B>> Contains<(A, B)> for FromContainsPair<CP> {
57+
fn contains((ref a, ref b): &(A, B)) -> bool {
58+
CP::contains(a, b)
59+
}
60+
}
61+
2862
/// A [`Contains`] implementation that contains every value.
2963
pub enum Everything {}
3064
impl<T> Contains<T> for Everything {
3165
fn contains(_: &T) -> bool {
3266
true
3367
}
3468
}
69+
impl<A, B> ContainsPair<A, B> for Everything {
70+
fn contains(_: &A, _: &B) -> bool {
71+
true
72+
}
73+
}
3574

3675
/// A [`Contains`] implementation that contains no value.
3776
pub enum Nothing {}
@@ -40,56 +79,125 @@ impl<T> Contains<T> for Nothing {
4079
false
4180
}
4281
}
82+
impl<A, B> ContainsPair<A, B> for Nothing {
83+
fn contains(_: &A, _: &B) -> bool {
84+
false
85+
}
86+
}
4387

44-
#[deprecated = "Use `Everything` instead"]
45-
pub type AllowAll = Everything;
46-
#[deprecated = "Use `Nothing` instead"]
47-
pub type DenyAll = Nothing;
48-
#[deprecated = "Use `Contains` instead"]
49-
pub trait Filter<T> {
50-
fn filter(t: &T) -> bool;
88+
/// A [`Contains`] implementation that contains everything except the values in `Exclude`.
89+
pub struct EverythingBut<Exclude>(PhantomData<Exclude>);
90+
impl<T, Exclude: Contains<T>> Contains<T> for EverythingBut<Exclude> {
91+
fn contains(t: &T) -> bool {
92+
!Exclude::contains(t)
93+
}
5194
}
52-
#[allow(deprecated)]
53-
impl<T, C: Contains<T>> Filter<T> for C {
54-
fn filter(t: &T) -> bool {
55-
Self::contains(t)
95+
impl<A, B, Exclude: ContainsPair<A, B>> ContainsPair<A, B> for EverythingBut<Exclude> {
96+
fn contains(a: &A, b: &B) -> bool {
97+
!Exclude::contains(a, b)
5698
}
5799
}
58100

59-
#[impl_trait_for_tuples::impl_for_tuples(1, 30)]
60-
impl<T> Contains<T> for Tuple {
101+
/// A [`Contains`] implementation that contains all members of `These` excepting any members in
102+
/// `Except`.
103+
pub struct TheseExcept<These, Except>(PhantomData<(These, Except)>);
104+
impl<T, These: Contains<T>, Except: Contains<T>> Contains<T> for TheseExcept<These, Except> {
61105
fn contains(t: &T) -> bool {
62-
for_tuples!( #(
63-
if Tuple::contains(t) { return true }
64-
)* );
65-
false
106+
These::contains(t) && !Except::contains(t)
107+
}
108+
}
109+
impl<A, B, These: ContainsPair<A, B>, Except: ContainsPair<A, B>> ContainsPair<A, B>
110+
for TheseExcept<These, Except>
111+
{
112+
fn contains(a: &A, b: &B) -> bool {
113+
These::contains(a, b) && !Except::contains(a, b)
114+
}
115+
}
116+
117+
/// A [`Contains`] implementation which contains all members of `These` which are also members of
118+
/// `Those`.
119+
pub struct InsideBoth<These, Those>(PhantomData<(These, Those)>);
120+
impl<T, These: Contains<T>, Those: Contains<T>> Contains<T> for InsideBoth<These, Those> {
121+
fn contains(t: &T) -> bool {
122+
These::contains(t) && Those::contains(t)
123+
}
124+
}
125+
impl<A, B, These: ContainsPair<A, B>, Those: ContainsPair<A, B>> ContainsPair<A, B>
126+
for InsideBoth<These, Those>
127+
{
128+
fn contains(a: &A, b: &B) -> bool {
129+
These::contains(a, b) && Those::contains(a, b)
66130
}
67131
}
68132

69133
/// Create a type which implements the `Contains` trait for a particular type with syntax similar
70134
/// to `matches!`.
71135
#[macro_export]
72-
macro_rules! match_type {
73-
( pub type $n:ident: impl Contains<$t:ty> = { $phead:pat_param $( | $ptail:pat )* } ; ) => {
136+
macro_rules! match_types {
137+
(
138+
pub type $n:ident: impl Contains<$t:ty> = {
139+
$phead:pat_param $( | $ptail:pat )*
140+
};
141+
$( $rest:tt )*
142+
) => {
74143
pub struct $n;
75144
impl $crate::traits::Contains<$t> for $n {
76145
fn contains(l: &$t) -> bool {
77146
matches!(l, $phead $( | $ptail )* )
78147
}
79148
}
149+
$crate::match_types!( $( $rest )* );
150+
};
151+
(
152+
pub type $n:ident: impl ContainsPair<$a:ty, $b:ty> = {
153+
$phead:pat_param $( | $ptail:pat )*
154+
};
155+
$( $rest:tt )*
156+
) => {
157+
pub struct $n;
158+
impl $crate::traits::ContainsPair<$a, $b> for $n {
159+
fn contains(a: &$a, b: &$b) -> bool {
160+
matches!((a, b), $phead $( | $ptail )* )
161+
}
162+
}
163+
$crate::match_types!( $( $rest )* );
164+
};
165+
() => {}
166+
}
167+
168+
/// Create a type which implements the `Contains` trait for a particular type with syntax similar
169+
/// to `matches!`.
170+
#[macro_export]
171+
#[deprecated = "Use `match_types!` instead"]
172+
macro_rules! match_type {
173+
($( $x:tt )*) => { $crate::match_types!( $( $x )* ); }
174+
}
175+
176+
#[deprecated = "Use `Everything` instead"]
177+
pub type AllowAll = Everything;
178+
#[deprecated = "Use `Nothing` instead"]
179+
pub type DenyAll = Nothing;
180+
#[deprecated = "Use `Contains` instead"]
181+
pub trait Filter<T> {
182+
fn filter(t: &T) -> bool;
183+
}
184+
#[allow(deprecated)]
185+
impl<T, C: Contains<T>> Filter<T> for C {
186+
fn filter(t: &T) -> bool {
187+
Self::contains(t)
80188
}
81189
}
82190

83191
#[cfg(test)]
84192
mod tests {
85193
use super::*;
86194

87-
match_type! {
195+
match_types! {
88196
pub type OneOrTenToTwenty: impl Contains<u8> = { 1 | 10..=20 };
89197
}
90198

91199
#[test]
92-
fn match_type_works() {
200+
fn match_types_works() {
93201
for i in 0..=255 {
94202
assert_eq!(OneOrTenToTwenty::contains(&i), i == 1 || i >= 10 && i <= 20);
95203
}

frame/support/src/traits/tokens/nonfungible.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ pub trait Mutate<AccountId>: Inspect<AccountId> {
8585
/// Burn some asset `instance`.
8686
///
8787
/// By default, this is not a supported operation.
88-
fn burn_from(_instance: &Self::InstanceId) -> DispatchResult {
88+
fn burn(
89+
_instance: &Self::InstanceId,
90+
_maybe_check_owner: Option<&AccountId>,
91+
) -> DispatchResult {
8992
Err(TokenError::Unsupported.into())
9093
}
9194

@@ -166,8 +169,8 @@ impl<
166169
fn mint_into(instance: &Self::InstanceId, who: &AccountId) -> DispatchResult {
167170
<F as nonfungibles::Mutate<AccountId>>::mint_into(&A::get(), instance, who)
168171
}
169-
fn burn_from(instance: &Self::InstanceId) -> DispatchResult {
170-
<F as nonfungibles::Mutate<AccountId>>::burn_from(&A::get(), instance)
172+
fn burn(instance: &Self::InstanceId, maybe_check_owner: Option<&AccountId>) -> DispatchResult {
173+
<F as nonfungibles::Mutate<AccountId>>::burn(&A::get(), instance, maybe_check_owner)
171174
}
172175
fn set_attribute(instance: &Self::InstanceId, key: &[u8], value: &[u8]) -> DispatchResult {
173176
<F as nonfungibles::Mutate<AccountId>>::set_attribute(&A::get(), instance, key, value)

frame/support/src/traits/tokens/nonfungibles.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ pub trait Mutate<AccountId>: Inspect<AccountId> {
165165
/// Burn some asset `instance` of `class`.
166166
///
167167
/// By default, this is not a supported operation.
168-
fn burn_from(_class: &Self::ClassId, _instance: &Self::InstanceId) -> DispatchResult {
168+
fn burn(
169+
_class: &Self::ClassId,
170+
_instance: &Self::InstanceId,
171+
_maybe_check_owner: Option<&AccountId>,
172+
) -> DispatchResult {
169173
Err(TokenError::Unsupported.into())
170174
}
171175

0 commit comments

Comments
 (0)