Skip to content

Commit 594f575

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

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

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)