Skip to content

Commit

Permalink
sim-rs: Split CLI program from core
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Oct 24, 2024
1 parent 0bc7dd0 commit c86f55e
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 211 deletions.
21 changes: 16 additions & 5 deletions sim-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 7 additions & 21 deletions sim-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
[package]
name = "sim-rs"
version = "0.1.0"
edition = "2021"
[workspace]

[dependencies]
anyhow = "1"
async-stream = "0.3"
clap = { version = "4", features = ["derive"] }
ctrlc = "3"
futures = "0.3"
netsim-async = { git = "https://github.com/SupernaviX/ce-netsim.git", rev = "535ae49" }
priority-queue = "2"
rand = "0.8"
rand_chacha = "0.3"
rand_distr = "0.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
toml = "0.8"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
members = [
"sim-cli",
"sim-core",
]

resolver = "2"

[profile.release]
debug = true
16 changes: 16 additions & 0 deletions sim-rs/sim-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "sim-cli"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
clap = { version = "4", features = ["derive"] }
ctrlc = "3"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sim-core = { path = "../sim-core" }
tokio = { version = "1", features = ["full"] }
toml = "0.8"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
22 changes: 22 additions & 0 deletions sim-rs/sim-cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::{fs, path::Path};

use anyhow::Result;
use sim_core::config::{NodeId, RawConfig, SimConfiguration};

pub fn read_config(
filename: &Path,
timescale: Option<u32>,
trace_nodes: &[usize],
) -> Result<SimConfiguration> {
let file = fs::read_to_string(filename)?;
let mut raw_config: RawConfig = toml::from_str(&file)?;
if let Some(ts) = timescale {
raw_config.timescale = Some(ts);
}
for id in trace_nodes {
raw_config.trace_nodes.insert(NodeId::new(*id));
}
let config: SimConfiguration = raw_config.into();
config.validate()?;
Ok(config)
}
87 changes: 7 additions & 80 deletions sim-rs/src/events.rs → sim-rs/sim-cli/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,15 @@
use std::{collections::BTreeMap, path::PathBuf, sync::Arc};
use std::{collections::BTreeMap, path::PathBuf};

use anyhow::Result;
use serde::Serialize;
use tokio::{fs::File, io::AsyncWriteExt, sync::mpsc};
use tracing::{info, info_span, warn};

use crate::{
clock::{Clock, Timestamp},
use sim_core::{
clock::Timestamp,
config::{NodeId, SimConfiguration},
model::{Block, InputBlock, Transaction, TransactionId},
events::Event,
model::TransactionId,
};

pub enum Event {
Transaction {
id: TransactionId,
bytes: u64,
},
Slot {
number: u64,
block: Option<Block>,
},
BlockReceived {
slot: u64,
sender: NodeId,
recipient: NodeId,
},
InputBlockGenerated {
block: Arc<InputBlock>,
},
InputBlockReceived {
block: Arc<InputBlock>,
sender: NodeId,
recipient: NodeId,
},
}
use tokio::{fs::File, io::AsyncWriteExt as _, sync::mpsc};
use tracing::{info, info_span};

#[derive(Clone, Serialize)]
enum OutputEvent {
Expand Down Expand Up @@ -71,55 +47,6 @@ enum OutputEvent {
},
}

#[derive(Clone)]
pub struct EventTracker {
sender: mpsc::UnboundedSender<(Event, Timestamp)>,
clock: Clock,
}

impl EventTracker {
pub fn new(sender: mpsc::UnboundedSender<(Event, Timestamp)>, clock: Clock) -> Self {
Self { sender, clock }
}

pub fn track_slot(&self, number: u64, block: Option<Block>) {
self.send(Event::Slot { number, block });
}

pub fn track_block_received(&self, slot: u64, sender: NodeId, recipient: NodeId) {
self.send(Event::BlockReceived {
slot,
sender,
recipient,
});
}

pub fn track_transaction(&self, transaction: &Transaction) {
self.send(Event::Transaction {
id: transaction.id,
bytes: transaction.bytes,
});
}

pub fn track_ib_generated(&self, block: Arc<InputBlock>) {
self.send(Event::InputBlockGenerated { block });
}

pub fn track_ib_received(&self, block: Arc<InputBlock>, sender: NodeId, recipient: NodeId) {
self.send(Event::InputBlockReceived {
block,
sender,
recipient,
});
}

fn send(&self, event: Event) {
if self.sender.send((event, self.clock.now())).is_err() {
warn!("tried sending event after aggregator finished");
}
}
}

pub struct EventMonitor {
node_ids: Vec<NodeId>,
pool_ids: Vec<NodeId>,
Expand Down
10 changes: 2 additions & 8 deletions sim-rs/src/main.rs → sim-rs/sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@ use std::{path::PathBuf, process, time::Instant};

use anyhow::Result;
use clap::Parser;
use clock::Clock;
use config::read_config;
use events::{EventMonitor, EventTracker};
use sim::Simulation;
use events::EventMonitor;
use sim_core::{clock::Clock, events::EventTracker, sim::Simulation};
use tokio::{
pin, select,
sync::{mpsc, oneshot},
};
use tracing::{level_filters::LevelFilter, warn};
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt, EnvFilter};

mod clock;
mod config;
mod events;
mod model;
mod network;
mod probability;
mod sim;

#[derive(Parser)]
struct Args {
Expand Down
17 changes: 17 additions & 0 deletions sim-rs/sim-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "sim-core"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
async-stream = "0.3"
futures = "0.3"
netsim-async = { git = "https://github.com/SupernaviX/ce-netsim.git", rev = "535ae49" }
priority-queue = "2"
rand = "0.8"
rand_chacha = "0.3"
rand_distr = "0.4"
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["macros", "time"] }
tracing = "0.1"
File renamed without changes.
Loading

0 comments on commit c86f55e

Please sign in to comment.