Skip to content

Commit

Permalink
sim-rs: add test for data generation
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Dec 19, 2024
1 parent afc4b17 commit 64e45bf
Show file tree
Hide file tree
Showing 8 changed files with 1,089 additions and 62 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ jobs:
################################################################################

sim-rs-check:
name: "sim-rs: Check"
name: "sim-rs: Test"
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- name: Check Rust packages compilation
- name: Test Rust packages
working-directory: sim-rs
run: |
cargo check
cargo test
if [ $? -ne 0 ]; then
echo "Cargo check failed"
echo "Cargo test failed"
exit 1
fi
Expand Down
40 changes: 3 additions & 37 deletions sim-rs/sim-cli/src/bin/gen-test-data/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{collections::HashSet, fs, path::PathBuf};
use std::{fs, path::PathBuf};

use anyhow::Result;
use clap::{Parser, Subcommand};
use sim_core::config::{DistributionConfig, RawConfig, SimConfiguration};
use sim_core::config::SimConfiguration;
use strategy::{globe, random_graph, simplified, GlobeArgs, RandomGraphArgs, SimplifiedArgs};

mod strategy;
Expand All @@ -24,46 +24,12 @@ enum Strategy {
fn main() -> Result<()> {
let args = Args::parse();

let (nodes, links) = match args.strategy {
let raw_config = match args.strategy {
Strategy::RandomGraph(args) => random_graph(&args)?,
Strategy::Simplified(args) => simplified(&args)?,
Strategy::Globe(args) => globe(&args)?,
};

let vote_probability = 500.0;
let vote_threshold = 150;

let raw_config = RawConfig {
seed: None,
timescale: None,
slots: None,
nodes,
trace_nodes: HashSet::new(),
links,
block_generation_probability: 0.05,
ib_generation_probability: 5.0,
eb_generation_probability: 5.0,
vote_probability,
vote_threshold,
ib_shards: 8,
max_block_size: 90112,
stage_length: 2,
deliver_stage_count: 2,
uniform_ib_generation: true,
max_ib_requests_per_peer: 1,
one_vote_per_vrf: true,
max_ib_size: 327680,
max_tx_size: 16384,
transaction_frequency_ms: DistributionConfig::Exp {
lambda: 0.85,
scale: Some(1000.0),
},
transaction_size_bytes: DistributionConfig::LogNormal {
mu: 6.833,
sigma: 1.127,
},
};

let serialized = toml::to_string_pretty(&raw_config)?;

let full_config: SimConfiguration = raw_config.into();
Expand Down
41 changes: 31 additions & 10 deletions sim-rs/sim-cli/src/bin/gen-test-data/strategy/globe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use anyhow::{bail, Result};
use clap::Parser;
use rand::{seq::SliceRandom as _, thread_rng, Rng as _};
use serde::Deserialize;
use sim_core::config::{RawLinkConfig, RawNodeConfig};
use sim_core::config::{RawConfig, RawNodeConfig};

use crate::strategy::utils::{distance, distribute_stake, LinkTracker};
use crate::strategy::utils::{distance, distribute_stake, generate_full_config, LinkTracker};

#[derive(Debug, Parser)]
pub struct GlobeArgs {
Expand All @@ -31,7 +31,7 @@ struct Country {

#[derive(Debug, Deserialize)]
struct RegionData {
asn: u64,
id: u64,
latitude: f64,
longitude: f64,
proportion: u64,
Expand All @@ -48,9 +48,9 @@ fn distribute_regions(node_count: usize, distribution: Distribution) -> Vec<Regi
for country in distribution.countries {
for region in country.regions {
for _ in 0..region.proportion {
let asn = region.asn;
let id = region.id;
let location = (region.latitude, (region.longitude + 180.0) / 2.0);
region_pool.push((country.name.clone(), asn, location));
region_pool.push((country.name.clone(), id, location));
}
}
}
Expand All @@ -59,14 +59,14 @@ fn distribute_regions(node_count: usize, distribution: Distribution) -> Vec<Regi
let mut results = vec![];
let mut rng = thread_rng();
for _ in 0..node_count {
let (country, asn, location) = region_pool
let (country, id, location) = region_pool
.get(rng.gen_range(0..region_pool.len()))
.unwrap();
let regions = country_regions.entry(country.clone()).or_default();
let number = match regions.iter().position(|r| r == asn) {
let number = match regions.iter().position(|r| r == id) {
Some(index) => index + 1,
None => {
regions.push(*asn);
regions.push(*id);
regions.len()
}
};
Expand All @@ -79,7 +79,7 @@ fn distribute_regions(node_count: usize, distribution: Distribution) -> Vec<Regi
results
}

pub fn globe(args: &GlobeArgs) -> Result<(Vec<RawNodeConfig>, Vec<RawLinkConfig>)> {
pub fn globe(args: &GlobeArgs) -> Result<RawConfig> {
if args.stake_pool_count >= args.node_count {
bail!("At least one node must not be a stake pool");
}
Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn globe(args: &GlobeArgs) -> Result<(Vec<RawNodeConfig>, Vec<RawLinkConfig>
}
}

Ok((nodes, links.links))
Ok(generate_full_config(nodes, links.links))
}

fn track_connections(
Expand All @@ -181,3 +181,24 @@ fn track_connections(
}
}
}

#[cfg(test)]
mod tests {
use sim_core::config::SimConfiguration;

use super::{globe, GlobeArgs};

#[test]
fn should_generate_valid_graph() {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/test_data/distribution.toml");
let args = GlobeArgs {
node_count: 1000,
stake_pool_count: 50,
distribution: path.into(),
};

let raw_config = globe(&args).unwrap();
let config: SimConfiguration = raw_config.into();
config.validate().unwrap();
}
}
27 changes: 23 additions & 4 deletions sim-rs/sim-cli/src/bin/gen-test-data/strategy/random_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::collections::{BTreeMap, BTreeSet};
use anyhow::{bail, Result};
use clap::Parser;
use rand::{seq::SliceRandom as _, thread_rng, Rng as _};
use sim_core::config::{RawLinkConfig, RawNodeConfig};
use sim_core::config::{RawConfig, RawNodeConfig};

use crate::strategy::utils::{distribute_stake, LinkTracker};
use crate::strategy::utils::{distribute_stake, generate_full_config, LinkTracker};

#[derive(Debug, Parser)]
pub struct RandomGraphArgs {
Expand All @@ -20,7 +20,7 @@ fn distance((lat1, long1): (f64, f64), (lat2, long2): (f64, f64)) -> f64 {
(dist_x.powi(2) + dist_y.powi(2)).sqrt()
}

pub fn random_graph(args: &RandomGraphArgs) -> Result<(Vec<RawNodeConfig>, Vec<RawLinkConfig>)> {
pub fn random_graph(args: &RandomGraphArgs) -> Result<RawConfig> {
if args.stake_pool_count >= args.node_count {
bail!("At least one node must not be a stake pool");
}
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn random_graph(args: &RandomGraphArgs) -> Result<(Vec<RawNodeConfig>, Vec<R
}
}

Ok((nodes, links.links))
Ok(generate_full_config(nodes, links.links))
}

fn track_connections(
Expand All @@ -115,3 +115,22 @@ fn track_connections(
}
}
}

#[cfg(test)]
mod tests {
use sim_core::config::SimConfiguration;

use super::{random_graph, RandomGraphArgs};

#[test]
fn should_generate_valid_graph() {
let args = RandomGraphArgs {
node_count: 1000,
stake_pool_count: 50,
};

let raw_config = random_graph(&args).unwrap();
let config: SimConfiguration = raw_config.into();
config.validate().unwrap();
}
}
24 changes: 20 additions & 4 deletions sim-rs/sim-cli/src/bin/gen-test-data/strategy/simplified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::time::Duration;
use anyhow::Result;
use clap::Parser;
use rand::{rngs::ThreadRng, thread_rng, Rng};
use sim_core::config::{RawLinkConfig, RawNodeConfig};
use sim_core::config::{RawConfig, RawNodeConfig};

use super::utils::{distribute_stake, LinkTracker};
use super::utils::{distribute_stake, generate_full_config, LinkTracker};

#[derive(Debug, Parser)]
pub struct SimplifiedArgs {
Expand Down Expand Up @@ -40,7 +40,7 @@ const SHORT_HOP: Duration = Duration::from_millis(12);
const MEDIUM_HOP: Duration = Duration::from_millis(69);
const LONG_HOP: Duration = Duration::from_millis(268);

pub fn simplified(args: &SimplifiedArgs) -> Result<(Vec<RawNodeConfig>, Vec<RawLinkConfig>)> {
pub fn simplified(args: &SimplifiedArgs) -> Result<RawConfig> {
let mut rng = thread_rng();

let mut nodes = vec![];
Expand Down Expand Up @@ -118,5 +118,21 @@ pub fn simplified(args: &SimplifiedArgs) -> Result<(Vec<RawNodeConfig>, Vec<RawL
}
}

Ok((nodes, links.links))
Ok(generate_full_config(nodes, links.links))
}

#[cfg(test)]
mod tests {
use sim_core::config::SimConfiguration;

use super::{simplified, SimplifiedArgs};

#[test]
fn should_generate_valid_graph() {
let args = SimplifiedArgs { pool_count: 1000 };

let raw_config = simplified(&args).unwrap();
let config: SimConfiguration = raw_config.into();
config.validate().unwrap();
}
}
40 changes: 38 additions & 2 deletions sim-rs/sim-cli/src/bin/gen-test-data/strategy/utils.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{
collections::{BTreeMap, BTreeSet},
collections::{BTreeMap, BTreeSet, HashSet},
time::Duration,
};

use anyhow::Result;
use sim_core::config::RawLinkConfig;
use sim_core::config::{DistributionConfig, RawConfig, RawLinkConfig, RawNodeConfig};
use statrs::distribution::{Beta, ContinuousCDF as _};

#[derive(Default)]
Expand Down Expand Up @@ -51,3 +51,39 @@ pub fn distance((lat1, long1): (f64, f64), (lat2, long2): (f64, f64)) -> f64 {
let dist_y = (long2 - long1).rem_euclid(180.0);
(dist_x.powi(2) + dist_y.powi(2)).sqrt()
}

pub fn generate_full_config(nodes: Vec<RawNodeConfig>, links: Vec<RawLinkConfig>) -> RawConfig {
let vote_probability = 500.0;
let vote_threshold = 150;

RawConfig {
seed: None,
timescale: None,
slots: None,
nodes,
trace_nodes: HashSet::new(),
links,
block_generation_probability: 0.05,
ib_generation_probability: 5.0,
eb_generation_probability: 5.0,
vote_probability,
vote_threshold,
ib_shards: 8,
max_block_size: 90112,
stage_length: 2,
deliver_stage_count: 2,
uniform_ib_generation: true,
max_ib_requests_per_peer: 1,
one_vote_per_vrf: true,
max_ib_size: 327680,
max_tx_size: 16384,
transaction_frequency_ms: DistributionConfig::Exp {
lambda: 0.85,
scale: Some(1000.0),
},
transaction_size_bytes: DistributionConfig::LogNormal {
mu: 6.833,
sigma: 1.127,
},
}
}
Loading

0 comments on commit 64e45bf

Please sign in to comment.