Skip to content

Commit 20a448a

Browse files
authored
Add authority benchmarks (#1041)
* add authority benchmarks * fix * update * update * update
1 parent 4799169 commit 20a448a

File tree

7 files changed

+295
-19
lines changed

7 files changed

+295
-19
lines changed

auction/src/benchmarking.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub use crate::*;
22

33
use frame_benchmarking::v2::*;
44
use frame_support::assert_ok;
5-
use frame_system::{EventRecord, RawOrigin};
5+
use frame_system::RawOrigin;
66

77
/// Helper trait for benchmarking.
88
pub trait BenchmarkHelper<BlockNumber, AccountId, Balance> {
@@ -58,14 +58,6 @@ impl<T: Config> BenchmarkHelper<BlockNumberFor<T>, T::AccountId, T::Balance> for
5858
}
5959
}
6060

61-
fn assert_last_event<T: Config>(generic_event: <T as frame_system::Config>::RuntimeEvent) {
62-
let events = frame_system::Pallet::<T>::events();
63-
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
64-
// compare to the last event record
65-
let EventRecord { event, .. } = &events[events.len() - 1];
66-
assert_eq!(event, &system_event);
67-
}
68-
6961
#[benchmarks]
7062
mod benchmarks {
7163
use super::*;
@@ -80,7 +72,7 @@ mod benchmarks {
8072
#[extrinsic_call]
8173
_(RawOrigin::Signed(bidder.clone()), auction_id, bid_price);
8274

83-
assert_last_event::<T>(
75+
frame_system::Pallet::<T>::assert_last_event(
8476
Event::Bid {
8577
auction_id,
8678
bidder: bidder,

authority/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ parity-scale-codec = { workspace = true }
1212
scale-info = { workspace = true }
1313
serde = { workspace = true, optional = true }
1414

15+
frame-benchmarking = { workspace = true, optional = true }
1516
frame-support = { workspace = true }
1617
frame-system = { workspace = true }
1718
sp-core = { workspace = true }
@@ -28,6 +29,7 @@ sp-io = { workspace = true, features = ["std"] }
2829
[features]
2930
default = [ "std" ]
3031
std = [
32+
"frame-benchmarking?/std",
3133
"frame-support/std",
3234
"frame-system/std",
3335
"parity-scale-codec/std",
@@ -39,6 +41,7 @@ std = [
3941
"sp-std/std",
4042
]
4143
runtime-benchmarks = [
44+
"frame-benchmarking/runtime-benchmarks",
4245
"frame-support/runtime-benchmarks",
4346
"frame-system/runtime-benchmarks",
4447
"sp-runtime/runtime-benchmarks",

authority/src/benchmarking.rs

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
pub use crate::*;
2+
3+
use frame_benchmarking::v2::*;
4+
use frame_support::assert_ok;
5+
use frame_system::RawOrigin;
6+
use sp_std::vec;
7+
8+
/// Helper trait for benchmarking.
9+
pub trait BenchmarkHelper<AsOriginId> {
10+
fn get_as_origin_id() -> Option<AsOriginId>;
11+
}
12+
13+
impl<AsOriginId> BenchmarkHelper<AsOriginId> for () {
14+
fn get_as_origin_id() -> Option<AsOriginId> {
15+
None
16+
}
17+
}
18+
19+
#[benchmarks(where
20+
<T as Config>::RuntimeCall: From<frame_system::Call<T>>
21+
)]
22+
mod benchmarks {
23+
use super::*;
24+
25+
// dispatch a dispatchable as other origin
26+
#[benchmark]
27+
fn dispatch_as() {
28+
let as_origin = T::BenchmarkHelper::get_as_origin_id().unwrap();
29+
30+
let call = frame_system::Call::remark { remark: vec![] }.into();
31+
32+
#[extrinsic_call]
33+
_(RawOrigin::Root, as_origin, Box::new(call));
34+
}
35+
36+
// schedule a dispatchable to be dispatched at later block.
37+
#[benchmark]
38+
fn schedule_dispatch_without_delay() {
39+
let as_origin = T::BenchmarkHelper::get_as_origin_id().unwrap();
40+
41+
let sub_call = frame_system::Call::remark { remark: vec![] }.into();
42+
43+
let call: Call<T> = Call::dispatch_as {
44+
as_origin: as_origin,
45+
call: Box::new(sub_call),
46+
};
47+
48+
let encoded_call = call.encode();
49+
let bounded_call = Box::new(Bounded::Inline(encoded_call.try_into().unwrap()));
50+
51+
#[extrinsic_call]
52+
schedule_dispatch(RawOrigin::Root, DispatchTime::At(2u32.into()), 0, false, bounded_call);
53+
}
54+
55+
// schedule a dispatchable to be dispatched at later block.
56+
// ensure that the delay is reached when scheduling
57+
#[benchmark]
58+
fn schedule_dispatch_with_delay() {
59+
let as_origin = T::BenchmarkHelper::get_as_origin_id().unwrap();
60+
61+
let sub_call = frame_system::Call::remark { remark: vec![] }.into();
62+
63+
let call: Call<T> = Call::dispatch_as {
64+
as_origin: as_origin,
65+
call: Box::new(sub_call),
66+
};
67+
68+
let encoded_call = call.encode();
69+
let bounded_call = Box::new(Bounded::Inline(encoded_call.try_into().unwrap()));
70+
71+
#[extrinsic_call]
72+
schedule_dispatch(RawOrigin::Root, DispatchTime::At(2u32.into()), 0, true, bounded_call);
73+
}
74+
75+
// fast track a scheduled dispatchable.
76+
#[benchmark]
77+
fn fast_track_scheduled_dispatch() {
78+
let as_origin = T::BenchmarkHelper::get_as_origin_id().unwrap();
79+
80+
let sub_call = frame_system::Call::remark { remark: vec![] }.into();
81+
82+
let call: Call<T> = Call::dispatch_as {
83+
as_origin: as_origin,
84+
call: Box::new(sub_call),
85+
};
86+
87+
let encoded_call = call.encode();
88+
let bounded_call = Box::new(Bounded::Inline(encoded_call.try_into().unwrap()));
89+
90+
frame_system::Pallet::<T>::set_block_number(1u32.into());
91+
assert_ok!(Pallet::<T>::schedule_dispatch(
92+
<T as frame_system::Config>::RuntimeOrigin::root(),
93+
DispatchTime::At(2u32.into()),
94+
0,
95+
true,
96+
bounded_call
97+
));
98+
99+
let schedule_origin = {
100+
let origin: <T as Config>::RuntimeOrigin = From::from(<T as Config>::RuntimeOrigin::root());
101+
let origin: <T as Config>::RuntimeOrigin =
102+
From::from(DelayedOrigin::<BlockNumberFor<T>, <T as Config>::PalletsOrigin>::new(
103+
1u32.into(),
104+
Box::new(origin.caller().clone()),
105+
));
106+
origin
107+
};
108+
109+
let pallets_origin = schedule_origin.caller().clone();
110+
111+
#[extrinsic_call]
112+
fast_track_scheduled_dispatch(
113+
RawOrigin::Root,
114+
Box::new(pallets_origin),
115+
0,
116+
DispatchTime::At(4u32.into()),
117+
);
118+
}
119+
120+
// delay a scheduled dispatchable.
121+
#[benchmark]
122+
fn delay_scheduled_dispatch() {
123+
let as_origin = T::BenchmarkHelper::get_as_origin_id().unwrap();
124+
125+
let sub_call = frame_system::Call::remark { remark: vec![] }.into();
126+
127+
let call: Call<T> = Call::dispatch_as {
128+
as_origin: as_origin,
129+
call: Box::new(sub_call),
130+
};
131+
132+
let encoded_call = call.encode();
133+
let bounded_call = Box::new(Bounded::Inline(encoded_call.try_into().unwrap()));
134+
135+
frame_system::Pallet::<T>::set_block_number(1u32.into());
136+
assert_ok!(Pallet::<T>::schedule_dispatch(
137+
<T as frame_system::Config>::RuntimeOrigin::root(),
138+
DispatchTime::At(2u32.into()),
139+
0,
140+
true,
141+
bounded_call
142+
));
143+
144+
let schedule_origin = {
145+
let origin: <T as Config>::RuntimeOrigin = From::from(<T as Config>::RuntimeOrigin::root());
146+
let origin: <T as Config>::RuntimeOrigin =
147+
From::from(DelayedOrigin::<BlockNumberFor<T>, <T as Config>::PalletsOrigin>::new(
148+
1u32.into(),
149+
Box::new(origin.caller().clone()),
150+
));
151+
origin
152+
};
153+
154+
let pallets_origin = schedule_origin.caller().clone();
155+
156+
#[extrinsic_call]
157+
_(RawOrigin::Root, Box::new(pallets_origin), 0, 5u32.into());
158+
}
159+
160+
// cancel a scheduled dispatchable
161+
#[benchmark]
162+
fn cancel_scheduled_dispatch() {
163+
let as_origin = T::BenchmarkHelper::get_as_origin_id().unwrap();
164+
165+
let sub_call = frame_system::Call::remark { remark: vec![] }.into();
166+
167+
let call: Call<T> = Call::dispatch_as {
168+
as_origin: as_origin,
169+
call: Box::new(sub_call),
170+
};
171+
172+
let encoded_call = call.encode();
173+
let bounded_call = Box::new(Bounded::Inline(encoded_call.try_into().unwrap()));
174+
175+
frame_system::Pallet::<T>::set_block_number(1u32.into());
176+
assert_ok!(Pallet::<T>::schedule_dispatch(
177+
<T as frame_system::Config>::RuntimeOrigin::root(),
178+
DispatchTime::At(2u32.into()),
179+
0,
180+
true,
181+
bounded_call
182+
));
183+
184+
let schedule_origin = {
185+
let origin: <T as Config>::RuntimeOrigin = From::from(<T as Config>::RuntimeOrigin::root());
186+
let origin: <T as Config>::RuntimeOrigin =
187+
From::from(DelayedOrigin::<BlockNumberFor<T>, <T as Config>::PalletsOrigin>::new(
188+
1u32.into(),
189+
Box::new(origin.caller().clone()),
190+
));
191+
origin
192+
};
193+
194+
let pallets_origin = schedule_origin.caller().clone();
195+
196+
#[extrinsic_call]
197+
_(RawOrigin::Root, Box::new(pallets_origin), 0u32.into());
198+
}
199+
200+
// authorize a call that can be triggered later
201+
#[benchmark]
202+
fn authorize_call() {
203+
let caller: T::AccountId = whitelisted_caller();
204+
205+
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();
206+
let hash = <T as frame_system::Config>::Hashing::hash_of(&call);
207+
208+
frame_system::Pallet::<T>::set_block_number(1u32.into());
209+
210+
#[extrinsic_call]
211+
_(RawOrigin::Root, Box::new(call.clone()), Some(caller.clone()));
212+
213+
assert_eq!(Pallet::<T>::saved_calls(&hash), Some((call, Some(caller))));
214+
}
215+
216+
#[benchmark]
217+
fn remove_authorized_call() {
218+
let caller: T::AccountId = whitelisted_caller();
219+
220+
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();
221+
let hash = <T as frame_system::Config>::Hashing::hash_of(&call);
222+
223+
frame_system::Pallet::<T>::set_block_number(1u32.into());
224+
assert_ok!(Pallet::<T>::authorize_call(
225+
RawOrigin::Root.into(),
226+
Box::new(call.clone()),
227+
Some(caller.clone())
228+
));
229+
230+
#[extrinsic_call]
231+
_(RawOrigin::Signed(caller), hash);
232+
233+
assert_eq!(Pallet::<T>::saved_calls(&hash), None);
234+
}
235+
236+
#[benchmark]
237+
fn trigger_call() {
238+
let caller: T::AccountId = whitelisted_caller();
239+
240+
let call: <T as Config>::RuntimeCall = frame_system::Call::remark { remark: vec![] }.into();
241+
let hash = <T as frame_system::Config>::Hashing::hash_of(&call);
242+
243+
let call_weight_bound = call.get_dispatch_info().call_weight;
244+
245+
frame_system::Pallet::<T>::set_block_number(1u32.into());
246+
assert_ok!(Pallet::<T>::authorize_call(
247+
RawOrigin::Root.into(),
248+
Box::new(call.clone()),
249+
Some(caller.clone())
250+
));
251+
252+
#[extrinsic_call]
253+
_(RawOrigin::Signed(caller), hash, call_weight_bound);
254+
255+
assert_eq!(Pallet::<T>::saved_calls(&hash), None);
256+
}
257+
258+
impl_benchmark_test_suite! {
259+
Pallet,
260+
crate::mock::ExtBuilder::default().build(),
261+
crate::mock::Runtime,
262+
}
263+
}

authority/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ use sp_std::prelude::*;
4949

5050
use frame_support::traits::schedule::v3::Named as ScheduleNamed;
5151

52+
#[cfg(feature = "runtime-benchmarks")]
53+
mod benchmarking;
5254
mod mock;
5355
mod tests;
5456
mod weights;
5557

58+
#[cfg(feature = "runtime-benchmarks")]
59+
pub use benchmarking::BenchmarkHelper;
5660
pub use weights::WeightInfo;
5761

5862
/// A delayed origin. Can only be dispatched via `dispatch_as` with a delay.
@@ -238,6 +242,9 @@ pub mod module {
238242

239243
/// Weight information for extrinsics in this module.
240244
type WeightInfo: WeightInfo;
245+
246+
#[cfg(feature = "runtime-benchmarks")]
247+
type BenchmarkHelper: BenchmarkHelper<Self::AsOriginId>;
241248
}
242249

243250
#[pallet::error]

authority/src/mock.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ impl AsOriginId<RuntimeOrigin, OriginCaller> for MockAsOriginId {
137137
}
138138
}
139139

140+
#[cfg(feature = "runtime-benchmarks")]
141+
pub struct MockBenchmarkHelper;
142+
#[cfg(feature = "runtime-benchmarks")]
143+
impl BenchmarkHelper<MockAsOriginId> for MockBenchmarkHelper {
144+
fn get_as_origin_id() -> Option<MockAsOriginId> {
145+
Some(MockAsOriginId::Root)
146+
}
147+
}
148+
140149
impl Config for Runtime {
141150
type RuntimeOrigin = RuntimeOrigin;
142151
type PalletsOrigin = OriginCaller;
@@ -145,6 +154,8 @@ impl Config for Runtime {
145154
type AsOriginId = MockAsOriginId;
146155
type AuthorityConfig = AuthorityConfigImpl;
147156
type WeightInfo = ();
157+
#[cfg(feature = "runtime-benchmarks")]
158+
type BenchmarkHelper = MockBenchmarkHelper;
148159
}
149160

150161
type Block = frame_system::mocking::MockBlock<Runtime>;

0 commit comments

Comments
 (0)