Use network interface and add readme

This commit is contained in:
Gusto 2024-11-06 04:20:39 +02:00
parent 3969267003
commit 6e18bc80d2
No known key found for this signature in database
3 changed files with 26 additions and 29 deletions

8
network-runner/README.md Normal file
View File

@ -0,0 +1,8 @@
# Network Simulator
## Running simulations
To run the simulation use this command line:
```bash
cargo run -- --input-settings config/mixnode.json
```

View File

@ -27,18 +27,5 @@
},
"node_count": 3,
"seed": 0,
"record_settings": {
"node_id": true,
"current_view": true,
"highest_voted_view": true,
"local_high_qc": true,
"safe_blocks": true,
"last_view_timeout_qc": true,
"latest_committed_block": true,
"latest_committed_view": true,
"root_committee": true,
"parent_committee": true,
"child_committees": true,
"committed_blocks": true
}
"record_settings": {}
}

View File

@ -1,10 +1,8 @@
use super::{Node, NodeId};
use crate::network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use crate::network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize};
use super::{Node, NodeId};
#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
pub struct MixNodeState {
pub mock_counter: usize,
@ -17,7 +15,7 @@ pub enum MixMessage {
impl PayloadSize for MixMessage {
fn size_bytes(&self) -> u32 {
todo!()
2208
}
}
@ -29,7 +27,7 @@ pub struct MixnodeSettings {
pub struct MixNode {
id: NodeId,
state: MixNodeState,
_settings: MixnodeSettings,
settings: MixnodeSettings,
network_interface: InMemoryNetworkInterface<MixMessage>,
}
@ -42,7 +40,7 @@ impl MixNode {
Self {
id,
network_interface,
_settings: settings,
settings,
state: MixNodeState::default(),
}
}
@ -62,14 +60,18 @@ impl Node for MixNode {
}
fn step(&mut self, _: Duration) {
let _messages = self.network_interface.receive_messages();
self.state.mock_counter += 1;
println!(">>>>> Node {}, Step: {}", self.id, self.state.mock_counter);
let messages = self.network_interface.receive_messages();
for message in messages {
println!(">>>>> Node {}, message: {message:?}", self.id);
}
// Do stuff on the messages;
// Network interface can be passed into the functions for outputting the messages:
// ```rust
// self.network_interface.send_message(receiving_node_id, payload);
// ```
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)),
)
}
}
}