apply some of octlol's suggestion

This commit is contained in:
Simon Wicky
2024-02-06 10:30:44 +01:00
committed by Simon Wicky
parent 06f1271a25
commit 293cf2fd3f
5 changed files with 57 additions and 65 deletions
+43 -44
View File
@@ -110,55 +110,54 @@ impl Client {
let connection_fut = TcpStream::connect(address);
let conn = match tokio::time::timeout(connection_timeout, connection_fut).await {
Ok(stream_res) => match stream_res {
Ok(stream) => {
debug!("Managed to establish connection to {}", address);
// if we managed to connect, reset the reconnection count (whatever it might have been)
current_reconnection.store(0, Ordering::Release);
//Get the topology, because we need the keys for the handshake
let topology_ref = match topology_access.current_topology().await {
Some(topology) => topology,
None => {
Ok(stream_res) => {
match stream_res {
Ok(stream) => {
debug!("Managed to establish connection to {}", address);
// if we managed to connect, reset the reconnection count (whatever it might have been)
current_reconnection.store(0, Ordering::Release);
//Get the topology, because we need the keys for the handshake
let Some(topology) = topology_access.current_topology().await else {
error!("Cannot perform Noise handshake to {address}, due to topology error");
return;
}
};
};
let epoch_id = match api_client.get_current_epoch_id().await {
Ok(id) => id,
Err(err) => {
error!("Cannot perform Noise handshake to {address}, due to epoch id error - {err}");
return;
}
};
let epoch_id = match api_client.get_current_epoch_id().await {
Ok(id) => id,
Err(err) => {
error!("Cannot perform Noise handshake to {address}, due to epoch id error - {err}");
return;
}
};
let noise_stream = match upgrade_noise_initiator_with_topology(
stream,
Default::default(),
&topology_ref,
epoch_id,
&local_identity.public_key().to_bytes(),
&local_identity.private_key().to_bytes(),
)
.await
{
Ok(noise_stream) => noise_stream,
Err(err) => {
error!("Failed to perform Noise handshake with {address} - {err}");
return;
}
};
debug!("Noise initiator handshake completed for {:?}", address);
Framed::new(noise_stream, NymCodec)
let noise_stream = match upgrade_noise_initiator_with_topology(
stream,
Default::default(),
&topology,
epoch_id,
&local_identity.public_key().to_bytes(),
&local_identity.private_key().to_bytes(),
)
.await
{
Ok(noise_stream) => noise_stream,
Err(err) => {
error!("Failed to perform Noise handshake with {address} - {err}");
return;
}
};
debug!("Noise initiator handshake completed for {:?}", address);
Framed::new(noise_stream, NymCodec)
}
Err(err) => {
debug!(
"failed to establish connection to {} (err: {})",
address, err
);
return;
}
}
Err(err) => {
debug!(
"failed to establish connection to {} (err: {})",
address, err
);
return;
}
},
}
Err(_) => {
debug!(
"failed to connect to {} within {:?}",
+1
View File
@@ -13,6 +13,7 @@ pub enum NoiseError {
#[error("encountered a Noise Protocol error - {0}")]
ProtocolError(Error),
#[error("encountered an IO error - {0}")]
IoError(#[from] io::Error),
+5 -7
View File
@@ -54,13 +54,11 @@ pub async fn upgrade_noise_initiator_with_topology(
local_private_key: &[u8],
) -> Result<Connection, NoiseError> {
//Get init material
let responder_addr = match conn.peer_addr() {
Ok(addr) => addr,
Err(err) => {
error!("Unable to extract peer address from connection - {err}");
return Err(Error::Prereq(Prerequisite::RemotePublicKey).into());
}
};
let responder_addr = conn.peer_addr().map_err(|err| {
error!("Unable to extract peer address from connection - {err}");
Error::Prereq(Prerequisite::RemotePublicKey)
})?;
let remote_pub_key = match topology.find_node_key_by_mix_host(responder_addr) {
Ok(Some(key)) => encryption::PublicKey::from_base58_string(key)?.to_bytes(),
Ok(None) => {
@@ -223,12 +223,9 @@ impl<St: Storage> ConnectionHandler<St> {
debug!("Starting connection handler for {:?}", remote);
shutdown.mark_as_success();
let topology_ref = match self.topology_access.current_topology().await {
Some(topology) => topology,
None => {
error!("Cannot perform Noise handshake to {remote}, due to topology error");
return;
}
let Some(topology) = self.topology_access.current_topology().await else {
error!("Cannot perform Noise handshake to {remote}, due to topology error");
return;
};
let epoch_id = match self.api_client.get_current_epoch_id().await {
@@ -242,7 +239,7 @@ impl<St: Storage> ConnectionHandler<St> {
let noise_stream = match upgrade_noise_responder_with_topology(
conn,
Default::default(),
&topology_ref,
&topology,
epoch_id,
&self.local_identity.public_key().to_bytes(),
&self.local_identity.private_key().to_bytes(),
@@ -105,12 +105,9 @@ impl ConnectionHandler {
shutdown.mark_as_success();
let topology_ref = match self.topology_access.current_topology().await {
Some(topology) => topology,
None => {
error!("Cannot perform Noise handshake to {remote}, due to topology error");
return;
}
let Some(topology) = self.topology_access.current_topology().await else {
error!("Cannot perform Noise handshake to {remote}, due to topology error");
return;
};
let epoch_id = match self.api_client.get_current_epoch_id().await {
@@ -124,7 +121,7 @@ impl ConnectionHandler {
let noise_stream = match upgrade_noise_responder_with_topology(
conn,
Default::default(),
&topology_ref,
&topology,
epoch_id,
&self.local_identity.public_key().to_bytes(),
&self.local_identity.private_key().to_bytes(),