Skip to main content

ethrex_p2p/rlpx/
error.rs

1use super::{message::Message, p2p::DisconnectReason};
2use crate::snap::error::SnapError;
3use aes::cipher::InvalidLength;
4use ethrex_blockchain::error::{ChainError, MempoolError};
5use ethrex_rlp::error::{RLPDecodeError, RLPEncodeError};
6use ethrex_storage::error::StoreError;
7#[cfg(feature = "l2")]
8use ethrex_storage_rollup::RollupStoreError;
9use spawned_concurrency::error::ActorError;
10use thiserror::Error;
11
12#[derive(Debug, Error)]
13pub enum CryptographyError {
14    #[error("Invalid key: {0}")]
15    InvalidKey(String),
16    #[error("Invalid generated secret: {0}")]
17    InvalidGeneratedSecret(String),
18    #[error("Couldn't get keys from shared secret: {0}")]
19    CouldNotGetKeyFromSecret(String),
20}
21
22// TODO improve errors
23#[derive(Debug, Error)]
24pub enum PeerConnectionError {
25    #[error("{0}")]
26    HandshakeError(String),
27    #[error("Invalid connection state: {0}")]
28    StateError(String),
29    #[error("No matching capabilities")]
30    NoMatchingCapabilities,
31    #[error("Too many peers")]
32    TooManyPeers,
33    #[error("Peer disconnected")]
34    Disconnected,
35    #[error("Disconnect requested: {0}")]
36    DisconnectReceived(DisconnectReason),
37    #[error("Disconnect sent: {0}")]
38    DisconnectSent(DisconnectReason),
39    #[error("Not Found: {0}")]
40    NotFound(String),
41    #[error("Invalid peer id")]
42    InvalidPeerId,
43    #[error("Invalid recovery id")]
44    InvalidRecoveryId,
45    #[error("Invalid message length")]
46    InvalidMessageLength,
47    #[error("Request id not present: {0}")]
48    ExpectedRequestId(String),
49    #[error("Cannot handle message: {0}")]
50    MessageNotHandled(String),
51    #[error("Bad Request: {0}")]
52    BadRequest(String),
53    #[error(transparent)]
54    RLPDecodeError(#[from] RLPDecodeError),
55    #[error(transparent)]
56    RLPEncodeError(#[from] RLPEncodeError),
57    #[error(transparent)]
58    StoreError(#[from] StoreError),
59    #[error(transparent)]
60    #[cfg(feature = "l2")]
61    RollupStoreError(#[from] RollupStoreError),
62    #[error("Error in cryptographic library: {0}")]
63    CryptographyError(String),
64    #[error("Failed to broadcast msg: {0}")]
65    BroadcastError(String),
66    #[error("RecvError: {0}")]
67    RecvError(String),
68    #[error("Failed to send msg: {0}")]
69    SendMessage(String),
70    #[error("Error when inserting transaction in the mempool: {0}")]
71    MempoolError(#[from] MempoolError),
72    #[error("Error when adding a block to the blockchain: {0}")]
73    BlockchainError(#[from] ChainError),
74    #[error("Io Error: {0}")]
75    IoError(#[from] std::io::Error),
76    #[error("Failed to decode message due to invalid frame: {0}")]
77    InvalidMessageFrame(String),
78    #[error("Failed due to an internal error: {0}")]
79    InternalError(String),
80    #[error("Incompatible Protocol")]
81    IncompatibleProtocol,
82    #[error("Invalid block range")]
83    InvalidBlockRange,
84    #[error("An L2 functionality was used but it was not previously negotiated")]
85    L2CapabilityNotNegotiated,
86    #[error("Received invalid block range update")]
87    InvalidBlockRangeUpdate,
88    #[error(transparent)]
89    ActorError(#[from] ActorError),
90    #[error("Request timeouted")]
91    Timeout,
92    #[error("Unexpected response: Expected {0}, got {1}")]
93    UnexpectedResponse(String, String),
94}
95
96// tokio::sync::mpsc::error::SendError<Message> is too large to be part of the RLPxError enum directly
97// so we will instead save the error's display message
98impl From<tokio::sync::mpsc::error::SendError<Message>> for PeerConnectionError {
99    fn from(value: tokio::sync::mpsc::error::SendError<Message>) -> Self {
100        Self::SendMessage(value.to_string())
101    }
102}
103
104// Grouping all cryptographic related errors in a single CryptographicError variant
105// We can improve this to individual errors if required
106impl From<secp256k1::Error> for PeerConnectionError {
107    fn from(e: secp256k1::Error) -> Self {
108        PeerConnectionError::CryptographyError(e.to_string())
109    }
110}
111
112impl From<InvalidLength> for PeerConnectionError {
113    fn from(e: InvalidLength) -> Self {
114        PeerConnectionError::CryptographyError(e.to_string())
115    }
116}
117
118impl From<aes::cipher::StreamCipherError> for PeerConnectionError {
119    fn from(e: aes::cipher::StreamCipherError) -> Self {
120        PeerConnectionError::CryptographyError(e.to_string())
121    }
122}
123
124impl From<tokio::sync::broadcast::error::RecvError> for PeerConnectionError {
125    fn from(e: tokio::sync::broadcast::error::RecvError) -> Self {
126        PeerConnectionError::RecvError(e.to_string())
127    }
128}
129
130impl From<tokio::sync::oneshot::error::RecvError> for PeerConnectionError {
131    fn from(e: tokio::sync::oneshot::error::RecvError) -> Self {
132        PeerConnectionError::RecvError(e.to_string())
133    }
134}
135
136impl From<SnapError> for PeerConnectionError {
137    fn from(e: SnapError) -> Self {
138        match e {
139            SnapError::Store(e) => PeerConnectionError::StoreError(e),
140            SnapError::Protocol(e) => e,
141            SnapError::BadRequest(msg) => PeerConnectionError::BadRequest(msg),
142            other => PeerConnectionError::InternalError(other.to_string()),
143        }
144    }
145}