tycho_network/proto/
overlay.rs1use std::sync::Arc;
2
3use tl_proto::{TlRead, TlWrite};
4use tycho_util::tl;
5
6use crate::types::PeerId;
7
8#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, TlRead, TlWrite)]
10#[tl(boxed, id = "overlay.publicEntryToSign", scheme = "proto.tl")]
11pub struct PublicEntryToSign<'tl> {
12 pub overlay_id: &'tl [u8; 32],
14 pub peer_id: &'tl PeerId,
16 pub created_at: u32,
18}
19
20#[derive(Debug, Clone, Hash, PartialEq, Eq, TlRead, TlWrite)]
22#[tl(boxed, id = "overlay.publicEntry", scheme = "proto.tl")]
23pub struct PublicEntry {
24 pub peer_id: PeerId,
26 pub created_at: u32,
28 #[tl(signature, with = "tl::signature_owned")]
30 pub signature: Box<[u8; 64]>,
31}
32
33impl PublicEntry {
34 pub fn is_expired(&self, at: u32, ttl_sec: u32) -> bool {
35 const CLOCK_THRESHOLD: u32 = 1;
36
37 self.created_at > at + CLOCK_THRESHOLD || self.created_at.saturating_add(ttl_sec) < at
38 }
39}
40
41#[derive(Debug, Clone, Hash, PartialEq, Eq, TlRead, TlWrite)]
43#[tl(boxed, scheme = "proto.tl")]
44pub enum PublicEntriesResponse {
45 #[tl(id = "overlay.publicEntries")]
47 PublicEntries(#[tl(with = "tl::VecWithMaxLen::<20>")] Vec<Arc<PublicEntry>>),
48 #[tl(id = "overlay.overlayNotFound")]
49 OverlayNotFound,
50}
51
52#[derive(Debug, Clone, Hash, PartialEq, Eq, TlRead, TlWrite)]
54#[tl(boxed, scheme = "proto.tl")]
55pub enum PublicEntryResponse {
56 #[tl(id = "overlay.publicEntry.found")]
57 Found(Arc<PublicEntry>),
58 #[tl(id = "overlay.publicEntry.overlayNotFound")]
59 OverlayNotFound,
60}
61
62pub mod rpc {
64 use super::*;
65
66 #[derive(Debug, Clone, TlRead, TlWrite)]
68 #[tl(boxed, id = "overlay.exchangeRandomPublicEntries", scheme = "proto.tl")]
69 pub struct ExchangeRandomPublicEntries {
70 pub overlay_id: [u8; 32],
72 #[tl(with = "tl::VecWithMaxLen::<20>")]
74 pub entries: Vec<Arc<PublicEntry>>,
75 }
76
77 #[derive(Debug, Clone, TlRead, TlWrite)]
79 #[tl(boxed, id = "overlay.getPublicEntry", scheme = "proto.tl")]
80 pub struct GetPublicEntry {
81 pub overlay_id: [u8; 32],
83 }
84
85 #[derive(Debug, Clone, TlRead, TlWrite)]
87 #[tl(boxed, id = "overlay.prefix", scheme = "proto.tl")]
88 pub struct Prefix<'tl> {
89 pub overlay_id: &'tl [u8; 32],
90 }
91}