83 lines
1.8 KiB
Rust
Raw Normal View History

2024-11-06 06:14:29 +02:00
pub mod state;
2024-11-06 08:45:25 +01:00
mod step_scheduler;
use super::{Node, NodeId};
use crate::network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize};
2024-11-06 06:14:29 +02:00
use serde::Deserialize;
use state::MixnodeState;
use std::time::Duration;
#[derive(Debug, Clone)]
pub enum MixMessage {
Dummy(String),
}
impl PayloadSize for MixMessage {
fn size_bytes(&self) -> u32 {
2208
}
}
2024-11-06 06:14:29 +02:00
#[derive(Clone, Default, Deserialize)]
pub struct MixnodeSettings {
pub connected_peers: Vec<NodeId>,
}
/// This node implementation only used for testing different streaming implementation purposes.
pub struct MixNode {
id: NodeId,
2024-11-06 06:14:29 +02:00
state: MixnodeState,
settings: MixnodeSettings,
network_interface: InMemoryNetworkInterface<MixMessage>,
}
impl MixNode {
pub fn new(
id: NodeId,
settings: MixnodeSettings,
network_interface: InMemoryNetworkInterface<MixMessage>,
) -> Self {
Self {
id,
network_interface,
settings,
2024-11-06 06:14:29 +02:00
state: MixnodeState {
node_id: id,
mock_counter: 0,
step_id: 0,
},
}
}
}
impl Node for MixNode {
type Settings = MixnodeSettings;
2024-11-06 06:14:29 +02:00
type State = MixnodeState;
fn id(&self) -> NodeId {
self.id
}
fn state(&self) -> &Self::State {
&self.state
}
fn step(&mut self, _: Duration) {
let messages = self.network_interface.receive_messages();
for message in messages {
println!(">>>>> Node {}, message: {message:?}", self.id);
}
2024-11-06 06:14:29 +02:00
self.state.step_id += 1;
self.state.mock_counter += 1;
for node_id in self.settings.connected_peers.iter() {
self.network_interface.send_message(
*node_id,
MixMessage::Dummy(format!("Hello from node: {}", self.id)),
)
}
}
}