use async_trait::async_trait;
use std::{fmt::Debug, result::Result};
use hopr_crypto_types::prelude::*;
use hopr_internal_types::prelude::*;
use crate::prelude::DbError;
#[async_trait]
pub trait HoprDbProtocolOperations {
async fn handle_acknowledgement(&self, ack: Acknowledgement, me: &ChainKeypair)
-> crate::errors::Result<AckResult>;
async fn to_send(
&self,
data: Box<[u8]>,
me: ChainKeypair,
path: Vec<OffchainPublicKey>,
outgoing_ticket_win_prob: f64,
) -> Result<TransportPacketWithChainData, DbError>;
#[allow(clippy::wrong_self_convention)]
async fn from_recv(
&self,
data: Box<[u8]>,
me: ChainKeypair,
pkt_keypair: &OffchainKeypair,
sender: OffchainPublicKey,
outgoing_ticket_win_prob: f64,
) -> crate::errors::Result<TransportPacketWithChainData>;
}
#[allow(clippy::large_enum_variant)] pub enum AckResult {
Sender(HalfKeyChallenge),
RelayerWinning(AcknowledgedTicket),
RelayerLosing,
}
impl Debug for AckResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Sender(_) => f.debug_tuple("Sender").finish(),
Self::RelayerWinning(_) => f.debug_tuple("RelayerWinning").finish(),
Self::RelayerLosing => write!(f, "RelayerLosing"),
}
}
}
pub enum TransportPacketWithChainData {
Final {
packet_tag: PacketTag,
previous_hop: OffchainPublicKey,
plain_text: Box<[u8]>,
ack: Acknowledgement,
},
Forwarded {
packet_tag: PacketTag,
previous_hop: OffchainPublicKey,
next_hop: OffchainPublicKey,
data: Box<[u8]>,
ack: Acknowledgement,
},
Outgoing {
next_hop: OffchainPublicKey,
ack_challenge: HalfKeyChallenge,
data: Box<[u8]>,
},
}
#[allow(clippy::large_enum_variant)] pub enum ResolvedAcknowledgement {
Sending(HalfKeyChallenge),
RelayingWin(AcknowledgedTicket),
RelayingLoss(Hash),
}
impl From<ResolvedAcknowledgement> for AckResult {
fn from(value: ResolvedAcknowledgement) -> Self {
match value {
ResolvedAcknowledgement::Sending(ack_challenge) => AckResult::Sender(ack_challenge),
ResolvedAcknowledgement::RelayingWin(ack_ticket) => AckResult::RelayerWinning(ack_ticket),
ResolvedAcknowledgement::RelayingLoss(_) => AckResult::RelayerLosing,
}
}
}