use std::sync::Arc;
use tl_proto::{TlRead, TlWrite};
use tycho_util::tl;
use crate::types::PeerId;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, TlRead, TlWrite)]
#[tl(boxed, id = "overlay.publicEntryToSign", scheme = "proto.tl")]
pub struct PublicEntryToSign<'tl> {
pub overlay_id: &'tl [u8; 32],
pub peer_id: &'tl PeerId,
pub created_at: u32,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, TlRead, TlWrite)]
#[tl(boxed, id = "overlay.publicEntry", scheme = "proto.tl")]
pub struct PublicEntry {
pub peer_id: PeerId,
pub created_at: u32,
#[tl(signature, with = "tl::signature_owned")]
pub signature: Box<[u8; 64]>,
}
impl PublicEntry {
pub fn is_expired(&self, at: u32, ttl_sec: u32) -> bool {
const CLOCK_THRESHOLD: u32 = 1;
self.created_at > at + CLOCK_THRESHOLD || self.created_at.saturating_add(ttl_sec) < at
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, TlRead, TlWrite)]
#[tl(boxed, scheme = "proto.tl")]
pub enum PublicEntriesResponse {
#[tl(id = "overlay.publicEntries")]
PublicEntries(#[tl(with = "tl::VecWithMaxLen::<20>")] Vec<Arc<PublicEntry>>),
#[tl(id = "overlay.overlayNotFound")]
OverlayNotFound,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, TlRead, TlWrite)]
#[tl(boxed, scheme = "proto.tl")]
pub enum PublicEntryResponse {
#[tl(id = "overlay.publicEntry.found")]
Found(Arc<PublicEntry>),
#[tl(id = "overlay.publicEntry.overlayNotFound")]
OverlayNotFound,
}
pub mod rpc {
use super::*;
#[derive(Debug, Clone, TlRead, TlWrite)]
#[tl(boxed, id = "overlay.exchangeRandomPublicEntries", scheme = "proto.tl")]
pub struct ExchangeRandomPublicEntries {
pub overlay_id: [u8; 32],
#[tl(with = "tl::VecWithMaxLen::<20>")]
pub entries: Vec<Arc<PublicEntry>>,
}
#[derive(Debug, Clone, TlRead, TlWrite)]
#[tl(boxed, id = "overlay.getPublicEntry", scheme = "proto.tl")]
pub struct GetPublicEntry {
pub overlay_id: [u8; 32],
}
#[derive(Debug, Clone, TlRead, TlWrite)]
#[tl(boxed, id = "overlay.prefix", scheme = "proto.tl")]
pub struct Prefix<'tl> {
pub overlay_id: &'tl [u8; 32],
}
}