reth_eth_wire/errors/
eth.rs

1//! Error handling for (`EthStream`)[`crate::EthStream`]
2
3use crate::{
4    errors::P2PStreamError, message::MessageError, version::ParseVersionError, DisconnectReason,
5};
6use alloy_chains::Chain;
7use alloy_primitives::B256;
8use reth_eth_wire_types::EthVersion;
9use reth_ethereum_forks::ValidationError;
10use reth_primitives_traits::{GotExpected, GotExpectedBoxed};
11use std::io;
12
13/// Errors when sending/receiving messages
14#[derive(thiserror::Error, Debug)]
15pub enum EthStreamError {
16    #[error(transparent)]
17    /// Error of the underlying P2P connection.
18    P2PStreamError(#[from] P2PStreamError),
19    #[error(transparent)]
20    /// Failed to parse peer's version.
21    ParseVersionError(#[from] ParseVersionError),
22    #[error(transparent)]
23    /// Failed Ethereum handshake.
24    EthHandshakeError(#[from] EthHandshakeError),
25    /// Thrown when decoding a message failed.
26    #[error(transparent)]
27    InvalidMessage(#[from] MessageError),
28    #[error("message size ({0}) exceeds max length (10MB)")]
29    /// Received a message whose size exceeds the standard limit.
30    MessageTooBig(usize),
31    #[error(
32        "TransactionHashes invalid len of fields: hashes_len={hashes_len} types_len={types_len} sizes_len={sizes_len}"
33    )]
34    /// Received malformed transaction hashes message with discrepancies in field lengths.
35    TransactionHashesInvalidLenOfFields {
36        /// The number of transaction hashes.
37        hashes_len: usize,
38        /// The number of transaction types.
39        types_len: usize,
40        /// The number of transaction sizes.
41        sizes_len: usize,
42    },
43    /// Error when data is not received from peer for a prolonged period.
44    #[error("never received data from remote peer")]
45    StreamTimeout,
46    /// Error triggered when an unknown or unsupported Ethereum message ID is received.
47    #[error("Received unknown ETH message ID: 0x{message_id:X}")]
48    UnsupportedMessage {
49        /// The identifier of the unknown Ethereum message.
50        message_id: u8,
51    },
52}
53
54// === impl EthStreamError ===
55
56impl EthStreamError {
57    /// Returns the [`DisconnectReason`] if the error is a disconnect message
58    pub const fn as_disconnected(&self) -> Option<DisconnectReason> {
59        if let Self::P2PStreamError(err) = self {
60            err.as_disconnected()
61        } else {
62            None
63        }
64    }
65
66    /// Returns the [`io::Error`] if it was caused by IO
67    pub const fn as_io(&self) -> Option<&io::Error> {
68        if let Self::P2PStreamError(P2PStreamError::Io(io)) = self {
69            return Some(io)
70        }
71        None
72    }
73}
74
75impl From<io::Error> for EthStreamError {
76    fn from(err: io::Error) -> Self {
77        P2PStreamError::from(err).into()
78    }
79}
80
81/// Error  that can occur during the `eth` sub-protocol handshake.
82#[derive(thiserror::Error, Debug)]
83pub enum EthHandshakeError {
84    /// Status message received or sent outside of the handshake process.
85    #[error("status message can only be recv/sent in handshake")]
86    StatusNotInHandshake,
87    /// Receiving a non-status message during the handshake phase.
88    #[error("received non-status message when trying to handshake")]
89    NonStatusMessageInHandshake,
90    #[error("no response received when sending out handshake")]
91    /// No response received during the handshake process.
92    NoResponse,
93    #[error(transparent)]
94    /// Invalid fork data.
95    InvalidFork(#[from] ValidationError),
96    #[error("mismatched genesis in status message: {0}")]
97    /// Mismatch in the genesis block during status exchange.
98    MismatchedGenesis(GotExpectedBoxed<B256>),
99    #[error("mismatched protocol version in status message: {0}")]
100    /// Mismatched protocol versions in status messages.
101    MismatchedProtocolVersion(GotExpected<EthVersion>),
102    #[error("mismatched chain in status message: {0}")]
103    /// Mismatch in chain details in status messages.
104    MismatchedChain(GotExpected<Chain>),
105    #[error("total difficulty bitlen is too large: got {got}, maximum {maximum}")]
106    /// Excessively large total difficulty bit lengths.
107    TotalDifficultyBitLenTooLarge {
108        /// The actual bit length of the total difficulty.
109        got: usize,
110        /// The maximum allowed bit length for the total difficulty.
111        maximum: usize,
112    },
113}