From db53d68e9bde73736f4b7dc0e05e1d0cea750308 Mon Sep 17 00:00:00 2001
From: Branislav Kontur <bkontur@gmail.com>
Date: Wed, 26 Apr 2023 21:36:48 +0200
Subject: [PATCH] Removed relayer_account: &AccountId from MessageDispatch 
 (#2080)

* SourceBridgeHubChain/TargetBridgeHubChain replaced with RelayerAccountChain

* Fix

* Removed `relayer_account: &AccountId` from `MessageDispatch`
---
 .../bin/millau/runtime/src/rialto_messages.rs |  2 -
 .../runtime/src/rialto_parachain_messages.rs  |  2 -
 bridges/bin/millau/runtime/src/xcm_config.rs  |  6 +-
 .../bin/rialto-parachain/runtime/src/lib.rs   |  3 +-
 .../runtime/src/millau_messages.rs            |  2 -
 .../bin/rialto/runtime/src/millau_messages.rs |  2 -
 bridges/bin/rialto/runtime/src/xcm_config.rs  |  3 +-
 .../src/messages_xcm_extension.rs             | 22 ++------
 bridges/modules/grandpa/src/lib.rs            |  6 +-
 bridges/modules/messages/src/inbound_lane.rs  | 56 +++++++------------
 bridges/modules/messages/src/lib.rs           | 14 +----
 bridges/modules/messages/src/mock.rs          |  3 +-
 .../primitives/messages/src/target_chain.rs   |  9 +--
 13 files changed, 39 insertions(+), 91 deletions(-)

diff --git a/bridges/bin/millau/runtime/src/rialto_messages.rs b/bridges/bin/millau/runtime/src/rialto_messages.rs
index 84535283d48..573e3f5e68d 100644
--- a/bridges/bin/millau/runtime/src/rialto_messages.rs
+++ b/bridges/bin/millau/runtime/src/rialto_messages.rs
@@ -64,8 +64,6 @@ pub type ToRialtoMessagesDeliveryProof =
 /// Call-dispatch based message dispatch for Rialto -> Millau messages.
 pub type FromRialtoMessageDispatch =
 	bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
-		bp_millau::Millau,
-		bp_rialto::Rialto,
 		crate::xcm_config::OnMillauBlobDispatcher,
 		(),
 	>;
diff --git a/bridges/bin/millau/runtime/src/rialto_parachain_messages.rs b/bridges/bin/millau/runtime/src/rialto_parachain_messages.rs
index bef8a281188..041d3256f42 100644
--- a/bridges/bin/millau/runtime/src/rialto_parachain_messages.rs
+++ b/bridges/bin/millau/runtime/src/rialto_parachain_messages.rs
@@ -59,8 +59,6 @@ pub type FromRialtoParachainMessagePayload = messages::target::FromBridgedChainM
 /// Call-dispatch based message dispatch for RialtoParachain -> Millau messages.
 pub type FromRialtoParachainMessageDispatch =
 	bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
-		bp_millau::Millau,
-		bp_rialto::Rialto,
 		crate::xcm_config::OnMillauBlobDispatcher,
 		(),
 	>;
diff --git a/bridges/bin/millau/runtime/src/xcm_config.rs b/bridges/bin/millau/runtime/src/xcm_config.rs
index 4aaec83771b..cabb70f40cc 100644
--- a/bridges/bin/millau/runtime/src/xcm_config.rs
+++ b/bridges/bin/millau/runtime/src/xcm_config.rs
@@ -349,8 +349,7 @@ mod tests {
 
 		// we care only about handing message to the XCM dispatcher, so we don't care about its
 		// actual dispatch
-		let dispatch_result =
-			FromRialtoMessageDispatch::dispatch(&AccountId::from([0u8; 32]), incoming_message);
+		let dispatch_result = FromRialtoMessageDispatch::dispatch(incoming_message);
 		assert!(matches!(
 			dispatch_result.dispatch_level_result,
 			XcmBlobMessageDispatchResult::NotDispatched(_),
@@ -363,8 +362,7 @@ mod tests {
 
 		// we care only about handing message to the XCM dispatcher, so we don't care about its
 		// actual dispatch
-		let dispatch_result =
-			FromRialtoMessageDispatch::dispatch(&AccountId::from([0u8; 32]), incoming_message);
+		let dispatch_result = FromRialtoMessageDispatch::dispatch(incoming_message);
 		assert!(matches!(
 			dispatch_result.dispatch_level_result,
 			XcmBlobMessageDispatchResult::NotDispatched(_),
diff --git a/bridges/bin/rialto-parachain/runtime/src/lib.rs b/bridges/bin/rialto-parachain/runtime/src/lib.rs
index cd5c45ec4ba..de8ca493520 100644
--- a/bridges/bin/rialto-parachain/runtime/src/lib.rs
+++ b/bridges/bin/rialto-parachain/runtime/src/lib.rs
@@ -926,8 +926,7 @@ mod tests {
 
 			// we care only about handing message to the XCM dispatcher, so we don't care about its
 			// actual dispatch
-			let dispatch_result =
-				FromMillauMessageDispatch::dispatch(&AccountId::from([0u8; 32]), incoming_message);
+			let dispatch_result = FromMillauMessageDispatch::dispatch(incoming_message);
 			assert!(matches!(
 				dispatch_result.dispatch_level_result,
 				XcmBlobMessageDispatchResult::NotDispatched(_),
diff --git a/bridges/bin/rialto-parachain/runtime/src/millau_messages.rs b/bridges/bin/rialto-parachain/runtime/src/millau_messages.rs
index 5d4a92b5091..9c00e6bad34 100644
--- a/bridges/bin/rialto-parachain/runtime/src/millau_messages.rs
+++ b/bridges/bin/rialto-parachain/runtime/src/millau_messages.rs
@@ -60,8 +60,6 @@ pub type FromMillauMessagePayload = messages::target::FromBridgedChainMessagePay
 /// Call-dispatch based message dispatch for Millau -> RialtoParachain messages.
 pub type FromMillauMessageDispatch =
 	bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
-		bp_rialto_parachain::RialtoParachain,
-		bp_millau::Millau,
 		crate::OnRialtoParachainBlobDispatcher,
 		(),
 	>;
diff --git a/bridges/bin/rialto/runtime/src/millau_messages.rs b/bridges/bin/rialto/runtime/src/millau_messages.rs
index ab4b7dd521d..7c960639f2f 100644
--- a/bridges/bin/rialto/runtime/src/millau_messages.rs
+++ b/bridges/bin/rialto/runtime/src/millau_messages.rs
@@ -57,8 +57,6 @@ pub type FromMillauMessagePayload = messages::target::FromBridgedChainMessagePay
 /// Call-dispatch based message dispatch for Millau -> Rialto messages.
 pub type FromMillauMessageDispatch =
 	bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
-		bp_rialto::Rialto,
-		bp_millau::Millau,
 		crate::xcm_config::OnRialtoBlobDispatcher,
 		(),
 	>;
diff --git a/bridges/bin/rialto/runtime/src/xcm_config.rs b/bridges/bin/rialto/runtime/src/xcm_config.rs
index 9f6488b4c4d..52c5af635b3 100644
--- a/bridges/bin/rialto/runtime/src/xcm_config.rs
+++ b/bridges/bin/rialto/runtime/src/xcm_config.rs
@@ -266,8 +266,7 @@ mod tests {
 
 		// we care only about handing message to the XCM dispatcher, so we don't care about its
 		// actual dispatch
-		let dispatch_result =
-			FromMillauMessageDispatch::dispatch(&AccountId::from([0u8; 32]), incoming_message);
+		let dispatch_result = FromMillauMessageDispatch::dispatch(incoming_message);
 		assert!(matches!(
 			dispatch_result.dispatch_level_result,
 			XcmBlobMessageDispatchResult::NotDispatched(_),
diff --git a/bridges/bin/runtime-common/src/messages_xcm_extension.rs b/bridges/bin/runtime-common/src/messages_xcm_extension.rs
index 4ccdd7a4b4d..96fdf1d5018 100644
--- a/bridges/bin/runtime-common/src/messages_xcm_extension.rs
+++ b/bridges/bin/runtime-common/src/messages_xcm_extension.rs
@@ -26,7 +26,7 @@ use bp_messages::{
 	target_chain::{DispatchMessage, MessageDispatch},
 	LaneId,
 };
-use bp_runtime::{messages::MessageDispatchResult, AccountIdOf, Chain};
+use bp_runtime::messages::MessageDispatchResult;
 use codec::{Decode, Encode};
 use frame_support::{dispatch::Weight, CloneNoBound, EqNoBound, PartialEqNoBound};
 use pallet_bridge_messages::WeightInfoExt as MessagesPalletWeights;
@@ -46,23 +46,12 @@ pub enum XcmBlobMessageDispatchResult {
 }
 
 /// [`XcmBlobMessageDispatch`] is responsible for dispatching received messages
-pub struct XcmBlobMessageDispatch<SourceBridgeHubChain, TargetBridgeHubChain, DispatchBlob, Weights>
-{
-	_marker: sp_std::marker::PhantomData<(
-		SourceBridgeHubChain,
-		TargetBridgeHubChain,
-		DispatchBlob,
-		Weights,
-	)>,
+pub struct XcmBlobMessageDispatch<DispatchBlob, Weights> {
+	_marker: sp_std::marker::PhantomData<(DispatchBlob, Weights)>,
 }
 
-impl<
-		SourceBridgeHubChain: Chain,
-		TargetBridgeHubChain: Chain,
-		BlobDispatcher: DispatchBlob,
-		Weights: MessagesPalletWeights,
-	> MessageDispatch<AccountIdOf<SourceBridgeHubChain>>
-	for XcmBlobMessageDispatch<SourceBridgeHubChain, TargetBridgeHubChain, BlobDispatcher, Weights>
+impl<BlobDispatcher: DispatchBlob, Weights: MessagesPalletWeights> MessageDispatch
+	for XcmBlobMessageDispatch<BlobDispatcher, Weights>
 {
 	type DispatchPayload = XcmAsPlainPayload;
 	type DispatchLevelResult = XcmBlobMessageDispatchResult;
@@ -78,7 +67,6 @@ impl<
 	}
 
 	fn dispatch(
-		_relayer_account: &AccountIdOf<SourceBridgeHubChain>,
 		message: DispatchMessage<Self::DispatchPayload>,
 	) -> MessageDispatchResult<Self::DispatchLevelResult> {
 		let payload = match message.data.payload {
diff --git a/bridges/modules/grandpa/src/lib.rs b/bridges/modules/grandpa/src/lib.rs
index 9d38c9723d7..329e4c21136 100644
--- a/bridges/modules/grandpa/src/lib.rs
+++ b/bridges/modules/grandpa/src/lib.rs
@@ -834,6 +834,7 @@ mod tests {
 		run_test(|| {
 			assert_eq!(BestFinalized::<TestRuntime>::get(), None,);
 			assert_eq!(Pallet::<TestRuntime>::best_finalized(), None);
+			assert_eq!(PalletOperatingMode::<TestRuntime>::try_get(), Err(()));
 
 			let init_data = init_with_origin(RuntimeOrigin::root()).unwrap();
 
@@ -843,7 +844,10 @@ mod tests {
 				CurrentAuthoritySet::<TestRuntime>::get().authorities,
 				init_data.authority_list
 			);
-			assert_eq!(PalletOperatingMode::<TestRuntime>::get(), BasicOperatingMode::Normal);
+			assert_eq!(
+				PalletOperatingMode::<TestRuntime>::try_get(),
+				Ok(BasicOperatingMode::Normal)
+			);
 		})
 	}
 
diff --git a/bridges/modules/messages/src/inbound_lane.rs b/bridges/modules/messages/src/inbound_lane.rs
index fa6480d962f..5ec4444dbdf 100644
--- a/bridges/modules/messages/src/inbound_lane.rs
+++ b/bridges/modules/messages/src/inbound_lane.rs
@@ -164,10 +164,9 @@ impl<S: InboundLaneStorage> InboundLane<S> {
 	}
 
 	/// Receive new message.
-	pub fn receive_message<Dispatch: MessageDispatch<AccountId>, AccountId>(
+	pub fn receive_message<Dispatch: MessageDispatch>(
 		&mut self,
 		relayer_at_bridged_chain: &S::Relayer,
-		relayer_at_this_chain: &AccountId,
 		nonce: MessageNonce,
 		message_data: DispatchMessageData<Dispatch::DispatchPayload>,
 	) -> ReceivalResult<Dispatch::DispatchLevelResult> {
@@ -189,13 +188,10 @@ impl<S: InboundLaneStorage> InboundLane<S> {
 		}
 
 		// then, dispatch message
-		let dispatch_result = Dispatch::dispatch(
-			relayer_at_this_chain,
-			DispatchMessage {
-				key: MessageKey { lane_id: self.storage.id(), nonce },
-				data: message_data,
-			},
-		);
+		let dispatch_result = Dispatch::dispatch(DispatchMessage {
+			key: MessageKey { lane_id: self.storage.id(), nonce },
+			data: message_data,
+		});
 
 		// now let's update inbound lane storage
 		match data.relayers.back_mut() {
@@ -234,8 +230,7 @@ mod tests {
 		nonce: MessageNonce,
 	) {
 		assert_eq!(
-			lane.receive_message::<TestMessageDispatch, _>(
-				&TEST_RELAYER_A,
+			lane.receive_message::<TestMessageDispatch>(
 				&TEST_RELAYER_A,
 				nonce,
 				inbound_message_data(REGULAR_PAYLOAD)
@@ -362,8 +357,7 @@ mod tests {
 		run_test(|| {
 			let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID);
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_A,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_A,
 					10,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -382,8 +376,7 @@ mod tests {
 				<TestRuntime as Config>::MaxUnrewardedRelayerEntriesAtInboundLane::get();
 			for current_nonce in 1..max_nonce + 1 {
 				assert_eq!(
-					lane.receive_message::<TestMessageDispatch, _>(
-						&(TEST_RELAYER_A + current_nonce),
+					lane.receive_message::<TestMessageDispatch>(
 						&(TEST_RELAYER_A + current_nonce),
 						current_nonce,
 						inbound_message_data(REGULAR_PAYLOAD)
@@ -393,8 +386,7 @@ mod tests {
 			}
 			// Fails to dispatch new message from different than latest relayer.
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&(TEST_RELAYER_A + max_nonce + 1),
+				lane.receive_message::<TestMessageDispatch>(
 					&(TEST_RELAYER_A + max_nonce + 1),
 					max_nonce + 1,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -403,8 +395,7 @@ mod tests {
 			);
 			// Fails to dispatch new messages from latest relayer. Prevents griefing attacks.
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&(TEST_RELAYER_A + max_nonce),
+				lane.receive_message::<TestMessageDispatch>(
 					&(TEST_RELAYER_A + max_nonce),
 					max_nonce + 1,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -421,8 +412,7 @@ mod tests {
 			let max_nonce = <TestRuntime as Config>::MaxUnconfirmedMessagesAtInboundLane::get();
 			for current_nonce in 1..=max_nonce {
 				assert_eq!(
-					lane.receive_message::<TestMessageDispatch, _>(
-						&TEST_RELAYER_A,
+					lane.receive_message::<TestMessageDispatch>(
 						&TEST_RELAYER_A,
 						current_nonce,
 						inbound_message_data(REGULAR_PAYLOAD)
@@ -432,8 +422,7 @@ mod tests {
 			}
 			// Fails to dispatch new message from different than latest relayer.
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_B,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_B,
 					max_nonce + 1,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -442,8 +431,7 @@ mod tests {
 			);
 			// Fails to dispatch new messages from latest relayer.
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_A,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_A,
 					max_nonce + 1,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -458,8 +446,7 @@ mod tests {
 		run_test(|| {
 			let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID);
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_A,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_A,
 					1,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -467,8 +454,7 @@ mod tests {
 				ReceivalResult::Dispatched(dispatch_result(0))
 			);
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_B,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_B,
 					2,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -476,8 +462,7 @@ mod tests {
 				ReceivalResult::Dispatched(dispatch_result(0))
 			);
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_A,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_A,
 					3,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -500,8 +485,7 @@ mod tests {
 		run_test(|| {
 			let mut lane = inbound_lane::<TestRuntime, _>(TEST_LANE_ID);
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_A,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_A,
 					1,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -509,8 +493,7 @@ mod tests {
 				ReceivalResult::Dispatched(dispatch_result(0))
 			);
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_B,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_B,
 					1,
 					inbound_message_data(REGULAR_PAYLOAD)
@@ -536,8 +519,7 @@ mod tests {
 			let mut payload = REGULAR_PAYLOAD;
 			*payload.dispatch_result.unspent_weight.ref_time_mut() = 1;
 			assert_eq!(
-				lane.receive_message::<TestMessageDispatch, _>(
-					&TEST_RELAYER_A,
+				lane.receive_message::<TestMessageDispatch>(
 					&TEST_RELAYER_A,
 					1,
 					inbound_message_data(payload)
diff --git a/bridges/modules/messages/src/lib.rs b/bridges/modules/messages/src/lib.rs
index 6985b9018e7..8f4911b8d03 100644
--- a/bridges/modules/messages/src/lib.rs
+++ b/bridges/modules/messages/src/lib.rs
@@ -162,10 +162,7 @@ pub mod pallet {
 		/// Source header chain, as it is represented on target chain.
 		type SourceHeaderChain: SourceHeaderChain;
 		/// Message dispatch.
-		type MessageDispatch: MessageDispatch<
-			Self::AccountId,
-			DispatchPayload = Self::InboundPayload,
-		>;
+		type MessageDispatch: MessageDispatch<DispatchPayload = Self::InboundPayload>;
 	}
 
 	/// Shortcut to messages proof type for Config.
@@ -361,9 +358,8 @@ pub mod pallet {
 						fail!(Error::<T, I>::InsufficientDispatchWeight);
 					}
 
-					let receival_result = lane.receive_message::<T::MessageDispatch, T::AccountId>(
+					let receival_result = lane.receive_message::<T::MessageDispatch>(
 						&relayer_id_at_bridged_chain,
-						&relayer_id_at_this_chain,
 						message.key.nonce,
 						message.data,
 					);
@@ -545,11 +541,7 @@ pub mod pallet {
 		MessageAccepted { lane_id: LaneId, nonce: MessageNonce },
 		/// Messages have been received from the bridged chain.
 		MessagesReceived(
-			Vec<
-				ReceivedMessages<
-					<T::MessageDispatch as MessageDispatch<T::AccountId>>::DispatchLevelResult,
-				>,
-			>,
+			Vec<ReceivedMessages<<T::MessageDispatch as MessageDispatch>::DispatchLevelResult>>,
 		),
 		/// Messages in the inclusive range have been delivered to the bridged chain.
 		MessagesDelivered { lane_id: LaneId, messages: DeliveredMessages },
diff --git a/bridges/modules/messages/src/mock.rs b/bridges/modules/messages/src/mock.rs
index 270222a1fd6..3d78ab562d2 100644
--- a/bridges/modules/messages/src/mock.rs
+++ b/bridges/modules/messages/src/mock.rs
@@ -416,7 +416,7 @@ impl SourceHeaderChain for TestSourceHeaderChain {
 #[derive(Debug)]
 pub struct TestMessageDispatch;
 
-impl MessageDispatch<AccountId> for TestMessageDispatch {
+impl MessageDispatch for TestMessageDispatch {
 	type DispatchPayload = TestPayload;
 	type DispatchLevelResult = TestDispatchLevelResult;
 
@@ -428,7 +428,6 @@ impl MessageDispatch<AccountId> for TestMessageDispatch {
 	}
 
 	fn dispatch(
-		_relayer_account: &AccountId,
 		message: DispatchMessage<TestPayload>,
 	) -> MessageDispatchResult<TestDispatchLevelResult> {
 		match message.data.payload.as_ref() {
diff --git a/bridges/primitives/messages/src/target_chain.rs b/bridges/primitives/messages/src/target_chain.rs
index 8496b90214c..3c2e8cf0cb0 100644
--- a/bridges/primitives/messages/src/target_chain.rs
+++ b/bridges/primitives/messages/src/target_chain.rs
@@ -83,7 +83,7 @@ pub trait SourceHeaderChain {
 }
 
 /// Called when inbound message is received.
-pub trait MessageDispatch<AccountId> {
+pub trait MessageDispatch {
 	/// Decoded message payload type. Valid message may contain invalid payload. In this case
 	/// message is delivered, but dispatch fails. Therefore, two separate types of payload
 	/// (opaque `MessagePayload` used in delivery and this `DispatchPayload` used in dispatch).
@@ -103,11 +103,7 @@ pub trait MessageDispatch<AccountId> {
 	///
 	/// It is up to the implementers of this trait to determine whether the message
 	/// is invalid (i.e. improperly encoded, has too large weight, ...) or not.
-	///
-	/// If your configuration allows paying dispatch fee at the target chain, then
-	/// it must be paid inside this method to the `relayer_account`.
 	fn dispatch(
-		relayer_account: &AccountId,
 		message: DispatchMessage<Self::DispatchPayload>,
 	) -> MessageDispatchResult<Self::DispatchLevelResult>;
 }
@@ -186,7 +182,7 @@ impl<MessagesProof: Parameter + Size, DispatchPayload> SourceHeaderChain
 	}
 }
 
-impl<MessagesProof, DispatchPayload: Decode, AccountId> MessageDispatch<AccountId>
+impl<MessagesProof, DispatchPayload: Decode> MessageDispatch
 	for ForbidInboundMessages<MessagesProof, DispatchPayload>
 {
 	type DispatchPayload = DispatchPayload;
@@ -197,7 +193,6 @@ impl<MessagesProof, DispatchPayload: Decode, AccountId> MessageDispatch<AccountI
 	}
 
 	fn dispatch(
-		_: &AccountId,
 		_: DispatchMessage<Self::DispatchPayload>,
 	) -> MessageDispatchResult<Self::DispatchLevelResult> {
 		MessageDispatchResult { unspent_weight: Weight::zero(), dispatch_level_result: () }
-- 
GitLab