mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 02:51:01 +00:00
Make relay CLI generic (#849)
* Start generalizing rialto-millau commands. * cargo fmt --all * Introduce generic balance. * Unify message payloads. * cargo fmt --all * init - generic * Attempt to unify send message. * Start moving things around. * cargo fmt --all * Move init-bridge. * cargo fmt --all * Improve UX of bridge argument. * Fix clippy. * Fix docs and scripts. * Add docs. * Apply suggestions from code review Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com> * Fix copyright. * Add issue numbers. * More todos. * Update comments. Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
This commit is contained in:
committed by
Bastian Köcher
parent
b6d034afaf
commit
904b9f4da5
@@ -0,0 +1,137 @@
|
||||
// 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, SourceConnectionParams, TargetConnectionParams, TargetSigningParams};
|
||||
use bp_runtime::Chain as ChainBase;
|
||||
use codec::Encode;
|
||||
use pallet_bridge_grandpa::InitializationData;
|
||||
use relay_substrate_client::{Chain, TransactionSignScheme};
|
||||
use sp_core::{Bytes, Pair};
|
||||
use structopt::{clap::arg_enum, StructOpt};
|
||||
|
||||
/// Initialize bridge pallet.
|
||||
#[derive(StructOpt)]
|
||||
pub struct InitBridge {
|
||||
/// A bridge instance to initalize.
|
||||
#[structopt(possible_values = &InitBridgeName::variants(), case_insensitive = true)]
|
||||
bridge: InitBridgeName,
|
||||
#[structopt(flatten)]
|
||||
source: SourceConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
target: TargetConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
target_sign: TargetSigningParams,
|
||||
}
|
||||
|
||||
// TODO [#851] Use kebab-case.
|
||||
arg_enum! {
|
||||
#[derive(Debug)]
|
||||
/// Bridge to initialize.
|
||||
pub enum InitBridgeName {
|
||||
MillauToRialto,
|
||||
RialtoToMillau,
|
||||
WestendToMillau,
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! select_bridge {
|
||||
($bridge: expr, $generic: tt) => {
|
||||
match $bridge {
|
||||
InitBridgeName::MillauToRialto => {
|
||||
type Source = relay_millau_client::Millau;
|
||||
type Target = relay_rialto_client::Rialto;
|
||||
|
||||
fn encode_init_bridge(
|
||||
init_data: InitializationData<<Source as ChainBase>::Header>,
|
||||
) -> <Target as Chain>::Call {
|
||||
rialto_runtime::SudoCall::sudo(Box::new(
|
||||
rialto_runtime::BridgeGrandpaMillauCall::initialize(init_data).into(),
|
||||
))
|
||||
.into()
|
||||
}
|
||||
|
||||
$generic
|
||||
}
|
||||
InitBridgeName::RialtoToMillau => {
|
||||
type Source = relay_rialto_client::Rialto;
|
||||
type Target = relay_millau_client::Millau;
|
||||
|
||||
fn encode_init_bridge(
|
||||
init_data: InitializationData<<Source as ChainBase>::Header>,
|
||||
) -> <Target as Chain>::Call {
|
||||
let initialize_call = millau_runtime::BridgeGrandpaRialtoCall::<
|
||||
millau_runtime::Runtime,
|
||||
millau_runtime::RialtoGrandpaInstance,
|
||||
>::initialize(init_data);
|
||||
millau_runtime::SudoCall::sudo(Box::new(initialize_call.into())).into()
|
||||
}
|
||||
|
||||
$generic
|
||||
}
|
||||
InitBridgeName::WestendToMillau => {
|
||||
type Source = relay_westend_client::Westend;
|
||||
type Target = relay_millau_client::Millau;
|
||||
|
||||
fn encode_init_bridge(
|
||||
init_data: InitializationData<<Source as ChainBase>::Header>,
|
||||
) -> <Target as Chain>::Call {
|
||||
// at Westend -> Millau initialization we're not using sudo, because otherwise our deployments
|
||||
// may fail, because we need to initialize both Rialto -> Millau and Westend -> Millau bridge.
|
||||
// => since there's single possible sudo account, one of transaction may fail with duplicate nonce error
|
||||
millau_runtime::BridgeGrandpaWestendCall::<
|
||||
millau_runtime::Runtime,
|
||||
millau_runtime::WestendGrandpaInstance,
|
||||
>::initialize(init_data)
|
||||
.into()
|
||||
}
|
||||
|
||||
$generic
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl InitBridge {
|
||||
/// 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 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))?;
|
||||
|
||||
crate::headers_initialize::initialize(
|
||||
source_client,
|
||||
target_client.clone(),
|
||||
target_sign.public().into(),
|
||||
move |transaction_nonce, initialization_data| {
|
||||
Bytes(
|
||||
Target::sign_transaction(
|
||||
*target_client.genesis_hash(),
|
||||
&target_sign,
|
||||
transaction_nonce,
|
||||
encode_init_bridge(initialization_data),
|
||||
)
|
||||
.encode(),
|
||||
)
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
// 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/>.
|
||||
|
||||
//! Deal with CLI args of substrate-to-substrate relay.
|
||||
|
||||
use std::convert::TryInto;
|
||||
|
||||
use crate::rialto_millau::cli as rialto_millau;
|
||||
use bp_messages::LaneId;
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::weights::Weight;
|
||||
use sp_runtime::app_crypto::Ss58Codec;
|
||||
use structopt::{clap::arg_enum, StructOpt};
|
||||
|
||||
mod init_bridge;
|
||||
mod relay_headers;
|
||||
|
||||
/// Parse relay CLI args.
|
||||
pub fn parse_args() -> Command {
|
||||
Command::from_args()
|
||||
}
|
||||
|
||||
/// Substrate-to-Substrate bridge utilities.
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(about = "Substrate-to-Substrate relay")]
|
||||
pub enum Command {
|
||||
/// Start headers relay between two chains.
|
||||
///
|
||||
/// The on-chain bridge component should have been already initialized with
|
||||
/// `init-bridge` sub-command.
|
||||
RelayHeaders(relay_headers::RelayHeaders),
|
||||
/// Start messages relay between two chains.
|
||||
///
|
||||
/// Ties up to `Messages` pallets on both chains and starts relaying messages.
|
||||
/// Requires the header relay to be already running.
|
||||
RelayMessages(RelayMessages),
|
||||
/// Initialize on-chain bridge pallet with current header data.
|
||||
///
|
||||
/// Sends initialization transaction to bootstrap the bridge with current finalized block data.
|
||||
InitBridge(init_bridge::InitBridge),
|
||||
/// Send custom message over the bridge.
|
||||
///
|
||||
/// 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),
|
||||
/// 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 `Messages::send_message` function and sent over
|
||||
/// the bridge.
|
||||
EncodeMessagePayload(EncodeMessagePayload),
|
||||
/// Estimate Delivery and Dispatch Fee required for message submission to messages pallet.
|
||||
EstimateFee(EstimateFee),
|
||||
/// Given a source chain `AccountId`, derive the corresponding `AccountId` for the target chain.
|
||||
DeriveAccount(DeriveAccount),
|
||||
}
|
||||
|
||||
impl Command {
|
||||
/// Run the command.
|
||||
pub async fn run(self) -> anyhow::Result<()> {
|
||||
match self {
|
||||
Self::RelayHeaders(arg) => arg.run().await?,
|
||||
Self::RelayMessages(arg) => arg.run().await?,
|
||||
Self::InitBridge(arg) => arg.run().await?,
|
||||
Self::SendMessage(arg) => arg.run().await?,
|
||||
Self::EncodeCall(arg) => arg.run().await?,
|
||||
Self::EncodeMessagePayload(arg) => arg.run().await?,
|
||||
Self::EstimateFee(arg) => arg.run().await?,
|
||||
Self::DeriveAccount(arg) => arg.run().await?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
#[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(())
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
#[structopt(flatten)]
|
||||
RialtoMillau(rialto_millau::EncodeMessagePayload),
|
||||
}
|
||||
|
||||
impl EncodeMessagePayload {
|
||||
/// 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 {
|
||||
#[structopt(flatten)]
|
||||
RialtoMillau(rialto_millau::EstimateFee),
|
||||
}
|
||||
|
||||
impl EstimateFee {
|
||||
/// Run the command.
|
||||
pub async fn run(self) -> anyhow::Result<()> {
|
||||
match self {
|
||||
Self::RialtoMillau(arg) => arg.run().await?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Given a source chain `AccountId`, derive the corresponding `AccountId` for the target chain.
|
||||
///
|
||||
/// The (derived) target chain `AccountId` is going to be used as dispatch origin of the call
|
||||
/// that has been sent over the bridge.
|
||||
/// This account can also be used to receive target-chain funds (or other form of ownership),
|
||||
/// since messages sent over the bridge will be able to spend these.
|
||||
#[derive(StructOpt)]
|
||||
pub enum DeriveAccount {
|
||||
#[structopt(flatten)]
|
||||
RialtoMillau(rialto_millau::DeriveAccount),
|
||||
}
|
||||
|
||||
impl DeriveAccount {
|
||||
/// Run the command.
|
||||
pub async fn run(self) -> anyhow::Result<()> {
|
||||
match self {
|
||||
Self::RialtoMillau(arg) => arg.run().await?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
arg_enum! {
|
||||
#[derive(Debug)]
|
||||
/// The origin to use when dispatching the message on the target chain.
|
||||
///
|
||||
/// - `Target` uses account existing on the target chain (requires target private key).
|
||||
/// - `Origin` uses account derived from the source-chain account.
|
||||
pub enum Origins {
|
||||
Target,
|
||||
Source,
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic balance type.
|
||||
#[derive(Debug)]
|
||||
pub struct Balance(pub u128);
|
||||
|
||||
impl std::str::FromStr for Balance {
|
||||
type Err = <u128 as std::str::FromStr>::Err;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(Self(s.parse()?))
|
||||
}
|
||||
}
|
||||
|
||||
impl Balance {
|
||||
/// Cast balance to `u64` type, panicking if it's too large.
|
||||
pub fn cast(&self) -> u64 {
|
||||
self.0.try_into().expect("Balance is too high for this chain.")
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic account id with custom parser.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AccountId {
|
||||
account: sp_runtime::AccountId32,
|
||||
ss58_format: sp_core::crypto::Ss58AddressFormat,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AccountId {
|
||||
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(fmt, "{}", self.account.to_ss58check_with_version(self.ss58_format))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for AccountId {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let (account, ss58_format) = sp_runtime::AccountId32::from_ss58check_with_version(s)
|
||||
.map_err(|err| format!("Unable to decode SS58 address: {:?}", err))?;
|
||||
Ok(Self { account, ss58_format })
|
||||
}
|
||||
}
|
||||
|
||||
const SS58_FORMAT_PROOF: &str = "u16 -> Ss58Format is infallible; qed";
|
||||
|
||||
impl AccountId {
|
||||
/// Create new SS58-formatted address from raw account id.
|
||||
pub fn from_raw<T: CliChain>(account: sp_runtime::AccountId32) -> Self {
|
||||
Self {
|
||||
account,
|
||||
ss58_format: T::ss58_format().try_into().expect(SS58_FORMAT_PROOF),
|
||||
}
|
||||
}
|
||||
|
||||
/// Enforces formatting account to be for given [`CliChain`] type.
|
||||
///
|
||||
/// This will change the `ss58format` of the account to match the requested one.
|
||||
/// Note that a warning will be produced in case the current format does not match
|
||||
/// the requested one, but the conversion always succeeds.
|
||||
pub fn enforce_chain<T: CliChain>(&mut self) {
|
||||
let original = self.clone();
|
||||
self.ss58_format = T::ss58_format().try_into().expect(SS58_FORMAT_PROOF);
|
||||
log::debug!("{} SS58 format: {} (RAW: {})", self, self.ss58_format, self.account);
|
||||
if original.ss58_format != self.ss58_format {
|
||||
log::warn!(
|
||||
target: "bridge",
|
||||
"Address {} does not seem to match {}'s SS58 format (got: {}, expected: {}).\nConverted to: {}",
|
||||
original,
|
||||
T::NAME,
|
||||
original.ss58_format,
|
||||
self.ss58_format,
|
||||
self,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the raw (no SS58-prefixed) account id.
|
||||
pub fn raw_id(&self) -> sp_runtime::AccountId32 {
|
||||
self.account.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Bridge-supported network definition.
|
||||
///
|
||||
/// Used to abstract away CLI commands.
|
||||
pub trait CliChain: relay_substrate_client::Chain {
|
||||
/// Chain's current version of the runtime.
|
||||
const RUNTIME_VERSION: sp_version::RuntimeVersion;
|
||||
|
||||
/// Crypto keypair type used to send messages.
|
||||
///
|
||||
/// In case of chains supporting multiple cryptos, pick one used by the CLI.
|
||||
type KeyPair: sp_core::crypto::Pair;
|
||||
|
||||
/// Bridge Message Payload type.
|
||||
///
|
||||
/// TODO [#854] This should be removed in favour of target-specifc types.
|
||||
type MessagePayload;
|
||||
|
||||
/// 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>;
|
||||
|
||||
/// Maximal extrinsic weight (from the runtime).
|
||||
fn max_extrinsic_weight() -> Weight;
|
||||
|
||||
/// Convert CLI signing parameters of `Source` chain into a `KeyPair` instance.
|
||||
fn source_signing_params(params: SourceSigningParams) -> Result<Self::KeyPair, String> {
|
||||
use sp_core::crypto::Pair;
|
||||
Self::KeyPair::from_string(¶ms.source_signer, params.source_signer_password.as_deref())
|
||||
.map_err(|e| format!("Failed to parse source-signer: {:?}", e))
|
||||
}
|
||||
|
||||
/// Convert CLI signing parameters of `Target` chain into a `KeyPair` instance.
|
||||
fn target_signing_params(params: TargetSigningParams) -> Result<Self::KeyPair, String> {
|
||||
use sp_core::crypto::Pair;
|
||||
Self::KeyPair::from_string(¶ms.target_signer, params.target_signer_password.as_deref())
|
||||
.map_err(|e| format!("Failed to parse target-signer: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
/// Lane id.
|
||||
#[derive(Debug)]
|
||||
pub struct HexLaneId(pub LaneId);
|
||||
|
||||
impl From<HexLaneId> for LaneId {
|
||||
fn from(lane_id: HexLaneId) -> LaneId {
|
||||
lane_id.0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for HexLaneId {
|
||||
type Err = hex::FromHexError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let mut lane_id = LaneId::default();
|
||||
hex::decode_to_slice(s, &mut lane_id)?;
|
||||
Ok(HexLaneId(lane_id))
|
||||
}
|
||||
}
|
||||
|
||||
/// Nicer formatting for raw bytes vectors.
|
||||
#[derive(Encode, Decode)]
|
||||
pub struct HexBytes(pub Vec<u8>);
|
||||
|
||||
impl std::str::FromStr for HexBytes {
|
||||
type Err = hex::FromHexError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(Self(hex::decode(s)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::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())
|
||||
}
|
||||
}
|
||||
|
||||
/// Prometheus metrics params.
|
||||
#[derive(StructOpt)]
|
||||
pub struct PrometheusParams {
|
||||
/// Do not expose a Prometheus metric endpoint.
|
||||
#[structopt(long)]
|
||||
pub no_prometheus: bool,
|
||||
/// Expose Prometheus endpoint at given interface.
|
||||
#[structopt(long, default_value = "127.0.0.1")]
|
||||
pub prometheus_host: String,
|
||||
/// Expose Prometheus endpoint at given port.
|
||||
#[structopt(long, default_value = "9616")]
|
||||
pub prometheus_port: u16,
|
||||
}
|
||||
|
||||
impl From<PrometheusParams> for Option<relay_utils::metrics::MetricsParams> {
|
||||
fn from(cli_params: PrometheusParams) -> Option<relay_utils::metrics::MetricsParams> {
|
||||
if !cli_params.no_prometheus {
|
||||
Some(relay_utils::metrics::MetricsParams {
|
||||
host: cli_params.prometheus_host,
|
||||
port: cli_params.prometheus_port,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Either explicit or maximal allowed value.
|
||||
#[derive(Debug)]
|
||||
pub enum ExplicitOrMaximal<V> {
|
||||
/// User has explicitly specified argument value.
|
||||
Explicit(V),
|
||||
/// Maximal allowed value for this argument.
|
||||
Maximal,
|
||||
}
|
||||
|
||||
impl<V: std::str::FromStr> std::str::FromStr for ExplicitOrMaximal<V>
|
||||
where
|
||||
V::Err: std::fmt::Debug,
|
||||
{
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.to_lowercase() == "max" {
|
||||
return Ok(ExplicitOrMaximal::Maximal);
|
||||
}
|
||||
|
||||
V::from_str(s)
|
||||
.map(ExplicitOrMaximal::Explicit)
|
||||
.map_err(|e| format!("Failed to parse '{:?}'. Expected 'max' or explicit value", e))
|
||||
}
|
||||
}
|
||||
|
||||
/// Create chain-specific set of configuration objects: connection parameters,
|
||||
/// signing parameters and bridge initialisation parameters.
|
||||
#[macro_export]
|
||||
macro_rules! declare_chain_options {
|
||||
($chain:ident, $chain_prefix:ident) => {
|
||||
paste::item! {
|
||||
#[doc = $chain " connection params."]
|
||||
#[derive(StructOpt)]
|
||||
pub struct [<$chain ConnectionParams>] {
|
||||
#[doc = "Connect to " $chain " node at given host."]
|
||||
#[structopt(long, default_value = "127.0.0.1")]
|
||||
pub [<$chain_prefix _host>]: String,
|
||||
#[doc = "Connect to " $chain " node websocket server at given port."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _port>]: u16,
|
||||
#[doc = "Use secure websocket connection."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _secure>]: bool,
|
||||
}
|
||||
|
||||
#[doc = $chain " signing params."]
|
||||
#[derive(StructOpt)]
|
||||
pub struct [<$chain SigningParams>] {
|
||||
#[doc = "The SURI of secret key to use when transactions are submitted to the " $chain " node."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _signer>]: String,
|
||||
#[doc = "The password for the SURI of secret key to use when transactions are submitted to the " $chain " node."]
|
||||
#[structopt(long)]
|
||||
pub [<$chain_prefix _signer_password>]: Option<String>,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// TODO [#852] Use structop renames instead of different fields.
|
||||
// TODO [#852] Add Into<ConnectionParams>?
|
||||
declare_chain_options!(Source, source);
|
||||
declare_chain_options!(Target, target);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::str::FromStr;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_format_addresses_with_ss58_format() {
|
||||
// given
|
||||
let rialto1 = "5sauUXUfPjmwxSgmb3tZ5d6yx24eZX4wWJ2JtVUBaQqFbvEU";
|
||||
let rialto2 = "5rERgaT1Z8nM3et2epA5i1VtEBfp5wkhwHtVE8HK7BRbjAH2";
|
||||
let millau1 = "752paRyW1EGfq9YLTSSqcSJ5hqnBDidBmaftGhBo8fy6ypW9";
|
||||
let millau2 = "74GNQjmkcfstRftSQPJgMREchqHM56EvAUXRc266cZ1NYVW5";
|
||||
|
||||
let expected = vec![rialto1, rialto2, millau1, millau2];
|
||||
|
||||
// when
|
||||
let parsed = expected
|
||||
.iter()
|
||||
.map(|s| AccountId::from_str(s).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let actual = parsed.iter().map(|a| format!("{}", a)).collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(actual, expected)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
// 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, PrometheusParams, SourceConnectionParams, TargetConnectionParams, TargetSigningParams};
|
||||
use structopt::{clap::arg_enum, StructOpt};
|
||||
|
||||
/// Start headers relayer process.
|
||||
#[derive(StructOpt)]
|
||||
pub struct RelayHeaders {
|
||||
/// A bridge instance to relay headers for.
|
||||
#[structopt(possible_values = &RelayHeadersBridge::variants(), case_insensitive = true)]
|
||||
bridge: RelayHeadersBridge,
|
||||
#[structopt(flatten)]
|
||||
source: SourceConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
target: TargetConnectionParams,
|
||||
#[structopt(flatten)]
|
||||
target_sign: TargetSigningParams,
|
||||
#[structopt(flatten)]
|
||||
prometheus_params: PrometheusParams,
|
||||
}
|
||||
|
||||
// TODO [#851] Use kebab-case.
|
||||
arg_enum! {
|
||||
#[derive(Debug)]
|
||||
/// Headers relay bridge.
|
||||
pub enum RelayHeadersBridge {
|
||||
MillauToRialto,
|
||||
RialtoToMillau,
|
||||
WestendToMillau,
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! select_bridge {
|
||||
($bridge: expr, $generic: tt) => {
|
||||
match $bridge {
|
||||
RelayHeadersBridge::MillauToRialto => {
|
||||
type Source = relay_millau_client::Millau;
|
||||
type Target = relay_rialto_client::Rialto;
|
||||
type Finality = crate::rialto_millau::millau_headers_to_rialto::MillauFinalityToRialto;
|
||||
|
||||
$generic
|
||||
}
|
||||
RelayHeadersBridge::RialtoToMillau => {
|
||||
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
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl RelayHeaders {
|
||||
/// 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 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))?;
|
||||
|
||||
crate::finality_pipeline::run(
|
||||
Finality::new(target_client.clone(), target_sign),
|
||||
source_client,
|
||||
target_client,
|
||||
self.prometheus_params.into(),
|
||||
)
|
||||
.await
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user