reth_eth_wire/errors/
p2p.rs1use std::io;
4
5use reth_eth_wire_types::{DisconnectReason, UnknownDisconnectReason};
6use reth_primitives_traits::GotExpected;
7
8use crate::{capability::SharedCapabilityError, Capability, ProtocolVersion};
9
10#[derive(thiserror::Error, Debug)]
12pub enum P2PStreamError {
13 #[error(transparent)]
15 Io(#[from] io::Error),
16
17 #[error(transparent)]
19 Rlp(#[from] alloy_rlp::Error),
20
21 #[error(transparent)]
23 Snap(#[from] snap::Error),
24
25 #[error(transparent)]
27 HandshakeError(#[from] P2PHandshakeError),
28
29 #[error("message size ({message_size}) exceeds max length ({max_size})")]
31 MessageTooBig {
32 message_size: usize,
34 max_size: usize,
36 },
37
38 #[error(
40 "message for subprotocol {capability} has size {message_size}, exceeding max length {max_size}"
41 )]
42 SubprotocolMessageTooBig {
43 capability: Capability,
45 message_size: usize,
47 max_size: usize,
49 },
50
51 #[error("inbound buffer for subprotocol {capability} is full")]
53 SubprotocolInboundBufferFull {
54 capability: Capability,
56 },
57
58 #[error("unknown subprotocol message id: {0:#04x}")]
60 UnknownSubprotocolMessageId(u8),
61
62 #[error("unknown reserved p2p message id: {0}")]
64 UnknownReservedMessageId(u8),
65
66 #[error("empty protocol message received")]
68 EmptyProtocolMessage,
69
70 #[error("invalid ping/pong payload for p2p message id: {0:#x}")]
72 InvalidPingPongPayload(u8),
73
74 #[error("too many pings received")]
76 TooManyPings,
77
78 #[error(transparent)]
80 PingerError(#[from] PingerError),
81
82 #[error("ping timed out with")]
84 PingTimeout,
85
86 #[error(transparent)]
88 ParseSharedCapability(#[from] SharedCapabilityError),
89
90 #[error("capability not supported on stream to this peer")]
92 CapabilityNotShared,
93
94 #[error("mismatched protocol version in Hello message: {0}")]
96 MismatchedProtocolVersion(GotExpected<ProtocolVersion>),
97
98 #[error("too many messages buffered before sending")]
100 SendBufferFull,
101
102 #[error("disconnected: {0}")]
104 Disconnected(DisconnectReason),
105
106 #[error("unknown disconnect reason: {0}")]
108 UnknownDisconnectReason(#[from] UnknownDisconnectReason),
109}
110
111impl P2PStreamError {
114 pub const fn as_disconnected(&self) -> Option<DisconnectReason> {
116 let reason = match self {
117 Self::HandshakeError(P2PHandshakeError::Disconnected(reason)) |
118 Self::Disconnected(reason) => reason,
119 _ => return None,
120 };
121
122 Some(*reason)
123 }
124}
125
126#[derive(thiserror::Error, Debug, Clone, Eq, PartialEq)]
128pub enum P2PHandshakeError {
129 #[error("hello message can only be recv/sent in handshake")]
131 HelloNotInHandshake,
132
133 #[error("received non-hello message when trying to handshake")]
135 NonHelloMessageInHandshake,
136
137 #[error("no capabilities shared with peer")]
139 NoSharedCapabilities,
140
141 #[error("no response received when sending out handshake")]
143 NoResponse,
144
145 #[error("handshake timed out")]
147 Timeout,
148
149 #[error("disconnected by peer: {0}")]
151 Disconnected(DisconnectReason),
152
153 #[error("error decoding a message during handshake: {0}")]
155 DecodeError(#[from] alloy_rlp::Error),
156}
157
158#[derive(Debug, thiserror::Error)]
160pub enum PingerError {
161 #[error("pong received while ready")]
163 UnexpectedPong,
164}