1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
pub mod wire_msg_header;
use self::wire_msg_header::{MessageKind, WireMsgHeader};
#[cfg(not(feature = "client-only"))]
use super::node;
use super::{client, section_info, DestInfo, Error, MessageId, MessageType, Result};
use bytes::Bytes;
use cookie_factory::{combinator::slice, gen_simple};
use std::fmt::Debug;
use threshold_crypto::PublicKey;
use xor_name::XorName;
#[derive(Debug, PartialEq, Clone)]
pub struct WireMsg {
header: WireMsgHeader,
payload: Bytes,
}
impl WireMsg {
pub fn new_section_info_msg(
query: §ion_info::SectionInfoMsg,
dest: XorName,
dest_section_pk: PublicKey,
) -> Result<Self> {
let payload_vec = rmp_serde::to_vec_named(&query).map_err(|err| {
Error::Serialisation(format!(
"could not serialize network info payload with Msgpack: {}",
err
))
})?;
Ok(Self {
header: WireMsgHeader::new(
MessageId::new(),
MessageKind::SectionInfo,
dest,
dest_section_pk,
None,
),
payload: Bytes::from(payload_vec),
})
}
pub fn new_client_msg(
msg: &client::ClientMsg,
dest: XorName,
dest_section_pk: PublicKey,
) -> Result<Self> {
let payload_vec = rmp_serde::to_vec_named(&msg).map_err(|err| {
Error::Serialisation(format!(
"could not serialize client message payload (id: {}) with Msgpack: {}",
msg.id(),
err
))
})?;
Ok(Self {
header: WireMsgHeader::new(msg.id(), MessageKind::Client, dest, dest_section_pk, None),
payload: Bytes::from(payload_vec),
})
}
#[cfg(not(feature = "client-only"))]
pub fn new_routing_msg(
msg: &node::RoutingMsg,
dest: XorName,
dest_section_pk: PublicKey,
) -> Result<Self> {
let payload_vec = rmp_serde::to_vec_named(&msg).map_err(|err| {
Error::Serialisation(format!(
"could not serialize node message payload with Msgpack: {}",
err
))
})?;
Ok(Self {
header: WireMsgHeader::new(msg.id, MessageKind::Routing, dest, dest_section_pk, None),
payload: Bytes::from(payload_vec),
})
}
#[cfg(not(feature = "client-only"))]
pub fn new_node_msg(
msg: &node::NodeMsg,
dest: XorName,
dest_section_pk: PublicKey,
src_section_pk: Option<PublicKey>,
) -> Result<Self> {
let payload_vec = rmp_serde::to_vec_named(&msg).map_err(|err| {
Error::Serialisation(format!(
"could not serialize a node command message payload with Msgpack: {}",
err
))
})?;
Ok(Self {
header: WireMsgHeader::new(
msg.id(),
MessageKind::Node,
dest,
dest_section_pk,
src_section_pk,
),
payload: Bytes::from(payload_vec),
})
}
pub fn from(bytes: Bytes) -> Result<Self> {
let (header, payload) = WireMsgHeader::from(bytes)?;
Ok(Self { header, payload })
}
pub fn serialize(&self) -> Result<Bytes> {
let mut buffer = vec![0u8; self.size()];
let buf_at_payload = self.header.write(&mut buffer)?;
let _ = gen_simple(slice(self.payload.clone()), buf_at_payload).map_err(|err| {
Error::Serialisation(format!("message payload couldn't be serialized: {}", err))
})?;
Ok(Bytes::from(buffer))
}
pub fn to_message(&self) -> Result<MessageType> {
let dest_info = DestInfo {
dest: self.dest(),
dest_section_pk: self.dest_section_pk(),
};
match self.header.kind() {
MessageKind::SectionInfo => {
let msg: section_info::SectionInfoMsg =
rmp_serde::from_slice(&self.payload).map_err(|err| {
Error::FailedToParse(format!(
"Client message payload as Msgpack: {}",
err
))
})?;
Ok(MessageType::SectionInfo{msg, dest_info})
}
MessageKind::Client => {
let msg: client::ClientMsg =
rmp_serde::from_slice(&self.payload).map_err(|err| {
Error::FailedToParse(format!(
"Client message payload as Msgpack: {}",
err
))
})?;
Ok(MessageType::Client{msg, dest_info})
}
#[cfg(feature = "client-only")]
MessageKind::Routing => {
Err(Error::FailedToParse("Message payload is a Node message which is not supported when feature 'client-only' is set".to_string()))
}
#[cfg(not(feature = "client-only"))]
MessageKind::Routing => {
let msg: node::RoutingMsg =
rmp_serde::from_slice(&self.payload).map_err(|err| {
Error::FailedToParse(format!("Node message payload as Msgpack: {}", err))
})?;
Ok(MessageType::Routing{msg, dest_info})
}
#[cfg(feature = "client-only")]
MessageKind::Node => {
Err(Error::FailedToParse("Message payload is a NodeCmd message which is not supported when feature 'client-only' is set".to_string()))
}
#[cfg(not(feature = "client-only"))]
MessageKind::Node => {
let node_cmd: node::NodeMsg =
rmp_serde::from_slice(&self.payload).map_err(|err| {
Error::FailedToParse(format!("NodeCmd message payload as Msgpack: {}", err))
})?;
Ok(MessageType::Node{
msg: node_cmd,
dest_info,
src_section_pk: self.src_section_pk()
})
}
}
}
pub fn msg_id(&self) -> MessageId {
self.header.msg_id()
}
pub fn dest_section_pk(&self) -> PublicKey {
self.header.dest_section_pk()
}
pub fn dest(&self) -> XorName {
self.header.dest()
}
pub fn src_section_pk(&self) -> Option<PublicKey> {
self.header.src_section_pk()
}
pub fn deserialize(bytes: Bytes) -> Result<MessageType> {
Self::from(bytes)?.to_message()
}
pub fn serialize_section_info_msg(
query: §ion_info::SectionInfoMsg,
dest: XorName,
dest_section_pk: PublicKey,
) -> Result<Bytes> {
Self::new_section_info_msg(query, dest, dest_section_pk)?.serialize()
}
pub fn serialize_client_msg(
msg: &client::ClientMsg,
dest: XorName,
dest_section_pk: PublicKey,
) -> Result<Bytes> {
Self::new_client_msg(msg, dest, dest_section_pk)?.serialize()
}
#[cfg(not(feature = "client-only"))]
pub fn serialize_routing_msg(
msg: &node::RoutingMsg,
dest: XorName,
dest_section_pk: PublicKey,
) -> Result<Bytes> {
Self::new_routing_msg(msg, dest, dest_section_pk)?.serialize()
}
#[cfg(not(feature = "client-only"))]
pub fn serialize_node_msg(
msg: &node::NodeMsg,
dest: XorName,
dest_section_pk: PublicKey,
src_section_pk: Option<PublicKey>,
) -> Result<Bytes> {
Self::new_node_msg(msg, dest, dest_section_pk, src_section_pk)?.serialize()
}
fn size(&self) -> usize {
self.header.size() as usize + self.payload.len()
}
pub fn update_dest_info(&mut self, dest_pk: Option<PublicKey>, dest: Option<XorName>) {
if let Some(dest) = dest {
self.header.dest = dest
}
if let Some(dest_pk) = dest_pk {
self.header.dest_section_pk = dest_pk
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
use threshold_crypto::SecretKey;
use xor_name::XorName;
#[test]
fn serialisation_section_info_msg() -> Result<()> {
let dest = XorName::random();
let dest_section_pk = SecretKey::random().public_key();
let query = section_info::SectionInfoMsg::GetSectionQuery(dest_section_pk.into());
let wire_msg = WireMsg::new_section_info_msg(&query, dest, dest_section_pk)?;
let serialized = wire_msg.serialize()?;
let deserialized = WireMsg::from(serialized)?;
assert_eq!(deserialized, wire_msg);
assert_eq!(deserialized.msg_id(), wire_msg.msg_id());
assert_eq!(deserialized.dest(), dest);
assert_eq!(deserialized.dest_section_pk(), dest_section_pk);
assert_eq!(deserialized.src_section_pk(), None);
assert_eq!(
deserialized.to_message()?,
MessageType::SectionInfo {
msg: query,
dest_info: DestInfo {
dest,
dest_section_pk
}
}
);
Ok(())
}
#[test]
fn serialisation_and_update_dest_for_section_info_msg() -> Result<()> {
let dest = XorName::random();
let dest_section_pk = SecretKey::random().public_key();
let query = section_info::SectionInfoMsg::GetSectionQuery(dest_section_pk.into());
let mut wire_msg = WireMsg::new_section_info_msg(&query, dest, dest_section_pk)?;
let serialized = wire_msg.serialize()?;
let wire_msg2 = wire_msg.clone();
let dest_new = XorName::random();
wire_msg.update_dest_info(None, Some(dest_new));
let serialised_second_msg = wire_msg.serialize()?;
let deserialized = WireMsg::from(serialised_second_msg.clone())?;
assert_ne!(serialized, serialised_second_msg);
assert_ne!(wire_msg2, wire_msg);
assert_eq!(deserialized.dest(), dest_new);
assert_eq!(deserialized.dest_section_pk(), dest_section_pk);
assert_eq!(deserialized.src_section_pk(), None);
assert_eq!(
deserialized.to_message()?,
MessageType::SectionInfo {
msg: query,
dest_info: DestInfo {
dest: dest_new,
dest_section_pk
}
}
);
Ok(())
}
#[test]
#[cfg(not(feature = "client-only"))]
fn serialisation_node_msg() -> Result<()> {
use crate::MessageId;
use node::{NodeCmd, NodeMsg, NodeSystemCmd};
let dest = XorName::random();
let src_section_pk = SecretKey::random().public_key();
let dest_section_pk = SecretKey::random().public_key();
let node_cmd = NodeMsg::NodeCmd {
cmd: NodeCmd::System(NodeSystemCmd::RegisterWallet(dest_section_pk.into())),
id: MessageId::new(),
};
let wire_msg = WireMsg::new_node_msg(&node_cmd, dest, dest_section_pk, None)?;
let serialized = wire_msg.serialize()?;
let deserialized = WireMsg::from(serialized)?;
assert_eq!(deserialized, wire_msg);
assert_eq!(deserialized.msg_id(), wire_msg.msg_id());
assert_eq!(deserialized.dest(), dest);
assert_eq!(deserialized.dest_section_pk(), dest_section_pk);
assert_eq!(deserialized.src_section_pk(), None);
assert_eq!(
deserialized.to_message()?,
MessageType::Node {
msg: node_cmd.clone(),
dest_info: DestInfo {
dest,
dest_section_pk
},
src_section_pk: None
}
);
let wire_msg_with_src_pk =
WireMsg::new_node_msg(&node_cmd, dest, dest_section_pk, Some(src_section_pk))?;
let serialized = wire_msg_with_src_pk.serialize()?;
let deserialized = WireMsg::from(serialized)?;
assert_eq!(deserialized, wire_msg_with_src_pk);
assert_eq!(deserialized.msg_id(), wire_msg_with_src_pk.msg_id());
assert_eq!(deserialized.dest(), dest);
assert_eq!(deserialized.dest_section_pk(), dest_section_pk);
assert_eq!(deserialized.src_section_pk(), Some(src_section_pk));
assert_eq!(
deserialized.to_message()?,
MessageType::Node {
msg: node_cmd,
dest_info: DestInfo {
dest,
dest_section_pk
},
src_section_pk: Some(src_section_pk)
}
);
Ok(())
}
}