@@ -2,7 +2,11 @@ use crate::{
22 config:: { BuilderConfig , WalletlessProvider } ,
33 tasks:: bundler:: Bundle ,
44} ;
5- use alloy:: { consensus:: TxEnvelope , eips:: BlockId , providers:: Provider } ;
5+ use alloy:: {
6+ consensus:: TxEnvelope ,
7+ eips:: { BlockId , BlockNumberOrTag :: Latest } ,
8+ providers:: Provider ,
9+ } ;
610use signet_sim:: { BlockBuild , BuiltBlock , SimCache , SimItem } ;
711use signet_types:: { SlotCalculator , config:: SignetSystemConstants } ;
812use std:: {
@@ -11,8 +15,12 @@ use std::{
1115} ;
1216use tokio:: {
1317 select,
14- sync:: mpsc:: { self } ,
18+ sync:: {
19+ RwLock ,
20+ mpsc:: { self } ,
21+ } ,
1522 task:: JoinHandle ,
23+ time:: sleep,
1624} ;
1725use trevm:: {
1826 NoopBlock ,
@@ -92,42 +100,83 @@ impl Simulator {
92100 Ok ( block)
93101 }
94102
95- /// Spawns a task to handle incoming transactions and bundles, adding them
96- /// to the simulation cache.
103+ /// Spawns two tasks: one to handle incoming transactions and bundles,
104+ /// adding them to the simulation cache, and one to track the latest basefee .
97105 ///
98106 /// # Arguments
99107 /// - `tx_receiver`: A channel receiver for incoming transactions.
100108 /// - `bundle_receiver`: A channel receiver for incoming bundles.
101109 /// - `cache`: The simulation cache to store the received items.
102110 ///
103111 /// # Returns
104- /// A `JoinHandle` for the spawned task.
105- pub fn spawn_cache_handler (
112+ /// A `JoinHandle` for the basefee updater and a `JoinHandle` for the
113+ /// cache handler.
114+ pub fn spawn_cache_task (
106115 self : Arc < Self > ,
107116 mut tx_receiver : mpsc:: UnboundedReceiver < TxEnvelope > ,
108117 mut bundle_receiver : mpsc:: UnboundedReceiver < Bundle > ,
109118 cache : SimCache ,
110- ) -> JoinHandle < ( ) > {
119+ ) -> ( JoinHandle < ( ) > , JoinHandle < ( ) > ) {
111120 tracing:: debug!( "starting up cache handler" ) ;
112121
113- tokio:: spawn ( async move {
122+ let shared_price = Arc :: new ( RwLock :: new ( 0_u64 ) ) ;
123+ let price_updater = Arc :: clone ( & shared_price) ;
124+ let price_reader = Arc :: clone ( & shared_price) ;
125+
126+ // Update the basefee on a per-block cadence
127+ let basefee_jh = tokio:: spawn ( async move {
128+ loop {
129+ // calculate next slot position plus a small buffer to
130+ let time_remaining = self . slot_calculator . slot_duration ( )
131+ - self . slot_calculator . current_timepoint_within_slot ( )
132+ + 1 ;
133+ tracing:: debug!( time_remaining = ?time_remaining, "sleeping until next slot" ) ;
134+
135+ // wait until that point in time
136+ sleep ( Duration :: from_secs ( time_remaining) ) . await ;
137+
138+ // update the basefee with that price
139+ tracing:: debug!( "checking latest basefee" ) ;
140+ let resp = self . ru_provider . get_block_by_number ( Latest ) . await ;
141+
142+ if let Ok ( maybe_block) = resp {
143+ match maybe_block {
144+ Some ( block) => {
145+ let basefee = block. header . base_fee_per_gas . unwrap_or_default ( ) ;
146+ let mut w = price_updater. write ( ) . await ;
147+ * w = basefee;
148+ tracing:: debug!( basefee = %basefee, "basefee updated" ) ;
149+ }
150+ None => {
151+ tracing:: debug!( "no block found; basefee not updated." )
152+ }
153+ }
154+ }
155+ }
156+ } ) ;
157+
158+ // Update the sim cache whenever a transaction or bundle is received with respect to the basefee
159+ let cache_jh = tokio:: spawn ( async move {
114160 loop {
161+ let p = price_reader. read ( ) . await ;
115162 select ! {
116163 maybe_tx = tx_receiver. recv( ) => {
117164 if let Some ( tx) = maybe_tx {
118165 tracing:: debug!( tx = ?tx. hash( ) , "received transaction" ) ;
119- cache. add_item( SimItem :: Tx ( tx) ) ;
166+ cache. add_item( SimItem :: Tx ( tx) , * p ) ;
120167 }
121168 }
122169 maybe_bundle = bundle_receiver. recv( ) => {
123170 if let Some ( bundle) = maybe_bundle {
124171 tracing:: debug!( bundle = ?bundle. id, "received bundle" ) ;
125- cache. add_item( SimItem :: Bundle ( bundle. bundle) ) ;
172+ cache. add_item( SimItem :: Bundle ( bundle. bundle) , * p ) ;
126173 }
127174 }
128175 }
129176 }
130- } )
177+ } ) ;
178+
179+ ( basefee_jh, cache_jh)
131180 }
132181
133182 /// Spawns the simulator task, which handles the setup and sets the deadline
0 commit comments