Skip to content

Commit

Permalink
CLI: Encode Call & Multiple Bridge Instances. (paritytech#859)
Browse files Browse the repository at this point in the history
* Encode Call & Multiple Bridge Instances.

* Remove redundant clone.

* Fix comment.

* Rename pallet index bridge instance index.

* Update error messages related to target instances

Co-authored-by: Hernando Castano <hernando@hcastano.com>
  • Loading branch information
2 people authored and serban300 committed Apr 9, 2024
1 parent 33121c5 commit 0d16f3d
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 236 deletions.
249 changes: 249 additions & 0 deletions bridges/relays/bin-substrate/src/cli/encode_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.

// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use crate::cli::{AccountId, Balance, CliChain, ExplicitOrMaximal, HexBytes, HexLaneId};
use frame_support::dispatch::GetDispatchInfo;
use relay_substrate_client::Chain;
use structopt::{clap::arg_enum, StructOpt};

/// Encode source chain runtime call.
#[derive(StructOpt)]
pub struct EncodeCall {
/// A bridge instance to encode call for.
#[structopt(possible_values = &EncodeCallBridge::variants(), case_insensitive = true)]
bridge: EncodeCallBridge,
#[structopt(flatten)]
call: Call,
}

/// All possible messages that may be delivered to generic Substrate chain.
///
/// Note this enum may be used in the context of both Source (as part of `encode-call`)
/// and Target chain (as part of `encode-message/send-message`).
#[derive(StructOpt, Debug)]
pub enum Call {
/// Raw bytes for the message
Raw {
/// Raw, SCALE-encoded message
data: HexBytes,
},
/// Make an on-chain remark (comment).
Remark {
/// Explicit remark payload.
#[structopt(long, conflicts_with("remark_size"))]
remark_payload: HexBytes,
/// Remark size. If not passed, small UTF8-encoded string is generated by relay as remark.
#[structopt(long, conflicts_with("remark_payload"))]
remark_size: Option<ExplicitOrMaximal<usize>>,
},
/// Transfer the specified `amount` of native tokens to a particular `recipient`.
Transfer {
/// Address of an account to receive the transfer.
#[structopt(long)]
recipient: AccountId,
/// Amount of target tokens to send in target chain base currency units.
#[structopt(long)]
amount: Balance,
},
/// A call to the specific Bridge Messages pallet to queue message to be sent over a bridge.
BridgeSendMessage {
/// An index of the bridge instance which represents the expected target chain.
#[structopt(skip = 255)]
bridge_instance_index: u8,
/// 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 messages pallet.
///
/// This can be obtained by encoding call for the target chain.
#[structopt(long)]
payload: HexBytes,
/// Declared delivery and dispatch fee in base source-chain currency units.
#[structopt(long)]
fee: Balance,
},
}

pub trait CliEncodeCall: Chain {
/// Maximal size (in bytes) of any extrinsic (from the runtime).
fn max_extrinsic_size() -> u32;

/// Encode a CLI call.
fn encode_call(call: &Call) -> anyhow::Result<Self::Call>;
}

arg_enum! {
#[derive(Debug)]
/// Bridge to encode call for.
pub enum EncodeCallBridge {
MillauToRialto,
RialtoToMillau,
}
}

impl EncodeCallBridge {
fn bridge_instance_index(&self) -> u8 {
match self {
Self::MillauToRialto => MILLAU_TO_RIALTO_INDEX,
Self::RialtoToMillau => RIALTO_TO_MILLAU_INDEX,
}
}
}

pub const RIALTO_TO_MILLAU_INDEX: u8 = 0;
pub const MILLAU_TO_RIALTO_INDEX: u8 = 0;

macro_rules! select_bridge {
($bridge: expr, $generic: tt) => {
match $bridge {
EncodeCallBridge::MillauToRialto => {
type Source = relay_millau_client::Millau;
type Target = relay_rialto_client::Rialto;

$generic
}
EncodeCallBridge::RialtoToMillau => {
type Source = relay_rialto_client::Rialto;
type Target = relay_millau_client::Millau;

$generic
}
}
};
}

impl EncodeCall {
fn encode(&mut self) -> anyhow::Result<HexBytes> {
select_bridge!(self.bridge, {
preprocess_call::<Source, Target>(&mut self.call, self.bridge.bridge_instance_index());
let call = Source::encode_call(&self.call)?;

let encoded = HexBytes::encode(&call);

log::info!(target: "bridge", "Generated {} call: {:#?}", Source::NAME, call);
log::info!(target: "bridge", "Weight of {} call: {}", Source::NAME, call.get_dispatch_info().weight);
log::info!(target: "bridge", "Encoded {} call: {:?}", Source::NAME, encoded);

Ok(encoded)
})
}

/// Run the command.
pub async fn run(mut self) -> anyhow::Result<()> {
println!("{:?}", self.encode()?);
Ok(())
}
}

/// Prepare the call to be passed to [`CliEncodeCall::encode_call`].
///
/// This function will fill in all optional and missing pieces and will make sure that
/// values are converted to bridge-specific ones.
///
/// Most importantly, the method will fill-in [`bridge_instance_index`] parameter for
/// target-chain specific calls.
pub(crate) fn preprocess_call<Source: CliEncodeCall + CliChain, Target: CliEncodeCall>(
call: &mut Call,
bridge_instance: u8,
) {
match *call {
Call::Raw { .. } => {}
Call::Remark {
ref remark_size,
ref mut remark_payload,
} => {
if remark_payload.0.is_empty() {
*remark_payload = HexBytes(generate_remark_payload(
&remark_size,
compute_maximal_message_arguments_size(Source::max_extrinsic_size(), Target::max_extrinsic_size()),
));
}
}
Call::Transfer { ref mut recipient, .. } => {
recipient.enforce_chain::<Source>();
}
Call::BridgeSendMessage {
ref mut bridge_instance_index,
..
} => {
*bridge_instance_index = bridge_instance;
}
};
}

fn generate_remark_payload(remark_size: &Option<ExplicitOrMaximal<usize>>, maximal_allowed_size: u32) -> Vec<u8> {
match remark_size {
Some(ExplicitOrMaximal::Explicit(remark_size)) => vec![0; *remark_size],
Some(ExplicitOrMaximal::Maximal) => vec![0; maximal_allowed_size as _],
None => format!(
"Unix time: {}",
std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_secs(),
)
.as_bytes()
.to_vec(),
}
}

pub(crate) fn compute_maximal_message_arguments_size(
maximal_source_extrinsic_size: u32,
maximal_target_extrinsic_size: u32,
) -> u32 {
// assume that both signed extensions and other arguments fit 1KB
let service_tx_bytes_on_source_chain = 1024;
let maximal_source_extrinsic_size = maximal_source_extrinsic_size - service_tx_bytes_on_source_chain;
let maximal_call_size =
bridge_runtime_common::messages::target::maximal_incoming_message_size(maximal_target_extrinsic_size);
let maximal_call_size = if maximal_call_size > maximal_source_extrinsic_size {
maximal_source_extrinsic_size
} else {
maximal_call_size
};

// bytes in Call encoding that are used to encode everything except arguments
let service_bytes = 1 + 1 + 4;
maximal_call_size - service_bytes
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn should_encode_transfer_call() {
// given
let mut encode_call = EncodeCall::from_iter(vec![
"encode-call",
"RialtoToMillau",
"transfer",
"--amount",
"12345",
"--recipient",
"5sauUXUfPjmwxSgmb3tZ5d6yx24eZX4wWJ2JtVUBaQqFbvEU",
]);

// when
let hex = encode_call.encode().unwrap();

// then
assert_eq!(
format!("{:?}", hex),
"0x0d00d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27de5c0"
);
}
}
47 changes: 24 additions & 23 deletions bridges/relays/bin-substrate/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use frame_support::weights::Weight;
use sp_runtime::app_crypto::Ss58Codec;
use structopt::{clap::arg_enum, StructOpt};

pub(crate) mod encode_call;

mod derive_account;
mod init_bridge;
mod relay_headers;
Expand Down Expand Up @@ -63,7 +65,7 @@ pub enum Command {
///
/// The call can be used either as message payload or can be wrapped into a transaction
/// and executed on the chain directly.
EncodeCall(EncodeCall),
EncodeCall(encode_call::EncodeCall),
/// Generate SCALE-encoded `MessagePayload` object that can be sent over selected bridge.
///
/// The `MessagePayload` can be then fed to `Messages::send_message` function and sent over
Expand Down Expand Up @@ -109,23 +111,6 @@ impl SendMessage {
}
}

/// A call to encode.
#[derive(StructOpt)]
pub enum EncodeCall {
#[structopt(flatten)]
RialtoMillau(rialto_millau::EncodeCall),
}

impl EncodeCall {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
match self {
Self::RialtoMillau(arg) => arg.run().await?,
}
Ok(())
}
}

