De-duplicate signing/connection params. (#861)

Co-authored-by: Hernando Castano <hernando@hcastano.com>
This commit is contained in:
Tomasz Drwięga
2021-04-05 18:59:06 +02:00
committed by Bastian Köcher
parent 1928e2b870
commit 1dbba1b95b
5 changed files with 51 additions and 76 deletions
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
use crate::cli::{CliChain, SourceConnectionParams, TargetConnectionParams, TargetSigningParams};
use crate::cli::{SourceConnectionParams, TargetConnectionParams, TargetSigningParams};
use bp_runtime::Chain as ChainBase;
use codec::Encode;
use pallet_bridge_grandpa::InitializationData;
@@ -108,10 +108,9 @@ impl InitBridge {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
select_bridge!(self.bridge, {
let source_client = crate::rialto_millau::source_chain_client::<Source>(self.source).await?;
let target_client = crate::rialto_millau::target_chain_client::<Target>(self.target).await?;
let target_sign =
Target::target_signing_params(self.target_sign).map_err(|e| anyhow::format_err!("{}", e))?;
let source_client = self.source.into_client::<Source>().await?;
let target_client = self.target.into_client::<Target>().await?;
let target_sign = self.target_sign.into_keypair::<Target>()?;
crate::headers_initialize::initialize(
source_client,
+26 -16
View File
@@ -281,20 +281,6 @@ pub trait CliChain: relay_substrate_client::Chain {
/// Maximal extrinsic weight (from the runtime).
fn max_extrinsic_weight() -> Weight;
/// Convert CLI signing parameters of `Source` chain into a `KeyPair` instance.
fn source_signing_params(params: SourceSigningParams) -> Result<Self::KeyPair, String> {
use sp_core::crypto::Pair;
Self::KeyPair::from_string(&params.source_signer, params.source_signer_password.as_deref())
.map_err(|e| format!("Failed to parse source-signer: {:?}", e))
}
/// Convert CLI signing parameters of `Target` chain into a `KeyPair` instance.
fn target_signing_params(params: TargetSigningParams) -> Result<Self::KeyPair, String> {
use sp_core::crypto::Pair;
Self::KeyPair::from_string(&params.target_signer, params.target_signer_password.as_deref())
.map_err(|e| format!("Failed to parse target-signer: {:?}", e))
}
}
/// Lane id.
@@ -425,12 +411,36 @@ macro_rules! declare_chain_options {
#[structopt(long)]
pub [<$chain_prefix _signer_password>]: Option<String>,
}
impl [<$chain SigningParams>] {
/// Parse signing params into chain-specific KeyPair.
pub fn into_keypair<Chain: CliChain>(self) -> anyhow::Result<Chain::KeyPair> {
use sp_core::crypto::Pair;
Chain::KeyPair::from_string(
&self.[<$chain_prefix _signer>],
self.[<$chain_prefix _signer_password>].as_deref()
).map_err(|e| anyhow::format_err!("{:?}", e))
}
}
impl [<$chain ConnectionParams>] {
/// Convert connection params into Substrate client.
pub async fn into_client<Chain: CliChain>(
self,
) -> anyhow::Result<relay_substrate_client::Client<Chain>> {
Ok(relay_substrate_client::Client::new(relay_substrate_client::ConnectionParams {
host: self.[<$chain_prefix _host>],
port: self.[<$chain_prefix _port>],
secure: self.[<$chain_prefix _secure>],
})
.await?
)
}
}
}
};
}
// TODO [#852] Use structop renames instead of different fields.
// TODO [#852] Add Into<ConnectionParams>?
declare_chain_options!(Source, source);
declare_chain_options!(Target, target);
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
use crate::cli::{CliChain, PrometheusParams, SourceConnectionParams, TargetConnectionParams, TargetSigningParams};
use crate::cli::{PrometheusParams, SourceConnectionParams, TargetConnectionParams, TargetSigningParams};
use structopt::{clap::arg_enum, StructOpt};
/// Start headers relayer process.
@@ -74,10 +74,9 @@ impl RelayHeaders {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
select_bridge!(self.bridge, {
let source_client = crate::rialto_millau::source_chain_client::<Source>(self.source).await?;
let target_client = crate::rialto_millau::target_chain_client::<Target>(self.target).await?;
let target_sign =
Target::target_signing_params(self.target_sign).map_err(|e| anyhow::format_err!("{}", e))?;
let source_client = self.source.into_client::<Source>().await?;
let target_client = self.target.into_client::<Target>().await?;
let target_sign = self.target_sign.into_keypair::<Target>()?;
crate::finality_pipeline::run(
Finality::new(target_client.clone(), target_sign),
@@ -15,7 +15,7 @@
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
use crate::cli::{
CliChain, HexLaneId, PrometheusParams, SourceConnectionParams, SourceSigningParams, TargetConnectionParams,
HexLaneId, PrometheusParams, SourceConnectionParams, SourceSigningParams, TargetConnectionParams,
TargetSigningParams,
};
use structopt::{clap::arg_enum, StructOpt};
@@ -75,12 +75,10 @@ impl RelayMessages {
/// Run the command.
pub async fn run(self) -> anyhow::Result<()> {
select_bridge!(self.bridge, {
let source_client = crate::rialto_millau::source_chain_client::<Source>(self.source).await?;
let source_sign =
Source::source_signing_params(self.source_sign).map_err(|e| anyhow::format_err!("{}", e))?;
let target_client = crate::rialto_millau::target_chain_client::<Target>(self.target).await?;
let target_sign =
Target::target_signing_params(self.target_sign).map_err(|e| anyhow::format_err!("{}", e))?;
let source_client = self.source.into_client::<Source>().await?;
let source_sign = self.source_sign.into_keypair::<Source>()?;
let target_client = self.target.into_client::<Target>().await?;
let target_sign = self.target_sign.into_keypair::<Target>()?;
run(
source_client,