Skip to content

Commit

Permalink
feat: iterate on transport generalization for recursive declaration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx committed Nov 27, 2024
1 parent 79c9c00 commit 93d8425
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 208 deletions.
44 changes: 35 additions & 9 deletions crates/context/config/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;
use std::fmt::Debug;
use std::ops::Deref;

use either::Either;
use env::Method;
Expand All @@ -10,10 +11,11 @@ pub mod env;
pub mod protocol;
pub mod relayer;
pub mod transport;
pub mod utils;

use config::{ClientConfig, ClientSelectedSigner, Credentials};
use protocol::{near, starknet, Protocol};
use transport::{Both, Transport, TransportRequest};
use transport::{Both, Transport, TransportArguments, TransportRequest, UnsupportedProtocol};

pub type AnyTransport = Either<
relayer::RelayerTransport,
Expand Down Expand Up @@ -110,17 +112,43 @@ pub enum ClientError<T: Transport> {
Transport(T::Error),
#[error("codec error: {0}")]
Codec(#[from] eyre::Report),
#[error("unsupported protocol: {0}")]
UnsupportedProtocol(String),
#[error(
"unsupported protocol: `{found}`, expected {}",
utils::humanize_iter(expected.deref())
)]
UnsupportedProtocol {
found: String,
expected: Cow<'static, [Cow<'static, str>]>,
},
}

impl<'a, T: Transport> From<UnsupportedProtocol<'a>> for ClientError<T> {
fn from(err: UnsupportedProtocol<'a>) -> Self {
Self::UnsupportedProtocol {
found: err.args.protocol.into_owned(),
expected: err.expected,
}
}
}

