From 082c58176e73d2d45b1e323e7d8c831ad428afc3 Mon Sep 17 00:00:00 2001
From: Gavin Wood <gavin@parity.io>
Date: Fri, 22 Nov 2019 15:11:04 +0100
Subject: [PATCH] Publish the dispatch info in extrinsic events (#4148)

* Publish the dispatch info in extrinsic events

Place the DispatchInfo (which contains weight information about the
extrinsic that is otherwise difficult to determine) in the dispatch
result value.

* Runtime bump.

* Fix build

* Fix tests

* Fix build?
---
 substrate/bin/node/executor/src/lib.rs   | 25 ++++++++++++++++--------
 substrate/bin/node/runtime/src/lib.rs    |  4 ++--
 substrate/palette/executive/src/lib.rs   |  2 +-
 substrate/palette/support/src/weights.rs |  3 +--
 substrate/palette/system/src/lib.rs      | 18 ++++++++---------
 5 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/substrate/bin/node/executor/src/lib.rs b/substrate/bin/node/executor/src/lib.rs
index 0c3c9027b32..a09cc8ddc0c 100644
--- a/substrate/bin/node/executor/src/lib.rs
+++ b/substrate/bin/node/executor/src/lib.rs
@@ -37,7 +37,7 @@ mod tests {
 	use runtime_support::{
 		Hashable, StorageValue, StorageMap,
 		traits::Currency,
-		weights::GetDispatchInfo,
+		weights::{GetDispatchInfo, DispatchInfo, DispatchClass},
 	};
 	use state_machine::TestExternalities as CoreTestExternalities;
 	use primitives::{
@@ -45,8 +45,7 @@ mod tests {
 		traits::{CodeExecutor, Externalities}, storage::well_known_keys,
 	};
 	use sr_primitives::{
-		Fixed64,
-		traits::{Header as HeaderT, Hash as HashT, Convert}, ApplyResult,
+		Fixed64, traits::{Header as HeaderT, Hash as HashT, Convert}, ApplyResult,
 		transaction_validity::InvalidTransaction,
 	};
 	use contracts::ContractAddressFor;
@@ -467,7 +466,9 @@ mod tests {
 			let events = vec![
 				EventRecord {
 					phase: Phase::ApplyExtrinsic(0),
-					event: Event::system(system::Event::ExtrinsicSuccess),
+					event: Event::system(system::Event::ExtrinsicSuccess(
+						DispatchInfo { weight: 10000, class: DispatchClass::Operational }
+					)),
 					topics: vec![],
 				},
 				EventRecord {
@@ -487,7 +488,9 @@ mod tests {
 				},
 				EventRecord {
 					phase: Phase::ApplyExtrinsic(1),
-					event: Event::system(system::Event::ExtrinsicSuccess),
+					event: Event::system(system::Event::ExtrinsicSuccess(
+						DispatchInfo { weight: 1000000, class: DispatchClass::Normal }
+					)),
 					topics: vec![],
 				},
 			];
@@ -516,7 +519,9 @@ mod tests {
 			let events = vec![
 				EventRecord {
 					phase: Phase::ApplyExtrinsic(0),
-					event: Event::system(system::Event::ExtrinsicSuccess),
+					event: Event::system(system::Event::ExtrinsicSuccess(
+						DispatchInfo { weight: 10000, class: DispatchClass::Operational }
+					)),
 					topics: vec![],
 				},
 				EventRecord {
@@ -538,7 +543,9 @@ mod tests {
 				},
 				EventRecord {
 					phase: Phase::ApplyExtrinsic(1),
-					event: Event::system(system::Event::ExtrinsicSuccess),
+					event: Event::system(system::Event::ExtrinsicSuccess(
+						DispatchInfo { weight: 1000000, class: DispatchClass::Normal }
+					)),
 					topics: vec![],
 				},
 				EventRecord {
@@ -560,7 +567,9 @@ mod tests {
 				},
 				EventRecord {
 					phase: Phase::ApplyExtrinsic(2),
-					event: Event::system(system::Event::ExtrinsicSuccess),
+					event: Event::system(system::Event::ExtrinsicSuccess(
+						DispatchInfo { weight: 1000000, class: DispatchClass::Normal }
+					)),
 					topics: vec![],
 				},
 			];
diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs
index 746508f2352..364a36c1ec3 100644
--- a/substrate/bin/node/runtime/src/lib.rs
+++ b/substrate/bin/node/runtime/src/lib.rs
@@ -78,8 +78,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
 	// and set impl_version to equal spec_version. If only runtime
 	// implementation changes and behavior does not, then leave spec_version as
 	// is and increment impl_version.
-	spec_version: 194,
-	impl_version: 196,
+	spec_version: 195,
+	impl_version: 195,
 	apis: RUNTIME_API_VERSIONS,
 };
 
diff --git a/substrate/palette/executive/src/lib.rs b/substrate/palette/executive/src/lib.rs
index 6868d732a91..75e386b87b9 100644
--- a/substrate/palette/executive/src/lib.rs
+++ b/substrate/palette/executive/src/lib.rs
@@ -269,7 +269,7 @@ where
 		let dispatch_info = xt.get_dispatch_info();
 		let r = Applyable::apply::<UnsignedValidator>(xt, dispatch_info, encoded_len)?;
 
-		<system::Module<System>>::note_applied_extrinsic(&r, encoded_len as u32);
+		<system::Module<System>>::note_applied_extrinsic(&r, encoded_len as u32, dispatch_info);
 
 		Ok(r)
 	}
diff --git a/substrate/palette/support/src/weights.rs b/substrate/palette/support/src/weights.rs
index 7aaa8b31366..3a5863ee372 100644
--- a/substrate/palette/support/src/weights.rs
+++ b/substrate/palette/support/src/weights.rs
@@ -129,8 +129,7 @@ impl From<SimpleDispatchInfo> for DispatchClass {
 }
 
 /// A bundle of static information collected from the `#[weight = $x]` attributes.
-#[cfg_attr(feature = "std", derive(PartialEq, Eq))]
-#[derive(Clone, Copy, Default, RuntimeDebug)]
+#[derive(Clone, Copy, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)]
 pub struct DispatchInfo {
 	/// Weight of this transaction.
 	pub weight: Weight,
diff --git a/substrate/palette/system/src/lib.rs b/substrate/palette/system/src/lib.rs
index c7038f41686..1d4c49801f8 100644
--- a/substrate/palette/system/src/lib.rs
+++ b/substrate/palette/system/src/lib.rs
@@ -311,9 +311,9 @@ decl_event!(
 	/// Event for the System module.
 	pub enum Event {
 		/// An extrinsic completed successfully.
-		ExtrinsicSuccess,
+		ExtrinsicSuccess(DispatchInfo),
 		/// An extrinsic failed.
-		ExtrinsicFailed(DispatchError),
+		ExtrinsicFailed(DispatchError, DispatchInfo),
 	}
 );
 
@@ -754,11 +754,11 @@ impl<T: Trait> Module<T> {
 	}
 
 	/// To be called immediately after an extrinsic has been applied.
-	pub fn note_applied_extrinsic(r: &ApplyOutcome, _encoded_len: u32) {
+	pub fn note_applied_extrinsic(r: &ApplyOutcome, _encoded_len: u32, info: DispatchInfo) {
 		Self::deposit_event(
 			match r {
-				Ok(()) => Event::ExtrinsicSuccess,
-				Err(err) => Event::ExtrinsicFailed(err.clone()),
+				Ok(()) => Event::ExtrinsicSuccess(info),
+				Err(err) => Event::ExtrinsicFailed(err.clone(), info),
 			}
 		);
 
@@ -1175,8 +1175,8 @@ mod tests {
 	impl From<Event> for u16 {
 		fn from(e: Event) -> u16 {
 			match e {
-				Event::ExtrinsicSuccess => 100,
-				Event::ExtrinsicFailed(_) => 101,
+				Event::ExtrinsicSuccess(..) => 100,
+				Event::ExtrinsicFailed(..) => 101,
 			}
 		}
 	}
@@ -1224,8 +1224,8 @@ mod tests {
 
 			System::initialize(&2, &[0u8; 32].into(), &[0u8; 32].into(), &Default::default());
 			System::deposit_event(42u16);
-			System::note_applied_extrinsic(&Ok(()), 0);
-			System::note_applied_extrinsic(&Err(DispatchError::new(Some(1), 2, None)), 0);
+			System::note_applied_extrinsic(&Ok(()), 0, Default::default());
+			System::note_applied_extrinsic(&Err(DispatchError::new(Some(1), 2, None)), 0, Default::default());
 			System::note_finished_extrinsics();
 			System::deposit_event(3u16);
 			System::finalize();
-- 
GitLab