refactor(net): rename and restructure wire error types (#614)

This commit is contained in:
Matthias Seitz
2022-12-26 15:33:59 +01:00
committed by GitHub
parent 8779000460
commit ebd686a407
13 changed files with 89 additions and 77 deletions

View File

@ -0,0 +1,53 @@
//! Error handling for (`EthStream`)[crate::EthStream]
use crate::{errors::P2PStreamError, DisconnectReason};
use reth_primitives::{Chain, ValidationError, H256};
use std::io;
/// Errors when sending/receiving messages
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum EthStreamError {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Rlp(#[from] reth_rlp::DecodeError),
#[error(transparent)]
P2PStreamError(#[from] P2PStreamError),
#[error(transparent)]
EthHandshakeError(#[from] EthHandshakeError),
#[error("message size ({0}) exceeds max length (10MB)")]
MessageTooBig(usize),
}
// === impl EthStreamError ===
impl EthStreamError {
/// Returns the [`DisconnectReason`] if the error is a disconnect message
pub fn as_disconnected(&self) -> Option<DisconnectReason> {
if let EthStreamError::P2PStreamError(err) = self {
err.as_disconnected()
} else {
None
}
}
}
/// Error variants that can occur during the `eth` sub-protocol handshake.
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum EthHandshakeError {
#[error("status message can only be recv/sent in handshake")]
StatusNotInHandshake,
#[error("received non-status message when trying to handshake")]
NonStatusMessageInHandshake,
#[error("no response received when sending out handshake")]
NoResponse,
#[error(transparent)]
InvalidFork(#[from] ValidationError),
#[error("mismatched genesis in Status message. expected: {expected:?}, got: {got:?}")]
MismatchedGenesis { expected: H256, got: H256 },
#[error("mismatched protocol version in Status message. expected: {expected:?}, got: {got:?}")]
MismatchedProtocolVersion { expected: u8, got: u8 },
#[error("mismatched chain in Status message. expected: {expected:?}, got: {got:?}")]
MismatchedChain { expected: Chain, got: Chain },
}

View File

@ -0,0 +1,7 @@
//! Error types for stream variants
mod eth;
mod p2p;
pub use eth::*;
pub use p2p::*;

View File

@ -1,60 +1,10 @@
//! Error cases when handling a [`crate::EthStream`]
//! Error handling for [`P2PStream`](crate::P2PStream)
use std::io;
use reth_primitives::{Chain, ValidationError, H256};
use crate::{
capability::SharedCapabilityError, disconnect::UnknownDisconnectReason, DisconnectReason,
};
/// Errors when sending/receiving messages
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum EthStreamError {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Rlp(#[from] reth_rlp::DecodeError),
#[error(transparent)]
P2PStreamError(#[from] P2PStreamError),
#[error(transparent)]
HandshakeError(#[from] HandshakeError),
#[error("message size ({0}) exceeds max length (10MB)")]
MessageTooBig(usize),
}
// === impl EthStreamError ===
impl EthStreamError {
/// Returns the [`DisconnectReason`] if the error is a disconnect message
pub fn as_disconnected(&self) -> Option<DisconnectReason> {
if let EthStreamError::P2PStreamError(err) = self {
err.as_disconnected()
} else {
None
}
}
}
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]
pub enum HandshakeError {
#[error("status message can only be recv/sent in handshake")]
StatusNotInHandshake,
#[error("received non-status message when trying to handshake")]
NonStatusMessageInHandshake,
#[error("no response received when sending out handshake")]
NoResponse,
#[error(transparent)]
InvalidFork(#[from] ValidationError),
#[error("mismatched genesis in Status message. expected: {expected:?}, got: {got:?}")]
MismatchedGenesis { expected: H256, got: H256 },
#[error("mismatched protocol version in Status message. expected: {expected:?}, got: {got:?}")]
MismatchedProtocolVersion { expected: u8, got: u8 },
#[error("mismatched chain in Status message. expected: {expected:?}, got: {got:?}")]
MismatchedChain { expected: Chain, got: Chain },
}
/// Errors when sending/receiving p2p messages. These should result in kicking the peer.
#[derive(thiserror::Error, Debug)]
#[allow(missing_docs)]

View File

@ -1,5 +1,5 @@
use crate::{
error::{EthStreamError, HandshakeError},
errors::{EthHandshakeError, EthStreamError},
message::{EthBroadcastMessage, ProtocolBroadcastMessage},
types::{EthMessage, ProtocolMessage, Status},
};
@ -65,7 +65,7 @@ where
.inner
.next()
.await
.ok_or(EthStreamError::HandshakeError(HandshakeError::NoResponse))??;
.ok_or(EthStreamError::EthHandshakeError(EthHandshakeError::NoResponse))??;
if their_msg.len() > MAX_MESSAGE_SIZE {
return Err(EthStreamError::MessageTooBig(their_msg.len()))
@ -84,7 +84,7 @@ where
match msg.message {
EthMessage::Status(resp) => {
if status.genesis != resp.genesis {
return Err(HandshakeError::MismatchedGenesis {
return Err(EthHandshakeError::MismatchedGenesis {
expected: status.genesis,
got: resp.genesis,
}
@ -92,7 +92,7 @@ where
}
if status.version != resp.version {
return Err(HandshakeError::MismatchedProtocolVersion {
return Err(EthHandshakeError::MismatchedProtocolVersion {
expected: status.version,
got: resp.version,
}
@ -100,14 +100,14 @@ where
}
if status.chain != resp.chain {
return Err(HandshakeError::MismatchedChain {
return Err(EthHandshakeError::MismatchedChain {
expected: status.chain,
got: resp.chain,
}
.into())
}
fork_filter.validate(resp.forkid).map_err(HandshakeError::InvalidFork)?;
fork_filter.validate(resp.forkid).map_err(EthHandshakeError::InvalidFork)?;
// now we can create the `EthStream` because the peer has successfully completed
// the handshake
@ -115,7 +115,9 @@ where
Ok((stream, resp))
}
_ => Err(EthStreamError::HandshakeError(HandshakeError::NonStatusMessageInHandshake)),
_ => Err(EthStreamError::EthHandshakeError(
EthHandshakeError::NonStatusMessageInHandshake,
)),
}
}
}
@ -201,8 +203,8 @@ where
};
if matches!(msg.message, EthMessage::Status(_)) {
return Poll::Ready(Some(Err(EthStreamError::HandshakeError(
HandshakeError::StatusNotInHandshake,
return Poll::Ready(Some(Err(EthStreamError::EthHandshakeError(
EthHandshakeError::StatusNotInHandshake,
))))
}
@ -223,7 +225,7 @@ where
fn start_send(self: Pin<&mut Self>, item: EthMessage) -> Result<(), Self::Error> {
if matches!(item, EthMessage::Status(_)) {
return Err(EthStreamError::HandshakeError(HandshakeError::StatusNotInHandshake))
return Err(EthStreamError::EthHandshakeError(EthHandshakeError::StatusNotInHandshake))
}
let mut bytes = BytesMut::new();

View File

@ -9,7 +9,7 @@
pub mod builder;
pub mod capability;
mod disconnect;
pub mod error;
pub mod errors;
mod ethstream;
mod hello;
mod p2pstream;

View File

@ -1,7 +1,7 @@
#![allow(dead_code, unreachable_pub, missing_docs, unused_variables)]
use crate::{
capability::{Capability, SharedCapability},
error::{P2PHandshakeError, P2PStreamError},
errors::{P2PHandshakeError, P2PStreamError},
pinger::{Pinger, PingerEvent},
DisconnectReason, HelloMessage,
};

View File

@ -1,4 +1,4 @@
use crate::error::PingerError;
use crate::errors::PingerError;
use std::{
pin::Pin,
task::{Context, Poll},

View File

@ -2,7 +2,7 @@
use crate::session::PendingSessionHandshakeError;
use reth_eth_wire::{
error::{EthStreamError, HandshakeError, P2PHandshakeError, P2PStreamError},
errors::{EthHandshakeError, EthStreamError, P2PHandshakeError, P2PStreamError},
DisconnectReason,
};
use std::fmt;
@ -48,7 +48,7 @@ impl SessionError for EthStreamError {
EthStreamError::P2PStreamError(P2PStreamError::HandshakeError(
P2PHandshakeError::NonHelloMessageInHandshake,
)) => true,
EthStreamError::HandshakeError(err) => !matches!(err, HandshakeError::NoResponse),
EthStreamError::EthHandshakeError(err) => !matches!(err, EthHandshakeError::NoResponse),
_ => false,
}
}
@ -83,7 +83,7 @@ impl SessionError for EthStreamError {
P2PStreamError::MismatchedProtocolVersion { .. }
)
}
EthStreamError::HandshakeError(err) => !matches!(err, HandshakeError::NoResponse),
EthStreamError::EthHandshakeError(err) => !matches!(err, EthHandshakeError::NoResponse),
_ => false,
}
}
@ -91,7 +91,7 @@ impl SessionError for EthStreamError {
fn should_backoff(&self) -> bool {
matches!(
self,
EthStreamError::HandshakeError(HandshakeError::NoResponse) |
EthStreamError::EthHandshakeError(EthHandshakeError::NoResponse) |
EthStreamError::P2PStreamError(P2PStreamError::HandshakeError(
P2PHandshakeError::NoResponse
))

View File

@ -7,7 +7,7 @@ use crate::{
session::{Direction, PendingSessionHandshakeError},
};
use futures::StreamExt;
use reth_eth_wire::{error::EthStreamError, DisconnectReason};
use reth_eth_wire::{errors::EthStreamError, DisconnectReason};
use reth_net_common::ban_list::BanList;
use reth_primitives::{ForkId, PeerId};
use std::{
@ -871,7 +871,7 @@ mod test {
PeersConfig,
};
use reth_eth_wire::{
error::{EthStreamError, HandshakeError, P2PHandshakeError, P2PStreamError},
errors::{EthHandshakeError, EthStreamError, P2PHandshakeError, P2PStreamError},
DisconnectReason,
};
use reth_net_common::ban_list::BanList;
@ -1042,8 +1042,8 @@ mod test {
peers.on_pending_session_dropped(
&socket_addr,
&peer,
&PendingSessionHandshakeError::Eth(EthStreamError::HandshakeError(
HandshakeError::NoResponse,
&PendingSessionHandshakeError::Eth(EthStreamError::EthHandshakeError(
EthHandshakeError::NoResponse,
)),
);

View File

@ -12,7 +12,7 @@ use futures::{stream::Fuse, SinkExt, StreamExt};
use reth_ecies::stream::ECIESStream;
use reth_eth_wire::{
capability::Capabilities,
error::{EthStreamError, HandshakeError, P2PStreamError},
errors::{EthHandshakeError, EthStreamError, P2PStreamError},
message::{EthBroadcastMessage, RequestPair},
DisconnectReason, EthMessage, EthStream, P2PStream,
};
@ -136,7 +136,7 @@ impl ActiveSession {
match msg {
msg @ EthMessage::Status(_) => {
return Some((
EthStreamError::HandshakeError(HandshakeError::StatusNotInHandshake),
EthStreamError::EthHandshakeError(EthHandshakeError::StatusNotInHandshake),
msg,
))
}

View File

@ -6,7 +6,7 @@ use crate::{
use reth_ecies::{stream::ECIESStream, ECIESError};
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
error::EthStreamError,
errors::EthStreamError,
DisconnectReason, EthStream, P2PStream, Status,
};
use reth_primitives::PeerId;

View File

@ -16,7 +16,7 @@ use futures::{future::Either, io, FutureExt, StreamExt};
use reth_ecies::{stream::ECIESStream, ECIESError};
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
error::EthStreamError,
errors::EthStreamError,
DisconnectReason, HelloMessage, Status, UnauthedEthStream, UnauthedP2PStream,
};
use reth_primitives::{ForkFilter, ForkId, ForkTransition, PeerId, H256, U256};

View File

@ -8,7 +8,7 @@ use crate::{
use futures::Stream;
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
error::EthStreamError,
errors::EthStreamError,
Status,
};
use reth_primitives::PeerId;