Skip to content

Commit

Permalink
CLI: Send Message (paritytech#886)
Browse files Browse the repository at this point in the history
* Send Message WiP

* It compiles.

* Add tests.

* Use common macro.

* Nicer balance display.

* Get rid of redundant send_message_call function.

* Fix clippy.

Co-authored-by: Svyatoslav Nikolsky <svyatonik@gmail.com>
  • Loading branch information
2 people authored and serban300 committed Apr 9, 2024
1 parent d251f86 commit 727af70
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 368 deletions.
2 changes: 2 additions & 0 deletions bridges/relays/bin-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0" }
futures = "0.3.12"
hex = "0.4"
log = "0.4.14"
num-format = "0.4"
num-traits = "0.2"
paste = "1.0"
structopt = "0.3"
Expand Down Expand Up @@ -56,3 +57,4 @@ sp-version = { git = "https://github.com/paritytech/substrate", branch = "master

[dev-dependencies]
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" }
hex-literal = "0.3"
18 changes: 18 additions & 0 deletions bridges/relays/bin-substrate/src/cli/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,43 @@ macro_rules! select_full_bridge {
#[allow(dead_code)]
type Target = relay_rialto_client::Rialto;

// Derive-account
#[allow(unused_imports)]
use bp_millau::derive_account_from_rialto_id as derive_account;

// Relay-messages
#[allow(unused_imports)]
use crate::rialto_millau::millau_messages_to_rialto::run as relay_messages;

// Send-message
#[allow(unused_imports)]
use bp_millau::TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD as ESTIMATE_MESSAGE_FEE_METHOD;
// Send-message
#[allow(unused_imports)]
use millau_runtime::rialto_account_ownership_digest as account_ownership_digest;

$generic
}
FullBridge::RialtoToMillau => {
type Source = relay_rialto_client::Rialto;
#[allow(dead_code)]
type Target = relay_millau_client::Millau;

// Derive-account
#[allow(unused_imports)]
use bp_rialto::derive_account_from_millau_id as derive_account;

// Relay-messages
#[allow(unused_imports)]
use crate::rialto_millau::rialto_messages_to_millau::run as relay_messages;

// Send-message
#[allow(unused_imports)]
use bp_rialto::TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD as ESTIMATE_MESSAGE_FEE_METHOD;
// Send-message
#[allow(unused_imports)]
use rialto_runtime::millau_account_ownership_digest as account_ownership_digest;

$generic
}
}
Expand Down
6 changes: 3 additions & 3 deletions bridges/relays/bin-substrate/src/cli/init_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ impl InitBridge {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
select_bridge!(self.bridge, {
let source_client = self.source.into_client::<Source>().await?;
let target_client = self.target.into_client::<Target>().await?;
let target_sign = self.target_sign.into_keypair::<Target>()?;
let source_client = self.source.to_client::<Source>().await?;
let target_client = self.target.to_client::<Target>().await?;
let target_sign = self.target_sign.to_keypair::<Target>()?;

crate::headers_initialize::initialize(
source_client,
Expand Down
41 changes: 16 additions & 25 deletions bridges/relays/bin-substrate/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod derive_account;
mod init_bridge;
mod relay_headers;
mod relay_messages;
mod send_message;

/// Parse relay CLI args.
pub fn parse_args() -> Command {
Expand Down Expand Up @@ -62,7 +63,7 @@ pub enum Command {
/// Allows interacting with the bridge by sending messages over `Messages` component.
/// The message is being sent to the source chain, delivered to the target chain and dispatched
/// there.
SendMessage(SendMessage),
SendMessage(send_message::SendMessage),
/// Generate SCALE-encoded `Call` for choosen network.
///
/// The call can be used either as message payload or can be wrapped into a transaction
Expand Down Expand Up @@ -96,23 +97,6 @@ impl Command {
}
}

/// Send bridge message.
#[derive(StructOpt)]
pub enum SendMessage {
#[structopt(flatten)]
RialtoMillau(rialto_millau::SendMessage),
}

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

/// Estimate Delivery & Dispatch Fee command.
#[derive(StructOpt)]
pub enum EstimateFee {
Expand Down Expand Up @@ -143,9 +127,16 @@ arg_enum! {
}

/// Generic balance type.
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Balance(pub u128);

impl std::fmt::Display for Balance {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
use num_format::{Locale, ToFormattedString};
write!(fmt, "{}", self.0.to_formatted_string(&Locale::en))
}
}

impl std::str::FromStr for Balance {
type Err = <u128 as std::str::FromStr>::Err;

Expand Down Expand Up @@ -251,7 +242,7 @@ pub trait CliChain: relay_substrate_client::Chain {
}

/// Lane id.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct HexLaneId(pub LaneId);

impl From<HexLaneId> for LaneId {
Expand Down Expand Up @@ -330,7 +321,7 @@ impl From<PrometheusParams> for relay_utils::metrics::MetricsParams {
}

/// Either explicit or maximal allowed value.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum ExplicitOrMaximal<V> {
/// User has explicitly specified argument value.
Explicit(V),
Expand Down Expand Up @@ -388,7 +379,7 @@ macro_rules! declare_chain_options {

impl [<$chain SigningParams>] {
/// Parse signing params into chain-specific KeyPair.
pub fn into_keypair<Chain: CliChain>(self) -> anyhow::Result<Chain::KeyPair> {
pub fn to_keypair<Chain: CliChain>(&self) -> anyhow::Result<Chain::KeyPair> {
use sp_core::crypto::Pair;
Chain::KeyPair::from_string(
&self.[<$chain_prefix _signer>],
Expand All @@ -399,11 +390,11 @@ macro_rules! declare_chain_options {

impl [<$chain ConnectionParams>] {
/// Convert connection params into Substrate client.
pub async fn into_client<Chain: CliChain>(
self,
pub async fn to_client<Chain: CliChain>(
&self,
) -> anyhow::Result<relay_substrate_client::Client<Chain>> {
Ok(relay_substrate_client::Client::new(relay_substrate_client::ConnectionParams {
host: self.[<$chain_prefix _host>],
host: self.[<$chain_prefix _host>].clone(),
port: self.[<$chain_prefix _port>],
secure: self.[<$chain_prefix _secure>],
})
Expand Down
8 changes: 5 additions & 3 deletions bridges/relays/bin-substrate/src/cli/relay_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ macro_rules! select_bridge {
type Source = relay_rialto_client::Rialto;
type Target = relay_millau_client::Millau;
type Finality = crate::rialto_millau::rialto_headers_to_millau::RialtoFinalityToMillau;

$generic
}
RelayHeadersBridge::WestendToMillau => {
type Source = relay_westend_client::Westend;
type Target = relay_millau_client::Millau;
type Finality = crate::rialto_millau::westend_headers_to_millau::WestendFinalityToMillau;

$generic
}
}
Expand All @@ -75,9 +77,9 @@ impl RelayHeaders {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
select_bridge!(self.bridge, {
let source_client = self.source.into_client::<Source>().await?;
let target_client = self.target.into_client::<Target>().await?;
let target_sign = self.target_sign.into_keypair::<Target>()?;
let source_client = self.source.to_client::<Source>().await?;
let target_client = self.target.to_client::<Target>().await?;
let target_sign = self.target_sign.to_keypair::<Target>()?;
let metrics_params = Finality::customize_metrics(self.prometheus_params.into())?;

crate::finality_pipeline::run(
Expand Down
8 changes: 4 additions & 4 deletions bridges/relays/bin-substrate/src/cli/relay_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ impl RelayMessages {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
select_full_bridge!(self.bridge, {
let source_client = self.source.into_client::<Source>().await?;
let source_sign = self.source_sign.into_keypair::<Source>()?;
let target_client = self.target.into_client::<Target>().await?;
let target_sign = self.target_sign.into_keypair::<Target>()?;
let source_client = self.source.to_client::<Source>().await?;
let source_sign = self.source_sign.to_keypair::<Source>()?;
let target_client = self.target.to_client::<Target>().await?;
let target_sign = self.target_sign.to_keypair::<Target>()?;

relay_messages(
source_client,
Expand Down
Loading

0 comments on commit 727af70

Please sign in to comment.