Skip to content

Commit 9370f42

Browse files
authored
Adds force_origin support (paritytech#13845)
* Adds force_origin support * Moves a couple of tests to showcase v2 with force_origin * Adds remaining tests * adds documentation * minor * adds test for invalid origin * ".git/.scripts/commands/fmt/fmt.sh" * updates param to use MaxCalls * Fixes compilation error * Updates doc comment * Fixes test outputs * Fixes test output * ".git/.scripts/commands/fmt/fmt.sh" --------- Co-authored-by: command-bot <>
1 parent c7d26dc commit 9370f42

File tree

5 files changed

+141
-61
lines changed

5 files changed

+141
-61
lines changed

frame/benchmarking/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ pub use v1::*;
160160
/// The underscore will be substituted with the name of the benchmark (i.e. the name of the
161161
/// function in the benchmark function definition).
162162
///
163+
/// In case of a `force_origin` where you want to elevate the privileges of the provided origin,
164+
/// this is the general syntax:
165+
/// ```ignore
166+
/// #[extrinsic_call]
167+
/// _(force_origin as T::RuntimeOrigin, 0u32.into(), 0);
168+
/// ```
169+
///
163170
/// Regardless of whether `#[extrinsic_call]` or `#[block]` is used, this attribute also serves
164171
/// the purpose of designating the boundary between the setup code portion of the benchmark
165172
/// (everything before the `#[extrinsic_call]` or `#[block]` attribute) and the verification

frame/lottery/src/benchmarking.rs

Lines changed: 85 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@
2121

2222
use super::*;
2323

24-
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller, BenchmarkError};
24+
use crate::Pallet as Lottery;
25+
use frame_benchmarking::{
26+
impl_benchmark_test_suite,
27+
v1::{account, whitelisted_caller, BenchmarkError},
28+
v2::*,
29+
};
2530
use frame_support::{
2631
storage::bounded_vec::BoundedVec,
2732
traits::{EnsureOrigin, OnInitialize},
2833
};
2934
use frame_system::RawOrigin;
3035
use sp_runtime::traits::{Bounded, Zero};
3136

32-
use crate::Pallet as Lottery;
33-
3437
// Set up and start a lottery
3538
fn setup_lottery<T: Config>(repeat: bool) -> Result<(), &'static str> {
3639
let price = T::Currency::minimum_balance();
@@ -50,72 +53,100 @@ fn setup_lottery<T: Config>(repeat: bool) -> Result<(), &'static str> {
5053
Ok(())
5154
}
5255

