CLI: Send Message (#886)

* 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>
This commit is contained in:
Tomasz Drwięga
2021-04-13 12:37:08 +02:00
committed by Bastian Köcher
parent 7c639687b6
commit 9f01c459bd
9 changed files with 336 additions and 368 deletions
@@ -29,244 +29,20 @@ pub type MillauClient = relay_substrate_client::Client<Millau>;
pub type RialtoClient = relay_substrate_client::Client<Rialto>;
use crate::cli::{
bridge::{MILLAU_TO_RIALTO_INDEX, RIALTO_TO_MILLAU_INDEX},
bridge,
encode_call::{self, Call, CliEncodeCall},
encode_message, CliChain, ExplicitOrMaximal, HexBytes, Origins,
encode_message, CliChain, HexBytes,
};
use codec::{Decode, Encode};
use frame_support::weights::{GetDispatchInfo, Weight};
use pallet_bridge_dispatch::{CallOrigin, MessagePayload};
use relay_millau_client::Millau;
use relay_rialto_client::Rialto;
use relay_substrate_client::{Chain, TransactionSignScheme};
use relay_substrate_client::Chain;
use relay_westend_client::Westend;
use sp_core::{Bytes, Pair};
use sp_runtime::{traits::IdentifyAccount, MultiSigner};
use sp_version::RuntimeVersion;
use std::fmt::Debug;
async fn run_send_message(command: cli::SendMessage) -> Result<(), String> {
match command {
cli::SendMessage::MillauToRialto {
source,
source_sign,
target_sign,
lane,
mut message,
dispatch_weight,
fee,
origin,
..
} => {
type Source = Millau;
type Target = Rialto;
let account_ownership_digest = |target_call, source_account_id| {
millau_runtime::rialto_account_ownership_digest(
&target_call,
source_account_id,
Target::RUNTIME_VERSION.spec_version,
)
};
let estimate_message_fee_method = bp_rialto::TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD;
let fee = fee.map(|x| x.cast());
let send_message_call = |lane, payload, fee| {
millau_runtime::Call::BridgeRialtoMessages(millau_runtime::MessagesCall::send_message(
lane, payload, fee,
))
};
let source_client = source.into_client::<Source>().await.map_err(format_err)?;
let source_sign = source_sign.into_keypair::<Source>().map_err(format_err)?;
let target_sign = target_sign.into_keypair::<Target>().map_err(format_err)?;
encode_call::preprocess_call::<Source, Target>(&mut message, MILLAU_TO_RIALTO_INDEX);
let target_call = Target::encode_call(&message).map_err(|e| e.to_string())?;
let payload = {
let target_call_weight = prepare_call_dispatch_weight(
dispatch_weight,
ExplicitOrMaximal::Explicit(target_call.get_dispatch_info().weight),
compute_maximal_message_dispatch_weight(Target::max_extrinsic_weight()),
);
let source_sender_public: MultiSigner = source_sign.public().into();
let source_account_id = source_sender_public.into_account();
message_payload(
Target::RUNTIME_VERSION.spec_version,
target_call_weight,
match origin {
Origins::Source => CallOrigin::SourceAccount(source_account_id),
Origins::Target => {
let digest = account_ownership_digest(&target_call, source_account_id.clone());
let target_origin_public = target_sign.public();
let digest_signature = target_sign.sign(&digest);
CallOrigin::TargetAccount(
source_account_id,
target_origin_public.into(),
digest_signature.into(),
)
}
},
&target_call,
)
};
let dispatch_weight = payload.weight;
let lane = lane.into();
let fee = get_fee(fee, || {
estimate_message_delivery_and_dispatch_fee(
&source_client,
estimate_message_fee_method,
lane,
payload.clone(),
)
})
.await?;
source_client
.submit_signed_extrinsic(source_sign.public().into(), |transaction_nonce| {
let send_message_call = send_message_call(lane, payload, fee);
let signed_source_call = Source::sign_transaction(
*source_client.genesis_hash(),
&source_sign,
transaction_nonce,
send_message_call,
)
.encode();
log::info!(
target: "bridge",
"Sending message to {}. Size: {}. Dispatch weight: {}. Fee: {}",
Target::NAME,
signed_source_call.len(),
dispatch_weight,
fee,
);
log::info!(
target: "bridge",
"Signed {} Call: {:?}",
Source::NAME,
HexBytes::encode(&signed_source_call)
);
Bytes(signed_source_call)
})
.await?;
}
cli::SendMessage::RialtoToMillau {
source,
source_sign,
target_sign,
lane,
mut message,
dispatch_weight,
fee,
origin,
..
} => {
type Source = Rialto;
type Target = Millau;
let account_ownership_digest = |target_call, source_account_id| {
rialto_runtime::millau_account_ownership_digest(
&target_call,
source_account_id,
Target::RUNTIME_VERSION.spec_version,
)
};
let estimate_message_fee_method = bp_millau::TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD;
let fee = fee.map(|x| x.0);
let send_message_call = |lane, payload, fee| {
rialto_runtime::Call::BridgeMillauMessages(rialto_runtime::MessagesCall::send_message(
lane, payload, fee,
))
};
let source_client = source.into_client::<Source>().await.map_err(format_err)?;
let source_sign = source_sign.into_keypair::<Source>().map_err(format_err)?;
let target_sign = target_sign.into_keypair::<Target>().map_err(format_err)?;
encode_call::preprocess_call::<Source, Target>(&mut message, RIALTO_TO_MILLAU_INDEX);
let target_call = Target::encode_call(&message).map_err(|e| e.to_string())?;
let payload = {
let target_call_weight = prepare_call_dispatch_weight(
dispatch_weight,
ExplicitOrMaximal::Explicit(target_call.get_dispatch_info().weight),
compute_maximal_message_dispatch_weight(Target::max_extrinsic_weight()),
);
let source_sender_public: MultiSigner = source_sign.public().into();
let source_account_id = source_sender_public.into_account();
message_payload(
Target::RUNTIME_VERSION.spec_version,
target_call_weight,
match origin {
Origins::Source => CallOrigin::SourceAccount(source_account_id),
Origins::Target => {
let digest = account_ownership_digest(&target_call, source_account_id.clone());
let target_origin_public = target_sign.public();
let digest_signature = target_sign.sign(&digest);
CallOrigin::TargetAccount(
source_account_id,
target_origin_public.into(),
digest_signature.into(),
)
}
},
&target_call,
)
};
let dispatch_weight = payload.weight;
let lane = lane.into();
let fee = get_fee(fee, || {
estimate_message_delivery_and_dispatch_fee(
&source_client,
estimate_message_fee_method,
lane,
payload.clone(),
)
})
.await?;
source_client
.submit_signed_extrinsic(source_sign.public().into(), |transaction_nonce| {
let send_message_call = send_message_call(lane, payload, fee);
let signed_source_call = Source::sign_transaction(
*source_client.genesis_hash(),
&source_sign,
transaction_nonce,
send_message_call,
)
.encode();
log::info!(
target: "bridge",
"Sending message to {}. Size: {}. Dispatch weight: {}. Fee: {}",
Target::NAME,
signed_source_call.len(),
dispatch_weight,
fee,
);
log::info!(
target: "bridge",
"Signed {} Call: {:?}",
Source::NAME,
HexBytes::encode(&signed_source_call)
);
Bytes(signed_source_call)
})
.await?;
}
}
Ok(())
}
async fn run_estimate_fee(cmd: cli::EstimateFee) -> Result<(), String> {
match cmd {
cli::EstimateFee::RialtoToMillau { source, lane, payload } => {
@@ -275,7 +51,7 @@ async fn run_estimate_fee(cmd: cli::EstimateFee) -> Result<(), String> {
let estimate_message_fee_method = bp_millau::TO_MILLAU_ESTIMATE_MESSAGE_FEE_METHOD;
let source_client = source.into_client::<Source>().await.map_err(format_err)?;
let source_client = source.to_client::<Source>().await.map_err(format_err)?;
let lane = lane.into();
let payload = Source::encode_message(payload)?;
@@ -291,7 +67,7 @@ async fn run_estimate_fee(cmd: cli::EstimateFee) -> Result<(), String> {
let estimate_message_fee_method = bp_rialto::TO_RIALTO_ESTIMATE_MESSAGE_FEE_METHOD;
let source_client = source.into_client::<Source>().await.map_err(format_err)?;
let source_client = source.to_client::<Source>().await.map_err(format_err)?;
let lane = lane.into();
let payload = Source::encode_message(payload)?;
@@ -306,7 +82,7 @@ async fn run_estimate_fee(cmd: cli::EstimateFee) -> Result<(), String> {
Ok(())
}
async fn estimate_message_delivery_and_dispatch_fee<Fee: Decode, C: Chain, P: Encode>(
pub(crate) async fn estimate_message_delivery_and_dispatch_fee<Fee: Decode, C: Chain, P: Encode>(
client: &relay_substrate_client::Client<C>,
estimate_fee_method: &str,
lane: bp_messages::LaneId,
@@ -320,7 +96,7 @@ async fn estimate_message_delivery_and_dispatch_fee<Fee: Decode, C: Chain, P: En
Ok(decoded_response)
}
fn message_payload<SAccountId, TPublic, TSignature>(
pub(crate) fn message_payload<SAccountId, TPublic, TSignature>(
spec_version: u32,
weight: Weight,
origin: CallOrigin<SAccountId, TPublic, TSignature>,
@@ -357,35 +133,7 @@ where
}
}
fn prepare_call_dispatch_weight(
user_specified_dispatch_weight: Option<ExplicitOrMaximal<Weight>>,
weight_from_pre_dispatch_call: ExplicitOrMaximal<Weight>,
maximal_allowed_weight: Weight,
) -> Weight {
match user_specified_dispatch_weight.unwrap_or(weight_from_pre_dispatch_call) {
ExplicitOrMaximal::Explicit(weight) => weight,
ExplicitOrMaximal::Maximal => maximal_allowed_weight,
}
}
async fn get_fee<Fee, F, R, E>(fee: Option<Fee>, f: F) -> Result<Fee, String>
where
Fee: Decode,
F: FnOnce() -> R,
R: std::future::Future<Output = Result<Option<Fee>, E>>,
E: Debug,
{
match fee {
Some(fee) => Ok(fee),
None => match f().await {
Ok(Some(fee)) => Ok(fee),
Ok(None) => Err("Failed to estimate message fee. Message is too heavy?".into()),
Err(error) => Err(format!("Failed to estimate message fee: {:?}", error)),
},
}
}
fn compute_maximal_message_dispatch_weight(maximal_extrinsic_weight: Weight) -> Weight {
pub(crate) fn compute_maximal_message_dispatch_weight(maximal_extrinsic_weight: Weight) -> Weight {
bridge_runtime_common::messages::target::maximal_incoming_message_dispatch_weight(maximal_extrinsic_weight)
}
@@ -409,7 +157,7 @@ impl CliEncodeCall for Millau {
fee,
bridge_instance_index,
} => match *bridge_instance_index {
MILLAU_TO_RIALTO_INDEX => {
bridge::MILLAU_TO_RIALTO_INDEX => {
let payload = Decode::decode(&mut &*payload.0)?;
millau_runtime::Call::BridgeRialtoMessages(millau_runtime::MessagesCall::send_message(
lane.0,
@@ -452,7 +200,7 @@ impl CliChain for Millau {
sender.enforce_chain::<Source>();
let spec_version = Target::RUNTIME_VERSION.spec_version;
let origin = CallOrigin::SourceAccount(sender.raw_id());
encode_call::preprocess_call::<Source, Target>(&mut call, MILLAU_TO_RIALTO_INDEX);
encode_call::preprocess_call::<Source, Target>(&mut call, bridge::MILLAU_TO_RIALTO_INDEX);
let call = Target::encode_call(&call).map_err(|e| e.to_string())?;
let weight = call.get_dispatch_info().weight;
@@ -482,7 +230,7 @@ impl CliEncodeCall for Rialto {
fee,
bridge_instance_index,
} => match *bridge_instance_index {
RIALTO_TO_MILLAU_INDEX => {
bridge::RIALTO_TO_MILLAU_INDEX => {
let payload = Decode::decode(&mut &*payload.0)?;
rialto_runtime::Call::BridgeMillauMessages(rialto_runtime::MessagesCall::send_message(
lane.0, payload, fee.0,
@@ -522,7 +270,7 @@ impl CliChain for Rialto {
sender.enforce_chain::<Source>();
let spec_version = Target::RUNTIME_VERSION.spec_version;
let origin = CallOrigin::SourceAccount(sender.raw_id());
encode_call::preprocess_call::<Source, Target>(&mut call, RIALTO_TO_MILLAU_INDEX);
encode_call::preprocess_call::<Source, Target>(&mut call, bridge::RIALTO_TO_MILLAU_INDEX);
let call = Target::encode_call(&call).map_err(|e| e.to_string())?;
let weight = call.get_dispatch_info().weight;
@@ -559,6 +307,7 @@ fn format_err(e: anyhow::Error) -> String {
mod tests {
use super::*;
use bp_messages::source_chain::TargetHeaderChain;
use relay_substrate_client::TransactionSignScheme;
use sp_core::Pair;
use sp_runtime::traits::{IdentifyAccount, Verify};