@@ -9,14 +9,15 @@ use init4_bin_base::{
9
9
} ;
10
10
use signet_sim:: { BlockBuild , BuiltBlock , SimCache } ;
11
11
use signet_types:: constants:: SignetSystemConstants ;
12
- use std:: time:: { Duration , Instant } ;
12
+ use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
13
13
use tokio:: {
14
14
sync:: {
15
15
mpsc:: { self } ,
16
16
watch,
17
17
} ,
18
18
task:: JoinHandle ,
19
19
} ;
20
+ use tracing:: trace;
20
21
use trevm:: revm:: {
21
22
context:: BlockEnv ,
22
23
database:: { AlloyDB , WrapDatabaseAsync } ,
@@ -34,7 +35,6 @@ pub struct Simulator {
34
35
pub config : BuilderConfig ,
35
36
/// A provider that cannot sign transactions, used for interacting with the rollup.
36
37
pub ru_provider : RuProvider ,
37
-
38
38
/// The block configuration environment on which to simulate
39
39
pub block_env : watch:: Receiver < Option < BlockEnv > > ,
40
40
}
@@ -55,6 +55,7 @@ impl Simulator {
55
55
///
56
56
/// - `config`: The configuration for the builder.
57
57
/// - `ru_provider`: A provider for interacting with the rollup.
58
+ /// - `block_env`: A receiver for the block environment to simulate against.
58
59
///
59
60
/// # Returns
60
61
///
@@ -79,6 +80,7 @@ impl Simulator {
79
80
/// - `constants`: The system constants for the rollup.
80
81
/// - `sim_items`: The simulation cache containing transactions and bundles.
81
82
/// - `finish_by`: The deadline by which the block must be built.
83
+ /// - `block_env`: The block environment to simulate against.
82
84
///
83
85
/// # Returns
84
86
///
@@ -88,22 +90,34 @@ impl Simulator {
88
90
constants : SignetSystemConstants ,
89
91
sim_items : SimCache ,
90
92
finish_by : Instant ,
91
- block : BlockEnv ,
93
+ block_env : BlockEnv ,
92
94
) -> 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
+
93
102
let db = self . create_db ( ) . await . unwrap ( ) ;
103
+
94
104
let block_build: BlockBuild < _ , NoOpInspector > = BlockBuild :: new (
95
105
db,
96
106
constants,
97
107
self . config . cfg_env ( ) ,
98
- block ,
108
+ block_env ,
99
109
finish_by,
100
110
self . config . concurrency_limit ,
101
111
sim_items,
102
112
self . config . rollup_block_gas_limit ,
103
113
) ;
104
114
105
115
let built_block = block_build. build ( ) . await ;
106
- 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
+ ) ;
107
121
108
122
Ok ( built_block)
109
123
}
@@ -152,19 +166,16 @@ impl Simulator {
152
166
submit_sender : mpsc:: UnboundedSender < SimResult > ,
153
167
) {
154
168
loop {
155
- let sim_cache = cache. clone ( ) ;
156
- let finish_by = self . calculate_deadline ( ) ;
157
-
158
169
// Wait for the block environment to be set
159
170
if self . block_env . changed ( ) . await . is_err ( ) {
160
171
error ! ( "block_env channel closed" ) ;
161
172
return ;
162
173
}
163
174
164
- // If no env, skip this run
165
175
let Some ( block_env) = self . block_env . borrow_and_update ( ) . clone ( ) else { return } ;
166
- debug ! ( block_env = ?block_env, "building on block env" ) ;
167
176
177
+ let finish_by = self . calculate_deadline ( ) ;
178
+ let sim_cache = cache. clone ( ) ;
168
179
match self . handle_build ( constants, sim_cache, finish_by, block_env. clone ( ) ) . await {
169
180
Ok ( block) => {
170
181
debug ! ( block = ?block. block_number( ) , tx_count = block. transactions( ) . len( ) , "built block" ) ;
@@ -187,17 +198,25 @@ impl Simulator {
187
198
pub fn calculate_deadline ( & self ) -> Instant {
188
199
// Get the current timepoint within the slot.
189
200
let timepoint = self . slot_calculator ( ) . current_timepoint_within_slot ( ) ;
201
+ trace ! ( timepoint, "current timepoint within slot" ) ;
190
202
191
203
// We have the timepoint in seconds into the slot. To find out what's
192
204
// remaining, we need to subtract it from the slot duration
193
205
let remaining = self . slot_calculator ( ) . slot_duration ( ) - timepoint;
206
+ trace ! ( remaining, "time remaining in slot" ) ;
194
207
195
208
// We add a 1500 ms buffer to account for sequencer stopping signing.
196
-
197
- let candidate =
209
+ let deadline =
198
210
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" ) ;
215
+
216
+ let timestamp = self . instant_to_timestamp ( buffered_deadline) ;
217
+ trace ! ( ?timestamp, "deadline converted to timestamp" ) ;
199
218
200
- candidate . max ( Instant :: now ( ) )
219
+ buffered_deadline
201
220
}
202
221
203
222
/// Creates an `AlloyDB` instance from the rollup provider.
@@ -226,4 +245,23 @@ impl Simulator {
226
245
let wrapped_db: AlloyDatabaseProvider = WrapDatabaseAsync :: new ( alloy_db) . unwrap ( ) ;
227
246
Some ( wrapped_db)
228
247
}
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
+ }
229
267
}
0 commit comments