hopr_protocol_hopr/
types.rs

1use hopr_crypto_packet::prelude::*;
2use hopr_crypto_types::prelude::*;
3use hopr_internal_types::prelude::*;
4
5/// Packet that is being sent out by us.
6pub struct OutgoingPacket {
7    /// Offchain public key of the next hop.
8    pub next_hop: OffchainPublicKey,
9    /// Challenge to be solved from the acknowledgement of the next hop.
10    pub ack_challenge: HalfKeyChallenge,
11    /// Encoded HOPR packet.
12    pub data: Box<[u8]>,
13}
14
15impl std::fmt::Debug for OutgoingPacket {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        f.debug_struct("OutgoingPacket")
18            .field("next_hop", &self.next_hop)
19            .field("ack_challenge", &self.ack_challenge)
20            .finish_non_exhaustive()
21    }
22}
23
24/// Contains some miscellaneous information about a received packet.
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
26pub struct AuxiliaryPacketInfo {
27    /// Packet signals that the packet carried.
28    ///
29    /// Zero if no signal flags were specified.
30    pub packet_signals: PacketSignals,
31    /// Number of SURBs that the packet carried.
32    pub num_surbs: usize,
33}
34
35/// An incoming packet with a payload intended for us.
36pub struct IncomingFinalPacket {
37    /// Packet tag.
38    pub packet_tag: PacketTag,
39    /// Offchain public key of the previous hop.
40    pub previous_hop: OffchainPublicKey,
41    /// Sender pseudonym.
42    pub sender: HoprPseudonym,
43    /// Plain text payload of the packet.
44    pub plain_text: Box<[u8]>,
45    /// Acknowledgement to be sent to the previous hop.
46    pub ack_key: HalfKey,
47    /// Miscellaneous information about the packet.
48    pub info: AuxiliaryPacketInfo,
49}
50
51impl std::fmt::Debug for IncomingFinalPacket {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        f.debug_struct("IncomingFinalPacket")
54            .field("packet_tag", &self.packet_tag)
55            .field("previous_hop", &self.previous_hop)
56            .field("sender", &self.sender)
57            .field("ack_key", &self.ack_key)
58            .field("info", &self.info)
59            .finish_non_exhaustive()
60    }
61}
62
63/// Incoming packet that must be forwarded.
64pub struct IncomingForwardedPacket {
65    /// Packet tag.
66    pub packet_tag: PacketTag,
67    /// Offchain public key of the previous hop.
68    pub previous_hop: OffchainPublicKey,
69    /// Offchain public key of the next hop.
70    pub next_hop: OffchainPublicKey,
71    /// Data to be forwarded to the next hop.
72    pub data: Box<[u8]>,
73    /// Challenge to be solved from the acknowledgement received from the next hop.
74    pub ack_challenge: HalfKeyChallenge,
75    /// Ticket to be acknowledged by solving the `ack_challenge`.
76    pub received_ticket: UnacknowledgedTicket,
77    /// Acknowledgement payload to be sent to the previous hop
78    pub ack_key_prev_hop: HalfKey,
79}
80
81impl std::fmt::Debug for IncomingForwardedPacket {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        f.debug_struct("IncomingForwardedPacket")
84            .field("packet_tag", &self.packet_tag)
85            .field("previous_hop", &self.previous_hop)
86            .field("next_hop", &self.next_hop)
87            .field("received_ticket", &self.received_ticket)
88            .field("ack_challenge", &self.ack_challenge)
89            .field("ack_key_prev_hop", &self.ack_key_prev_hop)
90            .finish_non_exhaustive()
91    }
92}
93
94/// Incoming packet that represents an acknowledgement of a delivered packet.
95pub struct IncomingAcknowledgementPacket {
96    /// Packet tag.
97    pub packet_tag: PacketTag,
98    /// Offchain public key of the previous hop which sent the acknowledgement.
99    pub previous_hop: OffchainPublicKey,
100    /// Unverified acknowledgement.
101    pub received_ack: Acknowledgement,
102}
103
104impl std::fmt::Debug for IncomingAcknowledgementPacket {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        f.debug_struct("IncomingAcknowledgementPacket")
107            .field("packet_tag", &self.packet_tag)
108            .field("previous_hop", &self.previous_hop)
109            .field("ack", &self.received_ack)
110            .finish()
111    }
112}
113
114/// Incoming HOPR packet.
115#[derive(Debug)]
116pub enum IncomingPacket {
117    /// Packet is intended for us
118    Final(Box<IncomingFinalPacket>),
119    /// Packet must be forwarded
120    Forwarded(Box<IncomingForwardedPacket>),
121    /// The packet contains an acknowledgement of a delivered packet.
122    Acknowledgement(Box<IncomingAcknowledgementPacket>),
123}
124
125impl IncomingPacket {
126    /// Tag identifying the packet.
127    pub fn packet_tag(&self) -> &PacketTag {
128        match self {
129            IncomingPacket::Final(f) => &f.packet_tag,
130            IncomingPacket::Forwarded(f) => &f.packet_tag,
131            IncomingPacket::Acknowledgement(f) => &f.packet_tag,
132        }
133    }
134
135    /// Previous hop that sent us the packet.
136    pub fn previous_hop(&self) -> &OffchainPublicKey {
137        match self {
138            IncomingPacket::Final(f) => &f.previous_hop,
139            IncomingPacket::Forwarded(f) => &f.previous_hop,
140            IncomingPacket::Acknowledgement(f) => &f.previous_hop,
141        }
142    }
143}
144
145/// Contains a SURB found in the SURB ring buffer via  [`SurbStore::find_surb`].
146#[derive(Debug)]
147pub struct FoundSurb {
148    /// Complete sender ID of the SURB.
149    pub sender_id: HoprSenderId,
150    /// The SURB itself.
151    pub surb: HoprSurb,
152    /// Number of SURBs remaining in the ring buffer with the same pseudonym.
153    pub remaining: usize,
154}
155
156/// Determines the result of how an acknowledgement was resolved.
157#[derive(Debug)]
158pub enum ResolvedAcknowledgement {
159    /// The acknowledgement resulted in a winning ticket.
160    RelayingWin(Box<RedeemableTicket>),
161    /// The acknowledgement resulted in a losing ticket.
162    RelayingLoss(ChannelId),
163}