@@ -22,21 +22,6 @@ use tokio::{
2222} ;
2323use tracing:: { Instrument , Span , debug, instrument} ;
2424
25- /// `Simulator` is responsible for periodically building blocks and submitting them for
26- /// signing and inclusion in the blockchain. It wraps a rollup provider and a slot
27- /// calculator with a builder configuration.
28- #[ derive( Debug ) ]
29- pub struct Simulator {
30- /// Configuration for the builder.
31- pub config : BuilderConfig ,
32- /// Host Provider to interact with the host chain.
33- pub host_provider : HostProvider ,
34- /// A provider that cannot sign transactions, used for interacting with the rollup.
35- pub ru_provider : RuProvider ,
36- /// The block configuration environment on which to simulate
37- pub sim_env : watch:: Receiver < Option < SimEnv > > ,
38- }
39-
4025/// SimResult bundles a BuiltBlock to the BlockEnv it was simulated against.
4126#[ derive( Debug , Clone ) ]
4227pub struct SimResult {
@@ -79,25 +64,30 @@ impl SimResult {
7964 }
8065}
8166
82- impl Simulator {
83- /// Creates a new `Simulator` instance.
84- ///
85- /// # Arguments
86- ///
87- /// - `config`: The configuration for the builder.
88- /// - `ru_provider`: A provider for interacting with the rollup.
89- /// - `block_env`: A receiver for the block environment to simulate against.
90- ///
91- /// # Returns
92- ///
93- /// A new `Simulator` instance.
94- pub fn new (
95- config : & BuilderConfig ,
96- host_provider : HostProvider ,
97- ru_provider : RuProvider ,
98- sim_env : watch:: Receiver < Option < SimEnv > > ,
99- ) -> Self {
100- Self { config : config. clone ( ) , host_provider, ru_provider, sim_env }
67+ /// A task that builds blocks based on incoming [`SimEnv`]s and a simulation
68+ /// cache.
69+ #[ derive( Debug ) ]
70+ pub struct SimulatorTask {
71+ /// Configuration for the builder.
72+ config : & ' static BuilderConfig ,
73+ /// Host Provider to interact with the host chain.
74+ host_provider : HostProvider ,
75+ /// A provider that cannot sign transactions, used for interacting with the rollup.
76+ ru_provider : RuProvider ,
77+ /// The block configuration environments on which to simulate
78+ envs : watch:: Receiver < Option < SimEnv > > ,
79+ }
80+
81+ impl SimulatorTask {
82+ /// Create a new `SimulatorTask` instance. This task must be spawned to
83+ /// begin processing incoming block environments.
84+ pub async fn new ( envs : watch:: Receiver < Option < SimEnv > > ) -> eyre:: Result < Self > {
85+ let config = crate :: config ( ) ;
86+
87+ let ( host_provider, ru_provider) =
88+ tokio:: try_join!( config. connect_host_provider( ) , config. connect_ru_provider( ) ) ?;
89+
90+ Ok ( Self { config, host_provider, ru_provider, envs } )
10191 }
10292
10393 /// Get the slot calculator.
@@ -110,18 +100,17 @@ impl Simulator {
110100 & self . config . constants
111101 }
112102
113- /// Handles building a single block.
103+ /// Build a single block
114104 ///
115- /// Builds a block in the block environment with items from the simulation cache
116- /// against the database state. When the `finish_by` deadline is reached, it
117- /// stops simulating and returns the block.
105+ /// Build a block in the sim environment with items from the simulation
106+ /// cache against the database state. When the `finish_by` deadline is
107+ /// reached, it stops simulating and returns the block.
118108 ///
119109 /// # Arguments
120110 ///
121- /// - `constants`: The system constants for the rollup.
122111 /// - `sim_items`: The simulation cache containing transactions and bundles.
123112 /// - `finish_by`: The deadline by which the block must be built.
124- /// - `block_env `: The block environment to simulate against.
113+ /// - `sim_env `: The block environment to simulate against.
125114 ///
126115 /// # Returns
127116 ///
@@ -209,11 +198,11 @@ impl Simulator {
209198 ) {
210199 loop {
211200 // Wait for the block environment to be set
212- if self . sim_env . changed ( ) . await . is_err ( ) {
201+ if self . envs . changed ( ) . await . is_err ( ) {
213202 tracing:: error!( "block_env channel closed - shutting down simulator task" ) ;
214203 return ;
215204 }
216- let Some ( sim_env) = self . sim_env . borrow_and_update ( ) . clone ( ) else { return } ;
205+ let Some ( sim_env) = self . envs . borrow_and_update ( ) . clone ( ) else { return } ;
217206
218207 let span = sim_env. span ( ) ;
219208 span_info ! ( span, "new block environment received" ) ;
0 commit comments