1use crate::error::NetworkTestingError;
5use crate::TestMessage;
6use nym_sphinx::acknowledgements::AckKey;
7use nym_sphinx::addressing::clients::Recipient;
8use nym_sphinx::message::NymMessage;
9use nym_sphinx::params::PacketSize;
10use nym_sphinx::preparer::{FragmentPreparer, PreparedFragment};
11use nym_sphinx_params::PacketType;
12use nym_topology::node::RoutingNode;
13use nym_topology::{NymRouteProvider, NymTopology, Role};
14use rand::{CryptoRng, Rng};
15use serde::Serialize;
16use std::sync::Arc;
17use std::time::Duration;
18
19pub use nym_topology::node::LegacyMixLayer;
20
21pub struct NodeTester<R> {
22 rng: R,
23
24 base_topology: NymTopology,
25
26 self_address: Option<Recipient>,
30
31 packet_size: PacketSize,
32
33 deterministic_route_selection: bool,
35
36 average_packet_delay: Duration,
38
39 average_ack_delay: Duration,
41
42 ack_key: Arc<AckKey>,
45}
46
47impl<R> NodeTester<R>
48where
49 R: Rng + CryptoRng,
50{
51 #[allow(clippy::too_many_arguments)]
52 pub fn new(
53 rng: R,
54 base_topology: NymTopology,
55 self_address: Option<Recipient>,
56 packet_size: PacketSize,
57 deterministic_route_selection: bool,
58 average_packet_delay: Duration,
59 average_ack_delay: Duration,
60 ack_key: Arc<AckKey>,
61 ) -> Self {
62 Self {
63 rng,
64 base_topology,
65 self_address,
66 packet_size,
67 deterministic_route_selection,
68 average_packet_delay,
69 average_ack_delay,
70 ack_key,
71 }
72 }
73
74 pub fn testable_mix_topology(&self, layer: LegacyMixLayer, node: &RoutingNode) -> NymTopology {
75 let mut topology = self.base_topology.clone();
76 topology.set_testable_node(layer.into(), node.clone());
77 topology
78 }
79
80 pub fn testable_gateway_topology(&self, node: &RoutingNode) -> NymTopology {
81 let mut topology = self.base_topology.clone();
82 topology.set_testable_node(Role::EntryGateway, node.clone());
83 topology.set_testable_node(Role::ExitGateway, node.clone());
84 topology
85 }
86
87 pub fn mixnode_test_packets<T>(
88 &mut self,
89 mix: &RoutingNode,
90 legacy_mix_layer: LegacyMixLayer,
91 msg_ext: T,
92 test_packets: u32,
93 custom_recipient: Option<Recipient>,
94 ) -> Result<Vec<PreparedFragment>, NetworkTestingError>
95 where
96 T: Serialize + Clone,
97 {
98 let ephemeral_topology =
99 NymRouteProvider::from(self.testable_mix_topology(legacy_mix_layer, mix))
100 .with_ignore_egress_epoch_roles(true);
101
102 let mut packets = Vec::with_capacity(test_packets as usize);
103 for plaintext in TestMessage::mix_plaintexts(mix, test_packets, msg_ext)? {
104 packets.push(self.wrap_plaintext_data(
105 plaintext,
106 &ephemeral_topology,
107 custom_recipient,
108 )?);
109 }
110
111 Ok(packets)
112 }
113
114 pub fn mixnodes_test_packets<T>(
115 &mut self,
116 nodes: &[(LegacyMixLayer, RoutingNode)],
117 msg_ext: T,
118 test_packets: u32,
119 custom_recipient: Option<Recipient>,
120 ) -> Result<Vec<PreparedFragment>, NetworkTestingError>
121 where
122 T: Serialize + Clone,
123 {
124 let mut packets = Vec::new();
125 for (layer, node) in nodes {
126 packets.append(&mut self.mixnode_test_packets(
127 node,
128 *layer,
129 msg_ext.clone(),
130 test_packets,
131 custom_recipient,
132 )?)
133 }
134
135 Ok(packets)
136 }
137
138 pub fn existing_identity_mixnode_test_packets<T>(
139 &mut self,
140 encoded_mix_identity: String,
141 layer: LegacyMixLayer,
142 msg_ext: T,
143 test_packets: u32,
144 custom_recipient: Option<Recipient>,
145 ) -> Result<Vec<PreparedFragment>, NetworkTestingError>
146 where
147 T: Serialize + Clone,
148 {
149 let Ok(identity) = encoded_mix_identity.parse() else {
150 return Err(NetworkTestingError::NonExistentMixnodeIdentity {
151 mix_identity: encoded_mix_identity,
152 });
153 };
154
155 let Some(node) = self.base_topology.find_node_by_identity(identity) else {
156 return Err(NetworkTestingError::NonExistentMixnodeIdentity {
157 mix_identity: encoded_mix_identity,
158 });
159 };
160
161 self.mixnode_test_packets(
162 &node.clone(),
163 layer,
164 msg_ext,
165 test_packets,
166 custom_recipient,
167 )
168 }
169
170 pub fn legacy_gateway_test_packets<T>(
171 &mut self,
172 gateway: &RoutingNode,
173 msg_ext: T,
174 test_packets: u32,
175 custom_recipient: Option<Recipient>,
176 ) -> Result<Vec<PreparedFragment>, NetworkTestingError>
177 where
178 T: Serialize + Clone,
179 {
180 let ephemeral_topology = NymRouteProvider::from(self.testable_gateway_topology(gateway))
181 .with_ignore_egress_epoch_roles(true);
182
183 let mut packets = Vec::with_capacity(test_packets as usize);
184 for plaintext in TestMessage::legacy_gateway_plaintexts(gateway, test_packets, msg_ext)? {
185 packets.push(self.wrap_plaintext_data(
186 plaintext,
187 &ephemeral_topology,
188 custom_recipient,
189 )?);
190 }
191
192 Ok(packets)
193 }
194
195 pub fn wrap_plaintext_data(
196 &mut self,
197 plaintext: Vec<u8>,
198 topology: &NymRouteProvider,
199 custom_recipient: Option<Recipient>,
200 ) -> Result<PreparedFragment, NetworkTestingError> {
201 let message = NymMessage::new_plain(plaintext);
202
203 let mut fragments = self.pad_and_split_message(message, self.packet_size);
204
205 if fragments.len() != 1 {
206 return Err(NetworkTestingError::TestMessageTooLong);
207 }
208
209 let fragment = fragments.pop().unwrap();
212
213 let address = custom_recipient.unwrap_or(
215 self.self_address
216 .ok_or(NetworkTestingError::UnknownPacketRecipient)?,
217 );
218
219 let ack_key = Arc::clone(&self.ack_key);
221 Ok(self.prepare_chunk_for_sending(
222 fragment,
223 topology,
224 &ack_key,
225 &address,
226 &address,
227 PacketType::Mix,
228 )?)
229 }
230
231 pub fn create_test_packet<T>(
232 &mut self,
233 message: &TestMessage<T>,
234 topology: &NymRouteProvider,
235 custom_recipient: Option<Recipient>,
236 ) -> Result<PreparedFragment, NetworkTestingError>
237 where
238 T: Serialize,
239 {
240 let serialized = message.as_bytes()?;
241 self.wrap_plaintext_data(serialized, topology, custom_recipient)
242 }
243}
244
245impl<R: CryptoRng + Rng> FragmentPreparer for NodeTester<R> {
246 type Rng = R;
247
248 fn deterministic_route_selection(&self) -> bool {
249 self.deterministic_route_selection
250 }
251
252 fn rng(&mut self) -> &mut Self::Rng {
253 &mut self.rng
254 }
255
256 fn nonce(&self) -> i32 {
257 1
258 }
259
260 fn average_packet_delay(&self) -> Duration {
261 self.average_packet_delay
262 }
263
264 fn average_ack_delay(&self) -> Duration {
265 self.average_ack_delay
266 }
267}