/
native_client.rs
239 lines (211 loc) · 8.03 KB
/
native_client.rs
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
use crate::mixnet::client::MixnetClientBuilder;
use crate::mixnet::traits::MixnetMessageSender;
use crate::{Error, Result};
use async_trait::async_trait;
use futures::{ready, Stream, StreamExt};
use log::error;
use nym_client_core::client::base_client::GatewayConnection;
use nym_client_core::client::{
base_client::{ClientInput, ClientOutput, ClientState},
inbound_messages::InputMessage,
received_buffer::ReconstructedMessagesReceiver,
};
use nym_sphinx::addressing::clients::Recipient;
use nym_sphinx::{params::PacketType, receiver::ReconstructedMessage};
use nym_task::{
connections::{ConnectionCommandSender, LaneQueueLengths},
TaskHandle,
};
use nym_topology::NymTopology;
use std::pin::Pin;
use std::task::{Context, Poll};
/// Client connected to the Nym mixnet.
pub struct MixnetClient {
/// The nym address of this connected client.
pub(crate) nym_address: Recipient,
/// Input to the client from the users perspective. This can be either data to send or control
/// messages.
pub(crate) client_input: ClientInput,
/// Output from the client from the users perspective. This is typically messages arriving from
/// the mixnet.
#[allow(dead_code)]
pub(crate) client_output: ClientOutput,
/// The current state of the client that is exposed to the user. This includes things like
/// current message send queue length.
pub(crate) client_state: ClientState,
/// A channel for messages arriving from the mixnet after they have been reconstructed.
pub(crate) reconstructed_receiver: ReconstructedMessagesReceiver,
/// The task manager that controls all the spawned tasks that the clients uses to do it's job.
pub(crate) task_handle: TaskHandle,
pub(crate) packet_type: Option<PacketType>,
// internal state used for the `Stream` implementation
_buffered: Vec<ReconstructedMessage>,
}
impl MixnetClient {
pub(crate) fn new(
nym_address: Recipient,
client_input: ClientInput,
client_output: ClientOutput,
client_state: ClientState,
reconstructed_receiver: ReconstructedMessagesReceiver,
task_handle: TaskHandle,
packet_type: Option<PacketType>,
) -> Self {
Self {
nym_address,
client_input,
client_output,
client_state,
reconstructed_receiver,
task_handle,
packet_type,
_buffered: Vec::new(),
}
}
/// Create a new client and connect to the mixnet using ephemeral in-memory keys that are
/// discarded at application close.
///
/// # Examples
///
/// ```no_run
/// use nym_sdk::mixnet;
///
/// #[tokio::main]
/// async fn main() {
/// let mut client = mixnet::MixnetClient::connect_new().await;
/// }
///
/// ```
pub async fn connect_new() -> Result<Self> {
MixnetClientBuilder::new_ephemeral()
.build()?
.connect_to_mixnet()
.await
}
/// Get the nym address for this client, if it is available. The nym address is composed of the
/// client identity, the client encryption key, and the gateway identity.
pub fn nym_address(&self) -> &Recipient {
&self.nym_address
}
/// Get gateway connection information, like the file descriptor of the WebSocket
pub fn gateway_connection(&self) -> GatewayConnection {
self.client_state.gateway_connection
}
/// Get a shallow clone of [`MixnetClientSender`]. Useful if you want split the send and
/// receive logic in different locations.
pub fn split_sender(&self) -> MixnetClientSender {
MixnetClientSender {
client_input: self.client_input.clone(),
packet_type: self.packet_type,
}
}
/// Get a shallow clone of [`ConnectionCommandSender`]. This is useful if you want to e.g
/// explicitly close a transmission lane that is still sending data even though it should
/// cancel.
pub fn connection_command_sender(&self) -> ConnectionCommandSender {
self.client_input.connection_command_sender.clone()
}
/// Get a shallow clone of [`LaneQueueLengths`]. This is useful to manually implement some form
/// of backpressure logic.
pub fn shared_lane_queue_lengths(&self) -> LaneQueueLengths {
self.client_state.shared_lane_queue_lengths.clone()
}
/// Change the network topology used by this client for constructing sphinx packets into the
/// provided one.
pub async fn manually_overwrite_topology(&self, new_topology: NymTopology) {
self.client_state
.topology_accessor
.manually_change_topology(new_topology)
.await
}
/// Gets the value of the currently used network topology.
pub async fn read_current_topology(&self) -> Option<NymTopology> {
self.client_state.topology_accessor.current_topology().await
}
/// Restore default topology refreshing behaviour of this client.
pub fn restore_automatic_topology_refreshing(&self) {
self.client_state.topology_accessor.release_manual_control()
}
/// Wait for messages from the mixnet
pub async fn wait_for_messages(&mut self) -> Option<Vec<ReconstructedMessage>> {
self.reconstructed_receiver.next().await
}
/// Provide a callback to execute on incoming messages from the mixnet.
pub async fn on_messages<F>(&mut self, fun: F)
where
F: Fn(ReconstructedMessage),
{
while let Some(msgs) = self.wait_for_messages().await {
for msg in msgs {
fun(msg)
}
}
}
/// Disconnect from the mixnet. Currently it is not supported to reconnect a disconnected
/// client.
pub async fn disconnect(mut self) {
if let TaskHandle::Internal(task_manager) = &mut self.task_handle {
task_manager.signal_shutdown().ok();
task_manager.wait_for_shutdown().await;
}
// note: it's important to take ownership of the struct as if the shutdown is `TaskHandle::External`,
// it must be dropped to finalize the shutdown
}
}
#[derive(Clone)]
pub struct MixnetClientSender {
client_input: ClientInput,
packet_type: Option<PacketType>,
}
impl Stream for MixnetClient {
type Item = ReconstructedMessage;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if let Some(next) = self._buffered.pop() {
cx.waker().wake_by_ref();
return Poll::Ready(Some(next));
}
match ready!(Pin::new(&mut self.reconstructed_receiver).poll_next(cx)) {
None => Poll::Ready(None),
Some(mut msgs) => {
// the vector itself should never be empty
if let Some(next) = msgs.pop() {
// there's more than a single message - buffer them and wake the waker
// to get polled again immediately
if !msgs.is_empty() {
self._buffered = msgs;
cx.waker().wake_by_ref();
}
Poll::Ready(Some(next))
} else {
error!("the reconstructed messages vector is empty - please let the developers know if you see this message");
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
}
}
#[async_trait]
impl MixnetMessageSender for MixnetClient {
fn packet_type(&self) -> Option<PacketType> {
self.packet_type
}
async fn send(&self, message: InputMessage) -> Result<()> {
self.client_input
.send(message)
.await
.map_err(|_| Error::MessageSendingFailure)
}
}
#[async_trait]
impl MixnetMessageSender for MixnetClientSender {
fn packet_type(&self) -> Option<PacketType> {
self.packet_type
}
async fn send(&self, message: InputMessage) -> Result<()> {
self.client_input
.send(message)
.await
.map_err(|_| Error::MessageSendingFailure)
}
}