1
1
//! `block.rs` contains the Simulator and everything that wires it into an
2
2
//! actor that handles the simulation of a stream of bundles and transactions
3
3
//! and turns them into valid Pecorino blocks for network submission.
4
- use crate :: config:: { BuilderConfig , RuProvider } ;
5
- use alloy:: { eips:: BlockId , network:: Ethereum , providers:: Provider } ;
4
+ use crate :: {
5
+ config:: { BuilderConfig , RuProvider } ,
6
+ tasks:: env:: SimEnv ,
7
+ } ;
8
+ use alloy:: {
9
+ eips:: BlockId ,
10
+ network:: Ethereum ,
11
+ providers:: Provider ,
12
+ } ;
6
13
use init4_bin_base:: {
7
14
deps:: tracing:: { debug, error} ,
8
15
utils:: calc:: SlotCalculator ,
9
16
} ;
10
17
use signet_sim:: { BlockBuild , BuiltBlock , SimCache } ;
11
18
use signet_types:: constants:: SignetSystemConstants ;
19
+ use tracing:: info;
12
20
use std:: time:: { Duration , Instant } ;
13
21
use tokio:: {
14
22
sync:: {
@@ -35,7 +43,7 @@ pub struct Simulator {
35
43
/// A provider that cannot sign transactions, used for interacting with the rollup.
36
44
pub ru_provider : RuProvider ,
37
45
/// The block configuration environment on which to simulate
38
- pub block_env : watch:: Receiver < Option < BlockEnv > > ,
46
+ pub sim_env : watch:: Receiver < Option < SimEnv > > ,
39
47
}
40
48
41
49
/// SimResult bundles a BuiltBlock to the BlockEnv it was simulated against.
@@ -44,7 +52,7 @@ pub struct SimResult {
44
52
/// The block built with the successfully simulated transactions
45
53
pub block : BuiltBlock ,
46
54
/// The block environment the transactions were simulated against.
47
- pub env : BlockEnv ,
55
+ pub env : SimEnv ,
48
56
}
49
57
50
58
impl Simulator {
@@ -62,9 +70,9 @@ impl Simulator {
62
70
pub fn new (
63
71
config : & BuilderConfig ,
64
72
ru_provider : RuProvider ,
65
- block_env : watch:: Receiver < Option < BlockEnv > > ,
73
+ sim_env : watch:: Receiver < Option < SimEnv > > ,
66
74
) -> Self {
67
- Self { config : config. clone ( ) , ru_provider, block_env }
75
+ Self { config : config. clone ( ) , ru_provider, sim_env }
68
76
}
69
77
70
78
/// Get the slot calculator.
@@ -74,6 +82,10 @@ impl Simulator {
74
82
75
83
/// Handles building a single block.
76
84
///
85
+ /// Builds a block in the block environment with items from the simulation cache
86
+ /// against the database state. When the `finish_by` deadline is reached, it
87
+ /// stops simulating and returns the block.
88
+ ///
77
89
/// # Arguments
78
90
///
79
91
/// - `constants`: The system constants for the rollup.
@@ -93,7 +105,6 @@ impl Simulator {
93
105
) -> eyre:: Result < BuiltBlock > {
94
106
debug ! (
95
107
block_number = block_env. number,
96
- ?finish_by,
97
108
tx_count = sim_items. len( ) ,
98
109
"starting block build" ,
99
110
) ;
@@ -114,15 +125,15 @@ impl Simulator {
114
125
let built_block = block_build. build ( ) . await ;
115
126
debug ! (
116
127
tx_count = built_block. tx_count( ) ,
117
- block_number = ? built_block. block_number( ) ,
128
+ block_number = built_block. block_number( ) ,
118
129
"block simulation completed" ,
119
130
) ;
120
131
121
132
Ok ( built_block)
122
133
}
123
134
124
- /// Spawns the simulator task, which handles the setup and sets the deadline
125
- /// for the each round of simulation .
135
+ /// Spawns the simulator task, which ticks along the simulation loop
136
+ /// as it receives block environments .
126
137
///
127
138
/// # Arguments
128
139
///
@@ -144,14 +155,16 @@ impl Simulator {
144
155
tokio:: spawn ( async move { self . run_simulator ( constants, cache, submit_sender) . await } )
145
156
}
146
157
147
- /// Continuously runs the block simulation and submission loop.
158
+ /// This function runs indefinitely, waiting for the block environment to be set and checking
159
+ /// if the current slot is valid before building a block and sending it along for to the submit channel.
148
160
///
149
- /// This function clones the simulation cache, calculates a deadline for block building,
150
- /// attempts to build a block using the latest cache and constants, and submits the built
151
- /// block through the provided channel. If an error occurs during block building or submission,
152
- /// it logs the error and continues the loop.
153
- ///
154
- /// This function runs indefinitely and never returns.
161
+ /// If it is authorized for the current slot, then the simulator task
162
+ /// - clones the simulation cache,
163
+ /// - calculates a deadline for block building,
164
+ /// - attempts to build a block using the latest cache and constants,
165
+ /// - then submits the built block through the provided channel.
166
+ ///
167
+ /// If an error occurs during block building or submission, it logs the error and continues the loop.
155
168
///
156
169
/// # Arguments
157
170
///
@@ -166,19 +179,23 @@ impl Simulator {
166
179
) {
167
180
loop {
168
181
// Wait for the block environment to be set
169
- if self . block_env . changed ( ) . await . is_err ( ) {
170
- error ! ( "block_env channel closed" ) ;
182
+ if self . sim_env . changed ( ) . await . is_err ( ) {
183
+ error ! ( "block_env channel closed - shutting down simulator task " ) ;
171
184
return ;
172
185
}
186
+ let Some ( sim_env) = self . sim_env . borrow_and_update ( ) . clone ( ) else { return } ;
187
+ info ! ( block_number = sim_env. signet. number, "new block environment received" ) ;
173
188
174
- let Some ( block_env) = self . block_env . borrow_and_update ( ) . clone ( ) else { return } ;
175
-
189
+ // Calculate the deadline for this block simulation.
190
+ // NB: This must happen _after_ taking a reference to the sim cache,
191
+ // waiting for a new block, and checking current slot authorization.
176
192
let finish_by = self . calculate_deadline ( ) ;
177
193
let sim_cache = cache. clone ( ) ;
178
- match self . handle_build ( constants, sim_cache, finish_by, block_env. clone ( ) ) . await {
194
+ match self . handle_build ( constants, sim_cache, finish_by, sim_env. signet . clone ( ) ) . await
195
+ {
179
196
Ok ( block) => {
180
- debug ! ( block = ?block. block_number( ) , tx_count = block. transactions( ) . len( ) , "built block" ) ;
181
- let _ = submit_sender. send ( SimResult { block, env : block_env } ) ;
197
+ debug ! ( block = ?block. block_number( ) , tx_count = block. transactions( ) . len( ) , "built simulated block" ) ;
198
+ let _ = submit_sender. send ( SimResult { block, env : sim_env } ) ;
182
199
}
183
200
Err ( e) => {
184
201
error ! ( err = %e, "failed to build block" ) ;
0 commit comments