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    /// Packet is a replay of one already seen.
29    ///
30    /// Unlike [`IncomingPacketError::ProcessingError`], this comes from the replay token: usually detected before
31    /// the header MAC is verified, so the packet is unauthenticated, but also when a concurrent decode of the same
32    /// packet recorded the token first - and in that case the packet *has* authenticated. Either way it must not be
33    /// acknowledged: acknowledging an unauthenticated copy pays for a packet nobody has proved, and acknowledging
34    /// the authenticated loser re-pays for one whose original delivery was already acknowledged. Both would also
35    /// hide from the sender that its retransmission - which carries a fresh token - is the one to deliver.
36    #[error("packet from {0} is a replay")]
37    Replay(Box<OffchainPublicKey>),
38}
39
40impl<E: std::error::Error> IncomingPacketError<E> {
41    /// Packet is undecodable and should NOT be acknowledged.
42    pub fn undecodable<Err: Into<E>>(error: Err) -> Self {
43        Self::Undecodable(error.into())
44    }
45}
46
47#[derive(Error, Debug, strum::EnumIs)]
48pub enum HoprProtocolError {
49    #[error("packet is in invalid state: {0}")]
50    InvalidState(&'static str),
51
52    #[error("cannot decode the sender address of the packet")]
53    InvalidSender,
54
55    #[error("failed to resolve chain key or packet key")]
56    KeyNotFound,
57
58    #[error("channel {0} does not have at least {1} to create a ticket")]
59    OutOfFunds(ChannelId, HoprBalance),
60
61    #[error("failed to find channel {0} -> {1}")]
62    ChannelNotFound(Address, Address),
63
64    #[error("could not find unacknowledged ticket for challenge {0}")]
65    UnacknowledgedTicketNotFound(HalfKeyChallenge),
66
67    /// A ticket with this index (channel id, epoch, index) was already accepted and is still remembered
68    /// by the replay tracker.
69    #[error("ticket index {2} in channel {0} epoch {1} was already accepted")]
70    DuplicateTicketIndex(ChannelId, u32, u64),
71
72    #[error("chain resolver error: {0}")]
73    ResolverError(#[source] anyhow::Error),
74
75    #[error("ticket factory error: {0}")]
76    TicketFactoryError(#[source] anyhow::Error),
77
78    #[error(transparent)]
79    TicketValidationError(#[from] TicketValidationError),
80
81    #[error(transparent)]
82    CoreTypesError(#[from] hopr_api::types::internal::errors::CoreTypesError),
83
84    #[error(transparent)]
85    PacketError(#[from] hopr_crypto_packet::errors::PacketError),
86
87    #[error(transparent)]
88    GeneralError(#[from] hopr_api::types::primitive::errors::GeneralError),
89
90    #[error("rayon thread pool queue full: {0}")]
91    SpawnError(#[from] hopr_utils::parallelize::cpu::SpawnError),
92}
93
94impl HoprProtocolError {
95    /// Convenience method to create chain resolver error.
96    pub fn resolver<E: Into<anyhow::Error>>(e: E) -> Self {
97        Self::ResolverError(e.into())
98    }
99
100    /// Convenience method to create ticket factory error.
101    pub fn ticket_factory<E: Into<anyhow::Error>>(e: E) -> Self {
102        Self::TicketFactoryError(e.into())
103    }
104}