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, 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("unknown reserved p2p message id: {0}")]
40 UnknownReservedMessageId(u8),
41
42 #[error("empty protocol message received")]
44 EmptyProtocolMessage,
45
46 #[error(transparent)]
48 PingerError(#[from] PingerError),
49
50 #[error("ping timed out with")]
52 PingTimeout,
53
54 #[error(transparent)]
56 ParseSharedCapability(#[from] SharedCapabilityError),
57
58 #[error("capability not supported on stream to this peer")]
60 CapabilityNotShared,
61
62 #[error("mismatched protocol version in Hello message: {0}")]
64 MismatchedProtocolVersion(GotExpected<ProtocolVersion>),
65
66 #[error("started ping task before the handshake completed")]
68 PingBeforeHandshake,
69
70 #[error("too many messages buffered before sending")]
72 SendBufferFull,
73
74 #[error("disconnected")]
76 Disconnected(DisconnectReason),
77
78 #[error("unknown disconnect reason: {0}")]
80 UnknownDisconnectReason(#[from] UnknownDisconnectReason),
81}
82
83impl P2PStreamError {
86 pub const fn as_disconnected(&self) -> Option<DisconnectReason> {
88 let reason = match self {
89 Self::HandshakeError(P2PHandshakeError::Disconnected(reason)) |
90 Self::Disconnected(reason) => reason,
91 _ => return None,
92 };
93
94 Some(*reason)
95 }
96}
97
98#[derive(thiserror::Error, Debug, Clone, Eq, PartialEq)]
100pub enum P2PHandshakeError {
101 #[error("hello message can only be recv/sent in handshake")]
103 HelloNotInHandshake,
104
105 #[error("received non-hello message when trying to handshake")]
107 NonHelloMessageInHandshake,
108
109 #[error("no capabilities shared with peer")]
111 NoSharedCapabilities,
112
113 #[error("no response received when sending out handshake")]
115 NoResponse,
116
117 #[error("handshake timed out")]
119 Timeout,
120
121 #[error("disconnected by peer: {0}")]
123 Disconnected(DisconnectReason),
124
125 #[error("error decoding a message during handshake: {0}")]
127 DecodeError(#[from] alloy_rlp::Error),
128}
129
130#[derive(Debug, thiserror::Error)]
132pub enum PingerError {
133 #[error("pong received while ready")]
135 UnexpectedPong,
136}