Skip to content

Commit 44391df

Browse files
ggwpezark0f
authored andcommitted
Add base-weight to System::Extrinsic* events (paritytech#12329)
* Add base-weight to events Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Fix test Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
1 parent 53b81c4 commit 44391df

5 files changed

Lines changed: 198 additions & 148 deletions

File tree

bin/node/executor/tests/basic.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,19 @@ fn full_native_block_import_works() {
311311
let mut alice_last_known_balance: Balance = Default::default();
312312
let mut fees = t.execute_with(|| transfer_fee(&xt()));
313313

314-
let transfer_weight = default_transfer_call().get_dispatch_info().weight;
314+
let transfer_weight = default_transfer_call().get_dispatch_info().weight.saturating_add(
315+
<Runtime as frame_system::Config>::BlockWeights::get()
316+
.get(DispatchClass::Normal)
317+
.base_extrinsic,
318+
);
315319
let timestamp_weight = pallet_timestamp::Call::set::<Runtime> { now: Default::default() }
316320
.get_dispatch_info()
317-
.weight;
321+
.weight
322+
.saturating_add(
323+
<Runtime as frame_system::Config>::BlockWeights::get()
324+
.get(DispatchClass::Mandatory)
325+
.base_extrinsic,
326+
);
318327

319328
executor_call(&mut t, "Core_execute_block", &block1.0, true).0.unwrap();
320329

frame/system/src/extensions/check_weight.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ mod tests {
342342
.get(DispatchClass::Operational)
343343
.max_total
344344
.unwrap_or_else(|| weights.max_block);
345-
let base_weight = weights.get(DispatchClass::Normal).base_extrinsic;
345+
let base_weight = weights.get(DispatchClass::Operational).base_extrinsic;
346346

347347
let weight = operational_limit - base_weight;
348348
let okay =
@@ -378,11 +378,11 @@ mod tests {
378378
// Max normal is 768 (75%)
379379
// 10 is taken for block execution weight
380380
// So normal extrinsic can be 758 weight (-5 for base extrinsic weight)
381-
// And Operational can be 256 to produce a full block (-5 for base)
381+
// And Operational can be 246 to produce a full block (-10 for base)
382382
let max_normal =
383383
DispatchInfo { weight: Weight::from_ref_time(753), ..Default::default() };
384384
let rest_operational = DispatchInfo {
385-
weight: Weight::from_ref_time(251),
385+
weight: Weight::from_ref_time(246),
386386
class: DispatchClass::Operational,
387387
..Default::default()
388388
};
@@ -406,15 +406,15 @@ mod tests {
406406
let max_normal =
407407
DispatchInfo { weight: Weight::from_ref_time(753), ..Default::default() };
408408
let rest_operational = DispatchInfo {
409-
weight: Weight::from_ref_time(251),
409+
weight: Weight::from_ref_time(246),
410410
class: DispatchClass::Operational,
411411
..Default::default()
412412
};
413413

414414
let len = 0_usize;
415415

416416
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&rest_operational, len));
417-
// Extra 15 here from block execution + base extrinsic weight
417+
// Extra 20 here from block execution + base extrinsic weight
418418
assert_eq!(System::block_weight().total(), Weight::from_ref_time(266));
419419
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max_normal, len));
420420
assert_eq!(block_weight_limit(), Weight::from_ref_time(1024));
@@ -433,7 +433,7 @@ mod tests {
433433
..Default::default()
434434
};
435435
let dispatch_operational = DispatchInfo {
436-
weight: Weight::from_ref_time(251),
436+
weight: Weight::from_ref_time(246),
437437
class: DispatchClass::Operational,
438438
..Default::default()
439439
};

frame/system/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,9 +1509,15 @@ impl<T: Config> Pallet<T> {
15091509
}
15101510

15111511
/// To be called immediately after an extrinsic has been applied.
1512+
///
1513+
/// Emits an `ExtrinsicSuccess` or `ExtrinsicFailed` event depending on the outcome.
1514+
/// The emitted event contains the post-dispatch corrected weight including
1515+
/// the base-weight for its dispatch class.
15121516
pub fn note_applied_extrinsic(r: &DispatchResultWithPostInfo, mut info: DispatchInfo) {
1513-
info.weight = extract_actual_weight(r, &info);
1517+
info.weight = extract_actual_weight(r, &info)
1518+
.saturating_add(T::BlockWeights::get().get(info.class).base_extrinsic);
15141519
info.pays_fee = extract_actual_pays_fee(r, &info);
1520+
15151521
Self::deposit_event(match r {
15161522
Ok(_) => Event::ExtrinsicSuccess { dispatch_info: info },
15171523
Err(err) => {

frame/system/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ parameter_types! {
6767
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAX_BLOCK_WEIGHT);
6868
})
6969
.for_class(DispatchClass::Operational, |weights| {
70+
weights.base_extrinsic = Weight::from_ref_time(10);
7071
weights.max_total = Some(MAX_BLOCK_WEIGHT);
7172
weights.reserved = Some(
7273
MAX_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAX_BLOCK_WEIGHT

0 commit comments

Comments
 (0)