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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::{error, io, net, fmt};
use libc::{ENFILE, EMFILE};
use io::IoError;
use {tetsy_rlp, crypto, snappy};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DisconnectReason
{
DisconnectRequested,
TCPError,
BadProtocol,
UselessPeer,
TooManyPeers,
DuplicatePeer,
IncompatibleProtocol,
NullIdentity,
ClientQuit,
UnexpectedIdentity,
LocalIdentity,
PingTimeout,
Unknown,
}
impl DisconnectReason {
pub fn from_u8(n: u8) -> DisconnectReason {
match n {
0 => DisconnectReason::DisconnectRequested,
1 => DisconnectReason::TCPError,
2 => DisconnectReason::BadProtocol,
3 => DisconnectReason::UselessPeer,
4 => DisconnectReason::TooManyPeers,
5 => DisconnectReason::DuplicatePeer,
6 => DisconnectReason::IncompatibleProtocol,
7 => DisconnectReason::NullIdentity,
8 => DisconnectReason::ClientQuit,
9 => DisconnectReason::UnexpectedIdentity,
10 => DisconnectReason::LocalIdentity,
11 => DisconnectReason::PingTimeout,
_ => DisconnectReason::Unknown,
}
}
}
impl fmt::Display for DisconnectReason {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::DisconnectReason::*;
let msg = match *self {
DisconnectRequested => "disconnect requested",
TCPError => "TCP error",
BadProtocol => "bad protocol",
UselessPeer => "useless peer",
TooManyPeers => "too many peers",
DuplicatePeer => "duplicate peer",
IncompatibleProtocol => "incompatible protocol",
NullIdentity => "null identity",
ClientQuit => "client quit",
UnexpectedIdentity => "unexpected identity",
LocalIdentity => "local identity",
PingTimeout => "ping timeout",
Unknown => "unknown",
};
f.write_str(msg)
}
}
#[derive(Debug, derive_more::Display)]
pub enum Error {
SocketIo(IoError),
Decompression(snappy::InvalidInput),
Rlp(tetsy_rlp::DecoderError),
#[display(fmt = "Failed to parse network address")]
AddressParse,
#[display(fmt = "Failed to resolve network address {}", _0)]
AddressResolve(AddressResolveError),
#[display(fmt = "Authentication failure")]
Auth,
#[display(fmt = "Bad protocol")]
BadProtocol,
#[display(fmt = "Expired message")]
Expired,
#[display(fmt = "Peer not found")]
PeerNotFound,
#[display(fmt = "Peer disconnected: {}", _0)]
Disconnect(DisconnectReason),
#[display(fmt = "Invalid node id")]
InvalidNodeId,
#[display(fmt = "Packet is too large")]
OversizedPacket,
#[display(fmt = "Too many open files in this process. Check your resource limits and restart tetsy")]
ProcessTooManyFiles,
#[display(fmt = "Too many open files on system. Consider closing some processes/release some file handlers or increas the system-wide resource limits and restart tetsy.")]
SystemTooManyFiles,
#[display(fmt = "Unexpected IO error: {}", _0)]
Io(io::Error),
}
#[derive(Debug)]
pub struct AddressResolveError(Option<io::Error>);
impl fmt::Display for AddressResolveError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.0.as_ref().map_or("".to_string(), |e| e.to_string()))
}
}
impl From<Option<io::Error>> for AddressResolveError {
fn from(err: Option<io::Error>) -> Self {
AddressResolveError(err)
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Decompression(e) => Some(e),
Error::Rlp(e) => Some(e),
_ => None,
}
}
}
impl From<IoError> for Error {
fn from(err: IoError) -> Self {
Error::SocketIo(err)
}
}
impl From<snappy::InvalidInput> for Error {
fn from(err: snappy::InvalidInput) -> Self {
Error::Decompression(err)
}
}
impl From<tetsy_rlp::DecoderError> for Error {
fn from(err: tetsy_rlp::DecoderError) -> Self {
Error::Rlp(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
match err.raw_os_error() {
Some(ENFILE) => Error::ProcessTooManyFiles,
Some(EMFILE) => Error::SystemTooManyFiles,
_ => Error::Io(err)
}
}
}
impl From<crypto::publickey::Error> for Error {
fn from(_err: crypto::publickey::Error) -> Self {
Error::Auth
}
}
impl From<crypto::error::SymmError> for Error {
fn from(_err: crypto::error::SymmError) -> Self {
Error::Auth
}
}
impl From<net::AddrParseError> for Error {
fn from(_err: net::AddrParseError) -> Self { Error::AddressParse }
}
#[test]
fn test_errors() {
assert_eq!(DisconnectReason::ClientQuit, DisconnectReason::from_u8(8));
let mut r = DisconnectReason::DisconnectRequested;
for i in 0 .. 20 {
r = DisconnectReason::from_u8(i);
}
assert_eq!(DisconnectReason::Unknown, r);
match <Error as From<tetsy_rlp::DecoderError>>::from(tetsy_rlp::DecoderError::RlpIsTooBig) {
Error::Rlp(_) => {},
_ => panic!("Unexpected error"),
}
match <Error as From<crypto::publickey::Error>>::from(crypto::publickey::Error::InvalidMessage) {
Error::Auth => {},
_ => panic!("Unexpected error"),
}
}
#[test]
fn test_io_errors() {
use libc::{EMFILE, ENFILE};
assert_matches!(
<Error as From<io::Error>>::from(
io::Error::from_raw_os_error(ENFILE)
),
Error::ProcessTooManyFiles);
assert_matches!(
<Error as From<io::Error>>::from(
io::Error::from_raw_os_error(EMFILE)
),
Error::SystemTooManyFiles);
assert_matches!(
<Error as From<io::Error>>::from(
io::Error::from_raw_os_error(0)
),
Error::Io(_));
}