From 8147b7d7184e7e87b7319f93ccadb1d35e7a6778 Mon Sep 17 00:00:00 2001 From: Youngjoon Lee <5462944+youngjoon-lee@users.noreply.github.com> Date: Fri, 8 Nov 2024 17:14:27 +0700 Subject: [PATCH] Use logging instead of storing payload IDs in state (#42) --- simlib/mixnet-sims/src/main.rs | 4 +-- simlib/mixnet-sims/src/node/mix/mod.rs | 45 ++++++++++++++++-------- simlib/mixnet-sims/src/node/mix/state.rs | 7 ++-- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/simlib/mixnet-sims/src/main.rs b/simlib/mixnet-sims/src/main.rs index 3353c1b..7d91157 100644 --- a/simlib/mixnet-sims/src/main.rs +++ b/simlib/mixnet-sims/src/main.rs @@ -59,7 +59,7 @@ impl SimulationApp { pub fn run(self) -> anyhow::Result<()> { let Self { input_settings, - stream_type, + stream_type: _, log_format: _, log_to: _, no_netcap, @@ -135,7 +135,7 @@ impl SimulationApp { let network = Arc::try_unwrap(network) .expect("network is not used anywhere else") .into_inner(); - run::<_, _, _>(network, nodes, settings.simulation_settings, stream_type)?; + run::<_, _, _>(network, nodes, settings.simulation_settings, None)?; Ok(()) } } diff --git a/simlib/mixnet-sims/src/node/mix/mod.rs b/simlib/mixnet-sims/src/node/mix/mod.rs index eaf74b8..081f745 100644 --- a/simlib/mixnet-sims/src/node/mix/mod.rs +++ b/simlib/mixnet-sims/src/node/mix/mod.rs @@ -10,7 +10,7 @@ use cached::{Cached, TimedCache}; use crossbeam::channel; use futures::Stream; use lottery::StakeLottery; -use message::Payload; +use message::{Payload, PayloadId}; use multiaddr::Multiaddr; use netrunner::network::NetworkMessage; use netrunner::node::{Node, NodeId}; @@ -33,10 +33,9 @@ use nomos_mix_message::mock::MockMixMessage; use rand::SeedableRng; use rand_chacha::ChaCha12Rng; use scheduler::{Interval, TemporalRelease}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use state::MixnodeState; -use std::collections::HashMap; use std::{pin::pin, task::Poll, time::Duration}; use stream_wrapper::CrossbeamReceiverStream; @@ -193,8 +192,7 @@ impl MixNode { state: MixnodeState { node_id: id, step_id: 0, - messages_generated: HashMap::new(), - messages_fully_unwrapped: HashMap::new(), + num_messages_fully_unwrapped: 0, }, data_msg_lottery_update_time_sender, data_msg_lottery_interval, @@ -259,6 +257,22 @@ impl MixNode { self.epoch_update_sender.send(elapsed).unwrap(); self.slot_update_sender.send(elapsed).unwrap(); } + + fn log_message_generated(&self, payload: &Payload) { + self.log_message("MessageGenerated", payload); + } + + fn log_message_fully_unwrapped(&self, payload: &Payload) { + self.log_message("MessageFullyUnwrapped", payload); + } + + fn log_message(&self, tag: &str, payload: &Payload) { + let log = MessageLog { + payload_id: payload.id(), + step_id: self.state.step_id, + }; + tracing::info!("{}: {}", tag, serde_json::to_string(&log).unwrap()); + } } impl Node for MixNode { @@ -283,9 +297,7 @@ impl Node for MixNode { if let Poll::Ready(Some(_)) = pin!(&mut self.data_msg_lottery_interval).poll_next(&mut cx) { if self.data_msg_lottery.run() { let payload = Payload::new(); - self.state - .messages_generated - .insert(payload.id(), self.state.step_id); + self.log_message_generated(&payload); let message = self .crypto_processor .wrap_message(payload.as_bytes()) @@ -313,9 +325,8 @@ impl Node for MixNode { } MixOutgoingMessage::FullyUnwrapped(payload) => { let payload = Payload::load(payload); - self.state - .messages_fully_unwrapped - .insert(payload.id(), self.state.step_id); + self.log_message_fully_unwrapped(&payload); + self.state.num_messages_fully_unwrapped += 1; //TODO: create a tracing event } } @@ -324,9 +335,7 @@ impl Node for MixNode { // Generate a cover message probabilistically if let Poll::Ready(Some(_)) = pin!(&mut self.cover_traffic).poll_next(&mut cx) { let payload = Payload::new(); - self.state - .messages_generated - .insert(payload.id(), self.state.step_id); + self.log_message_generated(&payload); let message = self .crypto_processor .wrap_message(payload.as_bytes()) @@ -348,9 +357,15 @@ impl Node for MixNode { match ward { WardCondition::Max(_) => false, WardCondition::Sum(condition) => { - *condition.step_result.borrow_mut() += self.state.messages_fully_unwrapped.len(); + *condition.step_result.borrow_mut() += self.state.num_messages_fully_unwrapped; false } } } } + +#[derive(Debug, Serialize, Deserialize)] +struct MessageLog { + payload_id: PayloadId, + step_id: usize, +} diff --git a/simlib/mixnet-sims/src/node/mix/state.rs b/simlib/mixnet-sims/src/node/mix/state.rs index 6ec9d27..ff0d9c1 100644 --- a/simlib/mixnet-sims/src/node/mix/state.rs +++ b/simlib/mixnet-sims/src/node/mix/state.rs @@ -1,4 +1,4 @@ -use std::{any::Any, collections::HashMap}; +use std::any::Any; use serde::Serialize; @@ -9,15 +9,12 @@ use netrunner::{ warding::SimulationState, }; -use super::message::PayloadId; - #[derive(Debug, Clone, Serialize)] pub struct MixnodeState { #[serde(serialize_with = "serialize_node_id_as_index")] pub node_id: NodeId, pub step_id: usize, - pub messages_generated: HashMap, - pub messages_fully_unwrapped: HashMap, + pub num_messages_fully_unwrapped: usize, } #[derive(Serialize)]