From da41afd105bfd9cc6a9cbd82733c88f9e08f6367 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= <tomusdrw@users.noreply.github.com>
Date: Tue, 2 Mar 2021 16:20:47 +0100
Subject: [PATCH] Add Bridge calls encoding to relay CLI (#780)

* Bridge calls.

* Allow encoding messages for both Rialto and Millua.

* Add docs.

* Display full extrinsics in debug.

* cargo fmt --all

* Use info instead of debug,.

* Clarify units.
---
 bridges/relays/substrate/src/cli.rs  | 35 ++++++++++++++++++++++------
 bridges/relays/substrate/src/main.rs | 16 +++++++++++++
 2 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/bridges/relays/substrate/src/cli.rs b/bridges/relays/substrate/src/cli.rs
index 9914cd2e3a5..d9bec577474 100644
--- a/bridges/relays/substrate/src/cli.rs
+++ b/bridges/relays/substrate/src/cli.rs
@@ -176,7 +176,7 @@ pub enum SendMessage {
 		/// Dispatch weight of the message. If not passed, determined automatically.
 		#[structopt(long)]
 		dispatch_weight: Option<ExplicitOrMaximal<Weight>>,
-		/// Delivery and dispatch fee. If not passed, determined automatically.
+		/// Delivery and dispatch fee in source chain base currency units. If not passed, determined automatically.
 		#[structopt(long)]
 		fee: Option<bp_millau::Balance>,
 		/// Message type.
@@ -201,7 +201,7 @@ pub enum SendMessage {
 		/// Dispatch weight of the message. If not passed, determined automatically.
 		#[structopt(long)]
 		dispatch_weight: Option<ExplicitOrMaximal<Weight>>,
-		/// Delivery and dispatch fee. If not passed, determined automatically.
+		/// Delivery and dispatch fee in source chain base currency units. If not passed, determined automatically.
 		#[structopt(long)]
 		fee: Option<bp_rialto::Balance>,
 		/// Message type.
@@ -291,7 +291,6 @@ pub enum MillauToRialtoMessagePayload {
 	/// Raw, SCALE-encoded `MessagePayload`.
 	Raw {
 		/// Hex-encoded SCALE data.
-		#[structopt(long)]
 		data: Bytes,
 	},
 	/// Construct message to send over the bridge.
@@ -311,7 +310,6 @@ pub enum RialtoToMillauMessagePayload {
 	/// Raw, SCALE-encoded `MessagePayload`.
 	Raw {
 		/// Hex-encoded SCALE data.
-		#[structopt(long)]
 		data: Bytes,
 	},
 	/// Construct message to send over the bridge.
@@ -319,7 +317,6 @@ pub enum RialtoToMillauMessagePayload {
 		/// Message details.
 		#[structopt(flatten)]
 		message: ToMillauMessage,
-
 		/// SS58 encoded account that will send the payload (must have SS58Prefix = 42)
 		#[structopt(long)]
 		sender: AccountId,
@@ -345,10 +342,22 @@ pub enum ToRialtoMessage {
 		/// SS58 encoded account that will receive the transfer (must have SS58Prefix = 42)
 		#[structopt(long)]
 		recipient: AccountId,
-		/// Amount of target tokens to send.
+		/// Amount of target tokens to send in target chain base currency units.
 		#[structopt(long)]
 		amount: bp_rialto::Balance,
 	},
+	/// A call to the Millau Bridge Message Lane pallet to send a message over the bridge.
+	MillauSendMessage {
+		/// Hex-encoded lane id that should be served by the relay. Defaults to `00000000`.
+		#[structopt(long, default_value = "00000000")]
+		lane: HexLaneId,
+		/// Raw SCALE-encoded Message Payload to submit to the message lane pallet.
+		#[structopt(long)]
+		payload: Bytes,
+		/// Declared delivery and dispatch fee in base source-chain currency units.
+		#[structopt(long)]
+		fee: bp_rialto::Balance,
+	},
 }
 
 /// All possible messages that may be delivered to the Millau chain.
@@ -370,10 +379,22 @@ pub enum ToMillauMessage {
 		/// SS58 encoded account that will receive the transfer (must have SS58Prefix = 42)
 		#[structopt(long)]
 		recipient: AccountId,
-		/// Amount of target tokens to send.
+		/// Amount of target tokens to send in target chain base currency units.
 		#[structopt(long)]
 		amount: bp_millau::Balance,
 	},
+	/// A call to the Rialto Bridge Message Lane pallet to send a message over the bridge.
+	RialtoSendMessage {
+		/// Hex-encoded lane id that should be served by the relay. Defaults to `00000000`.
+		#[structopt(long, default_value = "00000000")]
+		lane: HexLaneId,
+		/// Raw SCALE-encoded Message Payload to submit to the message lane pallet.
+		#[structopt(long)]
+		payload: Bytes,
+		/// Declared delivery and dispatch fee in base source-chain currency units.
+		#[structopt(long)]
+		fee: bp_millau::Balance,
+	},
 }
 
 arg_enum! {
diff --git a/bridges/relays/substrate/src/main.rs b/bridges/relays/substrate/src/main.rs
index f5c8c6a1e43..3a3c3620aeb 100644
--- a/bridges/relays/substrate/src/main.rs
+++ b/bridges/relays/substrate/src/main.rs
@@ -283,6 +283,7 @@ async fn run_send_message(command: cli::SendMessage) -> Result<(), String> {
 				dispatch_weight,
 				fee,
 			);
+			log::info!(target: "bridge", "Signed Millau Call: {:?}", HexBytes::encode(&signed_millau_call));
 
 			millau_client.submit_extrinsic(Bytes(signed_millau_call)).await?;
 		}
@@ -338,6 +339,7 @@ async fn run_send_message(command: cli::SendMessage) -> Result<(), String> {
 				dispatch_weight,
 				fee,
 			);
+			log::info!(target: "bridge", "Signed Rialto Call: {:?}", HexBytes::encode(&signed_rialto_call));
 
 			rialto_client.submit_extrinsic(Bytes(signed_rialto_call)).await?;
 		}
@@ -728,6 +730,13 @@ impl crate::cli::ToRialtoMessage {
 				let recipient = recipient.into_rialto();
 				rialto_runtime::Call::Balances(rialto_runtime::BalancesCall::transfer(recipient, amount))
 			}
+			cli::ToRialtoMessage::MillauSendMessage { lane, payload, fee } => {
+				let payload = cli::RialtoToMillauMessagePayload::Raw { data: payload }.into_payload()?;
+				let lane = lane.into();
+				rialto_runtime::Call::BridgeMillauMessageLane(rialto_runtime::MessageLaneCall::send_message(
+					lane, payload, fee,
+				))
+			}
 		};
 
 		log::info!(target: "bridge", "Generated Rialto call: {:#?}", call);
@@ -758,6 +767,13 @@ impl crate::cli::ToMillauMessage {
 				let recipient = recipient.into_millau();
 				millau_runtime::Call::Balances(millau_runtime::BalancesCall::transfer(recipient, amount))
 			}
+			cli::ToMillauMessage::RialtoSendMessage { lane, payload, fee } => {
+				let payload = cli::MillauToRialtoMessagePayload::Raw { data: payload }.into_payload()?;
+				let lane = lane.into();
+				millau_runtime::Call::BridgeRialtoMessageLane(millau_runtime::MessageLaneCall::send_message(
+					lane, payload, fee,
+				))
+			}
 		};
 
 		log::info!(target: "bridge", "Generated Millau call: {:#?}", call);
-- 
GitLab