Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit d1923a4

Browse files
authored
Store dispatch info of calls locally in weight calculation (#7849)
* utility * sudo * more * recovery * better formatting
1 parent c8dd406 commit d1923a4

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed

frame/multisig/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,16 @@ decl_module! {
223223
/// - DB Weight: None
224224
/// - Plus Call Weight
225225
/// # </weight>
226-
#[weight = (
227-
T::WeightInfo::as_multi_threshold_1(call.using_encoded(|c| c.len() as u32))
228-
.saturating_add(call.get_dispatch_info().weight)
229-
// AccountData for inner call origin accountdata.
230-
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
231-
call.get_dispatch_info().class,
232-
)]
226+
#[weight = {
227+
let dispatch_info = call.get_dispatch_info();
228+
(
229+
T::WeightInfo::as_multi_threshold_1(call.using_encoded(|c| c.len() as u32))
230+
.saturating_add(dispatch_info.weight)
231+
// AccountData for inner call origin accountdata.
232+
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
233+
dispatch_info.class,
234+
)
235+
}]
233236
fn as_multi_threshold_1(origin,
234237
other_signatories: Vec<T::AccountId>,
235238
call: Box<<T as Config>::Call>,

frame/recovery/src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,16 @@ decl_module! {
352352
/// - The weight of the `call` + 10,000.
353353
/// - One storage lookup to check account is recovered by `who`. O(1)
354354
/// # </weight>
355-
#[weight = (
356-
call.get_dispatch_info().weight
357-
.saturating_add(10_000)
358-
// AccountData for inner call origin accountdata.
359-
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
360-
call.get_dispatch_info().class
361-
)]
355+
#[weight = {
356+
let dispatch_info = call.get_dispatch_info();
357+
(
358+
dispatch_info.weight
359+
.saturating_add(10_000)
360+
// AccountData for inner call origin accountdata.
361+
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
362+
dispatch_info.class,
363+
)
364+
}]
362365
fn as_recovered(origin,
363366
account: T::AccountId,
364367
call: Box<<T as Config>::Call>

frame/sudo/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ decl_module! {
130130
/// - One DB write (event).
131131
/// - Weight of derivative `call` execution + 10,000.
132132
/// # </weight>
133-
#[weight = (call.get_dispatch_info().weight + 10_000, call.get_dispatch_info().class)]
133+
#[weight = {
134+
let dispatch_info = call.get_dispatch_info();
135+
(dispatch_info.weight.saturating_add(10_000), dispatch_info.class)
136+
}]
134137
fn sudo(origin, call: Box<<T as Config>::Call>) -> DispatchResultWithPostInfo {
135138
// This is a public call, so we ensure that the origin is some signed account.
136139
let sender = ensure_signed(origin)?;
@@ -197,13 +200,16 @@ decl_module! {
197200
/// - One DB write (event).
198201
/// - Weight of derivative `call` execution + 10,000.
199202
/// # </weight>
200-
#[weight = (
201-
call.get_dispatch_info().weight
202-
.saturating_add(10_000)
203-
// AccountData for inner call origin accountdata.
204-
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
205-
call.get_dispatch_info().class
206-
)]
203+
#[weight = {
204+
let dispatch_info = call.get_dispatch_info();
205+
(
206+
dispatch_info.weight
207+
.saturating_add(10_000)
208+
// AccountData for inner call origin accountdata.
209+
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
210+
dispatch_info.class,
211+
)
212+
}]
207213
fn sudo_as(origin,
208214
who: <T::Lookup as StaticLookup>::Source,
209215
call: Box<<T as Config>::Call>

frame/utility/src/lib.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,24 @@ decl_module! {
133133
/// `BatchInterrupted` event is deposited, along with the number of successful calls made
134134
/// and the error of the failed call. If all were successful, then the `BatchCompleted`
135135
/// event is deposited.
136-
#[weight = (
137-
calls.iter()
138-
.map(|call| call.get_dispatch_info().weight)
136+
#[weight = {
137+
let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info()).collect::<Vec<_>>();
138+
let dispatch_weight = dispatch_infos.iter()
139+
.map(|di| di.weight)
139140
.fold(0, |total: Weight, weight: Weight| total.saturating_add(weight))
140-
.saturating_add(T::WeightInfo::batch(calls.len() as u32)),
141-
{
142-
let all_operational = calls.iter()
143-
.map(|call| call.get_dispatch_info().class)
141+
.saturating_add(T::WeightInfo::batch(calls.len() as u32));
142+
let dispatch_class = {
143+
let all_operational = dispatch_infos.iter()
144+
.map(|di| di.class)
144145
.all(|class| class == DispatchClass::Operational);
145146
if all_operational {
146147
DispatchClass::Operational
147148
} else {
148149
DispatchClass::Normal
149150
}
150-
},
151-
)]
151+
};
152+
(dispatch_weight, dispatch_class)
153+
}]
152154
fn batch(origin, calls: Vec<<T as Config>::Call>) -> DispatchResultWithPostInfo {
153155
let is_root = ensure_root(origin.clone()).is_ok();
154156
let calls_len = calls.len();
@@ -190,13 +192,16 @@ decl_module! {
190192
/// NOTE: Prior to version *12, this was called `as_limited_sub`.
191193
///
192194
/// The dispatch origin for this call must be _Signed_.
193-
#[weight = (
194-
T::WeightInfo::as_derivative()
195-
.saturating_add(call.get_dispatch_info().weight)
196-
// AccountData for inner call origin accountdata.
197-
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
198-
call.get_dispatch_info().class,
199-
)]
195+
#[weight = {
196+
let dispatch_info = call.get_dispatch_info();
197+
(
198+
T::WeightInfo::as_derivative()
199+
.saturating_add(dispatch_info.weight)
200+
// AccountData for inner call origin accountdata.
201+
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
202+
dispatch_info.class,
203+
)
204+
}]
200205
fn as_derivative(origin, index: u16, call: Box<<T as Config>::Call>) -> DispatchResultWithPostInfo {
201206
let mut origin = origin;
202207
let who = ensure_signed(origin.clone())?;
@@ -227,22 +232,24 @@ decl_module! {
227232
/// # <weight>
228233
/// - Complexity: O(C) where C is the number of calls to be batched.
229234
/// # </weight>
230-
#[weight = (
231-
calls.iter()
232-
.map(|call| call.get_dispatch_info().weight)
235+
#[weight = {
236+
let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info()).collect::<Vec<_>>();
237+
let dispatch_weight = dispatch_infos.iter()
238+
.map(|di| di.weight)
233239
.fold(0, |total: Weight, weight: Weight| total.saturating_add(weight))
234-
.saturating_add(T::WeightInfo::batch_all(calls.len() as u32)),
235-
{
236-
let all_operational = calls.iter()
237-
.map(|call| call.get_dispatch_info().class)
240+
.saturating_add(T::WeightInfo::batch_all(calls.len() as u32));
241+
let dispatch_class = {
242+
let all_operational = dispatch_infos.iter()
243+
.map(|di| di.class)
238244
.all(|class| class == DispatchClass::Operational);
239245
if all_operational {
240246
DispatchClass::Operational
241247
} else {
242248
DispatchClass::Normal
243249
}
244-
},
245-
)]
250+
};
251+
(dispatch_weight, dispatch_class)
252+
}]
246253
#[transactional]
247254
fn batch_all(origin, calls: Vec<<T as Config>::Call>) -> DispatchResultWithPostInfo {
248255
let is_root = ensure_root(origin.clone()).is_ok();

0 commit comments

Comments
 (0)