Skip to main content

nym_node_tester_utils/
message.rs

1// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::error::NetworkTestingError;
5use crate::node::{NodeType, TestableNode};
6use nym_sphinx::message::NymMessage;
7use nym_topology::node::RoutingNode;
8use serde::de::DeserializeOwned;
9use serde::{Deserialize, Serialize};
10
11#[derive(Serialize, Deserialize, Clone, Copy)]
12pub struct Empty;
13
14#[derive(Serialize, Deserialize, Clone)]
15pub struct TestMessage<T = Empty> {
16    pub tested_node: TestableNode,
17
18    pub msg_id: u32,
19    pub total_msgs: u32,
20
21    // any additional fields that might be required by a specific tester.
22    // For example nym-api might want to attach route ids
23    #[serde(flatten)]
24    pub ext: T,
25}
26
27impl<T> TestMessage<T> {
28    pub fn new(tested_node: TestableNode, msg_id: u32, total_msgs: u32, ext: T) -> Self {
29        TestMessage {
30            tested_node,
31            msg_id,
32            total_msgs,
33            ext,
34        }
35    }
36
37    pub fn new_mix(node: &RoutingNode, msg_id: u32, total_msgs: u32, ext: T) -> Self {
38        Self::new(
39            TestableNode::new_routing(node, NodeType::Mixnode),
40            msg_id,
41            total_msgs,
42            ext,
43        )
44    }
45
46    pub fn new_gateway(node: &RoutingNode, msg_id: u32, total_msgs: u32, ext: T) -> Self {
47        Self::new(
48            TestableNode::new_routing(node, NodeType::Gateway),
49            msg_id,
50            total_msgs,
51            ext,
52        )
53    }
54
55    pub fn new_plaintexts(
56        node: TestableNode,
57        total_msgs: u32,
58        ext: T,
59    ) -> Result<Vec<Vec<u8>>, NetworkTestingError>
60    where
61        T: Serialize + Clone,
62    {
63        let mut msgs = Vec::with_capacity(total_msgs as usize);
64        for msg_id in 1..=total_msgs {
65            msgs.push(Self::new(node.clone(), msg_id, total_msgs, ext.clone()).as_bytes()?)
66        }
67        Ok(msgs)
68    }
69
70    pub fn mix_plaintexts(
71        node: &RoutingNode,
72        total_msgs: u32,
73        ext: T,
74    ) -> Result<Vec<Vec<u8>>, NetworkTestingError>
75    where
76        T: Serialize + Clone,
77    {
78        Self::new_plaintexts(
79            TestableNode::new_routing(node, NodeType::Mixnode),
80            total_msgs,
81            ext,
82        )
83    }
84
85    pub fn legacy_gateway_plaintexts(
86        node: &RoutingNode,
87        total_msgs: u32,
88        ext: T,
89    ) -> Result<Vec<Vec<u8>>, NetworkTestingError>
90    where
91        T: Serialize + Clone,
92    {
93        Self::new_plaintexts(
94            TestableNode::new_routing(node, NodeType::Gateway),
95            total_msgs,
96            ext,
97        )
98    }
99
100    pub fn as_json_string(&self) -> Result<String, NetworkTestingError>
101    where
102        T: Serialize,
103    {
104        serde_json::to_string(self).map_err(Into::into)
105    }
106
107    pub fn as_bytes(&self) -> Result<Vec<u8>, NetworkTestingError>
108    where
109        T: Serialize,
110    {
111        // the test messages are supposed to be rather small so we can use the good old serde_json
112        // (the performance penalty over bincode or custom serialization should be minimal)
113        serde_json::to_vec(self).map_err(Into::into)
114    }
115
116    pub fn try_recover(msg: NymMessage) -> Result<Self, NetworkTestingError>
117    where
118        T: DeserializeOwned,
119    {
120        let inner = msg.into_inner_data();
121        Self::try_recover_from_bytes(&inner)
122    }
123
124    pub fn try_recover_from_bytes(raw: &[u8]) -> Result<Self, NetworkTestingError>
125    where
126        T: DeserializeOwned,
127    {
128        serde_json::from_slice(raw)
129            .map_err(|source| NetworkTestingError::MalformedTestMessageReceived { source })
130    }
131}