everscale_network/proto/
dht.rs

1use bytes::Bytes;
2use smallvec::SmallVec;
3use tl_proto::{BoxedConstructor, BoxedWrapper, TlRead, TlWrite};
4
5use super::{adnl, HashRef};
6
7#[derive(TlRead)]
8#[tl(boxed, scheme = "scheme.tl")]
9pub enum ValueResult<'tl> {
10    #[tl(id = "dht.valueFound")]
11    ValueFound(BoxedWrapper<Value<'tl>>),
12    #[tl(id = "dht.valueNotFound")]
13    ValueNotFound(NodesOwned),
14}
15
16#[derive(TlWrite)]
17#[tl(boxed, scheme = "scheme.tl")]
18pub enum ValueResultOwned {
19    #[tl(id = "dht.valueFound")]
20    ValueFound(BoxedWrapper<ValueOwned>),
21    #[tl(id = "dht.valueNotFound")]
22    ValueNotFound(NodesOwned),
23}
24
25#[derive(TlWrite, TlRead)]
26pub struct Nodes<'tl> {
27    pub nodes: SmallVec<[Node<'tl>; 5]>,
28}
29
30impl BoxedConstructor for Nodes<'_> {
31    const TL_ID: u32 = tl_proto::id!("dht.nodes", scheme = "scheme.tl");
32}
33
34#[derive(TlWrite, TlRead)]
35pub struct NodesOwned {
36    pub nodes: Vec<NodeOwned>,
37}
38
39impl BoxedConstructor for NodesOwned {
40    const TL_ID: u32 = Nodes::TL_ID;
41}
42
43#[derive(Debug, Copy, Clone, TlWrite, TlRead)]
44pub struct Node<'tl> {
45    pub id: everscale_crypto::tl::PublicKey<'tl>,
46    pub addr_list: adnl::AddressList,
47    pub version: u32,
48    pub signature: &'tl [u8],
49}
50
51impl BoxedConstructor for Node<'_> {
52    const TL_ID: u32 = tl_proto::id!("dht.node", scheme = "scheme.tl");
53}
54
55impl Node<'_> {
56    pub fn as_equivalent_owned(&self) -> NodeOwned {
57        NodeOwned {
58            id: self.id.as_equivalent_owned(),
59            addr_list: self.addr_list,
60            version: self.version,
61            signature: self.signature.to_vec().into(),
62        }
63    }
64}
65
66#[derive(Debug, Clone, TlWrite, TlRead)]
67pub struct NodeOwned {
68    pub id: everscale_crypto::tl::PublicKeyOwned,
69    pub addr_list: adnl::AddressList,
70    pub version: u32,
71    pub signature: Bytes,
72}
73
74impl BoxedConstructor for NodeOwned {
75    const TL_ID: u32 = Node::TL_ID;
76}
77
78impl NodeOwned {
79    pub fn as_equivalent_ref(&self) -> Node {
80        Node {
81            id: self.id.as_equivalent_ref(),
82            addr_list: self.addr_list,
83            version: self.version,
84            signature: &self.signature,
85        }
86    }
87}
88
89#[derive(Debug, Copy, Clone, TlWrite, TlRead)]
90pub struct Value<'tl> {
91    pub key: KeyDescription<'tl>,
92    pub value: &'tl [u8],
93    pub ttl: u32,
94    pub signature: &'tl [u8],
95}
96
97impl BoxedConstructor for Value<'_> {
98    const TL_ID: u32 = tl_proto::id!("dht.value", scheme = "scheme.tl");
99}
100
101impl Value<'_> {
102    pub fn as_equivalent_owned(&self) -> ValueOwned {
103        ValueOwned {
104            key: self.key.as_equivalent_owned(),
105            value: self.value.to_vec().into(),
106            ttl: self.ttl,
107            signature: self.signature.to_vec().into(),
108        }
109    }
110}
111
112#[derive(Debug, Clone, TlWrite, TlRead)]
113pub struct ValueOwned {
114    pub key: KeyDescriptionOwned,
115    pub value: Bytes,
116    pub ttl: u32,
117    pub signature: Bytes,
118}
119
120impl BoxedConstructor for ValueOwned {
121    const TL_ID: u32 = Value::TL_ID;
122}
123
124impl ValueOwned {
125    pub fn as_equivalent_ref(&self) -> Value {
126        Value {
127            key: self.key.as_equivalent_ref(),
128            value: &self.value,
129            ttl: self.ttl,
130            signature: &self.signature,
131        }
132    }
133}
134
135#[derive(Debug, Copy, Clone, TlWrite, TlRead)]
136pub struct KeyDescription<'tl> {
137    pub key: Key<'tl>,
138    pub id: everscale_crypto::tl::PublicKey<'tl>,
139    pub update_rule: UpdateRule,
140    pub signature: &'tl [u8],
141}
142
143impl BoxedConstructor for KeyDescription<'_> {
144    const TL_ID: u32 = tl_proto::id!("dht.keyDescription", scheme = "scheme.tl");
145}
146
147impl KeyDescription<'_> {
148    pub fn as_equivalent_owned(&self) -> KeyDescriptionOwned {
149        KeyDescriptionOwned {
150            key: self.key.as_equivalent_owned(),
151            id: self.id.as_equivalent_owned(),
152            update_rule: self.update_rule,
153            signature: self.signature.to_vec().into(),
154        }
155    }
156}
157
158#[derive(Debug, Clone, TlWrite, TlRead)]
159pub struct KeyDescriptionOwned {
160    pub key: KeyOwned,
161    pub id: everscale_crypto::tl::PublicKeyOwned,
162    pub update_rule: UpdateRule,
163    pub signature: Bytes,
164}
165
166impl BoxedConstructor for KeyDescriptionOwned {
167    const TL_ID: u32 = KeyDescription::TL_ID;
168}
169
170impl KeyDescriptionOwned {
171    pub fn as_equivalent_ref(&self) -> KeyDescription<'_> {
172        KeyDescription {
173            key: self.key.as_equivalent_ref(),
174            id: self.id.as_equivalent_ref(),
175            update_rule: self.update_rule,
176            signature: &self.signature,
177        }
178    }
179}
180
181#[derive(Debug, Copy, Clone, Eq, PartialEq, TlWrite, TlRead)]
182pub struct Key<'tl> {
183    #[tl(size_hint = 32)]
184    pub id: HashRef<'tl>,
185    pub name: &'tl [u8],
186    pub idx: u32,
187}
188
189impl BoxedConstructor for Key<'_> {
190    const TL_ID: u32 = tl_proto::id!("dht.key", scheme = "scheme.tl");
191}
192
193impl Key<'_> {
194    pub fn as_equivalent_owned(&self) -> KeyOwned {
195        KeyOwned {
196            id: *self.id,
197            name: self.name.to_vec().into(),
198            idx: self.idx,
199        }
200    }
201}
202
203#[derive(Debug, Clone, TlWrite, TlRead)]
204pub struct KeyOwned {
205    #[tl(size_hint = 32)]
206    pub id: [u8; 32],
207    pub name: Bytes,
208    pub idx: u32,
209}
210
211impl BoxedConstructor for KeyOwned {
212    const TL_ID: u32 = Key::TL_ID;
213}
214
215impl KeyOwned {
216    pub fn as_equivalent_ref(&self) -> Key {
217        Key {
218            id: &self.id,
219            name: &self.name,
220            idx: self.idx,
221        }
222    }
223}
224
225#[derive(Debug, Copy, Clone, Eq, PartialEq, TlWrite, TlRead)]
226#[tl(boxed, scheme = "scheme.tl")]
227pub enum UpdateRule {
228    #[tl(id = "dht.updateRule.anybody", size_hint = 0)]
229    Anybody,
230    #[tl(id = "dht.updateRule.overlayNodes", size_hint = 0)]
231    OverlayNodes,
232    #[tl(id = "dht.updateRule.signature", size_hint = 0)]
233    Signature,
234}
235
236#[derive(Copy, Clone, TlWrite, TlRead)]
237#[tl(boxed, id = "dht.pong", size_hint = 8, scheme = "scheme.tl")]
238pub struct Pong {
239    pub random_id: u64,
240}
241
242#[derive(Copy, Clone, TlWrite, TlRead)]
243#[tl(boxed, id = "dht.stored", size_hint = 0, scheme = "scheme.tl")]
244pub struct Stored;