From 0bf4d520a4f6ba722760efea91cffe0e548c936f Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Thu, 1 Dec 2022 14:22:30 +0100 Subject: [PATCH 1/2] `dry-run` mode for init bridge command --- primitives/messages/src/target_chain.rs | 2 +- relays/bin-substrate/src/cli/init_bridge.rs | 17 +++++++++++++---- .../src/finality/initialize.rs | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/primitives/messages/src/target_chain.rs b/primitives/messages/src/target_chain.rs index 63c8b639e6c..9c6b60e1e15 100644 --- a/primitives/messages/src/target_chain.rs +++ b/primitives/messages/src/target_chain.rs @@ -90,7 +90,7 @@ pub trait MessageDispatch { type DispatchPayload: Decode; /// Fine-grained result of single message dispatch (for better diagnostic purposes) - type DispatchLevelResult: Clone + Decode + sp_std::fmt::Debug + Eq; + type DispatchLevelResult: Clone + sp_std::fmt::Debug + Eq; /// Estimate dispatch weight. /// diff --git a/relays/bin-substrate/src/cli/init_bridge.rs b/relays/bin-substrate/src/cli/init_bridge.rs index 54a3c74a4b6..1fc2e12af8f 100644 --- a/relays/bin-substrate/src/cli/init_bridge.rs +++ b/relays/bin-substrate/src/cli/init_bridge.rs @@ -15,6 +15,7 @@ // along with Parity Bridges Common. If not, see . use async_trait::async_trait; +use codec::Encode; use crate::{ chains::{ @@ -46,6 +47,9 @@ pub struct InitBridge { target: TargetConnectionParams, #[structopt(flatten)] target_sign: TargetSigningParams, + /// Generates all required data, but does not submit extrinsic + #[structopt(long)] + dry_run: bool, } #[derive(Debug, EnumString, EnumVariantNames)] @@ -77,6 +81,7 @@ where let source_client = data.source.into_client::().await?; let target_client = data.target.into_client::().await?; let target_sign = data.target_sign.to_keypair::()?; + let dry_run = data.dry_run; let (spec_version, transaction_version) = target_client.simple_runtime_version().await?; substrate_relay_helper::finality::initialize::initialize::( @@ -90,11 +95,15 @@ where signer: target_sign, }, move |transaction_nonce, initialization_data| { - Ok(UnsignedTransaction::new( - Self::encode_init_bridge(initialization_data).into(), - transaction_nonce, - )) + let call = Self::encode_init_bridge(initialization_data); + log::info!( + target: "bridge", + "Initialize bridge call encoded as hex string: {:?}", + format!("0x{}", hex::encode(&call.encode())) + ); + Ok(UnsignedTransaction::new(call.into(), transaction_nonce)) }, + dry_run, ) .await; diff --git a/relays/lib-substrate-relay/src/finality/initialize.rs b/relays/lib-substrate-relay/src/finality/initialize.rs index 6ff185800cc..7fa2ff64535 100644 --- a/relays/lib-substrate-relay/src/finality/initialize.rs +++ b/relays/lib-substrate-relay/src/finality/initialize.rs @@ -40,6 +40,7 @@ pub async fn initialize< target_transactions_signer: TargetChain::AccountId, target_signing_data: SignParam, prepare_initialize_transaction: F, + dry_run: bool, ) where F: FnOnce( TargetChain::Index, @@ -54,6 +55,7 @@ pub async fn initialize< target_transactions_signer, target_signing_data, prepare_initialize_transaction, + dry_run, ) .await; @@ -88,6 +90,7 @@ async fn do_initialize< target_transactions_signer: TargetChain::AccountId, target_signing_data: SignParam, prepare_initialize_transaction: F, + dry_run: bool, ) -> Result< Option, Error::Number>, @@ -110,7 +113,9 @@ where SourceChain::NAME, TargetChain::NAME, ); - return Ok(None) + if !dry_run { + return Ok(None) + } } let initialization_data = E::prepare_initialization_data(source_client).await?; @@ -127,7 +132,14 @@ where target_transactions_signer, target_signing_data, move |_, transaction_nonce| { - prepare_initialize_transaction(transaction_nonce, initialization_data) + let tx = prepare_initialize_transaction(transaction_nonce, initialization_data); + if dry_run { + Err(SubstrateError::Custom(format!( + "Not submitting extrinsic in `dry-run` mode!" + ))) + } else { + tx + } }, ) .await From 3821151c929f556c4e7efededf16429718dd5675 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Thu, 1 Dec 2022 22:06:03 +0100 Subject: [PATCH 2/2] fix clippy --- relays/bin-substrate/src/cli/init_bridge.rs | 2 +- relays/lib-substrate-relay/src/finality/initialize.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/relays/bin-substrate/src/cli/init_bridge.rs b/relays/bin-substrate/src/cli/init_bridge.rs index 1fc2e12af8f..52a3a6fce5a 100644 --- a/relays/bin-substrate/src/cli/init_bridge.rs +++ b/relays/bin-substrate/src/cli/init_bridge.rs @@ -99,7 +99,7 @@ where log::info!( target: "bridge", "Initialize bridge call encoded as hex string: {:?}", - format!("0x{}", hex::encode(&call.encode())) + format!("0x{}", hex::encode(call.encode())) ); Ok(UnsignedTransaction::new(call.into(), transaction_nonce)) }, diff --git a/relays/lib-substrate-relay/src/finality/initialize.rs b/relays/lib-substrate-relay/src/finality/initialize.rs index 7fa2ff64535..938f1330b2e 100644 --- a/relays/lib-substrate-relay/src/finality/initialize.rs +++ b/relays/lib-substrate-relay/src/finality/initialize.rs @@ -134,9 +134,9 @@ where move |_, transaction_nonce| { let tx = prepare_initialize_transaction(transaction_nonce, initialization_data); if dry_run { - Err(SubstrateError::Custom(format!( - "Not submitting extrinsic in `dry-run` mode!" - ))) + Err(SubstrateError::Custom( + "Not submitting extrinsic in `dry-run` mode!".to_string(), + )) } else { tx }