|
| 1 | +use crate::{env::RollupEnv, outcome::SimulatedItem, SimCache, SimDb, SimEnv}; |
| 2 | +use core::fmt; |
| 3 | +use std::{ops::Deref, sync::Arc}; |
| 4 | +use tokio::{select, sync::watch}; |
| 5 | +use tracing::{instrument, trace}; |
| 6 | +use trevm::{ |
| 7 | + helpers::Ctx, |
| 8 | + revm::{inspector::NoOpInspector, DatabaseRef, Inspector}, |
| 9 | + Block, Cfg, |
| 10 | +}; |
| 11 | + |
| 12 | +/// A simulation environment. |
| 13 | +/// |
| 14 | +/// Contains enough information to run a simulation. |
| 15 | +pub struct SharedSimEnv<Db, Insp = NoOpInspector> { |
| 16 | + inner: Arc<SimEnv<Db, Insp>>, |
| 17 | +} |
| 18 | + |
| 19 | +impl<Db, Insp> fmt::Debug for SharedSimEnv<Db, Insp> { |
| 20 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 21 | + f.debug_struct("SharedSimEnv") |
| 22 | + .field("finish_by", &self.inner.finish_by()) |
| 23 | + .field("concurrency_limit", &self.inner.concurrency_limit()) |
| 24 | + .finish_non_exhaustive() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl<Db, Insp> Deref for SharedSimEnv<Db, Insp> { |
| 29 | + type Target = SimEnv<Db, Insp>; |
| 30 | + |
| 31 | + fn deref(&self) -> &Self::Target { |
| 32 | + &self.inner |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl<Db, Insp> From<SimEnv<Db, Insp>> for SharedSimEnv<Db, Insp> |
| 37 | +where |
| 38 | + Db: DatabaseRef + Send + Sync + 'static, |
| 39 | + Insp: Inspector<Ctx<SimDb<Db>>> + Default + Sync + 'static, |
| 40 | +{ |
| 41 | + fn from(inner: SimEnv<Db, Insp>) -> Self { |
| 42 | + Self { inner: Arc::new(inner) } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<Db, Insp> SharedSimEnv<Db, Insp> |
| 47 | +where |
| 48 | + Db: DatabaseRef + Send + Sync + 'static, |
| 49 | + Insp: Inspector<Ctx<SimDb<Db>>> + Default + Sync + 'static, |
| 50 | +{ |
| 51 | + /// Creates a new `SimEnv` instance. |
| 52 | + pub fn new<C, B>( |
| 53 | + rollup: RollupEnv<Db, Insp>, |
| 54 | + cfg: C, |
| 55 | + block: B, |
| 56 | + finish_by: std::time::Instant, |
| 57 | + concurrency_limit: usize, |
| 58 | + sim_items: SimCache, |
| 59 | + ) -> Self |
| 60 | + where |
| 61 | + C: Cfg, |
| 62 | + B: Block, |
| 63 | + { |
| 64 | + SimEnv::new(rollup, cfg, block, finish_by, concurrency_limit, sim_items).into() |
| 65 | + } |
| 66 | + |
| 67 | + /// Run a simulation round, returning the best item. |
| 68 | + #[instrument(skip(self))] |
| 69 | + pub async fn sim_round(&mut self, max_gas: u64) -> Option<SimulatedItem> { |
| 70 | + let (best_tx, mut best_watcher) = watch::channel(None); |
| 71 | + |
| 72 | + let this = self.inner.clone(); |
| 73 | + |
| 74 | + // Spawn a blocking task to run the simulations. |
| 75 | + let sim_task = tokio::task::spawn_blocking(move || this.sim_round(max_gas, best_tx)); |
| 76 | + |
| 77 | + // Either simulation is done, or we time out |
| 78 | + select! { |
| 79 | + _ = tokio::time::sleep_until(self.finish_by().into()) => { |
| 80 | + trace!("Sim round timed out"); |
| 81 | + }, |
| 82 | + _ = sim_task => { |
| 83 | + trace!("Sim round done"); |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + // Check what the current best outcome is. |
| 88 | + let best = best_watcher.borrow_and_update(); |
| 89 | + trace!(score = %best.as_ref().map(|candidate| candidate.score).unwrap_or_default(), "Read outcome from channel"); |
| 90 | + let outcome = best.as_ref()?; |
| 91 | + |
| 92 | + // Remove the item from the cache. |
| 93 | + let item = self.sim_items().remove(outcome.cache_rank)?; |
| 94 | + // Accept the cache from the simulation. |
| 95 | + Arc::get_mut(&mut self.inner) |
| 96 | + .expect("sims dropped already") |
| 97 | + .rollup_mut() |
| 98 | + .accept_cache_ref(&outcome.cache) |
| 99 | + .ok()?; |
| 100 | + |
| 101 | + Some(SimulatedItem { gas_used: outcome.gas_used, score: outcome.score, item }) |
| 102 | + } |
| 103 | +} |
0 commit comments