diff --git a/crates/interfaces/src/p2p/error.rs b/crates/interfaces/src/p2p/error.rs new file mode 100644 index 000000000..56e7bd340 --- /dev/null +++ b/crates/interfaces/src/p2p/error.rs @@ -0,0 +1,30 @@ +use tokio::sync::{mpsc, oneshot}; + +/// Result alias for result of a request. +pub type RequestResult = Result; + +/// Error variants that can happen when sending requests to a session. +#[derive(Debug, thiserror::Error)] +#[allow(missing_docs)] +pub enum RequestError { + #[error("Closed channel to the peer.")] + ChannelClosed, + #[error("Not connected to the peer.")] + NotConnected, + #[error("Capability Message is not supported by remote peer.")] + UnsupportedCapability, + #[error("Request timed out while awaiting response.")] + Timeout, +} + +impl From> for RequestError { + fn from(_: mpsc::error::SendError) -> Self { + RequestError::ChannelClosed + } +} + +impl From for RequestError { + fn from(_: oneshot::error::RecvError) -> Self { + RequestError::ChannelClosed + } +} diff --git a/crates/interfaces/src/p2p/mod.rs b/crates/interfaces/src/p2p/mod.rs index a62037e8b..fb351fdc2 100644 --- a/crates/interfaces/src/p2p/mod.rs +++ b/crates/interfaces/src/p2p/mod.rs @@ -6,6 +6,10 @@ /// [`HeadersClient`]: crate::p2p::headers::HeadersClient pub mod headers; +/// Error types broadly used by p2p interfaces for any operation which may produce an error when +/// interacting with the network implementation +pub mod error; + use futures::Stream; use std::pin::Pin; diff --git a/crates/net/network/src/fetch.rs b/crates/net/network/src/fetch.rs index 572154daa..7219681ff 100644 --- a/crates/net/network/src/fetch.rs +++ b/crates/net/network/src/fetch.rs @@ -1,12 +1,9 @@ //! Fetch data from the network. -use crate::{ - message::{BlockRequest, RequestResult}, - NodeId, -}; +use crate::{message::BlockRequest, NodeId}; use futures::StreamExt; use reth_eth_wire::{BlockBody, EthMessage}; -use reth_interfaces::p2p::headers::client::HeadersRequest; +use reth_interfaces::p2p::{error::RequestResult, headers::client::HeadersRequest}; use reth_primitives::{Header, H256, U256}; use std::{ collections::{HashMap, VecDeque}, diff --git a/crates/net/network/src/message.rs b/crates/net/network/src/message.rs index 5c44ac170..43dc28461 100644 --- a/crates/net/network/src/message.rs +++ b/crates/net/network/src/message.rs @@ -13,38 +13,10 @@ use std::task::{ready, Context, Poll}; use crate::NodeId; use reth_eth_wire::capability::CapabilityMessage; +use reth_interfaces::p2p::error::RequestResult; use reth_primitives::{Header, Receipt, TransactionSigned}; use tokio::sync::{mpsc, mpsc::error::TrySendError, oneshot}; -/// Result alias for result of a request. -pub type RequestResult = Result; - -/// Error variants that can happen when sending requests to a session. -#[derive(Debug, thiserror::Error)] -#[allow(missing_docs)] -pub enum RequestError { - #[error("Closed channel.")] - ChannelClosed, - #[error("Not connected to the node.")] - NotConnected, - #[error("Capability Message is not supported by remote peer.")] - UnsupportedCapability, - #[error("Network error: {0}")] - Io(String), -} - -impl From> for RequestError { - fn from(_: mpsc::error::SendError) -> Self { - RequestError::ChannelClosed - } -} - -impl From for RequestError { - fn from(_: oneshot::error::RecvError) -> Self { - RequestError::ChannelClosed - } -} - /// Represents all messages that can be sent to a peer session #[derive(Debug)] pub enum PeerMessage {