1use crate::*;
2
3#[derive(Debug, serde::Serialize, serde::Deserialize)]
4pub struct WireDhtOpData {
6 pub op_data: holochain_types::dht_op::DhtOp,
8}
9
10impl WireDhtOpData {
11 pub fn encode(self) -> Result<bytes::Bytes, HolochainP2pError> {
13 let mut b = bytes::BufMut::writer(bytes::BytesMut::new());
14 rmp_serde::encode::write_named(&mut b, &self).map_err(HolochainP2pError::other)?;
15 Ok(b.into_inner().freeze())
16 }
17
18 pub fn decode(data: &[u8]) -> Result<Self, HolochainP2pError> {
20 rmp_serde::decode::from_slice(data).map_err(HolochainP2pError::other)
21 }
22}
23
24#[derive(Debug, serde::Serialize, serde::Deserialize)]
26pub struct WirePreflightMessage {
27 pub compat: NetworkCompatParams,
29 pub agents: Vec<String>,
31}
32
33impl WirePreflightMessage {
34 pub fn encode(&self) -> Result<bytes::Bytes, HolochainP2pError> {
36 let mut b = bytes::BufMut::writer(bytes::BytesMut::new());
37 rmp_serde::encode::write_named(&mut b, self).map_err(HolochainP2pError::other)?;
38 Ok(b.into_inner().freeze())
39 }
40
41 pub fn decode(data: &[u8]) -> Result<Self, HolochainP2pError> {
43 rmp_serde::decode::from_slice(data).map_err(HolochainP2pError::other)
44 }
45}
46
47#[derive(Debug, serde::Serialize, serde::Deserialize)]
48#[serde(tag = "type", content = "content")]
49#[allow(missing_docs)]
50pub enum WireMessage {
51 ErrorRes {
52 msg_id: u64,
53 error: String,
54 },
55 CallRemoteReq {
56 msg_id: u64,
57 to_agent: AgentPubKey,
58 zome_call_params_serialized: ExternIO,
59 signature: Signature,
60 },
61 CallRemoteRes {
62 msg_id: u64,
63 response: SerializedBytes,
64 },
65 GetReq {
66 msg_id: u64,
67 to_agent: AgentPubKey,
68 dht_hash: holo_hash::AnyDhtHash,
69 options: event::GetOptions,
70 },
71 GetRes {
72 msg_id: u64,
73 response: WireOps,
74 },
75 GetMetaReq {
76 msg_id: u64,
77 to_agent: AgentPubKey,
78 dht_hash: holo_hash::AnyDhtHash,
79 options: event::GetMetaOptions,
80 },
81 GetMetaRes {
82 msg_id: u64,
83 response: MetadataSet,
84 },
85 GetLinksReq {
86 msg_id: u64,
87 to_agent: AgentPubKey,
88 link_key: WireLinkKey,
89 options: event::GetLinksOptions,
90 },
91 GetLinksRes {
92 msg_id: u64,
93 response: WireLinkOps,
94 },
95 CountLinksReq {
96 msg_id: u64,
97 to_agent: AgentPubKey,
98 query: WireLinkQuery,
99 },
100 CountLinksRes {
101 msg_id: u64,
102 response: CountLinksResponse,
103 },
104 GetAgentActivityReq {
105 msg_id: u64,
106 to_agent: AgentPubKey,
107 agent: AgentPubKey,
108 query: ChainQueryFilter,
109 options: event::GetActivityOptions,
110 },
111 GetAgentActivityRes {
112 msg_id: u64,
113 response: AgentActivityResponse,
114 },
115 MustGetAgentActivityReq {
116 msg_id: u64,
117 to_agent: AgentPubKey,
118 agent: AgentPubKey,
119 filter: holochain_zome_types::chain::ChainFilter,
120 },
121 MustGetAgentActivityRes {
122 msg_id: u64,
123 response: MustGetAgentActivityResponse,
124 },
125 SendValidationReceiptsReq {
126 msg_id: u64,
127 to_agent: AgentPubKey,
128 receipts: ValidationReceiptBundle,
129 },
130 SendValidationReceiptsRes {
131 msg_id: u64,
132 },
133 RemoteSignalEvt {
134 to_agent: AgentPubKey,
135 zome_call_params_serialized: ExternIO,
136 signature: Signature,
137 },
138 PublishCountersignEvt {
139 op: ChainOp,
140 },
141 CountersigningSessionNegotiationEvt {
142 to_agent: AgentPubKey,
143 message: event::CountersigningSessionNegotiationMessage,
144 },
145}
146
147fn next_msg_id() -> u64 {
148 static M: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
149 M.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
150}
151
152#[allow(missing_docs)]
153impl WireMessage {
154 pub fn encode_batch(batch: &[&WireMessage]) -> Result<bytes::Bytes, HolochainP2pError> {
155 let mut b = bytes::BufMut::writer(bytes::BytesMut::new());
156 rmp_serde::encode::write_named(&mut b, batch).map_err(HolochainP2pError::other)?;
157 Ok(b.into_inner().freeze())
158 }
159
160 pub fn decode_batch(data: &[u8]) -> Result<Vec<Self>, HolochainP2pError> {
161 rmp_serde::decode::from_slice(data).map_err(HolochainP2pError::other)
162 }
163
164 pub fn get_msg_id(&self) -> Option<u64> {
165 match self {
166 WireMessage::ErrorRes { msg_id, .. } => Some(*msg_id),
167 WireMessage::CallRemoteReq { msg_id, .. } => Some(*msg_id),
168 WireMessage::CallRemoteRes { msg_id, .. } => Some(*msg_id),
169 WireMessage::GetReq { msg_id, .. } => Some(*msg_id),
170 WireMessage::GetRes { msg_id, .. } => Some(*msg_id),
171 WireMessage::GetMetaReq { msg_id, .. } => Some(*msg_id),
172 WireMessage::GetMetaRes { msg_id, .. } => Some(*msg_id),
173 WireMessage::GetLinksReq { msg_id, .. } => Some(*msg_id),
174 WireMessage::GetLinksRes { msg_id, .. } => Some(*msg_id),
175 WireMessage::CountLinksReq { msg_id, .. } => Some(*msg_id),
176 WireMessage::CountLinksRes { msg_id, .. } => Some(*msg_id),
177 WireMessage::GetAgentActivityReq { msg_id, .. } => Some(*msg_id),
178 WireMessage::GetAgentActivityRes { msg_id, .. } => Some(*msg_id),
179 WireMessage::MustGetAgentActivityReq { msg_id, .. } => Some(*msg_id),
180 WireMessage::MustGetAgentActivityRes { msg_id, .. } => Some(*msg_id),
181 WireMessage::SendValidationReceiptsReq { msg_id, .. } => Some(*msg_id),
182 WireMessage::SendValidationReceiptsRes { msg_id, .. } => Some(*msg_id),
183 _ => None,
184 }
185 }
186
187 pub fn call_remote_req(
189 to_agent: holo_hash::AgentPubKey,
190 zome_call_params_serialized: ExternIO,
191 signature: Signature,
192 ) -> (u64, WireMessage) {
193 let msg_id = next_msg_id();
194 (
195 msg_id,
196 Self::CallRemoteReq {
197 msg_id,
198 to_agent,
199 zome_call_params_serialized,
200 signature,
201 },
202 )
203 }
204
205 pub fn call_remote_res(msg_id: u64, response: SerializedBytes) -> WireMessage {
207 Self::CallRemoteRes { msg_id, response }
208 }
209
210 pub fn get_req(
212 to_agent: AgentPubKey,
213 dht_hash: holo_hash::AnyDhtHash,
214 options: event::GetOptions,
215 ) -> (u64, WireMessage) {
216 let msg_id = next_msg_id();
217 (
218 msg_id,
219 Self::GetReq {
220 msg_id,
221 to_agent,
222 dht_hash,
223 options,
224 },
225 )
226 }
227
228 pub fn get_res(msg_id: u64, response: WireOps) -> WireMessage {
230 Self::GetRes { msg_id, response }
231 }
232
233 pub fn get_meta_req(
235 to_agent: AgentPubKey,
236 dht_hash: holo_hash::AnyDhtHash,
237 options: event::GetMetaOptions,
238 ) -> (u64, WireMessage) {
239 let msg_id = next_msg_id();
240 (
241 msg_id,
242 Self::GetMetaReq {
243 msg_id,
244 to_agent,
245 dht_hash,
246 options,
247 },
248 )
249 }
250
251 pub fn get_meta_res(msg_id: u64, response: MetadataSet) -> WireMessage {
253 Self::GetMetaRes { msg_id, response }
254 }
255
256 pub fn get_links_req(
258 to_agent: AgentPubKey,
259 link_key: WireLinkKey,
260 options: event::GetLinksOptions,
261 ) -> (u64, WireMessage) {
262 let msg_id = next_msg_id();
263 (
264 msg_id,
265 Self::GetLinksReq {
266 msg_id,
267 to_agent,
268 link_key,
269 options,
270 },
271 )
272 }
273
274 pub fn get_links_res(msg_id: u64, response: WireLinkOps) -> WireMessage {
276 Self::GetLinksRes { msg_id, response }
277 }
278
279 pub fn count_links_req(to_agent: AgentPubKey, query: WireLinkQuery) -> (u64, WireMessage) {
281 let msg_id = next_msg_id();
282 (
283 msg_id,
284 Self::CountLinksReq {
285 msg_id,
286 to_agent,
287 query,
288 },
289 )
290 }
291
292 pub fn count_links_res(msg_id: u64, response: CountLinksResponse) -> WireMessage {
294 Self::CountLinksRes { msg_id, response }
295 }
296
297 pub fn get_agent_activity_req(
299 to_agent: AgentPubKey,
300 agent: AgentPubKey,
301 query: ChainQueryFilter,
302 options: event::GetActivityOptions,
303 ) -> (u64, WireMessage) {
304 let msg_id = next_msg_id();
305 (
306 msg_id,
307 Self::GetAgentActivityReq {
308 msg_id,
309 to_agent,
310 agent,
311 query,
312 options,
313 },
314 )
315 }
316
317 pub fn get_agent_activity_res(msg_id: u64, response: AgentActivityResponse) -> WireMessage {
319 Self::GetAgentActivityRes { msg_id, response }
320 }
321
322 pub fn must_get_agent_activity_req(
324 to_agent: AgentPubKey,
325 agent: AgentPubKey,
326 filter: holochain_zome_types::chain::ChainFilter,
327 ) -> (u64, WireMessage) {
328 let msg_id = next_msg_id();
329 (
330 msg_id,
331 Self::MustGetAgentActivityReq {
332 msg_id,
333 to_agent,
334 agent,
335 filter,
336 },
337 )
338 }
339
340 pub fn must_get_agent_activity_res(
342 msg_id: u64,
343 response: MustGetAgentActivityResponse,
344 ) -> WireMessage {
345 Self::MustGetAgentActivityRes { msg_id, response }
346 }
347
348 pub fn send_validation_receipts_req(
350 to_agent: holo_hash::AgentPubKey,
351 receipts: ValidationReceiptBundle,
352 ) -> (u64, WireMessage) {
353 let msg_id = next_msg_id();
354 (
355 msg_id,
356 Self::SendValidationReceiptsReq {
357 msg_id,
358 to_agent,
359 receipts,
360 },
361 )
362 }
363
364 pub fn send_validation_receipts_res() -> (u64, WireMessage) {
366 let msg_id = next_msg_id();
367 (msg_id, Self::SendValidationReceiptsRes { msg_id })
368 }
369
370 pub fn remote_signal_evt(
372 to_agent: holo_hash::AgentPubKey,
373 zome_call_params_serialized: ExternIO,
374 signature: Signature,
375 ) -> WireMessage {
376 Self::RemoteSignalEvt {
377 to_agent,
378 zome_call_params_serialized,
379 signature,
380 }
381 }
382
383 pub fn publish_countersign_evt(op: ChainOp) -> WireMessage {
385 Self::PublishCountersignEvt { op }
386 }
387
388 pub fn countersigning_session_negotiation_evt(
390 to_agent: AgentPubKey,
391 message: event::CountersigningSessionNegotiationMessage,
392 ) -> WireMessage {
393 Self::CountersigningSessionNegotiationEvt { to_agent, message }
394 }
395}