impl<T: Transport> Client<T> {
async fn send(
&self,
protocol: Cow<'_, str>,
request: TransportRequest<'_>,
payload: Vec<u8>,
) -> Result<Vec<u8>, T::Error> {
self.transport.send(request, payload).await
) -> Result<Vec<u8>, ClientError<T>> {
let res: Result<_, _> = self
.transport
.try_send(TransportArguments {
protocol,
request,
payload,
})
.await
.into();

res?.map_err(ClientError::Transport)
}

pub fn query<'a, E: Environment<'a, T>>(
Expand Down Expand Up @@ -182,17 +210,15 @@ impl<'a, T: Transport> CallClient<'a, T> {
};

let request = TransportRequest {
protocol: Cow::Borrowed(&self.protocol),
network_id: Cow::Borrowed(&self.network_id),
contract_id: Cow::Borrowed(&self.contract_id),
operation,
};

let response = self
.client
.send(request, payload)
.await
.map_err(ClientError::Transport)?;
.send(self.protocol.as_ref().into(), request, payload)
.await?;

M::decode(response).map_err(ClientError::Codec)
}
Expand Down
9 changes: 5 additions & 4 deletions crates/context/config/src/client/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod utils {
use crate::client::{CallClient, ClientError, Operation};

// todo! when crates are broken up, appropriately locate this
pub(super) async fn send_near_or_starknet<M, R, T: Transport>(
pub(super) async fn send<M, R, T: Transport>(
client: &CallClient<'_, T>,
params: Operation<M>,
) -> Result<R, ClientError<T>>
Expand All @@ -34,9 +34,10 @@ mod utils {
match &*client.protocol {
Near::PROTOCOL => client.send::<Near, _>(params).await,
Starknet::PROTOCOL => client.send::<Starknet, _>(params).await,
unsupported_protocol => Err(ClientError::UnsupportedProtocol(
unsupported_protocol.to_owned(),
)),
unsupported_protocol => Err(ClientError::UnsupportedProtocol {
found: unsupported_protocol.to_owned(),
expected: vec![Near::PROTOCOL.into(), Starknet::PROTOCOL.into()].into(),
}),
}
}
}
2 changes: 1 addition & 1 deletion crates/context/config/src/client/env/config/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ impl<'a, T: Transport + Debug> ContextConfigMutateRequest<'a, T> {
kind: self.kind,
};

utils::send_near_or_starknet(&self.client, Operation::Write(request)).await
utils::send(&self.client, Operation::Write(request)).await
}
}
14 changes: 7 additions & 7 deletions crates/context/config/src/client/env/config/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> {
context_id: Repr::new(context_id),
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn application_revision(
Expand All @@ -39,7 +39,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> {
context_id: Repr::new(context_id),
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn members(
Expand All @@ -54,7 +54,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> {
length,
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn has_member(
Expand All @@ -67,7 +67,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> {
identity: Repr::new(identity),
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn members_revision(
Expand All @@ -78,7 +78,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> {
context_id: Repr::new(context_id),
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn privileges(
Expand All @@ -88,7 +88,7 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> {
) -> Result<BTreeMap<SignerId, Vec<Capability>>, ClientError<T>> {
let params = privileges::PrivilegesRequest::new(context_id, identities);

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn get_proxy_contract(
Expand All @@ -99,6 +99,6 @@ impl<'a, T: Transport> ContextConfigQuery<'a, T> {
context_id: Repr::new(context_id),
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}
}
2 changes: 1 addition & 1 deletion crates/context/config/src/client/env/proxy/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ impl<'a, T: Transport> ContextProxyMutateRequest<'a, T> {
raw_request: self.raw_request,
};

utils::send_near_or_starknet(&self.client, Operation::Write(request)).await
utils::send(&self.client, Operation::Write(request)).await
}
}
10 changes: 5 additions & 5 deletions crates/context/config/src/client/env/proxy/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a, T: Transport> ContextProxyQuery<'a, T> {
offset,
length: limit,
};
utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn proposal(
Expand All @@ -43,13 +43,13 @@ impl<'a, T: Transport> ContextProxyQuery<'a, T> {
proposal_id: Repr::new(proposal_id),
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn get_number_of_active_proposals(&self) -> Result<u16, ClientError<T>> {
let params = ActiveProposalRequest;

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn get_number_of_proposal_approvals(
Expand All @@ -60,7 +60,7 @@ impl<'a, T: Transport> ContextProxyQuery<'a, T> {
proposal_id: Repr::new(proposal_id),
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}

pub async fn get_proposal_approvers(
Expand All @@ -71,6 +71,6 @@ impl<'a, T: Transport> ContextProxyQuery<'a, T> {
proposal_id: Repr::new(proposal_id),
};

utils::send_near_or_starknet(&self.client, Operation::Read(params)).await
utils::send(&self.client, Operation::Read(params)).await
}
}
28 changes: 2 additions & 26 deletions crates/context/config/src/client/protocol/near.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use url::Url;

use super::Protocol;
use crate::client::transport::{
AssociatedTransport, Operation, Transport, TransportLike, TransportRequest,
AssociatedTransport, Operation, ProtocolTransport, TransportRequest,
};

#[derive(Copy, Clone, Debug)]
Expand All @@ -43,22 +43,6 @@ impl AssociatedTransport for NearTransport<'_> {
type Protocol = Near;
}

impl TransportLike for NearTransport<'_> {
type Error = NearError;

async fn try_send(
&self,
request: TransportRequest<'_>,
payload: &Vec<u8>,
) -> Option<Result<Vec<u8>, Self::Error>> {
if request.protocol == "near" {
Some(self.send(request, payload.to_vec()).await)
} else {
None
}
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(try_from = "serde_creds::Credentials")]
pub struct Credentials {
Expand Down Expand Up @@ -172,8 +156,6 @@ impl<'a> NearTransport<'a> {
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum NearError {
#[error("unsupported protocol `{0}`")]
UnsupportedProtocol(String),
#[error("unknown network `{0}`")]
UnknownNetwork(String),
#[error("invalid response from RPC while {operation}")]
Expand Down Expand Up @@ -206,20 +188,14 @@ pub enum ErrorOperation {
FetchAccount,
}

impl Transport for NearTransport<'_> {
impl ProtocolTransport for NearTransport<'_> {
type Error = NearError;

async fn send(
&self,
request: TransportRequest<'_>,
payload: Vec<u8>,
) -> Result<Vec<u8>, Self::Error> {
if request.protocol != Near::PROTOCOL {
return Err(NearError::UnsupportedProtocol(
request.protocol.into_owned(),
));
}

let Some(network) = self.networks.get(&request.network_id) else {
return Err(NearError::UnknownNetwork(request.network_id.into_owned()));
};
Expand Down
28 changes: 2 additions & 26 deletions crates/context/config/src/client/protocol/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use thiserror::Error;
use super::Protocol;
use crate::client::env::proxy::starknet::StarknetProposalWithApprovals;
use crate::client::transport::{
AssociatedTransport, Operation, Transport, TransportLike, TransportRequest,
AssociatedTransport, Operation, ProtocolTransport, TransportRequest,
};

#[derive(Copy, Clone, Debug)]
Expand All @@ -33,22 +33,6 @@ impl AssociatedTransport for StarknetTransport<'_> {
type Protocol = Starknet;
}

impl TransportLike for StarknetTransport<'_> {
type Error = StarknetError;

async fn try_send(
&self,
request: TransportRequest<'_>,
payload: &Vec<u8>,
) -> Option<Result<Vec<u8>, Self::Error>> {
if request.protocol == "near" {
Some(self.send(request, payload.to_vec()).await)
} else {
None
}
}
}

#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
#[serde(try_from = "serde_creds::Credentials")]
pub struct Credentials {
Expand Down Expand Up @@ -153,8 +137,6 @@ impl<'a> StarknetTransport<'a> {
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum StarknetError {
#[error("unsupported protocol: {0}")]
UnsupportedProtocol(String),
#[error("unknown network `{0}`")]
UnknownNetwork(String),
#[error("invalid response from RPC while {operation}")]
Expand Down Expand Up @@ -191,20 +173,14 @@ pub enum ErrorOperation {
FetchNonce,
}

impl Transport for StarknetTransport<'_> {
impl ProtocolTransport for StarknetTransport<'_> {
type Error = StarknetError;

async fn send(
&self,
request: TransportRequest<'_>,
payload: Vec<u8>,
) -> Result<Vec<u8>, Self::Error> {
if request.protocol != Starknet::PROTOCOL {
return Err(StarknetError::UnsupportedProtocol(
request.protocol.into_owned(),
));
}

let Some(network) = self.networks.get(&request.network_id) else {
return Err(StarknetError::UnknownNetwork(
request.network_id.into_owned(),
Expand Down
Loading

0 comments on commit 93d8425

Please sign in to comment.