Skip to content

Commit

Permalink
chore: touchups (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx authored Nov 6, 2024
1 parent 8bd0fd0 commit 6be490e
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 546 deletions.
8 changes: 8 additions & 0 deletions crates/context/config/src/client/protocol/near.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ 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 @@ -191,6 +193,12 @@ impl Transport for NearTransport<'_> {
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
8 changes: 8 additions & 0 deletions crates/context/config/src/client/protocol/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ 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 @@ -174,6 +176,12 @@ impl Transport for StarknetTransport<'_> {
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
61 changes: 32 additions & 29 deletions crates/context/config/src/client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,6 @@ pub trait Transport {
) -> Result<Vec<u8>, Self::Error>;
}

impl<L: Transport, R: Transport> Transport for Either<L, R> {
type Error = Either<L::Error, R::Error>;

async fn send(
&self,
request: TransportRequest<'_>,
payload: Vec<u8>,
) -> Result<Vec<u8>, Self::Error> {
match self {
Self::Left(left) => left.send(request, payload).await.map_err(Either::Left),
Self::Right(right) => right.send(request, payload).await.map_err(Either::Right),
}
}
}

#[derive(Debug)]
#[non_exhaustive]
pub struct TransportRequest<'a> {
Expand All @@ -59,6 +44,34 @@ impl<'a> TransportRequest<'a> {
}
}

#[derive(Debug, Error)]
pub enum EitherError<L, R> {
#[error(transparent)]
Left(L),
#[error(transparent)]
Right(R),
#[error("unsupported protocol: {0}")]
UnsupportedProtocol(String),
}

impl<L: Transport, R: Transport> Transport for Either<L, R> {
type Error = EitherError<L::Error, R::Error>;

async fn send(
&self,
request: TransportRequest<'_>,
payload: Vec<u8>,
) -> Result<Vec<u8>, Self::Error> {
match self {
Self::Left(left) => left.send(request, payload).await.map_err(EitherError::Left),
Self::Right(right) => right
.send(request, payload)
.await
.map_err(EitherError::Right),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
#[expect(clippy::exhaustive_enums, reason = "Considered to be exhaustive")]
pub enum Operation<'a> {
Expand All @@ -83,22 +96,12 @@ pub struct Both<L, R> {
pub right: R,
}

#[derive(Debug, Error)]
pub enum BothError<L, R> {
#[error("left error: {0}")]
Left(L),
#[error("right error: {0}")]
Right(R),
#[error("unsupported protocol: {0}")]
UnsupportedProtocol(String),
}

impl<L, R> Transport for Both<L, R>
where
L: AssociatedTransport,
R: AssociatedTransport,
{
type Error = BothError<L::Error, R::Error>;
type Error = EitherError<L::Error, R::Error>;

async fn send(
&self,
Expand All @@ -109,14 +112,14 @@ where
self.left
.send(request, payload)
.await
.map_err(BothError::Left)
.map_err(EitherError::Left)
} else if request.protocol == R::protocol() {
self.right
.send(request, payload)
.await
.map_err(BothError::Right)
.map_err(EitherError::Right)
} else {
return Err(BothError::UnsupportedProtocol(
return Err(EitherError::UnsupportedProtocol(
request.protocol.into_owned(),
));
}
Expand Down
4 changes: 0 additions & 4 deletions crates/context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,4 @@ impl ContextManager {

Ok(Some(stream))
}

pub fn is_application_blob_installed(&self, blob_id: BlobId) -> EyreResult<bool> {
self.blob_manager.has(blob_id)
}
}
7 changes: 3 additions & 4 deletions crates/network/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::pin::Pin;
use core::task::{Context, Poll};

use eyre::{bail, Result as EyreResult};
use futures_util::{Sink as FuturesSink, SinkExt, Stream as FuturesStream};
use futures_util::{Sink as FuturesSink, SinkExt, Stream as FuturesStream, StreamExt};
use libp2p::{PeerId, Stream as P2pStream, StreamProtocol};
use tokio::io::BufStream;
use tokio_util::codec::Framed;
Expand Down Expand Up @@ -43,9 +43,8 @@ impl Stream {
impl FuturesStream for Stream {
type Item = Result<Message<'static>, CodecError>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let inner = Pin::new(&mut self.get_mut().inner);
inner.poll_next(cx)
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.inner.poll_next_unpin(cx)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/network/src/stream/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ impl<'a> Message<'a> {
}

#[derive(Debug, ThisError)]
#[error("CodecError")]
#[non_exhaustive]
pub enum CodecError {
#[error(transparent)]
StdIo(#[from] IoError),
}

Expand Down
Loading

0 comments on commit 6be490e

Please sign in to comment.