Skip to content

Commit

Permalink
Add base-weight to System::Extrinsic* events (paritytech#12329)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ggwpez authored and ark0f committed Feb 27, 2023
1 parent 53b81c4 commit 44391df
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 148 deletions.
13 changes: 11 additions & 2 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,19 @@ fn full_native_block_import_works() {
let mut alice_last_known_balance: Balance = Default::default();
let mut fees = t.execute_with(|| transfer_fee(&xt()));

let transfer_weight = default_transfer_call().get_dispatch_info().weight;
let transfer_weight = default_transfer_call().get_dispatch_info().weight.saturating_add(
<Runtime as frame_system::Config>::BlockWeights::get()
.get(DispatchClass::Normal)
.base_extrinsic,
);
let timestamp_weight = pallet_timestamp::Call::set::<Runtime> { now: Default::default() }
.get_dispatch_info()
.weight;
.weight
.saturating_add(
<Runtime as frame_system::Config>::BlockWeights::get()
.get(DispatchClass::Mandatory)
.base_extrinsic,
);

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

Expand Down
12 changes: 6 additions & 6 deletions frame/system/src/extensions/check_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ mod tests {
.get(DispatchClass::Operational)
.max_total
.unwrap_or_else(|| weights.max_block);
let base_weight = weights.get(DispatchClass::Normal).base_extrinsic;
let base_weight = weights.get(DispatchClass::Operational).base_extrinsic;

let weight = operational_limit - base_weight;
let okay =
Expand Down Expand Up @@ -378,11 +378,11 @@ mod tests {
// Max normal is 768 (75%)
// 10 is taken for block execution weight
// So normal extrinsic can be 758 weight (-5 for base extrinsic weight)
// And Operational can be 256 to produce a full block (-5 for base)
// And Operational can be 246 to produce a full block (-10 for base)
let max_normal =
DispatchInfo { weight: Weight::from_ref_time(753), ..Default::default() };
let rest_operational = DispatchInfo {
weight: Weight::from_ref_time(251),
weight: Weight::from_ref_time(246),
class: DispatchClass::Operational,
..Default::default()
};
Expand All @@ -406,15 +406,15 @@ mod tests {
let max_normal =
DispatchInfo { weight: Weight::from_ref_time(753), ..Default::default() };
let rest_operational = DispatchInfo {
weight: Weight::from_ref_time(251),
weight: Weight::from_ref_time(246),
class: DispatchClass::Operational,
..Default::default()
};

let len = 0_usize;

assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&rest_operational, len));
// Extra 15 here from block execution + base extrinsic weight
// Extra 20 here from block execution + base extrinsic weight
assert_eq!(System::block_weight().total(), Weight::from_ref_time(266));
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max_normal, len));
assert_eq!(block_weight_limit(), Weight::from_ref_time(1024));
Expand All @@ -433,7 +433,7 @@ mod tests {
..Default::default()
};
let dispatch_operational = DispatchInfo {
weight: Weight::from_ref_time(251),
weight: Weight::from_ref_time(246),
class: DispatchClass::Operational,
..Default::default()
};
Expand Down
8 changes: 7 additions & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,9 +1509,15 @@ impl<T: Config> Pallet<T> {
}

/// To be called immediately after an extrinsic has been applied.
///
/// Emits an `ExtrinsicSuccess` or `ExtrinsicFailed` event depending on the outcome.
/// The emitted event contains the post-dispatch corrected weight including
/// the base-weight for its dispatch class.
pub fn note_applied_extrinsic(r: &DispatchResultWithPostInfo, mut info: DispatchInfo) {
info.weight = extract_actual_weight(r, &info);
info.weight = extract_actual_weight(r, &info)
.saturating_add(T::BlockWeights::get().get(info.class).base_extrinsic);
info.pays_fee = extract_actual_pays_fee(r, &info);

Self::deposit_event(match r {
Ok(_) => Event::ExtrinsicSuccess { dispatch_info: info },
Err(err) => {
Expand Down
1 change: 1 addition & 0 deletions frame/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ parameter_types! {
weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAX_BLOCK_WEIGHT);
})
.for_class(DispatchClass::Operational, |weights| {
weights.base_extrinsic = Weight::from_ref_time(10);
weights.max_total = Some(MAX_BLOCK_WEIGHT);
weights.reserved = Some(
MAX_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAX_BLOCK_WEIGHT
Expand Down
Loading

0 comments on commit 44391df

Please sign in to comment.