Use network interface and add readme
This commit is contained in:
parent
3969267003
commit
6e18bc80d2
|
@ -0,0 +1,8 @@
|
||||||
|
# Network Simulator
|
||||||
|
|
||||||
|
## Running simulations
|
||||||
|
|
||||||
|
To run the simulation use this command line:
|
||||||
|
```bash
|
||||||
|
cargo run -- --input-settings config/mixnode.json
|
||||||
|
```
|
|
@ -27,18 +27,5 @@
|
||||||
},
|
},
|
||||||
"node_count": 3,
|
"node_count": 3,
|
||||||
"seed": 0,
|
"seed": 0,
|
||||||
"record_settings": {
|
"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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
|
use super::{Node, NodeId};
|
||||||
|
use crate::network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::network::{InMemoryNetworkInterface, NetworkInterface, PayloadSize};
|
|
||||||
|
|
||||||
use super::{Node, NodeId};
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
|
||||||
pub struct MixNodeState {
|
pub struct MixNodeState {
|
||||||
pub mock_counter: usize,
|
pub mock_counter: usize,
|
||||||
|
@ -17,7 +15,7 @@ pub enum MixMessage {
|
||||||
|
|
||||||
impl PayloadSize for MixMessage {
|
impl PayloadSize for MixMessage {
|
||||||
fn size_bytes(&self) -> u32 {
|
fn size_bytes(&self) -> u32 {
|
||||||
todo!()
|
2208
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +27,7 @@ pub struct MixnodeSettings {
|
||||||
pub struct MixNode {
|
pub struct MixNode {
|
||||||
id: NodeId,
|
id: NodeId,
|
||||||
state: MixNodeState,
|
state: MixNodeState,
|
||||||
_settings: MixnodeSettings,
|
settings: MixnodeSettings,
|
||||||
network_interface: InMemoryNetworkInterface<MixMessage>,
|
network_interface: InMemoryNetworkInterface<MixMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +40,7 @@ impl MixNode {
|
||||||
Self {
|
Self {
|
||||||
id,
|
id,
|
||||||
network_interface,
|
network_interface,
|
||||||
_settings: settings,
|
settings,
|
||||||
state: MixNodeState::default(),
|
state: MixNodeState::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,14 +60,18 @@ impl Node for MixNode {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn step(&mut self, _: Duration) {
|
fn step(&mut self, _: Duration) {
|
||||||
let _messages = self.network_interface.receive_messages();
|
let messages = self.network_interface.receive_messages();
|
||||||
self.state.mock_counter += 1;
|
for message in messages {
|
||||||
println!(">>>>> Node {}, Step: {}", self.id, self.state.mock_counter);
|
println!(">>>>> Node {}, message: {message:?}", self.id);
|
||||||
|
}
|
||||||
|
|
||||||
// Do stuff on the messages;
|
self.state.mock_counter += 1;
|
||||||
// Network interface can be passed into the functions for outputting the messages:
|
|
||||||
// ```rust
|
for node_id in self.settings.connected_peers.iter() {
|
||||||
// self.network_interface.send_message(receiving_node_id, payload);
|
self.network_interface.send_message(
|
||||||
// ```
|
*node_id,
|
||||||
|
MixMessage::Dummy(format!("Hello from node: {}", self.id)),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue