Skip to content

Commit d00427a

Browse files
committed
some comments and minor improvements for future versions
1 parent 7fedc69 commit d00427a

File tree

4 files changed

+34
-46
lines changed

4 files changed

+34
-46
lines changed

common/nymnoise/keys/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use nym_crypto::asymmetric::x25519;
55
use nym_crypto::asymmetric::x25519::serde_helpers::bs58_x25519_pubkey;
66
use serde::{Deserialize, Serialize};
77

8-
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
8+
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq)]
99
#[serde(from = "u8", into = "u8")]
1010
pub enum NoiseVersion {
1111
V1 = 1,
12-
Unknown,
12+
Unknown, //Implies a newer version we don't know
1313
}
1414

1515
impl From<u8> for NoiseVersion {

common/nymnoise/src/connection.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use tokio::{
1111

1212
use crate::stream::NoiseStream;
1313

14+
//SW once plain TCP support is dropped, this whole enum can be dropped, and we can only propagate NoiseStream
1415
#[pin_project(project = ConnectionProj)]
1516
pub enum Connection {
1617
Tcp(#[pin] TcpStream),

common/nymnoise/src/lib.rs

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2025 - Nym Technologies SA <[email protected]>
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use crate::config::{NoiseConfig, NoisePattern};
4+
use crate::config::NoiseConfig;
55
use crate::connection::Connection;
66
use crate::error::NoiseError;
77
use crate::stream::NoiseStream;
@@ -19,12 +19,11 @@ pub mod stream;
1919

2020
const NOISE_PSK_PREFIX: &[u8] = b"NYMTECH_NOISE_dQw4w9WgXcQ";
2121

22-
pub const NOISE_VERSION: NoiseVersion = NoiseVersion::V1;
22+
pub const LATEST_NOISE_VERSION: NoiseVersion = NoiseVersion::V1;
2323

2424
async fn upgrade_noise_initiator_v1(
2525
conn: TcpStream,
26-
pattern: NoisePattern,
27-
local_private_key: &x25519::PrivateKey,
26+
config: &NoiseConfig,
2827
remote_pub_key: &x25519::PublicKey,
2928
) -> Result<Connection, NoiseError> {
3029
trace!("Perform Noise Handshake, initiator side");
@@ -36,10 +35,10 @@ async fn upgrade_noise_initiator_v1(
3635
.concat();
3736
let secret_hash = Sha256::digest(secret);
3837

39-
let handshake = Builder::new(pattern.as_str().parse()?)
40-
.local_private_key(&local_private_key.to_bytes())
38+
let handshake = Builder::new(config.pattern.as_str().parse()?)
39+
.local_private_key(&config.local_key.private_key().to_bytes())
4140
.remote_public_key(&remote_pub_key.to_bytes())
42-
.psk(pattern.psk_position(), &secret_hash)
41+
.psk(config.pattern.psk_position(), &secret_hash)
4342
.build_initiator()?;
4443

4544
let noise_stream = NoiseStream::new(conn, handshake);
@@ -64,51 +63,39 @@ pub async fn upgrade_noise_initiator(
6463

6564
match config.get_noise_key(&responder_addr) {
6665
Some(key) => match key.version {
67-
NoiseVersion::V1 => {
68-
upgrade_noise_initiator_v1(
69-
conn,
70-
config.pattern,
71-
config.local_key.private_key(),
72-
&key.x25519_pubkey,
73-
)
74-
.await
75-
}
66+
NoiseVersion::V1 => upgrade_noise_initiator_v1(conn, config, &key.x25519_pubkey).await,
67+
// We're talking to a more recent node, but we can't adapt. Let's try to do our best and if it fails, it fails.
68+
// If that node sees we're older, it will try to adapt too.
7669
NoiseVersion::Unknown => {
77-
error!(
78-
"{:?} is announcing an unknown version of Noise",
79-
responder_addr
80-
);
81-
Err(NoiseError::UnknownVersion)
70+
warn!("{responder_addr} is announcing an unknown version of Noise, we will still attempt our latest known version");
71+
upgrade_noise_initiator_v1(conn, config, &key.x25519_pubkey)
72+
.await
73+
.or(Err(NoiseError::UnknownVersion))
8274
}
8375
},
8476
None => {
85-
warn!(
86-
"{:?} can't speak Noise yet, falling back to TCP",
87-
responder_addr
88-
);
77+
warn!("{responder_addr} can't speak Noise yet, falling back to TCP");
8978
Ok(Connection::Tcp(conn))
9079
}
9180
}
9281
}
9382

9483
async fn upgrade_noise_responder_v1(
9584
conn: TcpStream,
96-
pattern: NoisePattern,
97-
local_public_key: &x25519::PublicKey,
98-
local_private_key: &x25519::PrivateKey,
85+
config: &NoiseConfig,
9986
) -> Result<Connection, NoiseError> {
10087
trace!("Perform Noise Handshake, responder side");
10188

10289
let secret = [
10390
NOISE_PSK_PREFIX.to_vec(),
104-
local_public_key.to_bytes().to_vec(),
91+
config.local_key.public_key().to_bytes().to_vec(),
10592
]
10693
.concat();
10794
let secret_hash = Sha256::digest(secret);
10895

109-
let handshake = Builder::new(pattern.as_str().parse()?)
110-
.local_private_key(&local_private_key.to_bytes())
111-
.psk(pattern.psk_position(), &secret_hash)
96+
let handshake = Builder::new(config.pattern.as_str().parse()?)
97+
.local_private_key(&config.local_key.private_key().to_bytes())
98+
.psk(config.pattern.psk_position(), &secret_hash)
11299
.build_responder()?;
113100

114101
let noise_stream = NoiseStream::new(conn, handshake);
@@ -140,16 +127,16 @@ pub async fn upgrade_noise_responder(
140127
warn!("{initiator_addr} can't speak Noise yet, falling back to TCP",);
141128
Ok(Connection::Tcp(conn))
142129
}
143-
//responder's info on version is shaky, so initiator has to adapt. This behavior can change in the future
144-
Some(_) => {
145-
//Existing node supporting Noise
146-
upgrade_noise_responder_v1(
147-
conn,
148-
config.pattern,
149-
config.local_key.public_key(),
150-
config.local_key.private_key(),
151-
)
152-
.await
153-
}
130+
// responder's info on version is shaky, so ideally, initiator has to adapt.
131+
// if we are newer, it won't ba able to, so let's try to meet him on his ground.
132+
Some(LATEST_NOISE_VERSION) | Some(NoiseVersion::Unknown) => {
133+
// Node is announcing the same version as us, great or
134+
// Node is announcing a newer version than us, it should adapt to us though
135+
upgrade_noise_responder_v1(conn, config).await
136+
} //SW sample of code to allow backwards compatibility when we introduce new versions
137+
// Some(IntermediateNoiseVersion) => {
138+
// Node is announcing an older version, let's try to adapt
139+
// upgrade_noise_responder_Vwhatever
140+
// }
154141
}
155142
}

nym-node/src/node/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ impl NymNode {
700700
&self.config,
701701
self.x25519_sphinx_keys.public_key(),
702702
&VersionedNoiseKey {
703-
version: nym_noise::NOISE_VERSION,
703+
version: nym_noise::LATEST_NOISE_VERSION,
704704
x25519_pubkey: *self.x25519_noise_keys.public_key(),
705705
},
706706
&self.ed25519_identity_keys,

0 commit comments

Comments
 (0)