tycho_network/proto/
overlay.rs

1use std::sync::Arc;
2
3use tl_proto::{TlRead, TlWrite};
4use tycho_util::tl;
5
6use crate::types::PeerId;
7
8/// A data to sign for [`PublicEntry`].
9#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, TlRead, TlWrite)]
10#[tl(boxed, id = "overlay.publicEntryToSign", scheme = "proto.tl")]
11pub struct PublicEntryToSign<'tl> {
12    /// Public overlay id.
13    pub overlay_id: &'tl [u8; 32],
14    /// Node public key.
15    pub peer_id: &'tl PeerId,
16    /// Unix timestamp when the info was generated.
17    pub created_at: u32,
18}
19
20/// A public overlay entry.
21#[derive(Debug, Clone, Hash, PartialEq, Eq, TlRead, TlWrite)]
22#[tl(boxed, id = "overlay.publicEntry", scheme = "proto.tl")]
23pub struct PublicEntry {
24    /// Node public key.
25    pub peer_id: PeerId,
26    /// Unix timestamp when the info was generated.
27    pub created_at: u32,
28    /// A signature of the [`PublicEntryToSign`] (as boxed).
29    #[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/// A list of public overlay entries.
42#[derive(Debug, Clone, Hash, PartialEq, Eq, TlRead, TlWrite)]
43#[tl(boxed, scheme = "proto.tl")]
44pub enum PublicEntriesResponse {
45    // TODO: Rename to `found`.
46    #[tl(id = "overlay.publicEntries")]
47    PublicEntries(#[tl(with = "tl::VecWithMaxLen::<20>")] Vec<Arc<PublicEntry>>),
48    #[tl(id = "overlay.overlayNotFound")]
49    OverlayNotFound,
50}
51
52/// A single public overlay entry.
53#[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
62/// Overlay RPC models.
63pub mod rpc {
64    use super::*;
65
66    /// Exchanges random entries of the specified public overlay.
67    #[derive(Debug, Clone, TlRead, TlWrite)]
68    #[tl(boxed, id = "overlay.exchangeRandomPublicEntries", scheme = "proto.tl")]
69    pub struct ExchangeRandomPublicEntries {
70        /// Public overlay id.
71        pub overlay_id: [u8; 32],
72        /// A list of public overlay entries.
73        #[tl(with = "tl::VecWithMaxLen::<20>")]
74        pub entries: Vec<Arc<PublicEntry>>,
75    }
76
77    /// Get peer entry of the specified public overlay.
78    #[derive(Debug, Clone, TlRead, TlWrite)]
79    #[tl(boxed, id = "overlay.getPublicEntry", scheme = "proto.tl")]
80    pub struct GetPublicEntry {
81        /// Public overlay id.
82        pub overlay_id: [u8; 32],
83    }
84
85    /// Overlay query/message prefix with an overlay id.
86    #[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}