diff --git a/bridges/relays/substrate/src/cli.rs b/bridges/relays/substrate/src/cli.rs index 38785665e1460191d8e294a70108cca7e78fef8c..090ff3f92a9f757429c1132d8303c23a174eb3db 100644 --- a/bridges/relays/substrate/src/cli.rs +++ b/bridges/relays/substrate/src/cli.rs @@ -51,8 +51,21 @@ pub enum Command { /// The message is being sent to the source chain, delivered to the target chain and dispatched /// there. SendMessage(SendMessage), + /// Generate SCALE-encoded `Call` for choosen network. + /// + /// The call can be used either as message payload or can be wrapped into a transaction + /// and executed on the chain directly. + EncodeCall(EncodeCall), + /// Generate SCALE-encoded `MessagePayload` object that can be sent over selected bridge. + /// + /// The `MessagePayload` can be then fed to `MessageLane::send_message` function and sent over + /// the bridge. + EncodeMessagePayload(EncodeMessagePayload), + /// Estimate Delivery and Dispatch Fee required for message submission to message lane. + EstimateFee(EstimateFee), } +/// Start headers relayer process. #[derive(StructOpt)] pub enum RelayHeaders { /// Relay Millau headers to Rialto. @@ -79,6 +92,7 @@ pub enum RelayHeaders { }, } +/// Start message relayer process. #[derive(StructOpt)] pub enum RelayMessages { /// Serve given lane of Millau -> Rialto messages. @@ -115,6 +129,7 @@ pub enum RelayMessages { }, } +/// Initialize bridge pallet. #[derive(StructOpt)] pub enum InitBridge { /// Initialize Millau headers bridge in Rialto. @@ -141,6 +156,7 @@ pub enum InitBridge { }, } +/// Send bridge message. #[derive(StructOpt)] pub enum SendMessage { /// Submit message to given Millau -> Rialto lane. @@ -195,9 +211,112 @@ pub enum SendMessage { }, } +/// A call to encode. +#[derive(StructOpt)] +pub enum EncodeCall { + /// Encode Rialto's Call. + Rialto { + #[structopt(flatten)] + call: ToRialtoMessage, + }, + /// Encode Millau's Call. + Millau { + #[structopt(flatten)] + call: ToMillauMessage, + }, +} + +/// A `MessagePayload` to encode. +#[derive(StructOpt)] +pub enum EncodeMessagePayload { + /// Message Payload of Rialto to Millau call. + RialtoToMillau { + #[structopt(flatten)] + payload: RialtoToMillauMessagePayload, + }, + /// Message Payload of Millau to Rialto call. + MillauToRialto { + #[structopt(flatten)] + payload: MillauToRialtoMessagePayload, + }, +} + +/// Estimate Delivery & Dispatch Fee command. +#[derive(StructOpt)] +pub enum EstimateFee { + /// Estimate fee of Rialto to Millau message. + RialtoToMillau { + #[structopt(flatten)] + rialto: RialtoConnectionParams, + /// Hex-encoded id of lane that will be delivering the message. + #[structopt(long)] + lane: HexLaneId, + /// Payload to send over the bridge. + #[structopt(flatten)] + payload: RialtoToMillauMessagePayload, + }, + /// Estimate fee of Rialto to Millau message. + MillauToRialto { + #[structopt(flatten)] + millau: MillauConnectionParams, + /// Hex-encoded id of lane that will be delivering the message. + #[structopt(long)] + lane: HexLaneId, + /// Payload to send over the bridge. + #[structopt(flatten)] + payload: MillauToRialtoMessagePayload, + }, +} + +/// MessagePayload that can be delivered to message lane pallet on Millau. +#[derive(StructOpt, Debug)] +pub enum MillauToRialtoMessagePayload { + /// Raw, SCALE-encoded `MessagePayload`. + Raw { + /// Hex-encoded SCALE data. + #[structopt(long)] + data: Bytes, + }, + /// Construct message to send over the bridge. + Message { + /// Message details. + #[structopt(flatten)] + message: ToRialtoMessage, + /// SS58 encoded account that will send the payload (must have SS58Prefix = 42) + #[structopt(long)] + sender: bp_rialto::AccountId, + }, +} + +/// MessagePayload that can be delivered to message lane pallet on Rialto. +#[derive(StructOpt, Debug)] +pub enum RialtoToMillauMessagePayload { + /// Raw, SCALE-encoded `MessagePayload`. + Raw { + /// Hex-encoded SCALE data. + #[structopt(long)] + data: Bytes, + }, + /// Construct message to send over the bridge. + Message { + /// Message details. + #[structopt(flatten)] + message: ToMillauMessage, + + /// SS58 encoded account that will send the payload (must have SS58Prefix = 42) + #[structopt(long)] + sender: bp_rialto::AccountId, + }, +} + /// All possible messages that may be delivered to the Rialto chain. #[derive(StructOpt, Debug)] pub enum ToRialtoMessage { + /// Raw bytes for the message + Raw { + /// Raw, SCALE-encoded message + data: Bytes, + }, /// Make an on-chain remark (comment). Remark { /// Remark size. If not passed, small UTF8-encoded string is generated by relay as remark. @@ -206,8 +325,10 @@ pub enum ToRialtoMessage { }, /// Transfer the specified `amount` of native tokens to a particular `recipient`. Transfer { + /// SS58 encoded account that will receive the transfer (must have SS58Prefix = 42) #[structopt(long)] recipient: bp_rialto::AccountId, + /// Amount of target tokens to send. #[structopt(long)] amount: bp_rialto::Balance, }, @@ -216,6 +337,11 @@ pub enum ToRialtoMessage { /// All possible messages that may be delivered to the Millau chain. #[derive(StructOpt, Debug)] pub enum ToMillauMessage { + /// Raw bytes for the message + Raw { + /// Raw, SCALE-encoded message + data: Bytes, + }, /// Make an on-chain remark (comment). Remark { /// Size of the remark. If not passed, small UTF8-encoded string is generated by relay as remark. @@ -224,8 +350,10 @@ pub enum ToMillauMessage { }, /// Transfer the specified `amount` of native tokens to a particular `recipient`. Transfer { + /// SS58 encoded account that will receive the transfer (must have SS58Prefix = 42) #[structopt(long)] recipient: bp_millau::AccountId, + /// Amount of target tokens to send. #[structopt(long)] amount: bp_millau::Balance, }, diff --git a/bridges/relays/substrate/src/main.rs b/bridges/relays/substrate/src/main.rs index c73533cdfca8af6bab6c88e9c1667e8035bc5337..ed17e309670c3f1325312cff74eed7891b288c53 100644 --- a/bridges/relays/substrate/src/main.rs +++ b/bridges/relays/substrate/src/main.rs @@ -28,6 +28,7 @@ use relay_substrate_client::{Chain, ConnectionParams, TransactionSignScheme}; use relay_utils::initialize::initialize_relay; use sp_core::{Bytes, Pair}; use sp_runtime::traits::IdentifyAccount; +use std::fmt::Debug; /// Kusama node client. pub type KusamaClient = relay_substrate_client::Client<Kusama>; @@ -64,6 +65,9 @@ async fn run_command(command: cli::Command) -> Result<(), String> { cli::Command::RelayHeaders(arg) => run_relay_headers(arg).await, cli::Command::RelayMessages(arg) => run_relay_messages(arg).await, cli::Command::SendMessage(arg) => run_send_message(arg).await, + cli::Command::EncodeCall(arg) => run_encode_call(arg).await, + cli::Command::EncodeMessagePayload(arg) => run_encode_message_payload(arg).await, + cli::Command::EstimateFee(arg) => run_estimate_fee(arg).await, } } @@ -240,7 +244,7 @@ async fn run_send_message(command: cli::SendMessage) -> Result<(), String> { let millau_client = millau.into_client().await?; let millau_sign = millau_sign.parse()?; let rialto_sign = rialto_sign.parse()?; - let rialto_call = message.into_call(); + let rialto_call = message.into_call()?; let payload = millau_to_rialto_message_payload(&millau_sign, &rialto_sign, &rialto_call, origin, dispatch_weight); @@ -295,7 +299,7 @@ async fn run_send_message(command: cli::SendMessage) -> Result<(), String> { let rialto_client = rialto.into_client().await?; let rialto_sign = rialto_sign.parse()?; let millau_sign = millau_sign.parse()?; - let millau_call = message.into_call(); + let millau_call = message.into_call()?; let payload = rialto_to_millau_message_payload(&rialto_sign, &millau_sign, &millau_call, origin, dispatch_weight); @@ -340,6 +344,74 @@ async fn run_send_message(command: cli::SendMessage) -> Result<(), String> { Ok(()) } +async fn run_encode_call(call: cli::EncodeCall) -> Result<(), String> { + match call { + cli::EncodeCall::Rialto { call } => { + let call = call.into_call()?; + + println!("{:?}", HexBytes::encode(&call)); + } + cli::EncodeCall::Millau { call } => { + let call = call.into_call()?; + println!("{:?}", HexBytes::encode(&call)); + } + } + Ok(()) +} + +async fn run_encode_message_payload(call: cli::EncodeMessagePayload) -> Result<(), String> { + match call { + cli::EncodeMessagePayload::RialtoToMillau { payload } => { + let payload = payload.into_payload()?; + + println!("{:?}", HexBytes::encode(&payload)); + } + cli::EncodeMessagePayload::MillauToRialto { payload } => { + let payload = payload.into_payload()?; + + println!("{:?}", HexBytes::encode(&payload)); + } + } + Ok(()) +} + +async fn run_estimate_fee(cmd: cli::EstimateFee) -> Result<(), String> { + match cmd { + cli::EstimateFee::RialtoToMillau { rialto, lane, payload } => { + let client = rialto.into_client().await?; + let lane = lane.into(); + let payload = payload.into_payload()?; + + let fee: Option<bp_rialto::Balance> = estimate_message_delivery_and_dispatch_fee( + &client, + bp_millau::TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD, + lane, + payload, + ) + .await?; + + println!("Fee: {:?}", fee); + } + cli::EstimateFee::MillauToRialto { millau, lane, payload } => { + let client = millau.into_client().await?; + let lane = lane.into(); + let payload = payload.into_payload()?; + + let fee: Option<bp_millau::Balance> = estimate_message_delivery_and_dispatch_fee( + &client, + bp_rialto::TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD, + lane, + payload, + ) + .await?; + + println!("Fee: {:?}", fee); + } + } + + Ok(()) +} + async fn estimate_message_delivery_and_dispatch_fee<Fee: Decode, C: Chain, P: Encode>( client: &relay_substrate_client::Client<C>, estimate_fee_method: &str, @@ -370,6 +442,43 @@ fn remark_payload(remark_size: Option<cli::ExplicitOrMaximal<usize>>, maximal_al } } +fn message_payload<SAccountId, TPublic, TSignature>( + spec_version: u32, + weight: Weight, + origin: CallOrigin<SAccountId, TPublic, TSignature>, + call: &impl Encode, +) -> MessagePayload<SAccountId, TPublic, TSignature, Vec<u8>> +where + SAccountId: Encode + Debug, + TPublic: Encode + Debug, + TSignature: Encode + Debug, +{ + // Display nicely formatted call. + let payload = MessagePayload { + spec_version, + weight, + origin, + call: HexBytes::encode(call), + }; + + log::info!(target: "bridge", "Created Message Payload: {:#?}", payload); + log::info!(target: "bridge", "Encoded Message Payload: {:?}", HexBytes::encode(&payload)); + + // re-pack to return `Vec<u8>` + let MessagePayload { + spec_version, + weight, + origin, + call, + } = payload; + MessagePayload { + spec_version, + weight, + origin, + call: call.0, + } +} + fn rialto_to_millau_message_payload( rialto_sign: &RialtoSigningParams, millau_sign: &MillauSigningParams, @@ -386,10 +495,10 @@ fn rialto_to_millau_message_payload( let rialto_account_id: bp_rialto::AccountId = rialto_sender_public.into_account(); let millau_origin_public = millau_sign.signer.public(); - MessagePayload { - spec_version: millau_runtime::VERSION.spec_version, - weight: millau_call_weight, - origin: match origin { + message_payload( + millau_runtime::VERSION.spec_version, + millau_call_weight, + match origin { cli::Origins::Source => CallOrigin::SourceAccount(rialto_account_id), cli::Origins::Target => { let digest = rialto_runtime::millau_account_ownership_digest( @@ -403,8 +512,8 @@ fn rialto_to_millau_message_payload( CallOrigin::TargetAccount(rialto_account_id, millau_origin_public.into(), digest_signature.into()) } }, - call: millau_call.encode(), - } + &millau_call, + ) } fn millau_to_rialto_message_payload( @@ -423,10 +532,10 @@ fn millau_to_rialto_message_payload( let millau_account_id: bp_millau::AccountId = millau_sender_public.into_account(); let rialto_origin_public = rialto_sign.signer.public(); - MessagePayload { - spec_version: rialto_runtime::VERSION.spec_version, - weight: rialto_call_weight, - origin: match origin { + message_payload( + rialto_runtime::VERSION.spec_version, + rialto_call_weight, + match origin { cli::Origins::Source => CallOrigin::SourceAccount(millau_account_id), cli::Origins::Target => { let digest = millau_runtime::rialto_account_ownership_digest( @@ -440,8 +549,8 @@ fn millau_to_rialto_message_payload( CallOrigin::TargetAccount(millau_account_id, rialto_origin_public.into(), digest_signature.into()) } }, - call: rialto_call.encode(), - } + &rialto_call, + ) } fn prepare_call_dispatch_weight( @@ -460,7 +569,7 @@ where Fee: Decode, F: FnOnce() -> R, R: std::future::Future<Output = Result<Option<Fee>, E>>, - E: std::fmt::Debug, + E: Debug, { match fee { Some(fee) => Ok(fee), @@ -496,6 +605,46 @@ fn compute_maximal_message_arguments_size( maximal_call_size - service_bytes } +impl crate::cli::MillauToRialtoMessagePayload { + /// Parse the CLI parameters and construct message payload. + pub fn into_payload( + self, + ) -> Result<MessagePayload<bp_rialto::AccountId, bp_rialto::AccountSigner, bp_rialto::Signature, Vec<u8>>, String> { + match self { + Self::Raw { data } => MessagePayload::decode(&mut &*data.0) + .map_err(|e| format!("Failed to decode Millau's MessagePayload: {:?}", e)), + Self::Message { message, sender } => { + let spec_version = rialto_runtime::VERSION.spec_version; + let origin = CallOrigin::SourceAccount(sender); + let call = message.into_call()?; + let weight = call.get_dispatch_info().weight; + + Ok(message_payload(spec_version, weight, origin, &call)) + } + } + } +} + +impl crate::cli::RialtoToMillauMessagePayload { + /// Parse the CLI parameters and construct message payload. + pub fn into_payload( + self, + ) -> Result<MessagePayload<bp_millau::AccountId, bp_millau::AccountSigner, bp_millau::Signature, Vec<u8>>, String> { + match self { + Self::Raw { data } => MessagePayload::decode(&mut &*data.0) + .map_err(|e| format!("Failed to decode Rialto's MessagePayload: {:?}", e)), + Self::Message { message, sender } => { + let spec_version = millau_runtime::VERSION.spec_version; + let origin = CallOrigin::SourceAccount(sender); + let call = message.into_call()?; + let weight = call.get_dispatch_info().weight; + + Ok(message_payload(spec_version, weight, origin, &call)) + } + } + } +} + impl crate::cli::RialtoSigningParams { /// Parse CLI parameters into typed signing params. pub fn parse(self) -> Result<RialtoSigningParams, String> { @@ -535,8 +684,11 @@ impl crate::cli::RialtoConnectionParams { impl crate::cli::ToRialtoMessage { /// Convert CLI call request into runtime `Call` instance. - pub fn into_call(self) -> rialto_runtime::Call { - match self { + pub fn into_call(self) -> Result<rialto_runtime::Call, String> { + let call = match self { + cli::ToRialtoMessage::Raw { data } => { + Decode::decode(&mut &*data.0).map_err(|e| format!("Unable to decode message: {:#?}", e))? + } cli::ToRialtoMessage::Remark { remark_size } => { rialto_runtime::Call::System(rialto_runtime::SystemCall::remark(remark_payload( remark_size, @@ -549,14 +701,23 @@ impl crate::cli::ToRialtoMessage { cli::ToRialtoMessage::Transfer { recipient, amount } => { rialto_runtime::Call::Balances(rialto_runtime::BalancesCall::transfer(recipient, amount)) } - } + }; + + log::info!(target: "bridge", "Generated Rialto call: {:#?}", call); + log::info!(target: "bridge", "Weight of Rialto call: {}", call.get_dispatch_info().weight); + log::info!(target: "bridge", "Encoded Rialto call: {:?}", HexBytes::encode(&call)); + + Ok(call) } } impl crate::cli::ToMillauMessage { /// Convert CLI call request into runtime `Call` instance. - pub fn into_call(self) -> millau_runtime::Call { - match self { + pub fn into_call(self) -> Result<millau_runtime::Call, String> { + let call = match self { + cli::ToMillauMessage::Raw { data } => { + Decode::decode(&mut &*data.0).map_err(|e| format!("Unable to decode message: {:#?}", e))? + } cli::ToMillauMessage::Remark { remark_size } => { millau_runtime::Call::System(millau_runtime::SystemCall::remark(remark_payload( remark_size, @@ -569,7 +730,30 @@ impl crate::cli::ToMillauMessage { cli::ToMillauMessage::Transfer { recipient, amount } => { millau_runtime::Call::Balances(millau_runtime::BalancesCall::transfer(recipient, amount)) } - } + }; + + log::info!(target: "bridge", "Generated Millau call: {:#?}", call); + log::info!(target: "bridge", "Weight of Millau call: {}", call.get_dispatch_info().weight); + log::info!(target: "bridge", "Encoded Millau call: {:?}", HexBytes::encode(&call)); + + Ok(call) + } +} + +/// Nicer formatting for raw bytes vectors. +#[derive(Encode, Decode)] +struct HexBytes(Vec<u8>); + +impl Debug for HexBytes { + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(fmt, "0x{}", hex::encode(&self.0)) + } +} + +impl HexBytes { + /// Encode given object and wrap into nicely formatted bytes. + pub fn encode<T: Encode>(t: &T) -> Self { + Self(t.encode()) } } @@ -630,22 +814,22 @@ mod tests { compute_maximal_message_arguments_size(bp_rialto::max_extrinsic_size(), bp_millau::max_extrinsic_size()); let call: millau_runtime::Call = millau_runtime::SystemCall::remark(vec![42; maximal_remark_size as _]).into(); - let payload = pallet_bridge_call_dispatch::MessagePayload { - spec_version: Default::default(), - weight: call.get_dispatch_info().weight, - origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot, - call: call.encode(), - }; + let payload = message_payload( + Default::default(), + call.get_dispatch_info().weight, + pallet_bridge_call_dispatch::CallOrigin::SourceRoot, + &call, + ); assert_eq!(Millau::verify_message(&payload), Ok(())); let call: millau_runtime::Call = millau_runtime::SystemCall::remark(vec![42; (maximal_remark_size + 1) as _]).into(); - let payload = pallet_bridge_call_dispatch::MessagePayload { - spec_version: Default::default(), - weight: call.get_dispatch_info().weight, - origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot, - call: call.encode(), - }; + let payload = message_payload( + Default::default(), + call.get_dispatch_info().weight, + pallet_bridge_call_dispatch::CallOrigin::SourceRoot, + &call, + ); assert!(Millau::verify_message(&payload).is_err()); } @@ -666,20 +850,20 @@ mod tests { let maximal_dispatch_weight = compute_maximal_message_dispatch_weight(bp_millau::max_extrinsic_weight()); let call: millau_runtime::Call = rialto_runtime::SystemCall::remark(vec![]).into(); - let payload = pallet_bridge_call_dispatch::MessagePayload { - spec_version: Default::default(), - weight: maximal_dispatch_weight, - origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot, - call: call.encode(), - }; + let payload = message_payload( + Default::default(), + maximal_dispatch_weight, + pallet_bridge_call_dispatch::CallOrigin::SourceRoot, + &call, + ); assert_eq!(Millau::verify_message(&payload), Ok(())); - let payload = pallet_bridge_call_dispatch::MessagePayload { - spec_version: Default::default(), - weight: maximal_dispatch_weight + 1, - origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot, - call: call.encode(), - }; + let payload = message_payload( + Default::default(), + maximal_dispatch_weight + 1, + pallet_bridge_call_dispatch::CallOrigin::SourceRoot, + &call, + ); assert!(Millau::verify_message(&payload).is_err()); } @@ -690,20 +874,20 @@ mod tests { let maximal_dispatch_weight = compute_maximal_message_dispatch_weight(bp_rialto::max_extrinsic_weight()); let call: rialto_runtime::Call = millau_runtime::SystemCall::remark(vec![]).into(); - let payload = pallet_bridge_call_dispatch::MessagePayload { - spec_version: Default::default(), - weight: maximal_dispatch_weight, - origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot, - call: call.encode(), - }; + let payload = message_payload( + Default::default(), + maximal_dispatch_weight, + pallet_bridge_call_dispatch::CallOrigin::SourceRoot, + &call, + ); assert_eq!(Rialto::verify_message(&payload), Ok(())); - let payload = pallet_bridge_call_dispatch::MessagePayload { - spec_version: Default::default(), - weight: maximal_dispatch_weight + 1, - origin: pallet_bridge_call_dispatch::CallOrigin::SourceRoot, - call: call.encode(), - }; + let payload = message_payload( + Default::default(), + maximal_dispatch_weight + 1, + pallet_bridge_call_dispatch::CallOrigin::SourceRoot, + &call, + ); assert!(Rialto::verify_message(&payload).is_err()); } }