diff --git a/core/src/environment.rs b/core/src/environment.rs index dad15aa4..c7e238db 100644 --- a/core/src/environment.rs +++ b/core/src/environment.rs @@ -7,13 +7,14 @@ use revm::{ primitives::{ExecutionResult, Log, TxEnv, U256}, EVM, }; + use std::{ fmt::Debug, - sync::{Arc, Mutex}, + sync::{atomic::{AtomicUsize, Ordering},{Arc, Mutex}}, thread, }; -use crate::utils::revm_logs_to_ethers_logs; +use crate::{utils::revm_logs_to_ethers_logs, math::stochastic_process::poisson_process}; use crate::{agent::Agent, middleware::RevmMiddleware}; /// Type Aliases for the event channel. @@ -22,6 +23,7 @@ pub(crate) type ExecutionSender = Sender; pub(crate) type TxEnvSender = Sender<(ToTransact, TxEnv, ExecutionSender)>; pub(crate) type TxEnvReceiver = Receiver<(ToTransact, TxEnv, ExecutionSender)>; +/// State enum for the [`Environment`]. #[derive(Debug, Eq, PartialEq, Clone, Copy)] pub enum State { /// The [`Environment`] is currently running. @@ -32,33 +34,30 @@ pub enum State { Stopped, } -pub struct Time; - -impl Time { - pub fn new(occurances: f64) -> Self { - Self {} - } - pub fn default() -> Self { - Self {} - } -} +/// The environment struct. pub struct Environment { + /// label for the environment pub label: String, pub(crate) state: State, pub(crate) evm: EVM>, + /// Connection to the environment pub connection: Connection, /// Clients (Agents) in the environment pub clients: Vec>>, - // pub deployed_contracts: HashMap>, - pub time: Time, + /// expected events per block + pub lambda: f64, } + +/// Connection struct for the [`Environment`]. #[derive(Debug, Clone)] pub struct Connection { pub(crate) tx_sender: TxEnvSender, tx_receiver: TxEnvReceiver, pub(crate) event_broadcaster: Arc>, + /// expected events per block + pub tx_per_block: Arc, } impl Environment { @@ -73,6 +72,7 @@ impl Environment { tx_sender: transaction_channel.0, tx_receiver: transaction_channel.1, event_broadcaster: Arc::new(Mutex::new(EventBroadcaster::new())), + tx_per_block: Arc::new(AtomicUsize::new(0)), }; Self { label, @@ -80,21 +80,17 @@ impl Environment { evm, connection, clients: vec![], - time: Time::default(), + lambda: 0.0, } } + /// Set the expected events per block + pub fn configure_lambda(&mut self, lambda: f64) { + self.lambda = lambda; + } - // // time config - // pub fn configure_time(&mut self, time: u64) { - - // self = - // self.evm.env.block.timestamp = U256::from(time); - // } - - // TODO: We need to make this the way to add agents to the environment. - // in `agent.rs` we have `new_simulation_agent` which should probably just be called from this function instead. - // OR agents can be created (without a connection?) and then added to the environment where they will gain a connection? - pub fn add_agent(&mut self, agent: Agent) { + /// Creates a new [`Agent= expected_occurance[0] as usize { + evm.env.block.number += U256::from(1); + counter.store(0, Ordering::Relaxed); + } evm.env.tx = tx; + counter.fetch_add(1, Ordering::Relaxed); if to_transact { let execution_result = match evm.transact_commit() { Ok(val) => val,