From 2d3b4f4b91cb507323d1bcc13f9a9f52f1d2af4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Mon, 18 Jan 2021 11:50:29 +0000 Subject: [PATCH] Feature/GitHub actions and clippy cleanup (#493) * Added github actions templates * removed travis .yml file * initial clippy cleanup pass * fixed the rest of clippy warnings * Made github badges more fancy and consistent with the ones in sphinx * Updated local rustc version and removed compilation warningns * ... and fresh clippy warnings * formatting * beta clippy specific warnings fixed * Fixed all nightly clippy warnings * Fixed trying to unwrap a () * Actually running all tests * Correctly passing the --all flag * Hopefullly third time's a charm in fixing argument passing --- .github/workflows/build.yml | 44 +++++++++++ .github/workflows/clippy_check.yml | 14 ++++ .travis.yml | 15 ---- Cargo.lock | 3 + README.md | 4 +- clients/client-core/src/client/key_manager.rs | 4 +- .../input_message_listener.rs | 3 + .../src/client/real_messages_control/mod.rs | 3 + .../real_traffic_stream.rs | 11 ++- .../src/client/topology_control.rs | 6 ++ clients/client-core/src/config/mod.rs | 2 +- .../native/websocket-requests/src/requests.rs | 6 +- clients/native/websocket-requests/src/text.rs | 6 +- clients/socks5/src/socks/client.rs | 9 +-- clients/webassembly/src/client/mod.rs | 11 ++- common/client-libs/gateway-client/Cargo.toml | 1 + .../client-libs/gateway-client/src/client.rs | 20 +++-- .../client-libs/gateway-client/src/error.rs | 10 +-- .../gateway-client/src/socket_state.rs | 13 ++-- .../client-libs/mixnet-client/src/client.rs | 2 +- .../validator-client/src/models/topology.rs | 10 +-- .../rest_requests/gateway_register_post.rs | 2 +- .../mix_mining_batch_status_post.rs | 2 +- .../rest_requests/mix_mining_status_post.rs | 2 +- .../src/rest_requests/mix_register_post.rs | 2 +- .../rest_requests/node_unregister_delete.rs | 2 +- .../src/rest_requests/set_reputation_patch.rs | 4 +- .../crypto/src/asymmetric/encryption/mod.rs | 38 +++++----- common/crypto/src/asymmetric/identity/mod.rs | 9 +-- common/crypto/src/shared_key.rs | 2 +- common/nonexhaustive-delayqueue/src/lib.rs | 6 ++ common/nymsphinx/addressing/Cargo.toml | 3 + common/nymsphinx/addressing/src/clients.rs | 16 ++-- common/nymsphinx/addressing/src/nodes.rs | 6 +- common/nymsphinx/chunking/src/fragment.rs | 24 +++--- common/nymsphinx/chunking/src/lib.rs | 26 +++---- .../nymsphinx/chunking/src/reconstruction.rs | 75 +++++++++++-------- common/nymsphinx/chunking/src/set.rs | 2 +- common/nymsphinx/framing/src/codec.rs | 10 +-- common/nymsphinx/framing/src/packet.rs | 7 +- common/nymsphinx/src/preparer/vpn_manager.rs | 2 +- common/socks5/ordered-buffer/src/buffer.rs | 7 +- common/socks5/ordered-buffer/src/sender.rs | 6 ++ .../proxy-helpers/src/available_reader.rs | 2 +- .../src/connection_controller.rs | 21 +++--- .../socks5/proxy-helpers/src/proxy_runner.rs | 8 +- common/topology/src/gateway.rs | 8 +- common/topology/src/lib.rs | 8 +- common/topology/src/mix.rs | 8 +- .../src/authentication/encrypted_address.rs | 6 +- .../gateway-requests/src/authentication/iv.rs | 6 +- .../src/registration/handshake/mod.rs | 4 - .../src/registration/handshake/shared_key.rs | 6 +- .../src/registration/handshake/state.rs | 2 +- gateway/gateway-requests/src/types.rs | 12 +-- gateway/src/commands/init.rs | 6 +- .../websocket/connection_handler.rs | 15 +++- .../client_handling/websocket/listener.rs | 2 + gateway/src/node/mod.rs | 7 +- gateway/src/node/presence.rs | 20 ++--- gateway/src/node/storage/ledger.rs | 16 ++-- mixnode/Cargo.toml | 1 + mixnode/src/commands/init.rs | 6 +- mixnode/src/commands/upgrade.rs | 4 +- mixnode/src/node/mod.rs | 7 +- mixnode/src/node/presence.rs | 20 ++--- network-monitor/src/chunker.rs | 10 +-- network-monitor/src/main.rs | 14 ++-- network-monitor/src/notifications/mod.rs | 2 +- network-monitor/src/packet_sender.rs | 20 ++--- network-monitor/src/test_packet.rs | 6 +- network-monitor/src/tested_network/mod.rs | 8 +- .../network-requester/src/allowed_hosts.rs | 2 +- 73 files changed, 392 insertions(+), 315 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/clippy_check.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000000..0d21fa474c --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,44 @@ +name: Continuous integration + +on: [push, pull_request] + +jobs: + ci: + runs-on: ubuntu-latest + continue-on-error: ${{ matrix.rust == 'nightly' }} + strategy: + matrix: + rust: + - stable + - beta + - nightly + + steps: + - uses: actions/checkout@v2 + + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.rust }} + override: true + components: rustfmt, clippy + + - uses: actions-rs/cargo@v1 + with: + command: build + args: --all + + - uses: actions-rs/cargo@v1 + with: + command: test + args: --all + + - uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check + + - uses: actions-rs/cargo@v1 + with: + command: clippy + args: -- -D warnings \ No newline at end of file diff --git a/.github/workflows/clippy_check.yml b/.github/workflows/clippy_check.yml new file mode 100644 index 0000000000..90789c64e0 --- /dev/null +++ b/.github/workflows/clippy_check.yml @@ -0,0 +1,14 @@ +name: Clippy check + +on: push + +jobs: + clippy_check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - run: rustup component add clippy + - uses: actions-rs/clippy-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + args: --all-features \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 5425860532..0000000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: rust -rust: - - stable - - beta - - nightly -jobs: - allow_failures: - - rust: nightly - fast_finish: true -before_script: - - rustup component add rustfmt -script: - - cargo build - - cargo test --all - - cargo fmt -- --check \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index b0f7337bc2..8a8432bf86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -889,6 +889,7 @@ dependencies = [ "gateway-requests", "log", "nymsphinx", + "rand", "tokio", "tokio-tungstenite", "tungstenite", @@ -1612,6 +1613,7 @@ dependencies = [ "nymsphinx", "pemstore", "pretty_env_logger", + "rand", "serde", "tokio", "tokio-util", @@ -1729,6 +1731,7 @@ version = "0.1.0" dependencies = [ "crypto", "nymsphinx-types", + "rand", "serde", ] diff --git a/README.md b/README.md index 44c1ef6dc5..bc39bb2d12 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ The platform is composed of multiple Rust crates. Top-level executable binary cr * nym-network-monitor - sends packets through the full system to check that they are working as expected, and stores node uptime histories as the basis of a rewards system ("mixmining" or "proof-of-mixing"). * nym-explorer - a (projected) block explorer and (existing) mixnet viewer. -[![Build Status](https://travis-ci.com/nymtech/nym.svg?branch=develop)](https://travis-ci.com/nymtech/nym) +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=for-the-badge)](https://opensource.org/licenses/Apache-2.0) +[![Build Status](https://img.shields.io/github/workflow/status/nymtech/nym/Continuous%20integration/develop?style=for-the-badge&logo=github-actions)](https://github.com/nymtech/nym/actions?query=branch%3Adevelop) + ### Building diff --git a/clients/client-core/src/client/key_manager.rs b/clients/client-core/src/client/key_manager.rs index 49a553c43d..08bad94e01 100644 --- a/clients/client-core/src/client/key_manager.rs +++ b/clients/client-core/src/client/key_manager.rs @@ -61,8 +61,8 @@ impl KeyManager { R: RngCore + CryptoRng, { KeyManager { - identity_keypair: Arc::new(identity::KeyPair::new_with_rng(rng)), - encryption_keypair: Arc::new(encryption::KeyPair::new_with_rng(rng)), + identity_keypair: Arc::new(identity::KeyPair::new(rng)), + encryption_keypair: Arc::new(encryption::KeyPair::new(rng)), gateway_shared_key: None, ack_key: Arc::new(AckKey::new(rng)), } diff --git a/clients/client-core/src/client/real_messages_control/acknowledgement_control/input_message_listener.rs b/clients/client-core/src/client/real_messages_control/acknowledgement_control/input_message_listener.rs index ff807a26ae..0de0485f61 100644 --- a/clients/client-core/src/client/real_messages_control/acknowledgement_control/input_message_listener.rs +++ b/clients/client-core/src/client/real_messages_control/acknowledgement_control/input_message_listener.rs @@ -49,6 +49,9 @@ impl InputMessageListener where R: CryptoRng + Rng, { + // at this point I'm not entirely sure how to deal with this warning without + // some considerable refactoring + #[allow(clippy::too_many_arguments)] pub(super) fn new( ack_key: Arc, ack_recipient: Recipient, diff --git a/clients/client-core/src/client/real_messages_control/mod.rs b/clients/client-core/src/client/real_messages_control/mod.rs index 5bc8e0f216..763b6ba6d6 100644 --- a/clients/client-core/src/client/real_messages_control/mod.rs +++ b/clients/client-core/src/client/real_messages_control/mod.rs @@ -73,6 +73,9 @@ pub struct Config { } impl Config { + // at this point I'm not entirely sure how to deal with this warning without + // some considerable refactoring + #[allow(clippy::too_many_arguments)] pub fn new( ack_key: Arc, ack_wait_multiplier: f64, diff --git a/clients/client-core/src/client/real_messages_control/real_traffic_stream.rs b/clients/client-core/src/client/real_messages_control/real_traffic_stream.rs index f45400c720..3d22b4f5a4 100644 --- a/clients/client-core/src/client/real_messages_control/real_traffic_stream.rs +++ b/clients/client-core/src/client/real_messages_control/real_traffic_stream.rs @@ -117,7 +117,7 @@ type BatchRealMessageReceiver = mpsc::UnboundedReceiver>; pub(crate) enum StreamMessage { Cover, - Real(RealMessage), + Real(Box), } impl Stream for OutQueueControl @@ -145,7 +145,7 @@ where // check if we have anything immediately available if let Some(real_available) = self.received_buffer.pop_front() { - return Poll::Ready(Some(StreamMessage::Real(real_available))); + return Poll::Ready(Some(StreamMessage::Real(Box::new(real_available)))); } // decide what kind of message to send @@ -158,9 +158,9 @@ where Poll::Ready(Some(real_messages)) => { self.received_buffer = real_messages.into(); // we MUST HAVE received at least ONE message - Poll::Ready(Some(StreamMessage::Real( + Poll::Ready(Some(StreamMessage::Real(Box::new( self.received_buffer.pop_front().unwrap(), - ))) + )))) } // otherwise construct a dummy one @@ -173,6 +173,9 @@ impl OutQueueControl where R: CryptoRng + Rng + Unpin, { + // at this point I'm not entirely sure how to deal with this warning without + // some considerable refactoring + #[allow(clippy::too_many_arguments)] pub(crate) fn new( config: Config, ack_key: Arc, diff --git a/clients/client-core/src/client/topology_control.rs b/clients/client-core/src/client/topology_control.rs index df87eb7d6e..016e33f7a6 100644 --- a/clients/client-core/src/client/topology_control.rs +++ b/clients/client-core/src/client/topology_control.rs @@ -129,6 +129,12 @@ impl TopologyAccessor { } } +impl Default for TopologyAccessor { + fn default() -> Self { + TopologyAccessor::new() + } +} + pub struct TopologyRefresherConfig { directory_server: String, refresh_rate: time::Duration, diff --git a/clients/client-core/src/config/mod.rs b/clients/client-core/src/config/mod.rs index 2ce486cb62..e3f3124f67 100644 --- a/clients/client-core/src/config/mod.rs +++ b/clients/client-core/src/config/mod.rs @@ -305,7 +305,7 @@ impl Config { true => Some( self.debug .vpn_key_reuse_limit - .unwrap_or_else(|| DEFAULT_VPN_KEY_REUSE_LIMIT), + .unwrap_or(DEFAULT_VPN_KEY_REUSE_LIMIT), ), } } diff --git a/clients/native/websocket-requests/src/requests.rs b/clients/native/websocket-requests/src/requests.rs index 49bdb139e9..01775a4546 100644 --- a/clients/native/websocket-requests/src/requests.rs +++ b/clients/native/websocket-requests/src/requests.rs @@ -206,11 +206,11 @@ impl ClientRequest { } // SELF_ADDRESS_REQUEST_TAG - fn deserialize_self_address(b: &[u8]) -> Result { + fn deserialize_self_address(b: &[u8]) -> Self { // this MUST match because it was called by 'deserialize' debug_assert_eq!(b[0], SELF_ADDRESS_REQUEST_TAG); - Ok(ClientRequest::SelfAddress) + ClientRequest::SelfAddress } pub fn serialize(self) -> Vec { @@ -255,7 +255,7 @@ impl ClientRequest { match request_tag { SEND_REQUEST_TAG => Self::deserialize_send(b), REPLY_REQUEST_TAG => Self::deserialize_reply(b), - SELF_ADDRESS_REQUEST_TAG => Self::deserialize_self_address(b), + SELF_ADDRESS_REQUEST_TAG => Ok(Self::deserialize_self_address(b)), n => Err(error::Error::new( ErrorKind::UnknownRequest, format!("type {}", n), diff --git a/clients/native/websocket-requests/src/text.rs b/clients/native/websocket-requests/src/text.rs index 806c58edd1..3155ec663e 100644 --- a/clients/native/websocket-requests/src/text.rs +++ b/clients/native/websocket-requests/src/text.rs @@ -115,15 +115,15 @@ impl TryFrom for ServerResponseText { } } -impl Into for ServerResponseText { - fn into(self) -> String { +impl From for String { + fn from(res: ServerResponseText) -> Self { // per serde_json docs: /* /// Serialization can fail if `T`'s implementation of `Serialize` decides to /// fail, or if `T` contains a map with non-string keys. */ // this is not the case here. - serde_json::to_string(&self).unwrap() + serde_json::to_string(&res).unwrap() } } diff --git a/clients/socks5/src/socks/client.rs b/clients/socks5/src/socks/client.rs index 411ee6fdaf..3d20d5fdab 100644 --- a/clients/socks5/src/socks/client.rs +++ b/clients/socks5/src/socks/client.rs @@ -220,14 +220,9 @@ impl SocksClient { } async fn send_connect_to_mixnet(&mut self, remote_address: RemoteAddress) { - let req = Request::new_connect( - self.connection_id, - remote_address.clone(), - self.self_address.clone(), - ); + let req = Request::new_connect(self.connection_id, remote_address, self.self_address); - let input_message = - InputMessage::new_fresh(self.service_provider.clone(), req.into_bytes(), false); + let input_message = InputMessage::new_fresh(self.service_provider, req.into_bytes(), false); self.input_sender.unbounded_send(input_message).unwrap(); } diff --git a/clients/webassembly/src/client/mod.rs b/clients/webassembly/src/client/mod.rs index 0a44aebe15..3082c81d37 100644 --- a/clients/webassembly/src/client/mod.rs +++ b/clients/webassembly/src/client/mod.rs @@ -31,8 +31,6 @@ use wasm_utils::{console_log, console_warn}; pub(crate) mod received_processor; -const DEFAULT_RNG: OsRng = OsRng; - const DEFAULT_AVERAGE_PACKET_DELAY: Duration = Duration::from_millis(200); const DEFAULT_AVERAGE_ACK_DELAY: Duration = Duration::from_millis(200); const DEFAULT_GATEWAY_RESPONSE_TIMEOUT: Duration = Duration::from_millis(1_500); @@ -67,10 +65,11 @@ pub struct NymClient { impl NymClient { #[wasm_bindgen(constructor)] pub fn new(validator_server: String) -> Self { + let mut rng = OsRng; // for time being generate new keys each time... - let identity = identity::KeyPair::new_with_rng(&mut DEFAULT_RNG); - let encryption_keys = encryption::KeyPair::new_with_rng(&mut DEFAULT_RNG); - let ack_key = AckKey::new(&mut DEFAULT_RNG); + let identity = identity::KeyPair::new_with_rng(&mut rng); + let encryption_keys = encryption::KeyPair::new_with_rng(&mut rng); + let ack_key = AckKey::new(&mut rng); Self { identity: Arc::new(identity), @@ -145,7 +144,7 @@ impl NymClient { }; let message_preparer = MessagePreparer::new( - DEFAULT_RNG, + &mut rand::rngs::OsRng, client.self_recipient(), DEFAULT_AVERAGE_PACKET_DELAY, DEFAULT_AVERAGE_ACK_DELAY, diff --git a/common/client-libs/gateway-client/Cargo.toml b/common/client-libs/gateway-client/Cargo.toml index b9216371e0..68a7e98f58 100644 --- a/common/client-libs/gateway-client/Cargo.toml +++ b/common/client-libs/gateway-client/Cargo.toml @@ -11,6 +11,7 @@ edition = "2018" # the entire crate futures = "0.3" log = "0.4" +rand = { version = "0.7.3", features = ["wasm-bindgen"] } # internal crypto = { path = "../../crypto" } diff --git a/common/client-libs/gateway-client/src/client.rs b/common/client-libs/gateway-client/src/client.rs index e4b88f2dae..874768bacf 100644 --- a/common/client-libs/gateway-client/src/client.rs +++ b/common/client-libs/gateway-client/src/client.rs @@ -23,10 +23,11 @@ use crypto::asymmetric::identity; use futures::{FutureExt, SinkExt, StreamExt}; use gateway_requests::authentication::encrypted_address::EncryptedAddressBytes; use gateway_requests::authentication::iv::AuthenticationIV; -use gateway_requests::registration::handshake::{client_handshake, SharedKeys, DEFAULT_RNG}; +use gateway_requests::registration::handshake::{client_handshake, SharedKeys}; use gateway_requests::{BinaryRequest, ClientControlRequest, ServerResponse}; use log::*; use nymsphinx::forwarding::packet::MixPacket; +use rand::rngs::OsRng; use std::convert::TryFrom; use std::sync::Arc; use std::time::Duration; @@ -178,7 +179,7 @@ impl GatewayClient { for i in 1..self.reconnection_attempts { info!("attempt {}...", i); - if let Ok(_) = self.authenticate_and_start().await { + if self.authenticate_and_start().await.is_ok() { info!("managed to reconnect!"); return Ok(()); } @@ -339,9 +340,13 @@ impl GatewayClient { debug_assert!(self.connection.is_available()); + // it's fine to instantiate it here as it's only used once (during authentication or registration) + // and putting it into the GatewayClient struct would be a hassle + let mut rng = OsRng; + let shared_key = match &mut self.connection { SocketState::Available(ws_stream) => client_handshake( - &mut DEFAULT_RNG, + &mut rng, ws_stream, self.local_identity.as_ref(), self.gateway_identity, @@ -365,11 +370,16 @@ impl GatewayClient { if !self.connection.is_established() { return Err(GatewayClientError::ConnectionNotEstablished); } + + // it's fine to instantiate it here as it's only used once (during authentication or registration) + // and putting it into the GatewayClient struct would be a hassle + let mut rng = OsRng; + // because of the previous check one of the unwraps MUST succeed let shared_key = shared_key .as_ref() .unwrap_or_else(|| self.shared_key.as_ref().unwrap()); - let iv = AuthenticationIV::new_random(&mut DEFAULT_RNG); + let iv = AuthenticationIV::new_random(&mut rng); let self_address = self .local_identity .as_ref() @@ -516,7 +526,7 @@ impl GatewayClient { .as_ref() .expect("no shared key present even though we're authenticated!"), ), - )? + ) } _ => unreachable!(), }; diff --git a/common/client-libs/gateway-client/src/error.rs b/common/client-libs/gateway-client/src/error.rs index beca4f5d5f..985074228a 100644 --- a/common/client-libs/gateway-client/src/error.rs +++ b/common/client-libs/gateway-client/src/error.rs @@ -50,12 +50,12 @@ impl GatewayClientError { match self { GatewayClientError::NetworkError(ws_err) => match ws_err { WsError::AlreadyClosed | WsError::ConnectionClosed => true, - WsError::Io(io_err) => match io_err.kind() { + WsError::Io(io_err) => matches!( + io_err.kind(), io::ErrorKind::ConnectionReset - | io::ErrorKind::ConnectionAborted - | io::ErrorKind::BrokenPipe => true, - _ => false, - }, + | io::ErrorKind::ConnectionAborted + | io::ErrorKind::BrokenPipe + ), _ => false, }, _ => false, diff --git a/common/client-libs/gateway-client/src/socket_state.rs b/common/client-libs/gateway-client/src/socket_state.rs index 0684fecbfc..5d0304248f 100644 --- a/common/client-libs/gateway-client/src/socket_state.rs +++ b/common/client-libs/gateway-client/src/socket_state.rs @@ -97,7 +97,7 @@ impl PartiallyDelegated { conn: WsConn, packet_router: PacketRouter, shared_key: Arc, - ) -> Result { + ) -> Self { // when called for, it NEEDS TO yield back the stream so that we could merge it and // read control request responses. let (notify_sender, notify_receiver) = oneshot::channel(); @@ -137,10 +137,10 @@ impl PartiallyDelegated { #[cfg(not(target_arch = "wasm32"))] tokio::spawn(mixnet_receiver_future); - Ok(PartiallyDelegated { + PartiallyDelegated { sink_half: sink, delegated_stream: (stream_receiver, notify_sender), - }) + } } // if we want to send a message and don't care about response, we can don't need to reunite the split, @@ -181,7 +181,7 @@ impl PartiallyDelegated { // this call failing is incredibly unlikely, but not impossible. // basically the gateway connection must have failed after executing previous line but // before starting execution of this one. - if let Err(_) = notify.send(()) { + if notify.send(()).is_err() { return Err(GatewayClientError::ConnectionAbruptlyClosed); } @@ -212,6 +212,9 @@ impl SocketState { } pub(crate) fn is_established(&self) -> bool { - matches!(self, SocketState::Available(_) | SocketState::PartiallyDelegated(_)) + matches!( + self, + SocketState::Available(_) | SocketState::PartiallyDelegated(_) + ) } } diff --git a/common/client-libs/mixnet-client/src/client.rs b/common/client-libs/mixnet-client/src/client.rs index a0781824f2..4b3281d1cf 100644 --- a/common/client-libs/mixnet-client/src/client.rs +++ b/common/client-libs/mixnet-client/src/client.rs @@ -144,7 +144,7 @@ impl Client { self.config .initial_reconnection_backoff .checked_mul(2_u32.pow(current_attempt)) - .unwrap_or_else(|| self.config.maximum_reconnection_backoff), + .unwrap_or(self.config.maximum_reconnection_backoff), self.config.maximum_reconnection_backoff, ), )) diff --git a/common/client-libs/validator-client/src/models/topology.rs b/common/client-libs/validator-client/src/models/topology.rs index 5b9eff2a76..96934049ed 100644 --- a/common/client-libs/validator-client/src/models/topology.rs +++ b/common/client-libs/validator-client/src/models/topology.rs @@ -31,12 +31,12 @@ pub struct Topology { // changed from `TryInto`. reason being is that we should not fail entire topology // conversion if there's one invalid node on the network screwing around -impl Into for Topology { - fn into(self) -> NymTopology { +impl From for NymTopology { + fn from(topology: Topology) -> Self { use std::collections::HashMap; let mut mixes = HashMap::new(); - for mix in self.mix_nodes.into_iter() { + for mix in topology.mix_nodes.into_iter() { let layer = mix.mix_info.layer as MixLayer; if layer == 0 || layer > 3 { warn!( @@ -57,8 +57,8 @@ impl Into for Topology { } } - let mut gateways = Vec::with_capacity(self.gateways.len()); - for gate in self.gateways.into_iter() { + let mut gateways = Vec::with_capacity(topology.gateways.len()); + for gate in topology.gateways.into_iter() { let gate_id = gate.gateway_info.node_info.identity_key.clone(); match gate.try_into() { Ok(gate) => gateways.push(gate), diff --git a/common/client-libs/validator-client/src/rest_requests/gateway_register_post.rs b/common/client-libs/validator-client/src/rest_requests/gateway_register_post.rs index a2ba865bee..a6037fe3da 100644 --- a/common/client-libs/validator-client/src/rest_requests/gateway_register_post.rs +++ b/common/client-libs/validator-client/src/rest_requests/gateway_register_post.rs @@ -35,7 +35,7 @@ impl RESTRequest for Request { _: Option>, body_payload: Option, ) -> Result { - let payload = body_payload.ok_or_else(|| RESTRequestError::NoPayloadProvided)?; + let payload = body_payload.ok_or(RESTRequestError::NoPayloadProvided)?; let url = Url::parse(&format!("{}{}", base_url, Self::RELATIVE_PATH)) .map_err(|err| RESTRequestError::MalformedUrl(err.to_string()))?; diff --git a/common/client-libs/validator-client/src/rest_requests/mix_mining_batch_status_post.rs b/common/client-libs/validator-client/src/rest_requests/mix_mining_batch_status_post.rs index df109f9bf1..214d91e53b 100644 --- a/common/client-libs/validator-client/src/rest_requests/mix_mining_batch_status_post.rs +++ b/common/client-libs/validator-client/src/rest_requests/mix_mining_batch_status_post.rs @@ -20,7 +20,7 @@ impl RESTRequest for Request { _: Option>, body_payload: Option, ) -> Result { - let payload = body_payload.ok_or_else(|| RESTRequestError::NoPayloadProvided)?; + let payload = body_payload.ok_or(RESTRequestError::NoPayloadProvided)?; let url = Url::parse(&format!("{}{}", base_url, Self::RELATIVE_PATH)) .map_err(|err| RESTRequestError::MalformedUrl(err.to_string()))?; Ok(Request { url, payload }) diff --git a/common/client-libs/validator-client/src/rest_requests/mix_mining_status_post.rs b/common/client-libs/validator-client/src/rest_requests/mix_mining_status_post.rs index 2f82642d29..88a4aa4e75 100644 --- a/common/client-libs/validator-client/src/rest_requests/mix_mining_status_post.rs +++ b/common/client-libs/validator-client/src/rest_requests/mix_mining_status_post.rs @@ -20,7 +20,7 @@ impl RESTRequest for Request { _: Option>, body_payload: Option, ) -> Result { - let payload = body_payload.ok_or_else(|| RESTRequestError::NoPayloadProvided)?; + let payload = body_payload.ok_or(RESTRequestError::NoPayloadProvided)?; let url = Url::parse(&format!("{}{}", base_url, Self::RELATIVE_PATH)) .map_err(|err| RESTRequestError::MalformedUrl(err.to_string()))?; Ok(Request { url, payload }) diff --git a/common/client-libs/validator-client/src/rest_requests/mix_register_post.rs b/common/client-libs/validator-client/src/rest_requests/mix_register_post.rs index 4a0c477a1a..fe2b997c54 100644 --- a/common/client-libs/validator-client/src/rest_requests/mix_register_post.rs +++ b/common/client-libs/validator-client/src/rest_requests/mix_register_post.rs @@ -35,7 +35,7 @@ impl RESTRequest for Request { _: Option>, body_payload: Option, ) -> Result { - let payload = body_payload.ok_or_else(|| RESTRequestError::NoPayloadProvided)?; + let payload = body_payload.ok_or(RESTRequestError::NoPayloadProvided)?; let url = Url::parse(&format!("{}{}", base_url, Self::RELATIVE_PATH)) .map_err(|err| RESTRequestError::MalformedUrl(err.to_string()))?; diff --git a/common/client-libs/validator-client/src/rest_requests/node_unregister_delete.rs b/common/client-libs/validator-client/src/rest_requests/node_unregister_delete.rs index a899724b0e..42c73e063e 100644 --- a/common/client-libs/validator-client/src/rest_requests/node_unregister_delete.rs +++ b/common/client-libs/validator-client/src/rest_requests/node_unregister_delete.rs @@ -33,7 +33,7 @@ impl RESTRequest for Request { _: Option, ) -> Result { // node unregister requires single path param - the node id - let path_params = path_params.ok_or_else(|| RESTRequestError::InvalidPathParams)?; + let path_params = path_params.ok_or(RESTRequestError::InvalidPathParams)?; if path_params.len() != 1 { return Err(RESTRequestError::InvalidPathParams); } diff --git a/common/client-libs/validator-client/src/rest_requests/set_reputation_patch.rs b/common/client-libs/validator-client/src/rest_requests/set_reputation_patch.rs index 3307fcccba..01e5fcfbaa 100644 --- a/common/client-libs/validator-client/src/rest_requests/set_reputation_patch.rs +++ b/common/client-libs/validator-client/src/rest_requests/set_reputation_patch.rs @@ -34,12 +34,12 @@ impl RESTRequest for Request { ) -> Result { // set reputation requires single path param - the node id // and single query param - what reputation should it be set to - let path_params = path_params.ok_or_else(|| RESTRequestError::InvalidPathParams)?; + let path_params = path_params.ok_or(RESTRequestError::InvalidPathParams)?; if path_params.len() != 1 { return Err(RESTRequestError::InvalidPathParams); } - let query_params = query_params.ok_or_else(|| RESTRequestError::InvalidQueryParams)?; + let query_params = query_params.ok_or(RESTRequestError::InvalidQueryParams)?; if query_params.len() != 1 { return Err(RESTRequestError::InvalidQueryParams); } diff --git a/common/crypto/src/asymmetric/encryption/mod.rs b/common/crypto/src/asymmetric/encryption/mod.rs index f080717d9f..f54fcbc77d 100644 --- a/common/crypto/src/asymmetric/encryption/mod.rs +++ b/common/crypto/src/asymmetric/encryption/mod.rs @@ -13,7 +13,7 @@ // limitations under the License. use pemstore::traits::{PemStorableKey, PemStorableKeyPair}; -use rand::{rngs::OsRng, CryptoRng, RngCore}; +use rand::{CryptoRng, RngCore}; use std::fmt::{self, Display, Formatter}; /// Size of a X25519 private key @@ -57,12 +57,7 @@ pub struct KeyPair { } impl KeyPair { - pub fn new() -> Self { - let mut rng = OsRng; - Self::new_with_rng(&mut rng) - } - - pub fn new_with_rng(rng: &mut R) -> Self { + pub fn new(rng: &mut R) -> Self { let private_key = x25519_dalek::StaticSecret::new(rng); let public_key = (&private_key).into(); @@ -206,16 +201,15 @@ impl PemStorableKey for PrivateKey { } // compatibility with sphinx keys: - -impl Into for PublicKey { - fn into(self) -> nymsphinx_types::PublicKey { - nymsphinx_types::PublicKey::from(self.to_bytes()) +impl From for nymsphinx_types::PublicKey { + fn from(key: PublicKey) -> Self { + nymsphinx_types::PublicKey::from(key.to_bytes()) } } -impl<'a> Into for &'a PublicKey { - fn into(self) -> nymsphinx_types::PublicKey { - nymsphinx_types::PublicKey::from(self.to_bytes()) +impl<'a> From<&'a PublicKey> for nymsphinx_types::PublicKey { + fn from(key: &'a PublicKey) -> Self { + nymsphinx_types::PublicKey::from(key.to_bytes()) } } @@ -225,15 +219,15 @@ impl From for PublicKey { } } -impl Into for PrivateKey { - fn into(self) -> nymsphinx_types::PrivateKey { - nymsphinx_types::PrivateKey::from(self.to_bytes()) +impl From for nymsphinx_types::PrivateKey { + fn from(key: PrivateKey) -> Self { + nymsphinx_types::PrivateKey::from(key.to_bytes()) } } -impl<'a> Into for &'a PrivateKey { - fn into(self) -> nymsphinx_types::PrivateKey { - nymsphinx_types::PrivateKey::from(self.to_bytes()) +impl<'a> From<&'a PrivateKey> for nymsphinx_types::PrivateKey { + fn from(key: &'a PrivateKey) -> Self { + nymsphinx_types::PrivateKey::from(key.to_bytes()) } } @@ -253,8 +247,10 @@ mod sphinx_key_conversion { #[test] fn works_for_forward_conversion() { + let mut rng = rand::rngs::OsRng; + for _ in 0..NUM_ITERATIONS { - let keys = KeyPair::new(); + let keys = KeyPair::new(&mut rng); let private = keys.private_key; let public = keys.public_key; diff --git a/common/crypto/src/asymmetric/identity/mod.rs b/common/crypto/src/asymmetric/identity/mod.rs index 3bec2a786e..e057e413fc 100644 --- a/common/crypto/src/asymmetric/identity/mod.rs +++ b/common/crypto/src/asymmetric/identity/mod.rs @@ -17,7 +17,7 @@ pub use ed25519_dalek::SignatureError; pub use ed25519_dalek::{Verifier, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, SIGNATURE_LENGTH}; use nymsphinx_types::{DestinationAddressBytes, DESTINATION_ADDRESS_LENGTH}; use pemstore::traits::{PemStorableKey, PemStorableKeyPair}; -use rand::{rngs::OsRng, CryptoRng, RngCore}; +use rand::{CryptoRng, RngCore}; use std::fmt::{self, Formatter}; #[derive(Debug)] @@ -56,12 +56,7 @@ pub struct KeyPair { } impl KeyPair { - pub fn new() -> Self { - let mut rng = OsRng; - Self::new_with_rng(&mut rng) - } - - pub fn new_with_rng(rng: &mut R) -> Self { + pub fn new(rng: &mut R) -> Self { let ed25519_keypair = ed25519_dalek::Keypair::generate(rng); KeyPair { diff --git a/common/crypto/src/shared_key.rs b/common/crypto/src/shared_key.rs index 0697b39a71..be8b4e475c 100644 --- a/common/crypto/src/shared_key.rs +++ b/common/crypto/src/shared_key.rs @@ -32,7 +32,7 @@ where D::OutputSize: ArrayLength, R: RngCore + CryptoRng, { - let ephemeral_keypair = encryption::KeyPair::new_with_rng(rng); + let ephemeral_keypair = encryption::KeyPair::new(rng); // after performing diffie-hellman we don't care about the private component anymore let dh_result = ephemeral_keypair.private_key().diffie_hellman(remote_key); diff --git a/common/nonexhaustive-delayqueue/src/lib.rs b/common/nonexhaustive-delayqueue/src/lib.rs index e7c7a6254e..669ab36a14 100644 --- a/common/nonexhaustive-delayqueue/src/lib.rs +++ b/common/nonexhaustive-delayqueue/src/lib.rs @@ -61,6 +61,12 @@ impl NonExhaustiveDelayQueue { } } +impl Default for NonExhaustiveDelayQueue { + fn default() -> Self { + NonExhaustiveDelayQueue::new() + } +} + impl Stream for NonExhaustiveDelayQueue { type Item = as Stream>::Item; diff --git a/common/nymsphinx/addressing/Cargo.toml b/common/nymsphinx/addressing/Cargo.toml index 5b7c85f039..b1c04c452b 100644 --- a/common/nymsphinx/addressing/Cargo.toml +++ b/common/nymsphinx/addressing/Cargo.toml @@ -10,3 +10,6 @@ edition = "2018" crypto = { path = "../../crypto" } # all addresses are expressed in terms on their crypto keys nymsphinx-types = { path = "../types" } # we need to be able to refer to some types defined inside sphinx crate serde = "1.0" # implementing serialization/deserialization for some types, like `Recipient` + +[dev-dependencies] +rand = "0.7" \ No newline at end of file diff --git a/common/nymsphinx/addressing/src/clients.rs b/common/nymsphinx/addressing/src/clients.rs index 67de7f8cc2..4b80b604c0 100644 --- a/common/nymsphinx/addressing/src/clients.rs +++ b/common/nymsphinx/addressing/src/clients.rs @@ -245,9 +245,11 @@ mod tests { #[test] fn string_conversion_works() { - let client_id_pair = identity::KeyPair::new(); - let client_enc_pair = encryption::KeyPair::new(); - let gateway_id_pair = identity::KeyPair::new(); + let mut rng = rand::thread_rng(); + + let client_id_pair = identity::KeyPair::new(&mut rng); + let client_enc_pair = encryption::KeyPair::new(&mut rng); + let gateway_id_pair = identity::KeyPair::new(&mut rng); let recipient = Recipient::new( *client_id_pair.public_key(), @@ -275,9 +277,11 @@ mod tests { #[test] fn bytes_conversion_works() { - let client_id_pair = identity::KeyPair::new(); - let client_enc_pair = encryption::KeyPair::new(); - let gateway_id_pair = identity::KeyPair::new(); + let mut rng = rand::thread_rng(); + + let client_id_pair = identity::KeyPair::new(&mut rng); + let client_enc_pair = encryption::KeyPair::new(&mut rng); + let gateway_id_pair = identity::KeyPair::new(&mut rng); let recipient = Recipient::new( *client_id_pair.public_key(), diff --git a/common/nymsphinx/addressing/src/nodes.rs b/common/nymsphinx/addressing/src/nodes.rs index 58c0b9cadf..a24e92afcb 100644 --- a/common/nymsphinx/addressing/src/nodes.rs +++ b/common/nymsphinx/addressing/src/nodes.rs @@ -139,9 +139,9 @@ impl From for NymNodeRoutingAddress { /// Considering `NymNodeRoutingAddress` is equivalent to a `SocketAddr` at this point, /// it makes perfect sense to allow the bilateral transformation. -impl Into for NymNodeRoutingAddress { - fn into(self) -> SocketAddr { - self.0 +impl From for SocketAddr { + fn from(addr: NymNodeRoutingAddress) -> Self { + addr.0 } } diff --git a/common/nymsphinx/chunking/src/fragment.rs b/common/nymsphinx/chunking/src/fragment.rs index f9031f9863..dc672f7033 100644 --- a/common/nymsphinx/chunking/src/fragment.rs +++ b/common/nymsphinx/chunking/src/fragment.rs @@ -169,25 +169,19 @@ impl Fragment { if payload.len() != linked_fragment_payload_max_len(max_plaintext_size) { return Err(ChunkingError::InvalidPayloadLengthError); } - } else { - if payload.len() > linked_fragment_payload_max_len(max_plaintext_size) { - return Err(ChunkingError::InvalidPayloadLengthError); - } + } else if payload.len() > linked_fragment_payload_max_len(max_plaintext_size) { + return Err(ChunkingError::InvalidPayloadLengthError); } } else if next_fragments_set_id.is_some() { if payload.len() != linked_fragment_payload_max_len(max_plaintext_size) { return Err(ChunkingError::InvalidPayloadLengthError); } - } else { - if total_fragments != current_fragment { - if payload.len() != unlinked_fragment_payload_max_len(max_plaintext_size) { - return Err(ChunkingError::InvalidPayloadLengthError); - } - } else { - if payload.len() > unlinked_fragment_payload_max_len(max_plaintext_size) { - return Err(ChunkingError::InvalidPayloadLengthError); - } + } else if total_fragments != current_fragment { + if payload.len() != unlinked_fragment_payload_max_len(max_plaintext_size) { + return Err(ChunkingError::InvalidPayloadLengthError); } + } else if payload.len() > unlinked_fragment_payload_max_len(max_plaintext_size) { + return Err(ChunkingError::InvalidPayloadLengthError); } Ok(Fragment { @@ -453,7 +447,7 @@ impl FragmentHeader { // everything below are tests #[cfg(test)] -mod fragment { +mod fragment_tests { use super::*; use nymsphinx_params::packet_sizes::PacketSize; use rand::{thread_rng, RngCore}; @@ -952,7 +946,7 @@ mod fragment_header { // clear the fragmentation flag header_bytes_low[0] &= !(1 << 7); - let mut header_bytes_high = header_bytes_low.clone(); + let mut header_bytes_high = header_bytes_low; // make sure first byte of id is non-empty (apart from the fragmentation flag) // note for anyone reading this test in the future: choice of '3' here is arbitrary. header_bytes_high[0] |= 1 << 3; diff --git a/common/nymsphinx/chunking/src/lib.rs b/common/nymsphinx/chunking/src/lib.rs index 864f602b93..cf6db0778b 100644 --- a/common/nymsphinx/chunking/src/lib.rs +++ b/common/nymsphinx/chunking/src/lib.rs @@ -115,21 +115,21 @@ pub fn number_of_required_fragments( // we must be careful with the last set as it might be the case that it only // consists of a single, linked, non-full fragment - if final_set_message_len < max_linked { - return (without_last + 1, max_linked - final_set_message_len); - } else if final_set_message_len == max_linked { - return (without_last + 1, 0); - } + match final_set_message_len { + n if n < max_linked => (without_last + 1, max_linked - final_set_message_len), + n if n == max_linked => (without_last + 1, 0), + _ => { + let remaining_len = final_set_message_len - max_linked; - let remaining_len = final_set_message_len - max_linked; + let quot = remaining_len / max_unlinked; + let rem = remaining_len % max_unlinked; - let quot = remaining_len / max_unlinked; - let rem = remaining_len % max_unlinked; - - if rem == 0 { - (without_last + quot + 1, 0) - } else { - (without_last + quot + 2, max_unlinked - rem) + if rem == 0 { + (without_last + quot + 1, 0) + } else { + (without_last + quot + 2, max_unlinked - rem) + } + } } } } diff --git a/common/nymsphinx/chunking/src/reconstruction.rs b/common/nymsphinx/chunking/src/reconstruction.rs index d748b8452c..a98b49ed01 100644 --- a/common/nymsphinx/chunking/src/reconstruction.rs +++ b/common/nymsphinx/chunking/src/reconstruction.rs @@ -456,8 +456,9 @@ mod reconstruction_buffer { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); - for i in 0..254 { - buf.insert_fragment(Fragment::try_from_bytes(&raw_fragments[i]).unwrap()); + + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize - 1) { + buf.insert_fragment(Fragment::try_from_bytes(&raw_fragment).unwrap()); } assert!(!buf.is_complete); @@ -536,12 +537,13 @@ mod message_reconstructor { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); + // first set is fully inserted - for i in 0..255 { + for raw_fragment in raw_fragments.iter() { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()) @@ -572,15 +574,17 @@ mod message_reconstructor { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); - for i in 0..254 { + + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); } + // finish next set for good measure assert!(reconstructor .insert_new_fragment( @@ -619,11 +623,11 @@ mod message_reconstructor { .collect(); // note that first set is not fully inserted - for i in 0..254 { + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize - 1) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -664,11 +668,12 @@ mod message_reconstructor { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); - for i in 0..255 { + + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -705,11 +710,12 @@ mod message_reconstructor { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); - for i in 0..255 { + + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -744,11 +750,11 @@ mod message_reconstructor { .collect(); // note that first set is not fully inserted - for i in 0..254 { + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize - 1) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -784,11 +790,12 @@ mod message_reconstructor { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); - for i in 0..(u8::max_value() as usize) * 2 { + + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize * 2) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -825,12 +832,17 @@ mod message_reconstructor { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); + // note that first set is not fully inserted - for i in 1..(u8::max_value() as usize) * 2 { + for raw_fragment in raw_fragments + .iter() + .skip(1) + .take(u8::max_value() as usize * 2 - 1) + { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -896,11 +908,11 @@ mod message_reconstructor { .collect(); // note that first set is not fully inserted - for i in 0..254 { + for raw_fragment in raw_fragments1.iter().take(u8::max_value() as usize - 1) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments1[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -930,11 +942,11 @@ mod message_reconstructor { .map(|x| x.into_bytes()) .collect(); - for i in 0..255 { + for raw_fragment in raw_fragments2.iter().take(u8::max_value() as usize) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments2[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -969,11 +981,12 @@ mod message_reconstructor { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); - for i in 0..255 { + + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize) { assert!(reconstructor .insert_new_fragment( reconstructor - .recover_fragment(raw_fragments[i].clone()) + .recover_fragment(raw_fragment.clone()) .unwrap() ) .is_none()); @@ -1265,8 +1278,9 @@ mod message_reconstructor { .flat_map(|fragment_set| fragment_set.into_iter()) .map(|x| x.into_bytes()) .collect(); - for i in 0..255 { - set_buf1.insert_fragment(Fragment::try_from_bytes(&raw_fragments[i]).unwrap()); + + for raw_fragment in raw_fragments.iter().take(u8::max_value() as usize) { + set_buf1.insert_fragment(Fragment::try_from_bytes(&raw_fragment).unwrap()); } set_buf2.insert_fragment(Fragment::try_from_bytes(&raw_fragments[255]).unwrap()); @@ -1496,11 +1510,12 @@ mod message_reconstruction { assert_eq!(fragments.len(), 30); let mut message_reconstructor = MessageReconstructor::default(); - for i in 0..29 { + + for fragment in fragments.iter().take(fragments.len() - 1) { assert!(message_reconstructor .insert_new_fragment( message_reconstructor - .recover_fragment(fragments[i].clone()) + .recover_fragment(fragment.clone()) .unwrap() ) .is_none()); @@ -1538,11 +1553,11 @@ mod message_reconstruction { fragments.shuffle(&mut rng); let mut message_reconstructor = MessageReconstructor::default(); - for i in 0..29 { + for fragment in fragments.iter().take(fragments.len() - 1) { assert!(message_reconstructor .insert_new_fragment( message_reconstructor - .recover_fragment(fragments[i].clone()) + .recover_fragment(fragment.clone()) .unwrap() ) .is_none()); diff --git a/common/nymsphinx/chunking/src/set.rs b/common/nymsphinx/chunking/src/set.rs index 9742eb96a3..6a08249dfd 100644 --- a/common/nymsphinx/chunking/src/set.rs +++ b/common/nymsphinx/chunking/src/set.rs @@ -389,7 +389,7 @@ mod tests { } } - fn verify_correct_link(left: &FragmentSet, right: &FragmentSet) { + fn verify_correct_link(left: &[Fragment], right: &[Fragment]) { let first_id = left[0].id(); let post_id = left[254].next_fragments_set_id().unwrap(); diff --git a/common/nymsphinx/framing/src/codec.rs b/common/nymsphinx/framing/src/codec.rs index a9f21a9f80..04efee85d7 100644 --- a/common/nymsphinx/framing/src/codec.rs +++ b/common/nymsphinx/framing/src/codec.rs @@ -35,9 +35,9 @@ impl From for SphinxCodecError { } } -impl Into for SphinxCodecError { - fn into(self) -> io::Error { - match self { +impl From for io::Error { + fn from(err: SphinxCodecError) -> Self { + match err { SphinxCodecError::InvalidPacketSize => { io::Error::new(io::ErrorKind::InvalidInput, "invalid packet size") } @@ -72,7 +72,7 @@ impl Encoder for SphinxCodec { type Error = SphinxCodecError; fn encode(&mut self, item: FramedSphinxPacket, dst: &mut BytesMut) -> Result<(), Self::Error> { - item.header.encode(dst)?; + item.header.encode(dst); dst.put(item.packet.to_bytes().as_ref()); Ok(()) } @@ -236,7 +236,7 @@ mod packet_encoding { packet_mode: Default::default(), }; let mut bytes = BytesMut::new(); - header.encode(&mut bytes).unwrap(); + header.encode(&mut bytes); assert!(SphinxCodec.decode(&mut bytes).unwrap().is_none()); assert_eq!(bytes.capacity(), Header::SIZE + packet_size.size()) diff --git a/common/nymsphinx/framing/src/packet.rs b/common/nymsphinx/framing/src/packet.rs index 1296f5e75b..4ae1e5b436 100644 --- a/common/nymsphinx/framing/src/packet.rs +++ b/common/nymsphinx/framing/src/packet.rs @@ -75,14 +75,13 @@ pub struct Header { impl Header { pub(crate) const SIZE: usize = 2; - pub(crate) fn encode(&self, dst: &mut BytesMut) -> Result<(), SphinxCodecError> { + pub(crate) fn encode(&self, dst: &mut BytesMut) { // we reserve one byte for `packet_size` and the other for `mode` dst.reserve(Self::SIZE); dst.put_u8(self.packet_size as u8); dst.put_u8(self.packet_mode as u8); // reserve bytes for the actual packet dst.reserve(self.packet_size.size()); - Ok(()) } pub(crate) fn decode(src: &mut BytesMut) -> Result, SphinxCodecError> { @@ -107,7 +106,7 @@ mod header_encoding { fn header_can_be_decoded_from_a_valid_encoded_instance() { let header = Header::default(); let mut bytes = BytesMut::new(); - header.encode(&mut bytes).unwrap(); + header.encode(&mut bytes); let decoded = Header::decode(&mut bytes).unwrap().unwrap(); assert_eq!(decoded, header); } @@ -158,7 +157,7 @@ mod header_encoding { packet_mode: Default::default(), }; let mut bytes = BytesMut::new(); - header.encode(&mut bytes).unwrap(); + header.encode(&mut bytes); assert_eq!(bytes.capacity(), bytes.len() + packet_size.size()) } } diff --git a/common/nymsphinx/src/preparer/vpn_manager.rs b/common/nymsphinx/src/preparer/vpn_manager.rs index dd589d38e4..94b53a8bf4 100644 --- a/common/nymsphinx/src/preparer/vpn_manager.rs +++ b/common/nymsphinx/src/preparer/vpn_manager.rs @@ -141,7 +141,7 @@ impl VPNManager { .load(Ordering::SeqCst) } - pub(super) async fn use_secret<'a, R>(&'a mut self, rng: R) -> SpinhxKeyRef<'a> + pub(super) async fn use_secret(&mut self, rng: R) -> SpinhxKeyRef<'_> where R: CryptoRng + Rng, { diff --git a/common/socks5/ordered-buffer/src/buffer.rs b/common/socks5/ordered-buffer/src/buffer.rs index 4034b2ca72..19d946f8d6 100644 --- a/common/socks5/ordered-buffer/src/buffer.rs +++ b/common/socks5/ordered-buffer/src/buffer.rs @@ -15,7 +15,6 @@ pub struct OrderedMessageBuffer { impl OrderedMessageBuffer { pub fn new() -> OrderedMessageBuffer { - trace!("Creating ordered message buffer."); OrderedMessageBuffer { next_index: 0, messages: HashMap::new(), @@ -71,6 +70,12 @@ impl OrderedMessageBuffer { } } +impl Default for OrderedMessageBuffer { + fn default() -> Self { + OrderedMessageBuffer::new() + } +} + #[cfg(test)] mod test_chunking_and_reassembling { use super::*; diff --git a/common/socks5/ordered-buffer/src/sender.rs b/common/socks5/ordered-buffer/src/sender.rs index c4c16e03a5..f348b4e904 100644 --- a/common/socks5/ordered-buffer/src/sender.rs +++ b/common/socks5/ordered-buffer/src/sender.rs @@ -24,6 +24,12 @@ impl OrderedMessageSender { } } +impl Default for OrderedMessageSender { + fn default() -> Self { + OrderedMessageSender::new() + } +} + #[cfg(test)] mod ordered_message_sender { use super::*; diff --git a/common/socks5/proxy-helpers/src/available_reader.rs b/common/socks5/proxy-helpers/src/available_reader.rs index 2fed57602e..d84331e4bb 100644 --- a/common/socks5/proxy-helpers/src/available_reader.rs +++ b/common/socks5/proxy-helpers/src/available_reader.rs @@ -90,7 +90,7 @@ impl<'a, R: AsyncRead + Unpin> Stream for AvailableReader<'a, R> { // if we read a non-0 amount, we're not done yet! if n == 0 { let buf = self.buf.replace(BytesMut::new()); - if buf.len() > 0 { + if !buf.is_empty() { Poll::Ready(Some(Ok(buf.freeze()))) } else { Poll::Ready(None) diff --git a/common/socks5/proxy-helpers/src/connection_controller.rs b/common/socks5/proxy-helpers/src/connection_controller.rs index eaf2cada75..1494b8986f 100644 --- a/common/socks5/proxy-helpers/src/connection_controller.rs +++ b/common/socks5/proxy-helpers/src/connection_controller.rs @@ -159,17 +159,18 @@ impl Controller { // TODO: // TODO: } + } else if !self.recently_closed.contains(&conn_id) { + warn!("Received a 'Send' before 'Connect' - going to buffer the data"); + let pending = self + .pending_messages + .entry(conn_id) + .or_insert_with(Vec::new); + pending.push((payload, is_closed)); } else { - if !self.recently_closed.contains(&conn_id) { - warn!("Received a 'Send' before 'Connect' - going to buffer the data"); - let pending = self.pending_messages.entry(conn_id).or_insert(Vec::new()); - pending.push((payload, is_closed)); - } else { - error!( - "Tried to write to closed connection ({} bytes were 'lost)", - payload.len() - ) - } + error!( + "Tried to write to closed connection ({} bytes were 'lost)", + payload.len() + ) } } diff --git a/common/socks5/proxy-helpers/src/proxy_runner.rs b/common/socks5/proxy-helpers/src/proxy_runner.rs index c951eaf299..0388cbae73 100644 --- a/common/socks5/proxy-helpers/src/proxy_runner.rs +++ b/common/socks5/proxy-helpers/src/proxy_runner.rs @@ -29,11 +29,11 @@ pub struct ProxyMessage { pub socket_closed: bool, } -impl Into for (Vec, bool) { - fn into(self) -> ProxyMessage { +impl From<(Vec, bool)> for ProxyMessage { + fn from(data: (Vec, bool)) -> Self { ProxyMessage { - data: self.0, - socket_closed: self.1, + data: data.0, + socket_closed: data.1, } } } diff --git a/common/topology/src/gateway.rs b/common/topology/src/gateway.rs index 39300ddef6..56d89ad69f 100644 --- a/common/topology/src/gateway.rs +++ b/common/topology/src/gateway.rs @@ -43,12 +43,12 @@ impl filter::Versioned for Node { } } -impl<'a> Into for &'a Node { - fn into(self) -> SphinxNode { - let node_address_bytes = NymNodeRoutingAddress::from(self.mixnet_listener) +impl<'a> From<&'a Node> for SphinxNode { + fn from(node: &'a Node) -> Self { + let node_address_bytes = NymNodeRoutingAddress::from(node.mixnet_listener) .try_into() .unwrap(); - SphinxNode::new(node_address_bytes, (&self.sphinx_key).into()) + SphinxNode::new(node_address_bytes, (&node.sphinx_key).into()) } } diff --git a/common/topology/src/lib.rs b/common/topology/src/lib.rs index 386fef0fc0..87ebd52085 100644 --- a/common/topology/src/lib.rs +++ b/common/topology/src/lib.rs @@ -125,7 +125,7 @@ impl NymTopology { { let gateway = self .get_gateway(gateway_identity) - .ok_or_else(|| NymTopologyError::NonExistentGatewayError)?; + .ok_or(NymTopologyError::NonExistentGatewayError)?; Ok(self .random_mix_route(rng, num_mix_hops)? @@ -224,11 +224,7 @@ mod converting_mixes_to_vec { let topology = NymTopology::new(mixes, vec![]); let mixvec = topology.mixes_as_vec(); - assert!(mixvec - .iter() - .map(|node| node.location.clone()) - .collect::>() - .contains(&"London".to_string())); + assert!(mixvec.iter().any(|node| node.location == "London")); } } diff --git a/common/topology/src/mix.rs b/common/topology/src/mix.rs index 3ee90e4d53..f5783e7cc0 100644 --- a/common/topology/src/mix.rs +++ b/common/topology/src/mix.rs @@ -37,10 +37,10 @@ impl filter::Versioned for Node { } } -impl<'a> Into for &'a Node { - fn into(self) -> SphinxNode { - let node_address_bytes = NymNodeRoutingAddress::from(self.host).try_into().unwrap(); +impl<'a> From<&'a Node> for SphinxNode { + fn from(node: &'a Node) -> Self { + let node_address_bytes = NymNodeRoutingAddress::from(node.host).try_into().unwrap(); - SphinxNode::new(node_address_bytes, (&self.sphinx_key).into()) + SphinxNode::new(node_address_bytes, (&node.sphinx_key).into()) } } diff --git a/gateway/gateway-requests/src/authentication/encrypted_address.rs b/gateway/gateway-requests/src/authentication/encrypted_address.rs index c970ab657a..d29750a888 100644 --- a/gateway/gateway-requests/src/authentication/encrypted_address.rs +++ b/gateway/gateway-requests/src/authentication/encrypted_address.rs @@ -89,8 +89,8 @@ impl EncryptedAddressBytes { } } -impl Into for EncryptedAddressBytes { - fn into(self) -> String { - self.to_base58_string() +impl From for String { + fn from(val: EncryptedAddressBytes) -> Self { + val.to_base58_string() } } diff --git a/gateway/gateway-requests/src/authentication/iv.rs b/gateway/gateway-requests/src/authentication/iv.rs index b92079004c..171da20f1a 100644 --- a/gateway/gateway-requests/src/authentication/iv.rs +++ b/gateway/gateway-requests/src/authentication/iv.rs @@ -73,8 +73,8 @@ impl AuthenticationIV { } } -impl Into for AuthenticationIV { - fn into(self) -> String { - self.to_base58_string() +impl From for String { + fn from(iv: AuthenticationIV) -> Self { + iv.to_base58_string() } } diff --git a/gateway/gateway-requests/src/registration/handshake/mod.rs b/gateway/gateway-requests/src/registration/handshake/mod.rs index f7e725c6c5..b48a1051c0 100644 --- a/gateway/gateway-requests/src/registration/handshake/mod.rs +++ b/gateway/gateway-requests/src/registration/handshake/mod.rs @@ -18,13 +18,9 @@ use self::gateway::GatewayHandshake; pub use self::shared_key::{SharedKeySize, SharedKeys}; use crypto::asymmetric::identity; use futures::{Sink, Stream}; -use rand::rngs::OsRng; use rand::{CryptoRng, RngCore}; use tungstenite::{Error as WsError, Message as WsMessage}; -// for ease of use -pub const DEFAULT_RNG: OsRng = OsRng; - pub(crate) type WsItem = Result; mod client; diff --git a/gateway/gateway-requests/src/registration/handshake/shared_key.rs b/gateway/gateway-requests/src/registration/handshake/shared_key.rs index b93d84600d..d716e7ced3 100644 --- a/gateway/gateway-requests/src/registration/handshake/shared_key.rs +++ b/gateway/gateway-requests/src/registration/handshake/shared_key.rs @@ -149,9 +149,9 @@ impl SharedKeys { } } -impl Into for SharedKeys { - fn into(self) -> String { - self.to_base58_string() +impl From for String { + fn from(keys: SharedKeys) -> Self { + keys.to_base58_string() } } diff --git a/gateway/gateway-requests/src/registration/handshake/state.rs b/gateway/gateway-requests/src/registration/handshake/state.rs index ca89fb2ec1..96766b1c2c 100644 --- a/gateway/gateway-requests/src/registration/handshake/state.rs +++ b/gateway/gateway-requests/src/registration/handshake/state.rs @@ -56,7 +56,7 @@ impl<'a, S> State<'a, S> { identity: &'a identity::KeyPair, remote_pubkey: Option, ) -> Self { - let ephemeral_keypair = encryption::KeyPair::new_with_rng(rng); + let ephemeral_keypair = encryption::KeyPair::new(rng); State { ws_stream, ephemeral_keypair, diff --git a/gateway/gateway-requests/src/types.rs b/gateway/gateway-requests/src/types.rs index 27f895fb3a..b963862300 100644 --- a/gateway/gateway-requests/src/types.rs +++ b/gateway/gateway-requests/src/types.rs @@ -139,11 +139,11 @@ impl ClientControlRequest { } } -impl Into for ClientControlRequest { - fn into(self) -> Message { +impl From for Message { + fn from(req: ClientControlRequest) -> Self { // it should be safe to call `unwrap` here as the message is generated by the server // so if it fails (and consequently panics) it's a bug that should be resolved - let str_req = serde_json::to_string(&self).unwrap(); + let str_req = serde_json::to_string(&req).unwrap(); Message::Text(str_req) } } @@ -193,11 +193,11 @@ impl ServerResponse { } } -impl Into for ServerResponse { - fn into(self) -> Message { +impl From for Message { + fn from(res: ServerResponse) -> Self { // it should be safe to call `unwrap` here as the message is generated by the server // so if it fails (and consequently panics) it's a bug that should be resolved - let str_res = serde_json::to_string(&self).unwrap(); + let str_res = serde_json::to_string(&res).unwrap(); Message::Text(str_res) } } diff --git a/gateway/src/commands/init.rs b/gateway/src/commands/init.rs index e6ca156fe1..f57bc50023 100644 --- a/gateway/src/commands/init.rs +++ b/gateway/src/commands/init.rs @@ -128,8 +128,10 @@ pub fn execute(matches: &ArgMatches) { // if gateway was already initialised, don't generate new keys if !already_init { - let identity_keys = identity::KeyPair::new(); - let sphinx_keys = encryption::KeyPair::new(); + let mut rng = rand::rngs::OsRng; + + let identity_keys = identity::KeyPair::new(&mut rng); + let sphinx_keys = encryption::KeyPair::new(&mut rng); let pathfinder = GatewayPathfinder::new_from_config(&config); pemstore::store_keypair( &sphinx_keys, diff --git a/gateway/src/node/client_handling/websocket/connection_handler.rs b/gateway/src/node/client_handling/websocket/connection_handler.rs index 70d472c611..0b324dec4f 100644 --- a/gateway/src/node/client_handling/websocket/connection_handler.rs +++ b/gateway/src/node/client_handling/websocket/connection_handler.rs @@ -26,12 +26,13 @@ use futures::{ use gateway_requests::authentication::encrypted_address::EncryptedAddressBytes; use gateway_requests::authentication::iv::AuthenticationIV; use gateway_requests::registration::handshake::error::HandshakeError; -use gateway_requests::registration::handshake::{gateway_handshake, SharedKeys, DEFAULT_RNG}; +use gateway_requests::registration::handshake::{gateway_handshake, SharedKeys}; use gateway_requests::types::{BinaryRequest, ClientControlRequest, ServerResponse}; use gateway_requests::BinaryResponse; use log::*; use mixnet_client::forwarder::MixForwardingSender; use nymsphinx::DestinationAddressBytes; +use rand::{CryptoRng, Rng}; use std::convert::TryFrom; use std::sync::Arc; use tokio::{prelude::*, stream::StreamExt}; @@ -58,7 +59,8 @@ impl SocketStream { } } -pub(crate) struct Handle { +pub(crate) struct Handle { + rng: R, remote_address: Option, shared_key: Option, clients_handler_sender: ClientsHandlerRequestSender, @@ -68,16 +70,21 @@ pub(crate) struct Handle { local_identity: Arc, } -impl Handle { +impl Handle +where + R: Rng + CryptoRng, +{ // for time being we assume handle is always constructed from raw socket. // if we decide we want to change it, that's not too difficult pub(crate) fn new( + rng: R, conn: S, clients_handler_sender: ClientsHandlerRequestSender, outbound_mix_sender: MixForwardingSender, local_identity: Arc, ) -> Self { Handle { + rng, remote_address: None, shared_key: None, clients_handler_sender, @@ -121,7 +128,7 @@ impl Handle { match &mut self.socket_connection { SocketStream::UpgradedWebSocket(ws_stream) => { gateway_handshake( - &mut DEFAULT_RNG, + &mut self.rng, ws_stream, self.local_identity.as_ref(), init_msg, diff --git a/gateway/src/node/client_handling/websocket/listener.rs b/gateway/src/node/client_handling/websocket/listener.rs index 2c353c0c82..e41a576fc4 100644 --- a/gateway/src/node/client_handling/websocket/listener.rs +++ b/gateway/src/node/client_handling/websocket/listener.rs @@ -17,6 +17,7 @@ use crate::node::client_handling::websocket::connection_handler::Handle; use crypto::asymmetric::identity; use log::*; use mixnet_client::forwarder::MixForwardingSender; +use rand::rngs::OsRng; use std::net::SocketAddr; use std::sync::Arc; use tokio::task::JoinHandle; @@ -51,6 +52,7 @@ impl Listener { // TODO: I think we *REALLY* need a mechanism for having a maximum number of connected // clients or spawned tokio tasks -> perhaps a worker system? let mut handle = Handle::new( + OsRng, socket, clients_handler_sender.clone(), outbound_mix_sender.clone(), diff --git a/gateway/src/node/mod.rs b/gateway/src/node/mod.rs index 7c3e2f3abf..8db7a7340d 100644 --- a/gateway/src/node/mod.rs +++ b/gateway/src/node/mod.rs @@ -189,14 +189,9 @@ impl Gateway { } if let Err(err) = presence::register_with_validator( - self.config.get_validator_rest_endpoint(), - self.config.get_mix_announce_address(), - self.config.get_clients_announce_address(), + &self.config, self.identity.public_key().to_base58_string(), self.encryption_keys.public_key().to_base58_string(), - self.config.get_version().to_string(), - self.config.get_location(), - self.config.get_incentives_address() ).await { error!("failed to register with the validator - {:?}", err); return diff --git a/gateway/src/node/presence.rs b/gateway/src/node/presence.rs index 6b67222a88..1b97f24450 100644 --- a/gateway/src/node/presence.rs +++ b/gateway/src/node/presence.rs @@ -12,32 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. +use crate::config::Config; use validator_client::models::gateway::GatewayRegistrationInfo; use validator_client::ValidatorClientError; // there's no point in keeping the validator client persistently as it might be literally hours or days // before it's used again pub(crate) async fn register_with_validator( - validator_endpoint: String, - mix_host: String, - clients_host: String, + gateway_config: &Config, identity_key: String, sphinx_key: String, - version: String, - location: String, - incentives_address: Option, ) -> Result<(), ValidatorClientError> { - let config = validator_client::Config::new(validator_endpoint); + let config = validator_client::Config::new(gateway_config.get_validator_rest_endpoint()); let validator_client = validator_client::Client::new(config); let registration_info = GatewayRegistrationInfo::new( - mix_host, - clients_host, + gateway_config.get_mix_announce_address(), + gateway_config.get_clients_announce_address(), identity_key, sphinx_key, - version, - location, - incentives_address, + gateway_config.get_version().to_string(), + gateway_config.get_location(), + gateway_config.get_incentives_address(), ); validator_client.register_gateway(registration_info).await diff --git a/gateway/src/node/storage/ledger.rs b/gateway/src/node/storage/ledger.rs index 9cfc42b240..6ddd11af43 100644 --- a/gateway/src/node/storage/ledger.rs +++ b/gateway/src/node/storage/ledger.rs @@ -22,9 +22,9 @@ use std::path::PathBuf; #[derive(Debug)] pub(crate) enum ClientLedgerError { - DbReadError(sled::Error), - DbWriteError(sled::Error), - DbOpenError(sled::Error), + Read(sled::Error), + Write(sled::Error), + Open(sled::Error), } #[derive(Debug, Clone)] @@ -37,7 +37,7 @@ pub(crate) struct ClientLedger { impl ClientLedger { pub(crate) fn load(file: PathBuf) -> Result { let db = match sled::open(file) { - Err(e) => return Err(ClientLedgerError::DbOpenError(e)), + Err(e) => return Err(ClientLedgerError::Open(e)), Ok(db) => db, }; @@ -93,7 +93,7 @@ impl ClientLedger { iv: &AuthenticationIV, ) -> Result { match self.db.get(&client_address.to_bytes()) { - Err(e) => Err(ClientLedgerError::DbReadError(e)), + Err(e) => Err(ClientLedgerError::Read(e)), Ok(existing_key) => match existing_key { Some(existing_key_ivec) => { let shared_key = &self.read_shared_key(existing_key_ivec); @@ -109,7 +109,7 @@ impl ClientLedger { client_address: &DestinationAddressBytes, ) -> Result, ClientLedgerError> { match self.db.get(&client_address.to_bytes()) { - Err(e) => Err(ClientLedgerError::DbReadError(e)), + Err(e) => Err(ClientLedgerError::Read(e)), Ok(existing_key) => Ok(existing_key.map(|key_ivec| self.read_shared_key(key_ivec))), } } @@ -123,7 +123,7 @@ impl ClientLedger { .db .insert(&client_address.to_bytes(), shared_key.to_bytes()) { - Err(e) => Err(ClientLedgerError::DbWriteError(e)), + Err(e) => Err(ClientLedgerError::Write(e)), Ok(existing_key) => { Ok(existing_key.map(|existing_key| self.read_shared_key(existing_key))) } @@ -139,7 +139,7 @@ impl ClientLedger { client_address: &DestinationAddressBytes, ) -> Result, ClientLedgerError> { let removal_result = match self.db.remove(&client_address.to_bytes()) { - Err(e) => Err(ClientLedgerError::DbWriteError(e)), + Err(e) => Err(ClientLedgerError::Write(e)), Ok(existing_key) => { Ok(existing_key.map(|existing_key| self.read_shared_key(existing_key))) } diff --git a/mixnode/Cargo.toml b/mixnode/Cargo.toml index 7c9120dabe..3836087e7e 100644 --- a/mixnode/Cargo.toml +++ b/mixnode/Cargo.toml @@ -16,6 +16,7 @@ futures = "0.3.1" humantime-serde = "1.0.1" log = "0.4" pretty_env_logger = "0.3" +rand = "0.7" serde = { version = "1.0.104", features = ["derive"] } tokio = { version = "0.2", features = ["full"] } tokio-util = { version = "0.3.1", features = ["codec"] } diff --git a/mixnode/src/commands/init.rs b/mixnode/src/commands/init.rs index e178ee8f91..48827133fe 100644 --- a/mixnode/src/commands/init.rs +++ b/mixnode/src/commands/init.rs @@ -154,8 +154,10 @@ pub fn execute(matches: &ArgMatches) { // if node was already initialised, don't generate new keys if !already_init { - let identity_keys = identity::KeyPair::new(); - let sphinx_keys = encryption::KeyPair::new(); + let mut rng = rand::rngs::OsRng; + + let identity_keys = identity::KeyPair::new(&mut rng); + let sphinx_keys = encryption::KeyPair::new(&mut rng); let pathfinder = MixNodePathfinder::new_from_config(&config); pemstore::store_keypair( &identity_keys, diff --git a/mixnode/src/commands/upgrade.rs b/mixnode/src/commands/upgrade.rs index 0b5e9ac68d..579c5ea825 100644 --- a/mixnode/src/commands/upgrade.rs +++ b/mixnode/src/commands/upgrade.rs @@ -124,7 +124,9 @@ fn pre_090_upgrade(from: &str, config: Config, matches: &ArgMatches) -> Config { ); println!("Generating new identity..."); - let identity_keys = identity::KeyPair::new(); + let mut rng = rand::rngs::OsRng; + + let identity_keys = identity::KeyPair::new(&mut rng); upgraded_config.set_default_identity_keypair_paths(); if let Err(err) = pemstore::store_keypair( diff --git a/mixnode/src/node/mod.rs b/mixnode/src/node/mod.rs index f8bf70548b..b53b859bfc 100644 --- a/mixnode/src/node/mod.rs +++ b/mixnode/src/node/mod.rs @@ -154,14 +154,9 @@ impl MixNode { } if let Err(err) = presence::register_with_validator( - self.config.get_validator_rest_endpoint(), - self.config.get_announce_address(), +&self.config, self.identity_keypair.public_key().to_base58_string(), self.sphinx_keypair.public_key().to_base58_string(), - self.config.get_version().to_string(), - self.config.get_location(), - self.config.get_layer(), - self.config.get_incentives_address(), ).await { error!("failed to register with the validator - {:?}", err); return; diff --git a/mixnode/src/node/presence.rs b/mixnode/src/node/presence.rs index a7d0f62eb8..7d9a33b795 100644 --- a/mixnode/src/node/presence.rs +++ b/mixnode/src/node/presence.rs @@ -12,32 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. +use crate::config::Config; use validator_client::models::mixnode::MixRegistrationInfo; use validator_client::ValidatorClientError; // there's no point in keeping the validator client persistently as it might be literally hours or days // before it's used again pub(crate) async fn register_with_validator( - validator_endpoint: String, - mix_host: String, + mixnode_config: &Config, identity_key: String, sphinx_key: String, - version: String, - location: String, - layer: u64, - incentives_address: Option, ) -> Result<(), ValidatorClientError> { - let config = validator_client::Config::new(validator_endpoint); + let config = validator_client::Config::new(mixnode_config.get_validator_rest_endpoint()); let validator_client = validator_client::Client::new(config); let registration_info = MixRegistrationInfo::new( - mix_host, + mixnode_config.get_announce_address(), identity_key, sphinx_key, - version, - location, - layer, - incentives_address, + mixnode_config.get_version().to_string(), + mixnode_config.get_location(), + mixnode_config.get_layer(), + mixnode_config.get_incentives_address(), ); validator_client.register_mix(registration_info).await diff --git a/network-monitor/src/chunker.rs b/network-monitor/src/chunker.rs index 3a22a893b7..bff5c15584 100644 --- a/network-monitor/src/chunker.rs +++ b/network-monitor/src/chunker.rs @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::{DefRng, DEFAULT_RNG}; use nymsphinx::forwarding::packet::MixPacket; use nymsphinx::params::PacketMode; use nymsphinx::{ acknowledgements::AckKey, addressing::clients::Recipient, preparer::MessagePreparer, }; +use rand::rngs::OsRng; use std::time::Duration; use topology::NymTopology; @@ -25,18 +25,18 @@ const DEFAULT_AVERAGE_PACKET_DELAY: Duration = Duration::from_millis(200); const DEFAULT_AVERAGE_ACK_DELAY: Duration = Duration::from_millis(200); pub(crate) struct Chunker { - rng: DefRng, + rng: OsRng, me: Recipient, - message_preparer: MessagePreparer, + message_preparer: MessagePreparer, } impl Chunker { pub(crate) fn new(me: Recipient) -> Self { Chunker { - rng: DEFAULT_RNG, + rng: OsRng, me, message_preparer: MessagePreparer::new( - DEFAULT_RNG, + OsRng, me, DEFAULT_AVERAGE_PACKET_DELAY, DEFAULT_AVERAGE_ACK_DELAY, diff --git a/network-monitor/src/main.rs b/network-monitor/src/main.rs index 3e9342651b..9fdc6d31bd 100644 --- a/network-monitor/src/main.rs +++ b/network-monitor/src/main.rs @@ -25,7 +25,6 @@ use monitor::{AckSender, MixnetSender, Monitor}; use notifications::Notifier; use nymsphinx::addressing::clients::Recipient; use packet_sender::PacketSender; -use rand::rngs::OsRng; use std::sync::Arc; use std::time; use std::time::Duration; @@ -39,9 +38,6 @@ mod run_info; mod test_packet; mod tested_network; -pub(crate) type DefRng = OsRng; -pub(crate) const DEFAULT_RNG: DefRng = OsRng; - const V4_TOPOLOGY_ARG: &str = "v4-topology-filepath"; const V6_TOPOLOGY_ARG: &str = "v6-topology-filepath"; const VALIDATOR_ARG: &str = "validator"; @@ -103,9 +99,7 @@ async fn main() { let v4_topology = parse_topology_file(v4_topology_path); let v6_topology = parse_topology_file(v6_topology_path); - let validator_rest_uri = matches - .value_of(VALIDATOR_ARG) - .unwrap_or_else(|| DEFAULT_VALIDATOR); + let validator_rest_uri = matches.value_of(VALIDATOR_ARG).unwrap_or(DEFAULT_VALIDATOR); let detailed_report = matches.is_present(DETAILED_REPORT_ARG); let sending_rate = matches .value_of(GATEWAY_SENDING_RATE_ARG) @@ -133,8 +127,10 @@ async fn main() { // Generate a new set of identity keys. These are ephemeral, and change on each run. // JS: do they? or rather should they? - let identity_keypair = identity::KeyPair::new(); - let encryption_keypair = encryption::KeyPair::new(); + let mut rng = rand::rngs::OsRng; + + let identity_keypair = identity::KeyPair::new(&mut rng); + let encryption_keypair = encryption::KeyPair::new(&mut rng); // We need our own address as a Recipient so we can send ourselves test packets let self_address = Recipient::new( diff --git a/network-monitor/src/notifications/mod.rs b/network-monitor/src/notifications/mod.rs index 91258aade3..aff151303a 100644 --- a/network-monitor/src/notifications/mod.rs +++ b/network-monitor/src/notifications/mod.rs @@ -136,7 +136,7 @@ impl Notifier { .message_receiver .insert_new_fragment(fragment) .map_err(|_| NotifierError::MalformedPacketReceived)? - .ok_or_else(|| NotifierError::NonTestPacketReceived)?; // if it's a test packet it MUST BE reconstructed with single fragment + .ok_or(NotifierError::NonTestPacketReceived)?; // if it's a test packet it MUST BE reconstructed with single fragment let all_received = self.current_test_run.received_packet(recovered.message); if all_received { diff --git a/network-monitor/src/packet_sender.rs b/network-monitor/src/packet_sender.rs index 0f2c749287..15739e8783 100644 --- a/network-monitor/src/packet_sender.rs +++ b/network-monitor/src/packet_sender.rs @@ -77,11 +77,7 @@ impl PacketSender { if version.major == 0 && version.minor >= 10 { return true; } - if version.minor >= 9 && version.patch >= 2 { - true - } else { - false - } + version.minor >= 9 && version.patch >= 2 } else { false } @@ -96,7 +92,7 @@ impl PacketSender { match mix { Err(err) => { error!("mix {} is malformed - {:?}", mix_id, err); - TestMix::MalformedMix(mix_id) + TestMix::Malformed(mix_id) } Ok(mix) => { if self.check_version_compatibility(&mix.version) { @@ -105,9 +101,9 @@ impl PacketSender { let v6_test_packet = TestPacket::new(mix.identity_key, IpVersion::V6, self.nonce); - TestMix::ValidMix(mix, [v4_test_packet, v6_test_packet]) + TestMix::Valid(mix, [v4_test_packet, v6_test_packet]) } else { - TestMix::IncompatibleMix(mix) + TestMix::Incompatible(mix) } } } @@ -133,12 +129,12 @@ impl PacketSender { for test_mix in test_mixes { match test_mix { - TestMix::ValidMix(.., mix_test_packets) => { + TestMix::Valid(.., mix_test_packets) => { test_packets.push(mix_test_packets[0]); test_packets.push(mix_test_packets[1]); } - TestMix::MalformedMix(pub_key) => malformed_mixes.push(pub_key.clone()), - TestMix::IncompatibleMix(mix) => incompatible_mixes + TestMix::Malformed(pub_key) => malformed_mixes.push(pub_key.clone()), + TestMix::Incompatible(mix) => incompatible_mixes .push((mix.identity_key.to_base58_string(), mix.version.clone())), } } @@ -177,7 +173,7 @@ impl PacketSender { for test_mix in test_mixes { match test_mix { - TestMix::ValidMix(mixnode, test_packets) => { + TestMix::Valid(mixnode, test_packets) => { let mut node_mix_packets = self.prepare_node_mix_packets(mixnode, test_packets).await; mix_packets.append(&mut node_mix_packets); diff --git a/network-monitor/src/test_packet.rs b/network-monitor/src/test_packet.rs index 977330dd78..079f8b534b 100644 --- a/network-monitor/src/test_packet.rs +++ b/network-monitor/src/test_packet.rs @@ -57,9 +57,9 @@ impl IpVersion { } } -impl Into for IpVersion { - fn into(self) -> String { - format!("{}", self) +impl From for String { + fn from(ipv: IpVersion) -> Self { + format!("{}", ipv) } } diff --git a/network-monitor/src/tested_network/mod.rs b/network-monitor/src/tested_network/mod.rs index 4e4bf0fc1f..d5b17c8fe9 100644 --- a/network-monitor/src/tested_network/mod.rs +++ b/network-monitor/src/tested_network/mod.rs @@ -24,14 +24,14 @@ use topology::{mix, NymTopology}; pub(crate) mod good_topology; pub(crate) enum TestMix { - ValidMix(mix::Node, [TestPacket; 2]), - IncompatibleMix(mix::Node), - MalformedMix(String), + Valid(mix::Node, [TestPacket; 2]), + Incompatible(mix::Node), + Malformed(String), } impl TestMix { pub(crate) fn is_valid(&self) -> bool { - matches!(self, TestMix::ValidMix(..)) + matches!(self, TestMix::Valid(..)) } } diff --git a/service-providers/network-requester/src/allowed_hosts.rs b/service-providers/network-requester/src/allowed_hosts.rs index 076074fb14..350746da1f 100644 --- a/service-providers/network-requester/src/allowed_hosts.rs +++ b/service-providers/network-requester/src/allowed_hosts.rs @@ -112,7 +112,7 @@ impl HostsStore { HostsStore { storefile, hosts } } - fn append(path: &PathBuf, text: &str) { + fn append(path: &Path, text: &str) { use std::io::Write; let mut file = OpenOptions::new() .write(true)