Skip to content

Commit 4188c9f

Browse files
committed
change buffer allocation method and use connection timeout
1 parent 7c10417 commit 4188c9f

File tree

6 files changed

+26
-9
lines changed

6 files changed

+26
-9
lines changed

common/client-libs/mixnet-client/src/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ mod tests {
343343
NoiseConfig::new(
344344
Arc::new(x25519::KeyPair::new(&mut rng)),
345345
NoiseNetworkView::new_empty(),
346+
Duration::from_millis(1_500),
346347
),
347348
Default::default(),
348349
)

common/nymnoise/src/config.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
collections::HashMap,
66
net::{IpAddr, SocketAddr},
77
sync::Arc,
8+
time::Duration,
89
};
910

1011
use arc_swap::ArcSwap;
@@ -91,16 +92,22 @@ pub struct NoiseConfig {
9192

9293
pub(crate) local_key: Arc<x25519::KeyPair>,
9394
pub(crate) pattern: NoisePattern,
95+
pub(crate) timeout: Duration,
9496

9597
pub(crate) unsafe_disabled: bool, // allows for nodes to not attempt to do a noise handshake, VERY UNSAFE, FOR DEBUG PURPOSE ONLY
9698
}
9799

98100
impl NoiseConfig {
99-
pub fn new(noise_key: Arc<x25519::KeyPair>, network: NoiseNetworkView) -> Self {
101+
pub fn new(
102+
noise_key: Arc<x25519::KeyPair>,
103+
network: NoiseNetworkView,
104+
timeout: Duration,
105+
) -> Self {
100106
NoiseConfig {
101107
network,
102108
local_key: noise_key,
103109
pattern: Default::default(),
110+
timeout,
104111
unsafe_disabled: false,
105112
}
106113
}

common/nymnoise/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pub enum NoiseError {
2424

2525
#[error("Unknown noise version")]
2626
UnknownVersion,
27+
28+
#[error("Handshake timeout")]
29+
HandshakeTimeout(#[from] tokio::time::error::Elapsed),
2730
}
2831

2932
impl From<Error> for NoiseError {

common/nymnoise/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ async fn upgrade_noise_initiator_v1(
3838
let secret_hash = generate_psk_v1(remote_pub_key);
3939
let noise_stream = NoiseStream::new_initiator(conn, config, remote_pub_key, &secret_hash)?;
4040

41-
Ok(Connection::Noise(noise_stream.perform_handshake().await?))
41+
Ok(Connection::Noise(
42+
tokio::time::timeout(config.timeout, noise_stream.perform_handshake()).await??,
43+
))
4244
}
4345

4446
pub async fn upgrade_noise_initiator(
@@ -84,7 +86,9 @@ async fn upgrade_noise_responder_v1(
8486
let secret_hash = generate_psk_v1(config.local_key.public_key());
8587
let noise_stream = NoiseStream::new_responder(conn, config, &secret_hash)?;
8688

87-
Ok(Connection::Noise(noise_stream.perform_handshake().await?))
89+
Ok(Connection::Noise(
90+
tokio::time::timeout(config.timeout, noise_stream.perform_handshake()).await??,
91+
))
8892
}
8993

9094
pub async fn upgrade_noise_responder(

common/nymnoise/src/stream.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use tokio::{
1717
};
1818
use tokio_util::codec::{Framed, LengthDelimitedCodec};
1919

20-
const MAXMSGLEN: usize = 65535;
2120
const TAGLEN: usize = 16;
21+
const HANDSHAKE_MAX_LEN: usize = 1024; // using this constant to limit the handshake's buffer size
2222

2323
pub(crate) type Psk = [u8; 32];
2424

@@ -66,7 +66,7 @@ impl NoiseStream {
6666
.new_framed(inner_stream),
6767
handshake: Some(handshake),
6868
noise: None,
69-
dec_buffer: BytesMut::with_capacity(MAXMSGLEN),
69+
dec_buffer: BytesMut::new(),
7070
}
7171
}
7272

@@ -92,7 +92,7 @@ impl NoiseStream {
9292
&mut self,
9393
handshake: &mut HandshakeState,
9494
) -> Result<(), NoiseError> {
95-
let mut buf = BytesMut::zeroed(MAXMSGLEN + TAGLEN);
95+
let mut buf = BytesMut::zeroed(HANDSHAKE_MAX_LEN); // we're in the handshake, we can afford a smaller buffer
9696
let len = handshake.write_message(&[], &mut buf)?;
9797
buf.truncate(len);
9898
self.inner_stream.send(buf.into()).await?;
@@ -105,7 +105,7 @@ impl NoiseStream {
105105
) -> Result<(), NoiseError> {
106106
match self.inner_stream.next().await {
107107
Some(Ok(msg)) => {
108-
let mut buf = vec![0u8; MAXMSGLEN];
108+
let mut buf = BytesMut::zeroed(HANDSHAKE_MAX_LEN); // we're in the handshake, we can afford a smaller buffer
109109
handshake.read_message(&msg, &mut buf)?;
110110
Ok(())
111111
}
@@ -136,7 +136,7 @@ impl AsyncRead for NoiseStream {
136136

137137
Poll::Ready(Some(Ok(noise_msg))) => {
138138
// We have a new noise msg
139-
let mut dec_msg = vec![0u8; MAXMSGLEN];
139+
let mut dec_msg = BytesMut::zeroed(noise_msg.len() - TAGLEN);
140140
let len = match projected_self.noise {
141141
Some(transport_state) => {
142142
match transport_state.read_message(&noise_msg, &mut dec_msg) {
@@ -187,7 +187,7 @@ impl AsyncWrite for NoiseStream {
187187
ready!(projected_self.inner_stream.as_mut().poll_ready(cx))?;
188188

189189
// Ready to send, encrypting message
190-
let mut noise_buf = BytesMut::zeroed(MAXMSGLEN + TAGLEN);
190+
let mut noise_buf = BytesMut::zeroed(buf.len() + TAGLEN);
191191

192192
let Ok(len) = (match projected_self.noise {
193193
Some(transport_state) => transport_state.write_message(buf, &mut noise_buf),

nym-node/src/node/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ impl NymNode {
10791079
let noise_config = nym_noise::config::NoiseConfig::new(
10801080
self.x25519_noise_keys.clone(),
10811081
NoiseNetworkView::new_empty(),
1082+
self.config.mixnet.debug.initial_connection_timeout,
10821083
)
10831084
.with_unsafe_disabled(true);
10841085

@@ -1128,6 +1129,7 @@ impl NymNode {
11281129
let noise_config = nym_noise::config::NoiseConfig::new(
11291130
self.x25519_noise_keys.clone(),
11301131
network_refresher.noise_view(),
1132+
self.config.mixnet.debug.initial_connection_timeout,
11311133
)
11321134
.with_unsafe_disabled(self.config.mixnet.debug.unsafe_disable_noise);
11331135

0 commit comments

Comments
 (0)