CLI: Encode Call & Multiple Bridge Instances. (#859)

* 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>
This commit is contained in:
Tomasz Drwięga
2021-04-05 19:48:47 +02:00
committed by Bastian Köcher
parent 1dbba1b95b
commit 5f16df28ed
4 changed files with 367 additions and 236 deletions
+24 -23
View File
@@ -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;
@@ -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
@@ -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 {
@@ -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>;
@@ -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 {
@@ -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))
}
}
@@ -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);
}
}