Include post dispatch corrected weight in extrinsic events (#6024)

* Include post dispatch corrected weight in extrinsic events

* Drop the 'Post' from ApplyExtrinsicResultWithPostInfo to make it less verbose

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Use proper Event type in pallet_system tests

* Add test that the actual weight is returned by events

* Make fn extract_actual_weight cap at pre dispatch weight

* Bump spec version

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Gavin Wood <gavin@parity.io>
This commit is contained in:
Alexander Theißen
2020-05-18 18:44:29 +02:00
committed by GitHub
parent 81b30fe6f6
commit 80accdbf7c
8 changed files with 202 additions and 54 deletions
+36 -1
View File
@@ -134,7 +134,7 @@ use sp_runtime::{
traits::SignedExtension,
generic::{CheckedExtrinsic, UncheckedExtrinsic},
};
use crate::dispatch::{DispatchErrorWithPostInfo, DispatchError};
use crate::dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, DispatchError};
/// Re-export priority as type
pub use sp_runtime::transaction_validity::TransactionPriority;
@@ -281,6 +281,14 @@ impl PostDispatchInfo {
}
}
/// Extract the actual weight from a dispatch result if any or fall back to the default weight.
pub fn extract_actual_weight(result: &DispatchResultWithPostInfo, info: &DispatchInfo) -> Weight {
match result {
Ok(post_info) => &post_info.actual_weight,
Err(err) => &err.post_info.actual_weight,
}.unwrap_or_else(|| info.weight).min(info.weight)
}
impl From<Option<Weight>> for PostDispatchInfo {
fn from(actual_weight: Option<Weight>) -> Self {
Self {
@@ -616,4 +624,31 @@ mod tests {
assert_eq!(Call::<TraitImpl>::f21().get_dispatch_info().weight, 45600);
assert_eq!(Call::<TraitImpl>::f2().get_dispatch_info().class, DispatchClass::Normal);
}
#[test]
fn extract_actual_weight_works() {
let pre = DispatchInfo {
weight: 1000,
.. Default::default()
};
assert_eq!(extract_actual_weight(&Ok(Some(7).into()), &pre), 7);
assert_eq!(extract_actual_weight(&Ok(Some(1000).into()), &pre), 1000);
assert_eq!(
extract_actual_weight(&Err(DispatchError::BadOrigin.with_weight(9)), &pre),
9
);
}
#[test]
fn extract_actual_weight_caps_at_pre_weight() {
let pre = DispatchInfo {
weight: 1000,
.. Default::default()
};
assert_eq!(extract_actual_weight(&Ok(Some(1250).into()), &pre), 1000);
assert_eq!(
extract_actual_weight(&Err(DispatchError::BadOrigin.with_weight(1300)), &pre),
1000
);
}
}