1use std::fmt::Formatter;
2
3pub use hopr_crypto_packet::{
4 HoprSurb,
5 prelude::{HoprSenderId, PacketSignals},
6};
7use hopr_crypto_types::prelude::*;
8use hopr_internal_types::prelude::*;
9use hopr_network_types::prelude::{ResolvedTransportRouting, SurbMatcher};
10use hopr_primitive_types::balance::HoprBalance;
11
12use crate::chain::{ChainKeyOperations, ChainReadChannelOperations, ChainValues};
13
14#[derive(Debug)]
16pub struct FoundSurb {
17 pub sender_id: HoprSenderId,
19 pub surb: HoprSurb,
21 pub remaining: usize,
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd)]
27pub struct SurbCacheConfig {
28 pub rb_capacity: usize,
30 pub distress_threshold: usize,
33}
34
35#[derive(Debug, strum::EnumIs, strum::EnumTryAs)]
37pub enum IncomingPacketError<E> {
38 Undecodable(E),
42 ProcessingError(E),
46}
47
48impl<E: std::fmt::Display> std::fmt::Display for IncomingPacketError<E> {
49 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50 match self {
51 IncomingPacketError::Undecodable(e) => write!(f, "undecodable packet: {e}"),
52 IncomingPacketError::ProcessingError(e) => write!(f, "packet processing error: {e}"),
53 }
54 }
55}
56
57impl<E: std::error::Error> std::error::Error for IncomingPacketError<E> {}
58
59#[async_trait::async_trait]
62#[auto_impl::auto_impl(&, Box, Arc)]
63pub trait HoprDbProtocolOperations {
64 type Error: std::error::Error + Send + Sync + 'static;
65
66 async fn handle_acknowledgement<R>(
73 &self,
74 ack: VerifiedAcknowledgement,
75 chain_resolver: &R,
76 ) -> Result<(), Self::Error>
77 where
78 R: ChainReadChannelOperations + ChainValues + Send + Sync;
79
80 async fn find_surb(&self, matcher: SurbMatcher) -> Result<FoundSurb, Self::Error>;
82
83 fn get_surb_config(&self) -> SurbCacheConfig;
85
86 async fn to_send_no_ack<R>(
88 &self,
89 data: Box<[u8]>,
90 destination: OffchainPublicKey,
91 resolver: &R,
92 ) -> Result<OutgoingPacket, Self::Error>
93 where
94 R: ChainKeyOperations + ChainValues + Send + Sync;
95
96 async fn to_send<R>(
98 &self,
99 data: Box<[u8]>,
100 routing: ResolvedTransportRouting,
101 outgoing_ticket_win_prob: Option<WinningProbability>,
102 outgoing_ticket_price: Option<HoprBalance>,
103 signals: PacketSignals,
104 resolver: &R,
105 ) -> Result<OutgoingPacket, Self::Error>
106 where
107 R: ChainReadChannelOperations + ChainKeyOperations + ChainValues + Send + Sync;
108
109 #[allow(clippy::wrong_self_convention)]
111 async fn from_recv<R>(
112 &self,
113 data: Box<[u8]>,
114 pkt_keypair: &OffchainKeypair,
115 sender: OffchainPublicKey,
116 outgoing_ticket_win_prob: Option<WinningProbability>,
117 outgoing_ticket_price: Option<HoprBalance>,
118 resolver: &R,
119 ) -> Result<IncomingPacket, IncomingPacketError<Self::Error>>
120 where
121 R: ChainReadChannelOperations + ChainKeyOperations + ChainValues + Send + Sync;
122}
123
124#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
126pub struct AuxiliaryPacketInfo {
127 pub packet_signals: PacketSignals,
131 pub num_surbs: usize,
133}
134
135#[allow(clippy::large_enum_variant)] pub enum IncomingPacket {
137 Final {
139 packet_tag: PacketTag,
140 previous_hop: OffchainPublicKey,
141 sender: HoprPseudonym,
142 plain_text: Box<[u8]>,
143 ack_key: HalfKey,
144 info: AuxiliaryPacketInfo,
145 },
146 Forwarded {
148 packet_tag: PacketTag,
149 previous_hop: OffchainPublicKey,
150 next_hop: OffchainPublicKey,
151 data: Box<[u8]>,
152 ack_key: HalfKey,
154 },
155 Acknowledgement {
157 packet_tag: PacketTag,
158 previous_hop: OffchainPublicKey,
159 ack: Acknowledgement,
160 },
161}
162
163pub struct OutgoingPacket {
165 pub next_hop: OffchainPublicKey,
166 pub ack_challenge: HalfKeyChallenge,
167 pub data: Box<[u8]>,
168}
169
170impl std::fmt::Debug for OutgoingPacket {
171 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172 f.debug_struct("OutgoingPacket")
173 .field("next_hop", &self.next_hop)
174 .field("ack_challenge", &self.ack_challenge)
175 .finish_non_exhaustive()
176 }
177}
178
179#[allow(clippy::large_enum_variant)] pub enum ResolvedAcknowledgement {
181 Sending(VerifiedAcknowledgement),
182 RelayingWin(RedeemableTicket),
183 RelayingLoss(Hash),
184}