Skip to main content

hopr_protocol_hopr/
errors.rs

1use hopr_api::{
2    Address,
3    types::{
4        crypto::{prelude::HalfKeyChallenge, types::OffchainPublicKey},
5        internal::prelude::ChannelId,
6        primitive::balance::HoprBalance,
7    },
8};
9use hopr_crypto_packet::errors::TicketValidationError;
10use thiserror::Error;
11
12/// Error that can occur when processing an incoming packet.
13#[derive(Debug, strum::EnumIs, strum::EnumTryAs, Error)]
14pub enum IncomingPacketError<E: std::error::Error> {
15    /// Packet is not decodable.
16    ///
17    /// Such errors are fatal and therefore the packet cannot be acknowledged.
18    #[error("packet is not decodable: {0}")]
19    Undecodable(E),
20    /// Packet is decodable but cannot be processed due to other reasons.
21    ///
22    /// Such errors are protocol-related and packets must be acknowledged.
23    #[error("packet from {0} decodable, but cannot be processed: {1}")]
24    ProcessingError(Box<OffchainPublicKey>, E),
25    /// Packet is decodable, but the ticket is invalid.
26    #[error("packet from {0} is decodable, but the ticket is invalid: {1}")]
27    InvalidTicket(Box<OffchainPublicKey>, TicketValidationError),
28}
29
30impl<E: std::error::Error> IncomingPacketError<E> {
31    /// Packet is undecodable and should NOT be acknowledged.
32    pub fn undecodable<Err: Into<E>>(error: Err) -> Self {
33        Self::Undecodable(error.into())
34    }
35}
36
37#[derive(Error, Debug)]
38pub enum HoprProtocolError {
39    #[error("packet is in invalid state: {0}")]
40    InvalidState(&'static str),
41
42    #[error("cannot decode the sender address of the packet")]
43    InvalidSender,
44
45    #[error("failed to resolve chain key or packet key")]
46    KeyNotFound,
47
48    #[error("channel {0} does not have at least {1} to create a ticket")]
49    OutOfFunds(ChannelId, HoprBalance),
50
51    #[error("packet replay detected")]
52    Replay,
53
54    #[error("failed to find channel {0} -> {1}")]
55    ChannelNotFound(Address, Address),
56
57    #[error("could not find unacknowledged ticket for challenge {0}")]
58    UnacknowledgedTicketNotFound(HalfKeyChallenge),
59
60    #[error("chain resolver error: {0}")]
61    ResolverError(#[source] anyhow::Error),
62
63    #[error("ticket factory error: {0}")]
64    TicketFactoryError(#[source] anyhow::Error),
65
66    #[error(transparent)]
67    TicketValidationError(#[from] TicketValidationError),
68
69    #[error(transparent)]
70    CoreTypesError(#[from] hopr_api::types::internal::errors::CoreTypesError),
71
72    #[error(transparent)]
73    PacketError(#[from] hopr_crypto_packet::errors::PacketError),
74
75    #[error(transparent)]
76    GeneralError(#[from] hopr_api::types::primitive::errors::GeneralError),
77
78    #[error("rayon thread pool queue full: {0}")]
79    SpawnError(#[from] hopr_utils::parallelize::cpu::SpawnError),
80}
81
82impl HoprProtocolError {
83    /// Convenience method to create chain resolver error.
84    pub fn resolver<E: Into<anyhow::Error>>(e: E) -> Self {
85        Self::ResolverError(e.into())
86    }
87
88    /// Convenience method to create ticket factory error.
89    pub fn ticket_factory<E: Into<anyhow::Error>>(e: E) -> Self {
90        Self::TicketFactoryError(e.into())
91    }
92}