CLI: Relay Messages (#858)

* Relay Messages.

* Fix docs.

* Fix copyright date.

* copyright increment

Co-authored-by: adoerr <0xad@gmx.net>
This commit is contained in:
Tomasz Drwięga
2021-04-02 21:24:01 +02:00
committed by Bastian Köcher
parent 025a9cad59
commit da3f94d99f
4 changed files with 103 additions and 126 deletions
+2 -18
View File
@@ -27,6 +27,7 @@ use structopt::{clap::arg_enum, StructOpt};
mod init_bridge;
mod relay_headers;
mod relay_messages;
/// Parse relay CLI args.
pub fn parse_args() -> Command {
@@ -46,7 +47,7 @@ pub enum Command {
///
/// Ties up to `Messages` pallets on both chains and starts relaying messages.
/// Requires the header relay to be already running.
RelayMessages(RelayMessages),
RelayMessages(relay_messages::RelayMessages),
/// Initialize on-chain bridge pallet with current header data.
///
/// Sends initialization transaction to bootstrap the bridge with current finalized block data.
@@ -90,23 +91,6 @@ impl Command {
}
}
/// Start message relayer process.
#[derive(StructOpt)]
pub enum RelayMessages {
#[structopt(flatten)]
RialtoMillau(rialto_millau::RelayMessages),
}
impl RelayMessages {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
match self {
Self::RialtoMillau(arg) => arg.run().await?,
}
Ok(())
}
}
/// Send bridge message.
#[derive(StructOpt)]
pub enum SendMessage {
@@ -0,0 +1,97 @@
// 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::{
CliChain, HexLaneId, PrometheusParams, SourceConnectionParams, SourceSigningParams, TargetConnectionParams,
TargetSigningParams,
};
use structopt::{clap::arg_enum, StructOpt};
/// Start messages relayer process.
#[derive(StructOpt)]
pub struct RelayMessages {
/// A bridge instance to relay messages for.
#[structopt(possible_values = &RelayMessagesBridge::variants(), case_insensitive = true)]
bridge: RelayMessagesBridge,
/// Hex-encoded lane id that should be served by the relay. Defaults to `00000000`.
#[structopt(long, default_value = "00000000")]
lane: HexLaneId,
#[structopt(flatten)]
source: SourceConnectionParams,
#[structopt(flatten)]
source_sign: SourceSigningParams,
#[structopt(flatten)]
target: TargetConnectionParams,
#[structopt(flatten)]
target_sign: TargetSigningParams,
#[structopt(flatten)]
prometheus_params: PrometheusParams,
}
arg_enum! {
#[derive(Debug)]
/// Headers relay bridge.
pub enum RelayMessagesBridge {
MillauToRialto,
RialtoToMillau,
}
}
macro_rules! select_bridge {
($bridge: expr, $generic: tt) => {
match $bridge {
RelayMessagesBridge::MillauToRialto => {
type Source = relay_millau_client::Millau;
type Target = relay_rialto_client::Rialto;
use crate::rialto_millau::millau_messages_to_rialto::run;
$generic
}
RelayMessagesBridge::RialtoToMillau => {
type Source = relay_rialto_client::Rialto;
type Target = relay_millau_client::Millau;
use crate::rialto_millau::rialto_messages_to_millau::run;
$generic
}
}
};
}
impl RelayMessages {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
select_bridge!(self.bridge, {
let source_client = crate::rialto_millau::source_chain_client::<Source>(self.source).await?;
let source_sign =
Source::source_signing_params(self.source_sign).map_err(|e| anyhow::format_err!("{}", e))?;
let target_client = crate::rialto_millau::target_chain_client::<Target>(self.target).await?;
let target_sign =
Target::target_signing_params(self.target_sign).map_err(|e| anyhow::format_err!("{}", e))?;
run(
source_client,
source_sign,
target_client,
target_sign,
self.lane.into(),
self.prometheus_params.into(),
)
.await
.map_err(|e| anyhow::format_err!("{}", e))
})
}
}
@@ -1,4 +1,4 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// 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
@@ -20,57 +20,10 @@ use frame_support::weights::Weight;
use structopt::StructOpt;
use crate::cli::{
AccountId, Balance, ExplicitOrMaximal, HexBytes, HexLaneId, Origins, PrometheusParams, SourceConnectionParams,
SourceSigningParams, TargetConnectionParams, TargetSigningParams,
AccountId, Balance, ExplicitOrMaximal, HexBytes, HexLaneId, Origins, SourceConnectionParams, SourceSigningParams,
TargetSigningParams,
};
/// Start message relayer process.
///
/// TODO [#855] Move to separate module.
#[derive(StructOpt)]
pub enum RelayMessages {
/// Serve given lane of Millau -> Rialto messages.
MillauToRialto {
#[structopt(flatten)]
source: SourceConnectionParams,
#[structopt(flatten)]
source_sign: SourceSigningParams,
#[structopt(flatten)]
target: TargetConnectionParams,
#[structopt(flatten)]
target_sign: TargetSigningParams,
#[structopt(flatten)]
prometheus_params: PrometheusParams,
/// Hex-encoded lane id that should be served by the relay. Defaults to `00000000`.
#[structopt(long, default_value = "00000000")]
lane: HexLaneId,
},
/// Serve given lane of Rialto -> Millau messages.
RialtoToMillau {
#[structopt(flatten)]
source: SourceConnectionParams,
#[structopt(flatten)]
source_sign: SourceSigningParams,
#[structopt(flatten)]
target: TargetConnectionParams,
#[structopt(flatten)]
target_sign: TargetSigningParams,
#[structopt(flatten)]
prometheus_params: PrometheusParams,
/// Hex-encoded lane id that should be served by the relay. Defaults to `00000000`.
#[structopt(long, default_value = "00000000")]
lane: HexLaneId,
},
}
impl RelayMessages {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
super::run_relay_messages(self).await.map_err(format_err)?;
Ok(())
}
}
/// Send bridge message.
///
/// TODO [#855] Move to separate module.
@@ -1,4 +1,4 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// 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
@@ -44,63 +44,6 @@ use sp_runtime::{traits::IdentifyAccount, MultiSigner};
use sp_version::RuntimeVersion;
use std::fmt::Debug;
async fn run_relay_messages(command: cli::RelayMessages) -> Result<(), String> {
match command {
cli::RelayMessages::MillauToRialto {
source,
source_sign,
target,
target_sign,
prometheus_params,
lane,
} => {
type Source = Millau;
type Target = Rialto;
let source_client = source_chain_client::<Source>(source).await?;
let source_sign = Source::source_signing_params(source_sign)?;
let target_client = target_chain_client::<Target>(target).await?;
let target_sign = Target::target_signing_params(target_sign)?;
millau_messages_to_rialto::run(
source_client,
source_sign,
target_client,
target_sign,
lane.into(),
prometheus_params.into(),
)
.await
}
cli::RelayMessages::RialtoToMillau {
source,
source_sign,
target,
target_sign,
prometheus_params,
lane,
} => {
type Source = Rialto;
type Target = Millau;
let source_client = source_chain_client::<Source>(source).await?;
let source_sign = Source::source_signing_params(source_sign)?;
let target_client = target_chain_client::<Target>(target).await?;
let target_sign = Target::target_signing_params(target_sign)?;
rialto_messages_to_millau::run(
source_client,
source_sign,
target_client,
target_sign,
lane.into(),
prometheus_params.into(),
)
.await
}
}
}
async fn run_send_message(command: cli::SendMessage) -> Result<(), String> {
match command {
cli::SendMessage::MillauToRialto {