From ea399e1eebaef4053235ec9836c90e6b49728937 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Fri, 6 Dec 2024 14:21:40 +0100 Subject: [PATCH] dedupe client definition between relayer and node --- crates/context/config/src/client.rs | 218 ++++++++++++++-------------- crates/merod/src/cli/relay.rs | 78 +--------- 2 files changed, 117 insertions(+), 179 deletions(-) diff --git a/crates/context/config/src/client.rs b/crates/context/config/src/client.rs index c7c8ba112..3378e0368 100644 --- a/crates/context/config/src/client.rs +++ b/crates/context/config/src/client.rs @@ -13,18 +13,17 @@ pub mod relayer; pub mod transport; pub mod utils; -use config::{ClientConfig, ClientSelectedSigner, Credentials}; +use config::{ClientConfig, ClientSelectedSigner, Credentials, LocalConfig}; use protocol::{icp, near, starknet, Protocol}; use transport::{Both, Transport, TransportArguments, TransportRequest, UnsupportedProtocol}; -pub type AnyTransport = Either< - relayer::RelayerTransport, - Both< - near::NearTransport<'static>, - Both, icp::IcpTransport<'static>>, - >, +pub type LocalTransports = Both< + near::NearTransport<'static>, + Both, icp::IcpTransport<'static>>, >; +pub type AnyTransport = Either; + #[derive(Clone, Debug)] pub struct Client { transport: T, @@ -41,113 +40,111 @@ impl Client { pub fn from_config(config: &ClientConfig) -> Self { let transport = match config.signer.selected { ClientSelectedSigner::Relayer => { - // If the selected signer is Relayer, use the Left variant. Either::Left(relayer::RelayerTransport::new(&relayer::RelayerConfig { url: config.signer.relayer.url.clone(), })) } + ClientSelectedSigner::Local => { + let local_client = Self::from_local_config(&config.signer.local); - ClientSelectedSigner::Local => Either::Right(Both { - left: near::NearTransport::new(&near::NearConfig { - networks: config - .signer - .local - .near - .iter() - .map(|(network, config)| { - let (account_id, secret_key) = match &config.credentials { - Credentials::Near(credentials) => ( - credentials.account_id.clone(), - credentials.secret_key.clone(), - ), - Credentials::Starknet(_) => { - panic!("Expected Near credentials but got something else.") - } - Credentials::Icp(_) => { - panic!("Expected Near credentials but got something else.") - } - }; - ( - network.clone().into(), - near::NetworkConfig { - rpc_url: config.rpc_url.clone(), - account_id, - access_key: secret_key, - }, - ) - }) - .collect(), - }), - right: Both { - left: starknet::StarknetTransport::new(&starknet::StarknetConfig { - networks: config - .signer - .local - .starknet - .iter() - .map(|(network, config)| { - let (account_id, secret_key) = match &config.credentials { - Credentials::Starknet(credentials) => { - (credentials.account_id, credentials.secret_key) - } - Credentials::Near(_) => { - panic!( - "Expected Starknet credentials but got something else." - ) - } - Credentials::Icp(_) => { - panic!( - "Expected Starknet credentials but got something else." - ) - } - }; - ( - network.clone().into(), - starknet::NetworkConfig { - rpc_url: config.rpc_url.clone(), - account_id, - access_key: secret_key, - }, - ) - }) - .collect(), - }), - right: icp::IcpTransport::new(&icp::IcpConfig { - networks: config - .signer - .local - .icp - .iter() - .map(|(network, config)| { - let (account_id, secret_key) = match &config.credentials { - Credentials::Icp(credentials) => ( - credentials.account_id.clone(), - credentials.secret_key.clone(), - ), - Credentials::Near(_) => { - panic!("Expected ICP credentials but got something else.") - } - Credentials::Starknet(_) => { - panic!("Expected ICP credentials but got something else.") - } - }; - ( - network.clone().into(), - icp::NetworkConfig { - rpc_url: config.rpc_url.clone(), - account_id, - secret_key, - }, - ) - }) - .collect(), - }), - }, - }), + Either::Right(local_client.transport) + } }; Self::new(transport) } + + pub fn from_local_config(config: &LocalConfig) -> Client { + let near_transport = near::NearTransport::new(&near::NearConfig { + networks: config + .near + .iter() + .map(|(network, config)| { + let (account_id, secret_key) = match &config.credentials { + Credentials::Near(credentials) => ( + credentials.account_id.clone(), + credentials.secret_key.clone(), + ), + Credentials::Starknet(_) | Credentials::Icp(_) => { + panic!("Expected Near credentials but got {:?}", config.credentials) + } + }; + ( + network.clone().into(), + near::NetworkConfig { + rpc_url: config.rpc_url.clone(), + account_id, + access_key: secret_key, + }, + ) + }) + .collect(), + }); + + let starknet_transport = starknet::StarknetTransport::new(&starknet::StarknetConfig { + networks: config + .starknet + .iter() + .map(|(network, config)| { + let (account_id, secret_key) = match &config.credentials { + Credentials::Starknet(credentials) => { + (credentials.account_id, credentials.secret_key) + } + Credentials::Near(_) | Credentials::Icp(_) => { + panic!( + "Expected Starknet credentials but got {:?}", + config.credentials + ) + } + }; + ( + network.clone().into(), + starknet::NetworkConfig { + rpc_url: config.rpc_url.clone(), + account_id, + access_key: secret_key, + }, + ) + }) + .collect(), + }); + + let icp_transport = icp::IcpTransport::new(&icp::IcpConfig { + networks: config + .icp + .iter() + .map(|(network, config)| { + let (account_id, secret_key) = match &config.credentials { + Credentials::Icp(credentials) => ( + credentials.account_id.clone(), + credentials.secret_key.clone(), + ), + Credentials::Near(_) | Credentials::Starknet(_) => { + panic!("Expected ICP credentials but got {:?}", config.credentials) + } + }; + ( + network.clone().into(), + icp::NetworkConfig { + rpc_url: config.rpc_url.clone(), + account_id, + secret_key, + }, + ) + }) + .collect(), + }); + + let all_transports = Both { + left: near_transport, + right: Both { + left: starknet_transport, + right: icp_transport, + }, + }; + + Client::new(all_transports) + } } #[derive(Debug, Error)] @@ -225,6 +222,17 @@ impl Client { } } +impl Transport for Client { + type Error = T::Error; + + async fn try_send<'a>( + &self, + args: TransportArguments<'a>, + ) -> Result, Self::Error>, UnsupportedProtocol<'a>> { + self.transport.try_send(args).await + } +} + #[derive(Debug)] pub struct CallClient<'a, T> { protocol: Cow<'a, str>, diff --git a/crates/merod/src/cli/relay.rs b/crates/merod/src/cli/relay.rs index 1c01612c1..b63bde22b 100644 --- a/crates/merod/src/cli/relay.rs +++ b/crates/merod/src/cli/relay.rs @@ -1,5 +1,4 @@ use core::net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr}; -use std::borrow::Cow; use std::env; use axum::extract::State; @@ -8,12 +7,9 @@ use axum::response::IntoResponse; use axum::routing::post; use axum::{Json, Router}; use calimero_config::ConfigFile; -use calimero_context_config::client::config::Credentials; -use calimero_context_config::client::protocol::{near, starknet}; use calimero_context_config::client::relayer::{RelayRequest, ServerError}; -use calimero_context_config::client::transport::{ - Both, Transport, TransportArguments, TransportRequest, -}; +use calimero_context_config::client::transport::{Transport, TransportArguments, TransportRequest}; +use calimero_context_config::client::Client; use clap::{Parser, ValueEnum}; use eyre::{bail, Result as EyreResult}; use futures_util::FutureExt; @@ -55,73 +51,7 @@ impl RelayCommand { let (tx, mut rx) = mpsc::channel::(32); - let near_transport = near::NearTransport::new(&near::NearConfig { - networks: config - .context - .client - .signer - .local - .near - .iter() - .map(|(network, config)| { - let (account_id, access_key) = match &config.credentials { - Credentials::Near(credentials) => ( - credentials.account_id.clone(), - credentials.secret_key.clone(), - ), - Credentials::Starknet(_) => { - bail!("Expected NEAR credentials, but got Starknet credentials.") - } - Credentials::Icp(_) => { - bail!("Expected NEAR credentials, but got Starknet credentials.") - } - _ => bail!("Expected NEAR credentials."), - }; - Ok(( - Cow::from(network.clone()), - near::NetworkConfig { - rpc_url: config.rpc_url.clone(), - account_id, - access_key, - }, - )) - }) - .collect::>()?, - }); - - let starknet_transport = starknet::StarknetTransport::new(&starknet::StarknetConfig { - networks: config - .context - .client - .signer - .local - .starknet - .iter() - .map(|(network, config)| { - let (account_id, access_key) = match &config.credentials { - Credentials::Starknet(credentials) => { - (credentials.account_id, credentials.secret_key) - } - Credentials::Near(_) => bail!("Expected Starknet credentials."), - Credentials::Icp(_) => bail!("Expected Starknet credentials."), - _ => bail!("Expected Starknet credentials."), - }; - Ok(( - Cow::from(network.clone()), - starknet::NetworkConfig { - rpc_url: config.rpc_url.clone(), - account_id, - access_key, - }, - )) - }) - .collect::>()?, - }); - - let both_transport = Both { - left: near_transport, - right: starknet_transport, - }; + let transports = Client::from_local_config(&config.context.client.signer.local); let handle = async move { while let Some((request, res_tx)) = rx.recv().await { @@ -135,7 +65,7 @@ impl RelayCommand { payload: request.payload, }; - let res = both_transport + let res = transports .try_send(args) .await .map(|res| res.map_err(Into::into))