4
4
use super :: cfg:: PecorinoBlockEnv ;
5
5
use crate :: {
6
6
config:: { BuilderConfig , RuProvider } ,
7
- tasks:: { block:: cfg:: PecorinoCfg , cache :: Bundle } ,
7
+ tasks:: block:: cfg:: PecorinoCfg ,
8
8
} ;
9
9
use alloy:: {
10
- consensus:: TxEnvelope ,
11
10
eips:: { BlockId , BlockNumberOrTag :: Latest } ,
12
11
network:: Ethereum ,
13
12
providers:: Provider ,
@@ -20,18 +19,10 @@ use init4_bin_base::{
20
19
} ;
21
20
use signet_sim:: { BlockBuild , BuiltBlock , SimCache } ;
22
21
use signet_types:: constants:: SignetSystemConstants ;
23
- use std:: {
24
- sync:: {
25
- Arc ,
26
- atomic:: { AtomicU64 , Ordering } ,
27
- } ,
28
- time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ,
29
- } ;
22
+ use std:: time:: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
30
23
use tokio:: {
31
- select,
32
24
sync:: mpsc:: { self } ,
33
25
task:: JoinHandle ,
34
- time:: sleep,
35
26
} ;
36
27
use trevm:: revm:: {
37
28
database:: { AlloyDB , WrapDatabaseAsync } ,
@@ -47,8 +38,6 @@ pub struct Simulator {
47
38
pub config : BuilderConfig ,
48
39
/// A provider that cannot sign transactions, used for interacting with the rollup.
49
40
pub ru_provider : RuProvider ,
50
- /// The slot calculator for determining when to wake up and build blocks.
51
- pub slot_calculator : SlotCalculator ,
52
41
}
53
42
54
43
type AlloyDatabaseProvider = WrapDatabaseAsync < AlloyDB < Ethereum , RuProvider > > ;
@@ -60,17 +49,17 @@ impl Simulator {
60
49
///
61
50
/// - `config`: The configuration for the builder.
62
51
/// - `ru_provider`: A provider for interacting with the rollup.
63
- /// - `slot_calculator`: A slot calculator for managing block timing.
64
52
///
65
53
/// # Returns
66
54
///
67
55
/// A new `Simulator` instance.
68
- pub fn new (
69
- config : & BuilderConfig ,
70
- ru_provider : RuProvider ,
71
- slot_calculator : SlotCalculator ,
72
- ) -> Self {
73
- Self { config : config. clone ( ) , ru_provider, slot_calculator }
56
+ pub fn new ( config : & BuilderConfig , ru_provider : RuProvider ) -> Self {
57
+ Self { config : config. clone ( ) , ru_provider }
58
+ }
59
+
60
+ /// Get the slot calculator.
61
+ pub const fn slot_calculator ( & self ) -> & SlotCalculator {
62
+ & self . config . slot_calculator
74
63
}
75
64
76
65
/// Handles building a single block.
@@ -110,85 +99,6 @@ impl Simulator {
110
99
Ok ( block)
111
100
}
112
101
113
- /// Spawns two tasks: one to handle incoming transactions and bundles,
114
- /// adding them to the simulation cache, and one to track the latest basefee.
115
- ///
116
- /// # Arguments
117
- ///
118
- /// - `tx_receiver`: A channel receiver for incoming transactions.
119
- /// - `bundle_receiver`: A channel receiver for incoming bundles.
120
- /// - `cache`: The simulation cache to store the received items.
121
- ///
122
- /// # Returns
123
- ///
124
- /// A `JoinHandle` for the basefee updater and a `JoinHandle` for the
125
- /// cache handler.
126
- pub fn spawn_cache_tasks (
127
- & self ,
128
- tx_receiver : mpsc:: UnboundedReceiver < TxEnvelope > ,
129
- bundle_receiver : mpsc:: UnboundedReceiver < Bundle > ,
130
- cache : SimCache ,
131
- ) -> ( JoinHandle < ( ) > , JoinHandle < ( ) > ) {
132
- debug ! ( "starting up cache handler" ) ;
133
-
134
- let basefee_price = Arc :: new ( AtomicU64 :: new ( 0_u64 ) ) ;
135
- let basefee_reader = Arc :: clone ( & basefee_price) ;
136
- let fut = self . basefee_updater_fut ( basefee_price) ;
137
-
138
- // Update the basefee on a per-block cadence
139
- let basefee_jh = tokio:: spawn ( fut) ;
140
-
141
- // Update the sim cache whenever a transaction or bundle is received with respect to the basefee
142
- let cache_jh = tokio:: spawn ( async move {
143
- cache_updater ( tx_receiver, bundle_receiver, cache, basefee_reader) . await
144
- } ) ;
145
-
146
- ( basefee_jh, cache_jh)
147
- }
148
-
149
- /// Periodically updates the shared basefee by querying the latest block.
150
- ///
151
- /// This function calculates the remaining time until the next slot,
152
- /// sleeps until that time, and then retrieves the latest basefee from the rollup provider.
153
- /// The updated basefee is stored in the provided `AtomicU64`.
154
- ///
155
- /// This function runs continuously.
156
- ///
157
- /// # Arguments
158
- ///
159
- /// - `price`: A shared `Arc<AtomicU64>` used to store the updated basefee value.
160
- fn basefee_updater_fut( & self , price : Arc < AtomicU64 > ) -> impl Future < Output = ( ) > + use<> {
161
- let slot_calculator = self . slot_calculator ;
162
- let ru_provider = self . ru_provider . clone ( ) ;
163
-
164
- async move {
165
- debug ! ( "starting basefee updater" ) ;
166
- loop {
167
- // calculate start of next slot plus a small buffer
168
- let time_remaining = slot_calculator. slot_duration ( )
169
- - slot_calculator. current_timepoint_within_slot ( )
170
- + 1 ;
171
- debug ! ( time_remaining = ?time_remaining, "basefee updater sleeping until next slot" ) ;
172
-
173
- // wait until that point in time
174
- sleep ( Duration :: from_secs ( time_remaining) ) . await ;
175
-
176
- // update the basefee with that price
177
- let resp = ru_provider. get_block_by_number ( Latest ) . await . inspect_err ( |e| {
178
- error ! ( error = %e, "RPC error during basefee update" ) ;
179
- } ) ;
180
-
181
- if let Ok ( Some ( block) ) = resp {
182
- let basefee = block. header . base_fee_per_gas . unwrap_or ( 0 ) ;
183
- price. store ( basefee, Ordering :: Relaxed ) ;
184
- debug ! ( basefee = basefee, "basefee updated" ) ;
185
- } else {
186
- warn ! ( "get basefee failed - an error likely occurred" ) ;
187
- }
188
- }
189
- }
190
- }
191
-
192
102
/// Spawns the simulator task, which handles the setup and sets the deadline
193
103
/// for the each round of simulation.
194
104
///
@@ -269,7 +179,7 @@ impl Simulator {
269
179
let now = SystemTime :: now ( ) ;
270
180
let unix_seconds = now. duration_since ( UNIX_EPOCH ) . expect ( "Time went backwards" ) . as_secs ( ) ;
271
181
// Calculate the time remaining in the current slot
272
- let remaining = self . slot_calculator . calculate_timepoint_within_slot ( unix_seconds) ;
182
+ let remaining = self . slot_calculator ( ) . calculate_timepoint_within_slot ( unix_seconds) ;
273
183
// Deadline is equal to the start of the next slot plus the time remaining in this slot
274
184
Instant :: now ( ) + Duration :: from_secs ( remaining)
275
185
}
@@ -365,41 +275,3 @@ impl Simulator {
365
275
Ok ( block. header . base_fee_per_gas )
366
276
}
367
277
}
368
-
369
- /// Continuously updates the simulation cache with incoming transactions and bundles.
370
- ///
371
- /// This function listens for new transactions and bundles on their respective
372
- /// channels and adds them to the simulation cache using the latest observed basefee.
373
- ///
374
- /// # Arguments
375
- ///
376
- /// - `tx_receiver`: A receiver channel for incoming Ethereum transactions.
377
- /// - `bundle_receiver`: A receiver channel for incoming transaction bundles.
378
- /// - `cache`: The simulation cache used to store transactions and bundles.
379
- /// - `price_reader`: An `Arc<AtomicU64>` providing the latest basefee for simulation pricing.
380
- async fn cache_updater (
381
- mut tx_receiver : mpsc:: UnboundedReceiver <
382
- alloy:: consensus:: EthereumTxEnvelope < alloy:: consensus:: TxEip4844Variant > ,
383
- > ,
384
- mut bundle_receiver : mpsc:: UnboundedReceiver < Bundle > ,
385
- cache : SimCache ,
386
- price_reader : Arc < AtomicU64 > ,
387
- ) -> ! {
388
- loop {
389
- let p = price_reader. load ( Ordering :: Relaxed ) ;
390
- select ! {
391
- maybe_tx = tx_receiver. recv( ) => {
392
- if let Some ( tx) = maybe_tx {
393
- debug!( tx = ?tx. hash( ) , "received transaction" ) ;
394
- cache. add_item( tx, p) ;
395
- }
396
- }
397
- maybe_bundle = bundle_receiver. recv( ) => {
398
- if let Some ( bundle) = maybe_bundle {
399
- debug!( bundle = ?bundle. id, "received bundle" ) ;
400
- cache. add_item( bundle. bundle, p) ;
401
- }
402
- }
403
- }
404
- }
405
- }
0 commit comments