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 super :: cfg:: PecorinoBlockEnv ;
5
- use crate :: {
6
- config:: { BuilderConfig , RuProvider } ,
7
- tasks:: block:: cfg:: PecorinoCfg ,
8
- } ;
9
- use alloy:: {
10
- eips:: { BlockId , BlockNumberOrTag :: Latest } ,
11
- network:: Ethereum ,
12
- providers:: Provider ,
13
- } ;
14
- use chrono:: { DateTime , Utc } ;
15
- use eyre:: { Context , bail} ;
4
+ use crate :: config:: { BuilderConfig , RuProvider } ;
5
+ use alloy:: { eips:: BlockId , network:: Ethereum , providers:: Provider } ;
16
6
use init4_bin_base:: {
17
- deps:: tracing:: { debug, error, info , warn } ,
7
+ deps:: tracing:: { debug, error} ,
18
8
utils:: calc:: SlotCalculator ,
19
9
} ;
20
10
use signet_sim:: { BlockBuild , BuiltBlock , SimCache } ;
21
11
use signet_types:: constants:: SignetSystemConstants ;
22
12
use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
23
13
use tokio:: {
24
- sync:: mpsc:: { self } ,
14
+ sync:: {
15
+ mpsc:: { self } ,
16
+ watch,
17
+ } ,
25
18
task:: JoinHandle ,
26
19
} ;
27
20
use trevm:: revm:: {
21
+ context:: BlockEnv ,
28
22
database:: { AlloyDB , WrapDatabaseAsync } ,
29
23
inspector:: NoOpInspector ,
30
24
} ;
@@ -38,6 +32,9 @@ pub struct Simulator {
38
32
pub config : BuilderConfig ,
39
33
/// A provider that cannot sign transactions, used for interacting with the rollup.
40
34
pub ru_provider : RuProvider ,
35
+
36
+ /// The block configuration environment on which to simulate
37
+ pub block_env : watch:: Receiver < Option < BlockEnv > > ,
41
38
}
42
39
43
40
type AlloyDatabaseProvider = WrapDatabaseAsync < AlloyDB < Ethereum , RuProvider > > ;
@@ -53,8 +50,12 @@ impl Simulator {
53
50
/// # Returns
54
51
///
55
52
/// A new `Simulator` instance.
56
- pub fn new ( config : & BuilderConfig , ru_provider : RuProvider ) -> Self {
57
- Self { config : config. clone ( ) , ru_provider }
53
+ pub fn new (
54
+ config : & BuilderConfig ,
55
+ ru_provider : RuProvider ,
56
+ block_env : watch:: Receiver < Option < BlockEnv > > ,
57
+ ) -> Self {
58
+ Self { config : config. clone ( ) , ru_provider, block_env }
58
59
}
59
60
60
61
/// Get the slot calculator.
@@ -78,14 +79,14 @@ impl Simulator {
78
79
constants : SignetSystemConstants ,
79
80
sim_items : SimCache ,
80
81
finish_by : Instant ,
81
- block : PecorinoBlockEnv ,
82
+ block : BlockEnv ,
82
83
) -> eyre:: Result < BuiltBlock > {
83
84
let db = self . create_db ( ) . await . unwrap ( ) ;
84
85
85
86
let block_build: BlockBuild < _ , NoOpInspector > = BlockBuild :: new (
86
87
db,
87
88
constants,
88
- PecorinoCfg { } ,
89
+ self . config . cfg_env ( ) ,
89
90
block,
90
91
finish_by,
91
92
self . config . concurrency_limit ,
@@ -146,14 +147,10 @@ impl Simulator {
146
147
let sim_cache = cache. clone ( ) ;
147
148
let finish_by = self . calculate_deadline ( ) ;
148
149
149
- let block_env = match self . next_block_env ( finish_by) . await {
150
- Ok ( block) => block,
151
- Err ( err) => {
152
- error ! ( err = %err, "failed to configure next block" ) ;
153
- break ;
154
- }
155
- } ;
156
- info ! ( block_env = ?block_env, "created block" ) ;
150
+ // If no env, skip this run
151
+ let Some ( block_env) = self . block_env . borrow ( ) . clone ( ) else { return } ;
152
+
153
+ debug ! ( block_env = ?block_env, "building on block" ) ;
157
154
158
155
match self . handle_build ( constants, sim_cache, finish_by, block_env) . await {
159
156
Ok ( block) => {
@@ -210,68 +207,4 @@ impl Simulator {
210
207
let wrapped_db: AlloyDatabaseProvider = WrapDatabaseAsync :: new ( alloy_db) . unwrap ( ) ;
211
208
Some ( wrapped_db)
212
209
}
213
-
214
- /// Prepares the next block environment.
215
- ///
216
- /// Prepares the next block environment to load into the simulator by fetching the latest block number,
217
- /// assigning the correct next block number, checking the basefee, and setting the timestamp,
218
- /// reward address, and gas configuration for the block environment based on builder configuration.
219
- ///
220
- /// # Arguments
221
- ///
222
- /// - finish_by: The deadline at which block simulation will end.
223
- async fn next_block_env ( & self , finish_by : Instant ) -> eyre:: Result < PecorinoBlockEnv > {
224
- let remaining = finish_by. duration_since ( Instant :: now ( ) ) ;
225
- let finish_time = SystemTime :: now ( ) + remaining;
226
- let deadline: DateTime < Utc > = finish_time. into ( ) ;
227
- debug ! ( deadline = %deadline, "preparing block env" ) ;
228
-
229
- // Fetch the latest block number and increment it by 1
230
- let latest_block_number = match self . ru_provider . get_block_number ( ) . await {
231
- Ok ( num) => num,
232
- Err ( err) => {
233
- error ! ( %err, "RPC error during block build" ) ;
234
- bail ! ( err)
235
- }
236
- } ;
237
- debug ! ( next_block_num = latest_block_number + 1 , "preparing block env" ) ;
238
-
239
- // Fetch the basefee from previous block to calculate gas for this block
240
- let basefee = match self . get_basefee ( ) . await ? {
241
- Some ( basefee) => basefee,
242
- None => {
243
- warn ! ( "get basefee failed - RPC error likely occurred" ) ;
244
- todo ! ( )
245
- }
246
- } ;
247
- debug ! ( basefee = basefee, "setting basefee" ) ;
248
-
249
- // Craft the Block environment to pass to the simulator
250
- let block_env = PecorinoBlockEnv :: new (
251
- self . config . clone ( ) ,
252
- latest_block_number + 1 ,
253
- deadline. timestamp ( ) as u64 ,
254
- basefee,
255
- ) ;
256
- debug ! ( block_env = ?block_env, "prepared block env" ) ;
257
-
258
- Ok ( block_env)
259
- }
260
-
261
- /// Returns the basefee of the latest block.
262
- ///
263
- /// # Returns
264
- ///
265
- /// The basefee of the previous (latest) block if the request was successful,
266
- /// or a sane default if the RPC failed.
267
- async fn get_basefee ( & self ) -> eyre:: Result < Option < u64 > > {
268
- let Some ( block) =
269
- self . ru_provider . get_block_by_number ( Latest ) . await . wrap_err ( "basefee error" ) ?
270
- else {
271
- return Ok ( None ) ;
272
- } ;
273
-
274
- debug ! ( basefee = ?block. header. base_fee_per_gas, "basefee found" ) ;
275
- Ok ( block. header . base_fee_per_gas )
276
- }
277
210
}
0 commit comments