/// A `MessagePayload` to encode.
#[derive(StructOpt)]
pub enum EncodeMessagePayload {
Expand Down Expand Up @@ -273,9 +258,6 @@ pub trait CliChain: relay_substrate_client::Chain {
/// Numeric value of SS58 format.
fn ss58_format() -> u16;

/// Parse CLI call and encode it to be dispatched on this specific chain.
fn encode_call(call: crate::rialto_millau::cli::Call) -> Result<Self::Call, String>;

/// Construct message payload to be sent over the bridge.
fn encode_message(message: crate::rialto_millau::cli::MessagePayload) -> Result<Self::MessagePayload, String>;

Expand Down Expand Up @@ -304,7 +286,7 @@ impl std::str::FromStr for HexLaneId {
}

/// Nicer formatting for raw bytes vectors.
#[derive(Encode, Decode)]
#[derive(Default, Encode, Decode)]
pub struct HexBytes(pub Vec<u8>);

impl std::str::FromStr for HexBytes {
Expand All @@ -317,7 +299,13 @@ impl std::str::FromStr for HexBytes {

impl std::fmt::Debug for HexBytes {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "0x{}", hex::encode(&self.0))
write!(fmt, "0x{}", self)
}
}

impl std::fmt::Display for HexBytes {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(fmt, "{}", hex::encode(&self.0))
}
}

Expand Down Expand Up @@ -470,4 +458,17 @@ mod tests {

assert_eq!(actual, expected)
}

#[test]
fn hex_bytes_display_matches_from_str_for_clap() {
// given
let hex = HexBytes(vec![1, 2, 3, 4]);
let display = format!("{}", hex);

// when
let hex2: HexBytes = display.parse().unwrap();

// then
assert_eq!(hex.0, hex2.0);
}
}
Loading

0 comments on commit 0d16f3d

Please sign in to comment.