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

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

use super::peer::PeerError;
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("Generic io error")]
    Io(std::io::Error),
    #[error("We don't have any utreexo peers")]
    NoUtreexoPeersAvailable,
    #[error("We couldn't find a peer to send the request")]
    NoPeerToSendRequest,
    #[error("Peer timed out")]
    PeerTimeout,
    #[error("Compact block filters error")]
    CompactBlockFiltersError(IteratableFilterStoreError),
    #[error("Poisoned lock")]
    PoisonedLock,
    #[error("We couldn't parse the provided address due to: {0}")]
    InvalidAddress(AddrParseError),
}

impl_error_from!(WireError, PeerError, PeerError);
impl_error_from!(WireError, BlockchainError, Blockchain);
impl_error_from!(
    WireError,
    IteratableFilterStoreError,
    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<io::Error> for WireError {
    fn from(err: io::Error) -> WireError {
        WireError::Io(err)
    }
}

#[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"),
        }
    }
}