Skip to content

Commit

Permalink
dry-run mode for init bridge command (#1690)
Browse files Browse the repository at this point in the history
* `dry-run` mode for init bridge command

* fix clippy
  • Loading branch information
bkontur committed Dec 1, 2022
1 parent e411d42 commit 0625544
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion primitives/messages/src/target_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub trait MessageDispatch<AccountId> {
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.
///
Expand Down
17 changes: 13 additions & 4 deletions relays/bin-substrate/src/cli/init_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.

use async_trait::async_trait;
use codec::Encode;

use crate::{
chains::{
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -77,6 +81,7 @@ where
let source_client = data.source.into_client::<Self::Source>().await?;
let target_client = data.target.into_client::<Self::Target>().await?;
let target_sign = data.target_sign.to_keypair::<Self::Target>()?;
let dry_run = data.dry_run;

let (spec_version, transaction_version) = target_client.simple_runtime_version().await?;
substrate_relay_helper::finality::initialize::initialize::<Self::Engine, _, _, _>(
Expand All @@ -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;

Expand Down
16 changes: 14 additions & 2 deletions relays/lib-substrate-relay/src/finality/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub async fn initialize<
target_transactions_signer: TargetChain::AccountId,
target_signing_data: SignParam<TargetChain>,
prepare_initialize_transaction: F,
dry_run: bool,
) where
F: FnOnce(
TargetChain::Index,
Expand All @@ -54,6 +55,7 @@ pub async fn initialize<
target_transactions_signer,
target_signing_data,
prepare_initialize_transaction,
dry_run,
)
.await;

Expand Down Expand Up @@ -88,6 +90,7 @@ async fn do_initialize<
target_transactions_signer: TargetChain::AccountId,
target_signing_data: SignParam<TargetChain>,
prepare_initialize_transaction: F,
dry_run: bool,
) -> Result<
Option<TargetChain::Hash>,
Error<SourceChain::Hash, <SourceChain::Header as HeaderT>::Number>,
Expand All @@ -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?;
Expand All @@ -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(
"Not submitting extrinsic in `dry-run` mode!".to_string(),
))
} else {
tx
}
},
)
.await
Expand Down

0 comments on commit 0625544

Please sign in to comment.