snarkos_errors/network/protocol/
handshake.rs

1// Copyright (C) 2019-2020 Aleo Systems Inc.
2// This file is part of the snarkOS library.
3
4// The snarkOS library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The snarkOS library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::network::{message::MessageError, ConnectError, SendError};
18use std::net::SocketAddr;
19
20#[derive(Debug, Error)]
21pub enum HandshakeError {
22    #[error("{}: {}", _0, _1)]
23    Crate(&'static str, String),
24
25    #[error("{}", _0)]
26    Message(String),
27
28    #[error("Peer disconnected {}", _0)]
29    PeerDisconnect(SocketAddr),
30
31    #[error("No handshake found for peer: {:?}", _0)]
32    HandshakeMissing(SocketAddr),
33
34    #[error("Handshake message expected. Got {:?}", _0)]
35    InvalidMessage(String),
36
37    #[error("Version message expected. Got {:?}", _0)]
38    InvalidVersion(String),
39
40    #[error("Verack message expected. Got {:?}", _0)]
41    InvalidVerack(String),
42
43    #[error("Expected nonce {}. Got {}", _0, _1)]
44    InvalidNonce(u64, u64),
45
46    #[error("{}", _0)]
47    ConnectError(ConnectError),
48
49    #[error("{}", _0)]
50    SendError(SendError),
51
52    #[error("{}", _0)]
53    MessageError(MessageError),
54}
55
56impl From<ConnectError> for HandshakeError {
57    fn from(error: ConnectError) -> Self {
58        HandshakeError::ConnectError(error)
59    }
60}
61
62impl From<MessageError> for HandshakeError {
63    fn from(error: MessageError) -> Self {
64        HandshakeError::MessageError(error)
65    }
66}
67
68impl From<SendError> for HandshakeError {
69    fn from(error: SendError) -> Self {
70        HandshakeError::SendError(error)
71    }
72}