Skip to content

Commit 5c564fb

Browse files
authored
chore: remove unnecessary arc (#91)
* refactor: continuing to clean * lint: fmt * lint: clippy
1 parent a166b86 commit 5c564fb

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed

bin/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async fn main() -> eyre::Result<()> {
4747
let sim = Arc::new(Simulator::new(&config, ru_provider.clone(), slot_calculator));
4848

4949
let (basefee_jh, sim_cache_jh) =
50-
sim.clone().spawn_cache_tasks(tx_receiver, bundle_receiver, sim_items.clone());
50+
sim.spawn_cache_tasks(tx_receiver, bundle_receiver, sim_items.clone());
5151

5252
let build_jh = sim.clone().spawn_simulator_task(constants, sim_items.clone(), submit_channel);
5353

src/tasks/block.rs

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl Simulator {
130130
/// A `JoinHandle` for the basefee updater and a `JoinHandle` for the
131131
/// cache handler.
132132
pub fn spawn_cache_tasks(
133-
self: Arc<Self>,
133+
&self,
134134
tx_receiver: mpsc::UnboundedReceiver<TxEnvelope>,
135135
bundle_receiver: mpsc::UnboundedReceiver<Bundle>,
136136
cache: SimCache,
@@ -139,9 +139,10 @@ impl Simulator {
139139

140140
let basefee_price = Arc::new(AtomicU64::new(0_u64));
141141
let basefee_reader = Arc::clone(&basefee_price);
142+
let fut = self.basefee_updater_fut(basefee_price);
142143

143144
// 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);
145146

146147
// Update the sim cache whenever a transaction or bundle is received with respect to the basefee
147148
let cache_jh = tokio::spawn(async move {
@@ -162,45 +163,35 @@ impl Simulator {
162163
/// # Arguments
163164
///
164165
/// - `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+
}
204195
}
205196
}
206197

0 commit comments

Comments
 (0)