reth_eth_wire/errors/
eth.rs1use crate::{
4 errors::P2PStreamError, message::MessageError, version::ParseVersionError, DisconnectReason,
5};
6use alloy_chains::Chain;
7use alloy_primitives::B256;
8use reth_eth_wire_types::{snap::SnapProtocolError, EthVersion};
9use reth_ethereum_forks::ValidationError;
10use reth_primitives_traits::{GotExpected, GotExpectedBoxed};
11use std::io;
12
13#[derive(thiserror::Error, Debug)]
15pub enum EthStreamError {
16 #[error(transparent)]
17 P2PStreamError(#[from] P2PStreamError),
19 #[error(transparent)]
20 ParseVersionError(#[from] ParseVersionError),
22 #[error(transparent)]
23 EthHandshakeError(#[from] EthHandshakeError),
25 #[error(transparent)]
27 InvalidMessage(#[from] MessageError),
28 #[error(transparent)]
30 InvalidSnapMessage(#[from] SnapProtocolError),
31 #[error("message size ({0}) exceeds max length (10MB)")]
32 MessageTooBig(usize),
34 #[error(
35 "TransactionHashes invalid len of fields: hashes_len={hashes_len} types_len={types_len} sizes_len={sizes_len}"
36 )]
37 TransactionHashesInvalidLenOfFields {
39 hashes_len: usize,
41 types_len: usize,
43 sizes_len: usize,
45 },
46 #[error("never received data from remote peer")]
48 StreamTimeout,
49 #[error("Received unknown ETH message ID: 0x{message_id:X}")]
51 UnsupportedMessage {
52 message_id: u8,
54 },
55}
56
57impl EthStreamError {
60 pub const fn as_disconnected(&self) -> Option<DisconnectReason> {
62 if let Self::P2PStreamError(err) = self {
63 err.as_disconnected()
64 } else {
65 None
66 }
67 }
68
69 pub const fn is_protocol_breach(&self) -> bool {
74 matches!(
75 self,
76 Self::InvalidMessage(_) |
77 Self::InvalidSnapMessage(_) |
78 Self::MessageTooBig(_) |
79 Self::TransactionHashesInvalidLenOfFields { .. } |
80 Self::UnsupportedMessage { .. } |
81 Self::P2PStreamError(
82 P2PStreamError::Rlp(_) |
83 P2PStreamError::Snap(_) |
84 P2PStreamError::MessageTooBig { .. } |
85 P2PStreamError::UnknownReservedMessageId(_) |
86 P2PStreamError::UnknownSubprotocolMessageId(_) |
87 P2PStreamError::EmptyProtocolMessage |
88 P2PStreamError::InvalidPingPongPayload(_) |
89 P2PStreamError::TooManyPings |
90 P2PStreamError::UnknownDisconnectReason(_)
91 )
92 )
93 }
94
95 pub const fn as_io(&self) -> Option<&io::Error> {
97 if let Self::P2PStreamError(P2PStreamError::Io(io)) = self {
98 return Some(io)
99 }
100 None
101 }
102}
103
104impl From<io::Error> for EthStreamError {
105 fn from(err: io::Error) -> Self {
106 P2PStreamError::from(err).into()
107 }
108}
109
110#[derive(thiserror::Error, Debug)]
112pub enum EthHandshakeError {
113 #[error("status message can only be recv/sent in handshake")]
115 StatusNotInHandshake,
116 #[error("received non-status message when trying to handshake")]
118 NonStatusMessageInHandshake,
119 #[error("no response received when sending out handshake")]
120 NoResponse,
122 #[error(transparent)]
123 InvalidFork(#[from] ValidationError),
125 #[error("mismatched genesis in status message: {0}")]
126 MismatchedGenesis(GotExpectedBoxed<B256>),
128 #[error("mismatched protocol version in status message: {0}")]
129 MismatchedProtocolVersion(GotExpected<EthVersion>),
131 #[error("mismatched chain in status message: {0}")]
132 MismatchedChain(GotExpected<Chain>),
134 #[error("total difficulty bitlen is too large: got {got}, maximum {maximum}")]
135 TotalDifficultyBitLenTooLarge {
137 got: usize,
139 maximum: usize,
141 },
142 #[error("earliest block > latest block: got {got}, latest {latest}")]
143 EarliestBlockGreaterThanLatestBlock {
145 got: u64,
147 latest: u64,
149 },
150 #[error("blockhash is zero")]
151 BlockhashZero,
153}