referrerpolicy=no-referrer-when-downgrade

substrate_relay_helper/cli/
init_bridge.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Bridges Common is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Primitives for exposing the bridge initialization functionality in the CLI.
18
19use async_trait::async_trait;
20use codec::Encode;
21
22use crate::{
23	cli::{bridge::CliBridgeBase, chain_schema::*},
24	finality_base::engine::Engine,
25};
26use bp_runtime::Chain as ChainBase;
27use clap::Parser;
28use relay_substrate_client::{AccountKeyPairOf, Chain, UnsignedTransaction};
29use sp_core::Pair;
30
31/// Bridge initialization params.
32#[derive(Parser)]
33pub struct InitBridgeParams {
34	#[command(flatten)]
35	source: SourceConnectionParams,
36	#[command(flatten)]
37	target: TargetConnectionParams,
38	#[command(flatten)]
39	target_sign: TargetSigningParams,
40	/// Generates all required data, but does not submit extrinsic
41	#[arg(long)]
42	dry_run: bool,
43}
44
45/// Trait used for bridge initializing.
46#[async_trait]
47pub trait BridgeInitializer: CliBridgeBase
48where
49	<Self::Target as ChainBase>::AccountId: From<<AccountKeyPairOf<Self::Target> as Pair>::Public>,
50{
51	/// The finality engine used by the source chain.
52	type Engine: Engine<Self::Source>;
53
54	/// Get the encoded call to init the bridge.
55	fn encode_init_bridge(
56		init_data: <Self::Engine as Engine<Self::Source>>::InitializationData,
57	) -> <Self::Target as Chain>::Call;
58
59	/// Initialize the bridge.
60	async fn init_bridge(data: InitBridgeParams) -> anyhow::Result<()> {
61		let source_client = data.source.into_client::<Self::Source>().await?;
62		let target_client = data.target.into_client::<Self::Target>().await?;
63		let target_sign = data.target_sign.to_keypair::<Self::Target>()?;
64		let dry_run = data.dry_run;
65
66		crate::finality::initialize::initialize::<Self::Engine, _, _, _>(
67			source_client,
68			target_client.clone(),
69			target_sign,
70			move |transaction_nonce, initialization_data| {
71				let call = Self::encode_init_bridge(initialization_data);
72				log::info!(
73					target: "bridge",
74					"Initialize bridge call encoded as hex string: {:?}",
75					format!("0x{}", hex::encode(call.encode()))
76				);
77				Ok(UnsignedTransaction::new(call.into(), transaction_nonce))
78			},
79			dry_run,
80		)
81		.await;
82
83		Ok(())
84	}
85}