53-
benchmarks! {
54-
buy_ticket {
56+
#[benchmarks]
57+
mod benchmarks {
58+
use super::*;
59+
60+
#[benchmark]
61+
fn buy_ticket() -> Result<(), BenchmarkError> {
5562
let caller = whitelisted_caller();
5663
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
5764
setup_lottery::<T>(false)?;
5865
// force user to have a long vec of calls participating
5966
let set_code_index: CallIndex = Lottery::<T>::call_to_index(
60-
&frame_system::Call::<T>::set_code{ code: vec![] }.into()
67+
&frame_system::Call::<T>::set_code { code: vec![] }.into(),
6168
)?;
6269
let already_called: (u32, BoundedVec<CallIndex, T::MaxCalls>) = (
6370
LotteryIndex::<T>::get(),
6471
BoundedVec::<CallIndex, T::MaxCalls>::try_from(vec![
6572
set_code_index;
66-
T::MaxCalls::get().saturating_sub(1) as usize
67-
]).unwrap(),
73+
T::MaxCalls::get().saturating_sub(1)
74+
as usize
75+
])
76+
.unwrap(),
6877
);
6978
Participants::<T>::insert(&caller, already_called);
7079

7180
let call = frame_system::Call::<T>::remark { remark: vec![] };
72-
}: _(RawOrigin::Signed(caller), Box::new(call.into()))
73-
verify {
81+
82+
#[extrinsic_call]
83+
_(RawOrigin::Signed(caller), Box::new(call.into()));
84+
7485
assert_eq!(TicketsCount::<T>::get(), 1);
86+
87+
Ok(())
7588
}
7689

77-
set_calls {
78-
let n in 0 .. T::MaxCalls::get() as u32;
90+
#[benchmark]
91+
fn set_calls(n: Linear<0, { T::MaxCalls::get() }>) -> Result<(), BenchmarkError> {
7992
let calls = vec![frame_system::Call::<T>::remark { remark: vec![] }.into(); n as usize];
8093
let origin =
8194
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
8295
assert!(CallIndices::<T>::get().is_empty());
83-
}: _<T::RuntimeOrigin>(origin, calls)
84-
verify {
96+
97+
#[extrinsic_call]
98+
_(origin as T::RuntimeOrigin, calls);
99+
85100
if !n.is_zero() {
86101
assert!(!CallIndices::<T>::get().is_empty());
87102
}
103+
104+
Ok(())
88105
}
89106

90-
start_lottery {
107+
#[benchmark]
108+
fn start_lottery() -> Result<(), BenchmarkError> {
91109
let price = BalanceOf::<T>::max_value();
92110
let end = 10u32.into();
93111
let payout = 5u32.into();
94112
let origin =
95113
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
96-
}: _<T::RuntimeOrigin>(origin, price, end, payout, true)
97-
verify {
114+
115+
#[extrinsic_call]
116+
_(origin as T::RuntimeOrigin, price, end, payout, true);
117+
98118
assert!(crate::Lottery::<T>::get().is_some());
119+
120+
Ok(())
99121
}
100122

101-
stop_repeat {
123+
#[benchmark]
124+
fn stop_repeat() -> Result<(), BenchmarkError> {
102125
setup_lottery::<T>(true)?;
103126
assert_eq!(crate::Lottery::<T>::get().unwrap().repeat, true);
104127
let origin =
105128
T::ManagerOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
106-
}: _<T::RuntimeOrigin>(origin)
107-
verify {
129+
130+
#[extrinsic_call]
131+
_(origin as T::RuntimeOrigin);
132+
108133
assert_eq!(crate::Lottery::<T>::get().unwrap().repeat, false);
134+
135+
Ok(())
109136
}
110137

111-
on_initialize_end {
138+
#[benchmark]
139+
fn on_initialize_end() -> Result<(), BenchmarkError> {
112140
setup_lottery::<T>(false)?;
113141
let winner = account("winner", 0, 0);
114142
// User needs more than min balance to get ticket
115143
T::Currency::make_free_balance_be(&winner, T::Currency::minimum_balance() * 10u32.into());
116144
// Make sure lottery account has at least min balance too
117145
let lottery_account = Lottery::<T>::account_id();
118-
T::Currency::make_free_balance_be(&lottery_account, T::Currency::minimum_balance() * 10u32.into());
146+
T::Currency::make_free_balance_be(
147+
&lottery_account,
148+
T::Currency::minimum_balance() * 10u32.into(),
149+
);
119150
// Buy a ticket
120151
let call = frame_system::Call::<T>::remark { remark: vec![] };
121152
Lottery::<T>::buy_ticket(RawOrigin::Signed(winner.clone()).into(), Box::new(call.into()))?;
@@ -124,29 +155,37 @@ benchmarks! {
124155
// Assert that lotto is set up for winner
125156
assert_eq!(TicketsCount::<T>::get(), 1);
126157
assert!(!Lottery::<T>::pot().1.is_zero());
127-
}: {
128-
// Generate `MaxGenerateRandom` numbers for worst case scenario
129-
for i in 0 .. T::MaxGenerateRandom::get() {
130-
Lottery::<T>::generate_random_number(i);
158+
159+
#[block]
160+
{
161+
// Generate `MaxGenerateRandom` numbers for worst case scenario
162+
for i in 0..T::MaxGenerateRandom::get() {
163+
Lottery::<T>::generate_random_number(i);
164+
}
165+
// Start lottery has block 15 configured for payout
166+
Lottery::<T>::on_initialize(15u32.into());
131167
}
132-
// Start lottery has block 15 configured for payout
133-
Lottery::<T>::on_initialize(15u32.into());
134-
}
135-
verify {
168+
136169
assert!(crate::Lottery::<T>::get().is_none());
137170
assert_eq!(TicketsCount::<T>::get(), 0);
138171
assert_eq!(Lottery::<T>::pot().1, 0u32.into());
139-
assert!(!T::Currency::free_balance(&winner).is_zero())
172+
assert!(!T::Currency::free_balance(&winner).is_zero());
173+
174+
Ok(())
140175
}
141176

142-
on_initialize_repeat {
177+
#[benchmark]
178+
fn on_initialize_repeat() -> Result<(), BenchmarkError> {
143179
setup_lottery::<T>(true)?;
144180
let winner = account("winner", 0, 0);
145181
// User needs more than min balance to get ticket
146182
T::Currency::make_free_balance_be(&winner, T::Currency::minimum_balance() * 10u32.into());
147183
// Make sure lottery account has at least min balance too
148184
let lottery_account = Lottery::<T>::account_id();
149-
T::Currency::make_free_balance_be(&lottery_account, T::Currency::minimum_balance() * 10u32.into());
185+
T::Currency::make_free_balance_be(
186+
&lottery_account,
187+
T::Currency::minimum_balance() * 10u32.into(),
188+
);
150189
// Buy a ticket
151190
let call = frame_system::Call::<T>::remark { remark: vec![] };
152191
Lottery::<T>::buy_ticket(RawOrigin::Signed(winner.clone()).into(), Box::new(call.into()))?;
@@ -155,20 +194,24 @@ benchmarks! {
155194
// Assert that lotto is set up for winner
156195
assert_eq!(TicketsCount::<T>::get(), 1);
157196
assert!(!Lottery::<T>::pot().1.is_zero());
158-
}: {
159-
// Generate `MaxGenerateRandom` numbers for worst case scenario
160-
for i in 0 .. T::MaxGenerateRandom::get() {
161-
Lottery::<T>::generate_random_number(i);
197+
198+
#[block]
199+
{
200+
// Generate `MaxGenerateRandom` numbers for worst case scenario
201+
for i in 0..T::MaxGenerateRandom::get() {
202+
Lottery::<T>::generate_random_number(i);
203+
}
204+
// Start lottery has block 15 configured for payout
205+
Lottery::<T>::on_initialize(15u32.into());
162206
}
163-
// Start lottery has block 15 configured for payout
164-
Lottery::<T>::on_initialize(15u32.into());
165-
}
166-
verify {
207+
167208
assert!(crate::Lottery::<T>::get().is_some());
168209
assert_eq!(LotteryIndex::<T>::get(), 2);
169210
assert_eq!(TicketsCount::<T>::get(), 0);
170211
assert_eq!(Lottery::<T>::pot().1, 0u32.into());
171-
assert!(!T::Currency::free_balance(&winner).is_zero())
212+
assert!(!T::Currency::free_balance(&winner).is_zero());
213+
214+
Ok(())
172215
}
173216

174217
impl_benchmark_test_suite!(Lottery, crate::mock::new_test_ext(), crate::mock::Test);

frame/support/procedural/src/benchmark.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod keywords {
5353
#[derive(Clone)]
5454
struct ParamDef {
5555
name: String,
56-
typ: Type,
56+
_typ: Type,
5757
start: syn::GenericArgument,
5858
end: syn::GenericArgument,
5959
}
@@ -229,7 +229,7 @@ fn parse_params(item_fn: &ItemFn) -> Result<Vec<ParamDef>> {
229229
let args = segment.arguments.to_token_stream().into();
230230
let Ok(args) = syn::parse::<RangeArgs>(args) else { return invalid_param(typ.span()) };
231231

232-
params.push(ParamDef { name, typ: typ.clone(), start: args.start, end: args.end });
232+
params.push(ParamDef { name, _typ: typ.clone(), start: args.start, end: args.end });
233233
}
234234
Ok(params)
235235
}
@@ -681,7 +681,6 @@ pub fn benchmarks(
681681
struct UnrolledParams {
682682
param_ranges: Vec<TokenStream2>,
683683
param_names: Vec<TokenStream2>,
684-
param_types: Vec<TokenStream2>,
685684
}
686685

687686
impl UnrolledParams {
@@ -703,14 +702,7 @@ impl UnrolledParams {
703702
quote!(#name)
704703
})
705704
.collect();
706-
let param_types: Vec<TokenStream2> = params
707-
.iter()
708-
.map(|p| {
709-
let typ = &p.typ;
710-
quote!(#typ)
711-
})
712-
.collect();
713-
UnrolledParams { param_ranges, param_names, param_types }
705+
UnrolledParams { param_ranges, param_names }
714706
}
715707
}
716708

@@ -726,7 +718,6 @@ fn expand_benchmark(
726718
Ok(ident) => ident,
727719
Err(err) => return err.to_compile_error().into(),
728720
};
729-
let home = quote!(#krate::v2);
730721
let codec = quote!(#krate::frame_support::codec);
731722
let traits = quote!(#krate::frame_support::traits);
732723
let setup_stmts = benchmark_def.setup_stmts;
@@ -738,7 +729,6 @@ fn expand_benchmark(
738729
let unrolled = UnrolledParams::from(&benchmark_def.params);
739730
let param_names = unrolled.param_names;
740731
let param_ranges = unrolled.param_ranges;
741-
let param_types = unrolled.param_types;
742732

743733
let type_use_generics = match is_instance {
744734
false => quote!(T),
@@ -763,6 +753,18 @@ fn expand_benchmark(
763753
}
764754
expr_call.args = final_args;
765755

756+
let origin = match origin {
757+
Expr::Cast(t) => {
758+
let ty = t.ty.clone();
759+
quote! {
760+
<<T as frame_system::Config>::RuntimeOrigin as From<#ty>>::from(#origin);
761+
}
762+
},
763+
_ => quote! {
764+
#origin.into();
765+
},
766+
};
767+
766768
// determine call name (handles `_` and normal call syntax)
767769
let expr_span = expr_call.span();
768770
let call_err = || {
@@ -803,7 +805,7 @@ fn expand_benchmark(
803805
let __call_decoded = <Call<#type_use_generics> as #codec::Decode>
804806
::decode(&mut &__benchmarked_call_encoded[..])
805807
.expect("call is encoded above, encoding must be correct");
806-
let __origin = #origin.into();
808+
let __origin = #origin;
807809
<Call<#type_use_generics> as #traits::UnfilteredDispatchable>::dispatch_bypass_filter(
808810
__call_decoded,
809811
__origin,
@@ -877,11 +879,6 @@ fn expand_benchmark(
877879
// benchmark function definition
878880
#fn_def
879881

880-
// compile-time assertions that each referenced param type implements ParamRange
881-
#(
882-
#home::assert_impl_all!(#param_types: #home::ParamRange);
883-
)*
884-
885882
#[allow(non_camel_case_types)]
886883
#(
887884
#fn_attrs
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use frame_benchmarking::v2::*;
2+
#[allow(unused_imports)]
3+
use frame_support_test::Config;
4+
use frame_support_test::Call;
5+
6+
#[benchmarks]
7+
mod benches {
8+
use super::*;
9+
10+
#[benchmark]
11+
fn bench() {
12+
#[extrinsic_call]
13+
thing(1);
14+
}
15+
}
16+
17+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0599]: no variant or associated item named `new_call_variant_thing` found for enum `Call` in the current scope
2+
--> tests/benchmark_ui/invalid_origin.rs:6:1
3+
|
4+
6 | #[benchmarks]
5+
| ^^^^^^^^^^^^^ variant or associated item not found in `Call<T>`
6+
|
7+
= note: this error originates in the attribute macro `benchmarks` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error[E0277]: the trait bound `<T as frame_support_test::Config>::RuntimeOrigin: From<{integer}>` is not satisfied
10+
--> tests/benchmark_ui/invalid_origin.rs:6:1
11+
|
12+
6 | #[benchmarks]
13+
| ^^^^^^^^^^^^^ the trait `From<{integer}>` is not implemented for `<T as frame_support_test::Config>::RuntimeOrigin`
14+
|
15+
= note: required for `{integer}` to implement `Into<<T as frame_support_test::Config>::RuntimeOrigin>`
16+
= note: this error originates in the attribute macro `benchmarks` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)