refactor: move RequestError from network to interfaces p2p (#197)

This commit is contained in:
Matthias Seitz
2022-11-13 09:46:38 +01:00
committed by GitHub
parent 139efee599
commit 5ca2cab97f
4 changed files with 37 additions and 34 deletions

View File

@ -0,0 +1,30 @@
use tokio::sync::{mpsc, oneshot};
/// Result alias for result of a request.
pub type RequestResult<T> = Result<T, RequestError>;
/// 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<T> From<mpsc::error::SendError<T>> for RequestError {
fn from(_: mpsc::error::SendError<T>) -> Self {
RequestError::ChannelClosed
}
}
impl From<oneshot::error::RecvError> for RequestError {
fn from(_: oneshot::error::RecvError) -> Self {
RequestError::ChannelClosed
}
}

View File

@ -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;

View File

@ -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},

View File

@ -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<T> = Result<T, RequestError>;
/// 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<T> From<mpsc::error::SendError<T>> for RequestError {
fn from(_: mpsc::error::SendError<T>) -> Self {
RequestError::ChannelClosed
}
}
impl From<oneshot::error::RecvError> 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 {