From 46d3cb927d195c1523da8d3dbef4c23141197150 Mon Sep 17 00:00:00 2001 From: Waylon Jepsen Date: Sun, 5 Nov 2023 18:49:23 +0100 Subject: [PATCH] remove multiparallel --- src/agents/block_admin.rs | 3 +- src/config/dispute_game.toml | 4 ++- src/main.rs | 2 +- src/settings/mod.rs | 54 ++------------------------------ src/simulations/dispute_game.rs | 10 ++++-- src/simulations/mod.rs | 55 +++++++++++++-------------------- 6 files changed, 36 insertions(+), 92 deletions(-) diff --git a/src/agents/block_admin.rs b/src/agents/block_admin.rs index 2525a8e..6d6f932 100644 --- a/src/agents/block_admin.rs +++ b/src/agents/block_admin.rs @@ -1,4 +1,3 @@ -use crate::settings::{SimulationConfig, parameters::Fixed}; use super::*; @@ -29,7 +28,7 @@ impl BlockAdmin { /// /// # Returns /// * [`Result`] - A result containing the new BlockAdmin or an error. - pub async fn new(environment: &Environment, config: &SimulationConfig) -> Result { + pub async fn new(environment: &Environment, config: &SimulationConfig) -> Result { let client = RevmMiddleware::new(environment, "block_admin".into())?; let timestep_size = config.block.timestep_size; let block_number = client.get_block_number().await?.as_u64(); diff --git a/src/config/dispute_game.toml b/src/config/dispute_game.toml index b8189cb..1b852c9 100644 --- a/src/config/dispute_game.toml +++ b/src/config/dispute_game.toml @@ -1,3 +1,5 @@ -simulation = "dispute_game" +simulation = "DisputeGame" output_directory = "analysis/dispute_game" +[block] +timestep_size = 15 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3d47344..0d7ca08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ struct Args { enum Commands { /// Represents the `Bind` subcommand. Simulate { - #[clap(index = 1, default_value = "src/config/gbm.toml")] + #[clap(index = 1, default_value = "src/config/dispute_game.toml")] config_path: String, }, } diff --git a/src/settings/mod.rs b/src/settings/mod.rs index ad91c06..de6436c 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; /// This struct holds all the necessary parameters and configurations needed to run a simulation. /// It encompasses several sub-configurations such as `TrajectoryParameters` and `GBMParameters`. #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct SimulationConfig> { +pub struct SimulationConfig { /// The type of simulation to run, defined by an enum `SimulationType`. pub simulation: SimulationType, @@ -24,17 +24,11 @@ pub struct SimulationConfig> { /// Name of the file where the simulation results will be written. pub output_file_name: Option, - /// Parameters specific to the trajectory of the simulation. - pub trajectory: TrajectoryParameters

, - - /// Parameters specific to the Geometric Brownian Motion (GBM) if applicable. - pub gbm: Option>, - /// Parameters related to block configurations. pub block: BlockParameters, } -impl SimulationConfig { +impl SimulationConfig { /// Creates a new `SimulationConfig` instance from a configuration file. /// /// Reads the specified configuration file and deserializes it into a `SimulationConfig` object. @@ -45,48 +39,4 @@ impl SimulationConfig { .build()?; s.try_deserialize() } -} - -impl Parameterized> for SimulationConfig { - /// Generates a list of `SimulationConfig` instances with fixed parameters. - /// - /// This method is responsible for taking the meta parameters defined in the configuration, - /// generating the actual fixed parameters, and creating a list of complete `SimulationConfig` instances. - fn generate(&self) -> Vec> { - let mut result = vec![]; - let trajectories = self.trajectory.generate(); - - let gbms = self - .gbm - .as_ref() - .map(|gbm| gbm.generate()) - .unwrap_or_default(); - - if gbms.is_empty() { - panic!("You must supply either a gbm configuration."); - } - - for trajectory in &trajectories { - for gbm in &gbms { - let output_directory = self.output_directory.clone() - + "/gbm_drift=" - + &gbm.drift.0.to_string() - + "_vol=" - + &gbm.volatility.0.to_string(); - let output_file_name = - format!("trajectory={}", trajectory.output_tag.clone().unwrap()); - result.push(SimulationConfig { - simulation: self.simulation, - max_parallel: None, - output_directory, - output_file_name: Some(output_file_name), - trajectory: trajectory.clone(), - gbm: Some(*gbm), - block: self.block, - }); - } - } - - result - } } \ No newline at end of file diff --git a/src/simulations/dispute_game.rs b/src/simulations/dispute_game.rs index 888728b..a6a42b0 100644 --- a/src/simulations/dispute_game.rs +++ b/src/simulations/dispute_game.rs @@ -1,5 +1,5 @@ use super::*; -use alloy_primitives::Bytes; +use anyhow::Ok; use arbiter_core::environment::{builder::EnvironmentBuilder, cheatcodes::Cheatcodes, Environment}; use ethers::types::U256 as eU256; @@ -27,10 +27,14 @@ pub struct SimulationContracts { pub factory: DisputeGameFactory, } -pub async fn setup(config: SimulationConfig) -> Result { +pub async fn setup(config: SimulationConfig) -> Result { let (environment, admin, alice, bob, multisig) = set_up_agents().await?; - todo!() + + let contracts = deploy_contracts(admin.clone()).await?; + + todo!("setup the agents and return them") + } pub async fn set_up_agents() -> Result<( Environment, diff --git a/src/simulations/mod.rs b/src/simulations/mod.rs index 85d7d72..a8f072f 100644 --- a/src/simulations/mod.rs +++ b/src/simulations/mod.rs @@ -42,7 +42,7 @@ impl SimulationType { /// /// This function matches on the `SimulationType` to determine which simulation setup to use, /// then executes the chosen simulation. - async fn run(config: SimulationConfig) -> Result<()> { + async fn run(config: SimulationConfig) -> Result<()> { let simulation = match config.simulation { SimulationType::DisputeGame => { dispute_game::setup(config.clone()).await? @@ -69,8 +69,6 @@ pub fn batch(config_path: &str) -> Result<()> { // let config = SimulationConfig::new(config_path)?; - let direct_configs: Vec> = config.generate(); - // Create a multi-threaded runtime let rt = Builder::new_multi_thread().build()?; @@ -80,39 +78,30 @@ pub fn batch(config_path: &str) -> Result<()> { .map(|max_parallel| Arc::new(Semaphore::new(max_parallel))); rt.block_on(async { - let mut handles = vec![]; let errors = Arc::new(tokio::sync::Mutex::new(vec![])); + let errors_clone = errors.clone(); + let semaphore_clone = semaphore.clone(); + // Acquire a permit inside the spawned task + let permit = if let Some(ref semaphore_clone) = semaphore_clone { + // Acquire a permit outside the spawned task + let permit = semaphore_clone.acquire().await.unwrap(); + Some(permit) + } else { + None + }; - for config in direct_configs { - let errors_clone = errors.clone(); - let semaphore_clone = semaphore.clone(); - handles.push(tokio::spawn(async move { - // Acquire a permit inside the spawned task - let permit = if let Some(ref semaphore_clone) = semaphore_clone { - // Acquire a permit outside the spawned task - let permit = semaphore_clone.acquire().await.unwrap(); - Some(permit) - } else { - None - }; - - let result = SimulationType::run(config).await; - match result { - Err(e) => { - let mut errors_clone_lock = errors_clone.lock().await; - errors_clone_lock.push(e); - // Drop the permit when the simulation is done. - drop(permit); - } - Result::Ok(_) => { - drop(permit); - } - } - })); - } + let result = SimulationType::run(config).await; + match result { + Err(e) => { + let mut errors_clone_lock = errors_clone.lock().await; + errors_clone_lock.push(e); + // Drop the permit when the simulation is done. + drop(permit); + } + Result::Ok(_) => { + drop(permit); + } - for handle in handles { - handle.await?; } Ok(())