@@ -130,7 +130,7 @@ impl Simulator {
130
130
/// A `JoinHandle` for the basefee updater and a `JoinHandle` for the
131
131
/// cache handler.
132
132
pub fn spawn_cache_tasks (
133
- self : Arc < Self > ,
133
+ & self ,
134
134
tx_receiver : mpsc:: UnboundedReceiver < TxEnvelope > ,
135
135
bundle_receiver : mpsc:: UnboundedReceiver < Bundle > ,
136
136
cache : SimCache ,
@@ -139,9 +139,10 @@ impl Simulator {
139
139
140
140
let basefee_price = Arc :: new ( AtomicU64 :: new ( 0_u64 ) ) ;
141
141
let basefee_reader = Arc :: clone ( & basefee_price) ;
142
+ let fut = self . basefee_updater_fut ( basefee_price) ;
142
143
143
144
// Update the basefee on a per-block cadence
144
- let basefee_jh = tokio:: spawn ( async move { self . basefee_updater ( basefee_price ) . await } ) ;
145
+ let basefee_jh = tokio:: spawn ( fut ) ;
145
146
146
147
// Update the sim cache whenever a transaction or bundle is received with respect to the basefee
147
148
let cache_jh = tokio:: spawn ( async move {
@@ -162,45 +163,35 @@ impl Simulator {
162
163
/// # Arguments
163
164
///
164
165
/// - `price`: A shared `Arc<AtomicU64>` used to store the updated basefee value.
165
- async fn basefee_updater ( self : Arc < Self > , price : Arc < AtomicU64 > ) {
166
- debug ! ( "starting basefee updater" ) ;
167
- loop {
168
- // calculate start of next slot plus a small buffer
169
- let time_remaining = self . slot_calculator . slot_duration ( )
170
- - self . slot_calculator . current_timepoint_within_slot ( )
171
- + 1 ;
172
- debug ! ( time_remaining = ?time_remaining, "basefee updater sleeping until next slot" ) ;
173
-
174
- // wait until that point in time
175
- sleep ( Duration :: from_secs ( time_remaining) ) . await ;
176
-
177
- // update the basefee with that price
178
- self . check_basefee ( & price) . await ;
179
- }
180
- }
181
-
182
- /// Queries the latest block from the rollup provider and updates the shared
183
- /// basefee value if a block is found.
184
- ///
185
- /// This function retrieves the latest block using the provider, extracts the
186
- /// `base_fee_per_gas` field from the block header (defaulting to zero if missing),
187
- /// and updates the shared `AtomicU64` price tracker. If no block is available,
188
- /// it logs a message without updating the price.
189
- ///
190
- /// # Arguments
191
- ///
192
- /// - `price`: A shared `Arc<AtomicU64>` used to store the updated basefee.
193
- async fn check_basefee ( & self , price : & Arc < AtomicU64 > ) {
194
- let resp = self . ru_provider . get_block_by_number ( Latest ) . await . inspect_err ( |e| {
195
- error ! ( error = %e, "RPC error during basefee update" ) ;
196
- } ) ;
197
-
198
- if let Ok ( Some ( block) ) = resp {
199
- let basefee = block. header . base_fee_per_gas . unwrap_or ( 0 ) ;
200
- price. store ( basefee, Ordering :: Relaxed ) ;
201
- debug ! ( basefee = basefee, "basefee updated" ) ;
202
- } else {
203
- warn ! ( "get basefee failed - an error likely occurred" ) ;
166
+ fn basefee_updater_fut( & self , price : Arc < AtomicU64 > ) -> impl Future < Output = ( ) > + use<> {
167
+ let slot_calculator = self . slot_calculator ;
168
+ let ru_provider = self . ru_provider . clone ( ) ;
169
+
170
+ async move {
171
+ debug ! ( "starting basefee updater" ) ;
172
+ loop {
173
+ // calculate start of next slot plus a small buffer
174
+ let time_remaining = slot_calculator. slot_duration ( )
175
+ - slot_calculator. current_timepoint_within_slot ( )
176
+ + 1 ;
177
+ debug ! ( time_remaining = ?time_remaining, "basefee updater sleeping until next slot" ) ;
178
+
179
+ // wait until that point in time
180
+ sleep ( Duration :: from_secs ( time_remaining) ) . await ;
181
+
182
+ // update the basefee with that price
183
+ let resp = ru_provider. get_block_by_number ( Latest ) . await . inspect_err ( |e| {
184
+ error ! ( error = %e, "RPC error during basefee update" ) ;
185
+ } ) ;
186
+
187
+ if let Ok ( Some ( block) ) = resp {
188
+ let basefee = block. header . base_fee_per_gas . unwrap_or ( 0 ) ;
189
+ price. store ( basefee, Ordering :: Relaxed ) ;
190
+ debug ! ( basefee = basefee, "basefee updated" ) ;
191
+ } else {
192
+ warn ! ( "get basefee failed - an error likely occurred" ) ;
193
+ }
194
+ }
204
195
}
205
196
}
206
197
0 commit comments