verbs_rs/
sim_runner.rs

1//! Simulation execution
2//!
3//! The sim-runner function updates the simulation
4//! state (i.e. the agents and EVM) over a fixed
5//! number of steps, where each step represents
6//! a new block on the simulated chain.
7//!
8
9use crate::agent::SimState;
10use crate::env::{Env, Validator};
11use crate::DB;
12use kdam::tqdm;
13use rand::SeedableRng;
14use rand_xoshiro::Xoroshiro128StarStar;
15
16// Represents blocks updating every 15s
17const BLOCK_INTERVAL: u64 = 15;
18
19/// Simulation execution function
20///
21/// Run a simulation for a fixed number of steps,
22/// each step of the simulation:
23///
24/// * Updates the state of all the agents and
25///   collects transactions to be submitted into
26///   the next block
27/// * Sort the transactions
28/// * Update the block number and timestamp
29/// * Process the transactions
30/// * Record the state of the agents
31///
32/// # Arguments
33///
34/// * `env` - Reference to an [Env] simulation environment
35/// * `agents` - Reference to a set of agents implementing
36///   the [SimState] trait
37/// * `seed` - Random seed
38/// * `n_steps` - Number of simulation steps
39///
40pub fn run<S: SimState, D: DB, V: Validator>(
41    env: &mut Env<D, V>,
42    agents: &mut S,
43    seed: u64,
44    n_steps: usize,
45) {
46    let mut rng = Xoroshiro128StarStar::seed_from_u64(seed);
47
48    for i in tqdm!(0..n_steps) {
49        // Move the events from the previous block into historical storage
50        env.clear_events();
51        // Update all agents
52        let transactions = agents.call_agents(&mut rng, env);
53        // Update the block-time and number
54        env.increment_time(&mut rng, BLOCK_INTERVAL);
55        // Process calls in order
56        env.process_transactions(transactions, &mut rng, i);
57        // Record data from agents
58        agents.record_agents(env);
59    }
60}