floresta_wire/p2p_wire/
error.rs

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
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::{self};
use std::io;
use std::net::IpAddr;

use floresta_chain::BlockchainError;
use floresta_common::impl_error_from;
use floresta_compact_filters::IterableFilterStoreError;
use thiserror::Error;

use super::peer::PeerError;
use super::transport::TransportError;
use crate::node::NodeRequest;

#[derive(Error, Debug)]
pub enum WireError {
    #[error("Blockchain error")]
    Blockchain(BlockchainError),
    #[error("Error while writing into a channel")]
    ChannelSend(tokio::sync::mpsc::error::SendError<NodeRequest>),
    #[error("Peer error")]
    PeerError(PeerError),
    #[error("Coinbase didn't mature")]
    CoinbaseNotMatured,
    #[error("Peer not found in our current connections")]
    PeerNotFound,
    #[error("We don't have any peers")]
    NoPeersAvailable,
    #[error("Our peer is misbehaving")]
    PeerMisbehaving,
    #[error("Failed to init Utreexo peers: anchors.json does not exist yet")]
    AnchorFileNotFound,
    #[error("Peer {0}:{1} already exists")]
    PeerAlreadyExists(IpAddr, u16),
    #[error("Peer {0}:{1} not found")]
    PeerNotFoundAtAddress(IpAddr, u16),
    #[error("Generic io error: {0}")]
    Io(std::io::Error),
    #[error("{0}")]
    Serde(serde_json::Error),
    #[error("Failed to save Utreexo peers: no peers to save to anchors.json")]
    NoUtreexoPeersAvailable,
    #[error("We couldn't find a peer to send the request")]
    NoPeerToSendRequest,
    #[error("Peer timed out")]
    PeerTimeout,
    #[error("Compact block filters error")]
    CompactBlockFiltersError(IterableFilterStoreError),
    #[error("Poisoned lock")]
    PoisonedLock,
    #[error("We couldn't parse the provided address due to: {0}")]
    InvalidAddress(AddrParseError),
    #[error("Transport error: {0}")]
    Transport(TransportError),
    #[error("Can't send back response for user request")]
    ResponseSendError,
    #[error("No addresses available")]
    NoAddressesAvailable,
}

impl_error_from!(WireError, PeerError, PeerError);
impl_error_from!(WireError, BlockchainError, Blockchain);
impl_error_from!(
    WireError,
    IterableFilterStoreError,
    CompactBlockFiltersError
);
impl_error_from!(WireError, AddrParseError, InvalidAddress);

impl From<tokio::sync::mpsc::error::SendError<NodeRequest>> for WireError {
    fn from(error: tokio::sync::mpsc::error::SendError<NodeRequest>) -> Self {
        WireError::ChannelSend(error)
    }
}

impl From<serde_json::Error> for WireError {
    fn from(err: serde_json::Error) -> WireError {
        WireError::Serde(err)
    }
}

impl From<io::Error> for WireError {
    fn from(err: io::Error) -> WireError {
        WireError::Io(err)
    }
}

impl From<tokio::sync::oneshot::error::RecvError> for WireError {
    fn from(_: tokio::sync::oneshot::error::RecvError) -> Self {
        WireError::ResponseSendError
    }
}

impl From<TransportError> for WireError {
    fn from(e: TransportError) -> Self {
        match e {
            TransportError::Io(io) => WireError::Io(io),
            other => WireError::Transport(other),
        }
    }
}

#[derive(Debug, Clone)]
pub enum AddrParseError {
    InvalidIpv6,
    InvalidIpv4,
    InvalidHostname,
    InvalidPort,
    Inconclusive,
}

impl Display for AddrParseError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            AddrParseError::InvalidIpv6 => write!(f, "Invalid ipv6"),
            AddrParseError::InvalidIpv4 => write!(f, "Invalid ipv4"),
            AddrParseError::InvalidHostname => write!(f, "Invalid hostname"),
            AddrParseError::InvalidPort => write!(f, "Invalid port"),
            AddrParseError::Inconclusive => write!(f, "Inconclusive"),
        }
    }
}