@@ -9,14 +9,15 @@ use init4_bin_base::{
99} ;
1010use signet_sim:: { BlockBuild , BuiltBlock , SimCache } ;
1111use signet_types:: constants:: SignetSystemConstants ;
12- use std:: time:: { Duration , Instant } ;
12+ use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
1313use tokio:: {
1414 sync:: {
1515 mpsc:: { self } ,
1616 watch,
1717 } ,
1818 task:: JoinHandle ,
1919} ;
20+ use tracing:: trace;
2021use trevm:: revm:: {
2122 context:: BlockEnv ,
2223 database:: { AlloyDB , WrapDatabaseAsync } ,
@@ -34,18 +35,27 @@ pub struct Simulator {
3435 pub config : BuilderConfig ,
3536 /// A provider that cannot sign transactions, used for interacting with the rollup.
3637 pub ru_provider : RuProvider ,
37-
3838 /// The block configuration environment on which to simulate
3939 pub block_env : watch:: Receiver < Option < BlockEnv > > ,
4040}
4141
42+ /// SimResult bundles a BuiltBlock to the BlockEnv it was simulated against.
43+ #[ derive( Debug , Clone ) ]
44+ pub struct SimResult {
45+ /// The block built with the successfully simulated transactions
46+ pub block : BuiltBlock ,
47+ /// The block environment the transactions were simulated against.
48+ pub env : BlockEnv ,
49+ }
50+
4251impl Simulator {
4352 /// Creates a new `Simulator` instance.
4453 ///
4554 /// # Arguments
4655 ///
4756 /// - `config`: The configuration for the builder.
4857 /// - `ru_provider`: A provider for interacting with the rollup.
58+ /// - `block_env`: A receiver for the block environment to simulate against.
4959 ///
5060 /// # Returns
5161 ///
@@ -70,6 +80,7 @@ impl Simulator {
7080 /// - `constants`: The system constants for the rollup.
7181 /// - `sim_items`: The simulation cache containing transactions and bundles.
7282 /// - `finish_by`: The deadline by which the block must be built.
83+ /// - `block_env`: The block environment to simulate against.
7384 ///
7485 /// # Returns
7586 ///
@@ -79,22 +90,34 @@ impl Simulator {
7990 constants : SignetSystemConstants ,
8091 sim_items : SimCache ,
8192 finish_by : Instant ,
82- block : BlockEnv ,
93+ block_env : BlockEnv ,
8394 ) -> eyre:: Result < BuiltBlock > {
95+ debug ! (
96+ block_number = block_env. number,
97+ deadline = ?self . instant_to_timestamp( finish_by) ,
98+ tx_count= sim_items. len( ) ,
99+ "starting block build" ,
100+ ) ;
101+
84102 let db = self . create_db ( ) . await . unwrap ( ) ;
103+
85104 let block_build: BlockBuild < _ , NoOpInspector > = BlockBuild :: new (
86105 db,
87106 constants,
88107 self . config . cfg_env ( ) ,
89- block ,
108+ block_env ,
90109 finish_by,
91110 self . config . concurrency_limit ,
92111 sim_items,
93112 self . config . rollup_block_gas_limit ,
94113 ) ;
95114
96115 let built_block = block_build. build ( ) . await ;
97- debug ! ( block_number = ?built_block. block_number( ) , "finished building block" ) ;
116+ debug ! (
117+ tx_count = built_block. tx_count( ) ,
118+ block_number = ?built_block. block_number( ) ,
119+ "block simulation completed" ,
120+ ) ;
98121
99122 Ok ( built_block)
100123 }
@@ -115,7 +138,7 @@ impl Simulator {
115138 self ,
116139 constants : SignetSystemConstants ,
117140 cache : SimCache ,
118- submit_sender : mpsc:: UnboundedSender < BuiltBlock > ,
141+ submit_sender : mpsc:: UnboundedSender < SimResult > ,
119142 ) -> JoinHandle < ( ) > {
120143 debug ! ( "starting simulator task" ) ;
121144
@@ -140,26 +163,23 @@ impl Simulator {
140163 mut self ,
141164 constants : SignetSystemConstants ,
142165 cache : SimCache ,
143- submit_sender : mpsc:: UnboundedSender < BuiltBlock > ,
166+ submit_sender : mpsc:: UnboundedSender < SimResult > ,
144167 ) {
145168 loop {
146- let sim_cache = cache. clone ( ) ;
147- let finish_by = self . calculate_deadline ( ) ;
148-
149169 // Wait for the block environment to be set
150170 if self . block_env . changed ( ) . await . is_err ( ) {
151171 error ! ( "block_env channel closed" ) ;
152172 return ;
153173 }
154174
155- // If no env, skip this run
156175 let Some ( block_env) = self . block_env . borrow_and_update ( ) . clone ( ) else { return } ;
157- debug ! ( block_env = ?block_env, "building on block env" ) ;
158176
159- match self . handle_build ( constants, sim_cache, finish_by, block_env) . await {
177+ let finish_by = self . calculate_deadline ( ) ;
178+ let sim_cache = cache. clone ( ) ;
179+ match self . handle_build ( constants, sim_cache, finish_by, block_env. clone ( ) ) . await {
160180 Ok ( block) => {
161- debug ! ( block = ?block, "built block" ) ;
162- let _ = submit_sender. send ( block) ;
181+ debug ! ( block = ?block. block_number ( ) , tx_count = block . transactions ( ) . len ( ) , "built block" ) ;
182+ let _ = submit_sender. send ( SimResult { block, env : block_env } ) ;
163183 }
164184 Err ( e) => {
165185 error ! ( err = %e, "failed to build block" ) ;
@@ -178,17 +198,25 @@ impl Simulator {
178198 pub fn calculate_deadline ( & self ) -> Instant {
179199 // Get the current timepoint within the slot.
180200 let timepoint = self . slot_calculator ( ) . current_timepoint_within_slot ( ) ;
201+ trace ! ( timepoint, "current timepoint within slot" ) ;
181202
182203 // We have the timepoint in seconds into the slot. To find out what's
183204 // remaining, we need to subtract it from the slot duration
184205 let remaining = self . slot_calculator ( ) . slot_duration ( ) - timepoint;
206+ trace ! ( remaining, "time remaining in slot" ) ;
185207
186208 // We add a 1500 ms buffer to account for sequencer stopping signing.
187-
188- let candidate =
209+ let deadline =
189210 Instant :: now ( ) + Duration :: from_secs ( remaining) - Duration :: from_millis ( 1500 ) ;
211+ trace ! ( deadline = ?self . instant_to_timestamp( deadline) , "calculated deadline for block simulation" ) ;
212+
213+ let buffered_deadline = deadline. max ( Instant :: now ( ) ) ;
214+ trace ! ( ?buffered_deadline, "final deadline for block simulation" ) ;
190215
191- candidate. max ( Instant :: now ( ) )
216+ let timestamp = self . instant_to_timestamp ( buffered_deadline) ;
217+ trace ! ( ?timestamp, "deadline converted to timestamp" ) ;
218+
219+ buffered_deadline
192220 }
193221
194222 /// Creates an `AlloyDB` instance from the rollup provider.
@@ -217,4 +245,23 @@ impl Simulator {
217245 let wrapped_db: AlloyDatabaseProvider = WrapDatabaseAsync :: new ( alloy_db) . unwrap ( ) ;
218246 Some ( wrapped_db)
219247 }
248+
249+ /// Converts an `Instant` to a UNIX timestamp in seconds and milliseconds.
250+ pub fn instant_to_timestamp ( & self , instant : Instant ) -> ( u64 , u128 ) {
251+ let now_instant = Instant :: now ( ) ;
252+ let now_system = SystemTime :: now ( ) ;
253+
254+ let duration_from_now = now_instant. duration_since ( instant) ;
255+
256+ // Subtract that duration from the system time
257+ let target_system_time = now_system - duration_from_now;
258+
259+ let duration_since_epoch =
260+ target_system_time. duration_since ( UNIX_EPOCH ) . expect ( "Time went backwards" ) ;
261+
262+ let seconds = duration_since_epoch. as_secs ( ) ;
263+ let milliseconds = duration_since_epoch. as_millis ( ) ;
264+
265+ ( seconds, milliseconds)
266+ }
220267}
0 commit comments