Skip to content

Commit

Permalink
dedupe client definition between relayer and node
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx committed Dec 6, 2024
1 parent 9666ca3 commit ea399e1
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 179 deletions.
218 changes: 113 additions & 105 deletions crates/context/config/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<starknet::StarknetTransport<'static>, icp::IcpTransport<'static>>,
>,
pub type LocalTransports = Both<
near::NearTransport<'static>,
Both<starknet::StarknetTransport<'static>, icp::IcpTransport<'static>>,
>;

pub type AnyTransport = Either<relayer::RelayerTransport, LocalTransports>;

#[derive(Clone, Debug)]
pub struct Client<T> {
transport: T,
Expand All @@ -41,113 +40,111 @@ impl Client<AnyTransport> {
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<LocalTransports> {
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)]
Expand Down Expand Up @@ -225,6 +222,17 @@ impl<T: Transport> Client<T> {
}
}

impl<T: Transport> Transport for Client<T> {
type Error = T::Error;

async fn try_send<'a>(
&self,
args: TransportArguments<'a>,
) -> Result<Result<Vec<u8>, Self::Error>, UnsupportedProtocol<'a>> {
self.transport.try_send(args).await
}
}

#[derive(Debug)]
pub struct CallClient<'a, T> {
protocol: Cow<'a, str>,
Expand Down
78 changes: 4 additions & 74 deletions crates/merod/src/cli/relay.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::net::{AddrParseError, IpAddr, Ipv4Addr, SocketAddr};
use std::borrow::Cow;
use std::env;

use axum::extract::State;
Expand All @@ -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;
Expand Down Expand Up @@ -55,73 +51,7 @@ impl RelayCommand {

let (tx, mut rx) = mpsc::channel::<RequestPayload>(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::<EyreResult<_>>()?,
});

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::<EyreResult<_>>()?,
});

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 {
Expand All @@ -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))
Expand Down

0 comments on commit ea399e1

Please sign in to comment.