1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
// Copyright 2023-2024 Paradigm.xyz
// This file is part of reth.
// Reth is a modular, contributor-friendly and blazing-fast implementation of
// the Ethereum protocol
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::EthRpcModule;
use std::{
collections::HashSet,
io::{self, ErrorKind},
net::SocketAddr,
};
/// Rpc server kind.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ServerKind {
/// Http.
Http(SocketAddr),
/// Websocket.
WS(SocketAddr),
/// WS and http on the same port
WsHttp(SocketAddr),
/// Auth.
Auth(SocketAddr),
}
impl ServerKind {
/// Returns the appropriate flags for each variant.
pub const fn flags(&self) -> &'static str {
match self {
Self::Http(_) => "--http.port",
Self::WS(_) => "--ws.port",
Self::WsHttp(_) => "--ws.port and --http.port",
Self::Auth(_) => "--authrpc.port",
}
}
}
impl std::fmt::Display for ServerKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Http(addr) => write!(f, "{addr} (HTTP-RPC server)"),
Self::WS(addr) => write!(f, "{addr} (WS-RPC server)"),
Self::WsHttp(addr) => write!(f, "{addr} (WS-HTTP-RPC server)"),
Self::Auth(addr) => write!(f, "{addr} (AUTH server)"),
}
}
}
/// Rpc Server related errors
#[derive(Debug, thiserror::Error)]
pub enum RpcError {
/// Thrown during server start.
#[error("Failed to start {kind} server: {error}")]
ServerError {
/// Server kind.
kind: ServerKind,
/// IO error.
error: io::Error,
},
/// Address already in use.
#[error("address {kind} is already in use (os error 98). Choose a different port using {}", kind.flags())]
AddressAlreadyInUse {
/// Server kind.
kind: ServerKind,
/// IO error.
error: io::Error,
},
/// Cors parsing error.
// #[error(transparent)]
// Cors(#[from] CorsDomainError),
/// Http and WS server configured on the same port but with conflicting
/// settings.
#[error(transparent)]
WsHttpSamePortError(#[from] WsHttpSamePortError),
/// Thrown when IPC server fails to start.
// #[error(transparent)]
// IpcServerError(#[from] IpcServerStartError),
/// Custom error.
#[error("{0}")]
Custom(String),
}
impl RpcError {
/// Converts an [`io::Error`] to a more descriptive `RpcError`.
pub fn server_error(io_error: io::Error, kind: ServerKind) -> Self {
if io_error.kind() == ErrorKind::AddrInUse {
return Self::AddressAlreadyInUse {
kind,
error: io_error,
};
}
Self::ServerError {
kind,
error: io_error,
}
}
}
/// Conflicting modules between http and ws servers.
#[derive(Debug)]
pub struct ConflictingModules {
/// Modules present in both http and ws.
pub overlap: HashSet<EthRpcModule>,
/// Modules present in http but not in ws.
pub http_not_ws: HashSet<EthRpcModule>,
/// Modules present in ws but not in http.
pub ws_not_http: HashSet<EthRpcModule>,
}
impl std::fmt::Display for ConflictingModules {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"different API modules for HTTP and WS on the same port is currently not supported: \
Overlap: {:?}, \
HTTP modules not present in WS: {:?} \
WS modules not present in HTTP: {:?}
",
self.overlap, self.http_not_ws, self.ws_not_http
)
}
}
/// Errors when trying to launch ws and http server on the same port.
#[derive(Debug, thiserror::Error)]
pub enum WsHttpSamePortError {
/// Ws and http server configured on same port but with different cors
/// domains.
#[error(
"CORS domains for HTTP and WS are different, but they are on the same port: \
HTTP: {http_cors_domains:?}, WS: {ws_cors_domains:?}"
)]
ConflictingCorsDomains {
/// Http cors domains.
http_cors_domains: Option<String>,
/// Ws cors domains.
ws_cors_domains: Option<String>,
},
/// Ws and http server configured on same port but with different modules.
#[error("{0}")]
ConflictingModules(Box<ConflictingModules>),
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::{Ipv4Addr, SocketAddrV4};
#[test]
fn test_address_in_use_message() {
let addr = SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::new(127, 0, 0, 1),
1234,
));
let kinds = [
ServerKind::Http(addr),
ServerKind::WS(addr),
ServerKind::WsHttp(addr),
ServerKind::Auth(addr),
];
for kind in &kinds {
let err = RpcError::AddressAlreadyInUse {
kind: *kind,
error: io::Error::from(ErrorKind::AddrInUse),
};
assert!(err.to_string().contains(kind.flags()));
}
}
}