From 95bec7422ca2f27b7db10de46007bfc4911cad7d Mon Sep 17 00:00:00 2001 From: Simon Wicky Date: Fri, 15 May 2026 12:47:07 +0200 Subject: [PATCH] tweaks and checked arithmetic --- common/nym-lp-data/src/clients/traits.rs | 15 ++++++++++++--- common/nym-lp-data/src/common/traits.rs | 12 ++++++++++-- common/nym-lp-data/src/lib.rs | 2 ++ nym-mix-sim/README.md | 6 +++--- nym-mix-sim/src/topology/directory.rs | 2 +- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/common/nym-lp-data/src/clients/traits.rs b/common/nym-lp-data/src/clients/traits.rs index 3dea3aad5e..d7cd22499d 100644 --- a/common/nym-lp-data/src/clients/traits.rs +++ b/common/nym-lp-data/src/clients/traits.rs @@ -145,12 +145,21 @@ where let mut chunk_size = self.frame_size(); if input_options.routing_security() { - chunk_size = - chunk_size * self.nb_frames() - >::OVERHEAD_SIZE; + // SAFETY : While this CAN technically fail, it means that something is wrong in the code and it's pointless to continue anyway + #[allow(clippy::expect_used)] + let pre_security_chunk_size = (chunk_size * self.nb_frames()) + .checked_sub(>::OVERHEAD_SIZE) + .expect("not enough room in a packet for routing security overhead"); + chunk_size = pre_security_chunk_size; } if input_options.reliability() { - chunk_size -= >::OVERHEAD_SIZE; + // SAFETY : While this CAN technically fail, it means that something is wrong in the code and it's pointless to continue anyway + #[allow(clippy::expect_used)] + let pre_reliability_chunk_size = chunk_size + .checked_sub(>::OVERHEAD_SIZE) + .expect("not enough room in a packet for reliability overhead"); + chunk_size = pre_reliability_chunk_size; } chunk_size diff --git a/common/nym-lp-data/src/common/traits.rs b/common/nym-lp-data/src/common/traits.rs index bbe9e6db90..66b9102d3a 100644 --- a/common/nym-lp-data/src/common/traits.rs +++ b/common/nym-lp-data/src/common/traits.rs @@ -121,12 +121,20 @@ where Ts: Clone, NdId: Clone, { + // IMPORTANT NOTE : This fn can be not constant to allow e.g. flexible MTU + // However, every possible value must be able to accomodate the different overhead. + // If it doesn't, the pipeline becomes unusable fn packet_size(&self) -> usize; fn frame_size(&self) -> usize { + // SAFETY : While this CAN technically fail, it means that something is wrong in the code and it's pointless to continue anyway + #[allow(clippy::expect_used)] self.packet_size() - - >::OVERHEAD_SIZE - - >::OVERHEAD_SIZE + .checked_sub( + >::OVERHEAD_SIZE + + >::OVERHEAD_SIZE, + ) + .expect("packet_size smaller than transport + framing overhead") } fn wire_wrap( diff --git a/common/nym-lp-data/src/lib.rs b/common/nym-lp-data/src/lib.rs index 3fc0791ded..3ea6a1918a 100644 --- a/common/nym-lp-data/src/lib.rs +++ b/common/nym-lp-data/src/lib.rs @@ -38,6 +38,7 @@ pub type PipelinePayload = PipelineData, Opts, NdId> /// pipeline. It is produced by [`clients::traits::Chunking`] and propagated /// unchanged (or with the timestamp transformed) through every subsequent /// pipeline stage until the packet is sent on the wire. +#[derive(Clone)] pub struct TimedData { pub timestamp: Ts, pub data: D, @@ -108,6 +109,7 @@ impl TimedData { /// [`Framing`]: crate::common::traits::Framing /// [`Transport`]: crate::common::traits::Transport /// [`InputOptions`]: crate::clients::InputOptions +#[derive(Clone)] pub struct PipelineData { pub data: TimedData, pub options: Opts, diff --git a/nym-mix-sim/README.md b/nym-mix-sim/README.md index c35b38ee6e..cd55f4fb96 100644 --- a/nym-mix-sim/README.md +++ b/nym-mix-sim/README.md @@ -33,7 +33,7 @@ cargo run --bin mix-client -- --src 6 --dst 7 Generates a `topology.json` file describing nodes and clients. -``` +```bash cargo run --bin nym-mix-sim -- init-topology [OPTIONS] ``` @@ -49,7 +49,7 @@ Nodes are assigned sequential ports starting at `127.0.0.1:9000`. Clients get tw Starts the simulation loop. -``` +```bash cargo run --bin nym-mix-sim -- run [OPTIONS] ``` @@ -65,7 +65,7 @@ cargo run --bin nym-mix-sim -- run [OPTIONS] Injects messages into a running simulation from stdin. -``` +```bash cargo run --bin mix-client -- --src --dst [--topology ] ``` diff --git a/nym-mix-sim/src/topology/directory.rs b/nym-mix-sim/src/topology/directory.rs index 9eb3b4eefb..69dccd7839 100644 --- a/nym-mix-sim/src/topology/directory.rs +++ b/nym-mix-sim/src/topology/directory.rs @@ -48,7 +48,7 @@ impl Directory { /// Look up a client by its [`ClientId`]. /// /// Returns `None` when `id` is not present in the directory - pub fn client(&self, id: NodeId) -> Option<&SocketAddr> { + pub fn client(&self, id: ClientId) -> Option<&SocketAddr> { self.clients.get(&id) }