Skip to content

Commit 9e1bc74

Browse files
committed
adds basefee checker to sim cache handling
1 parent 4639365 commit 9e1bc74

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

bin/builder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ async fn main() -> eyre::Result<()> {
5757

5858
let sim = Arc::new(Simulator::new(&config, ru_provider.clone(), slot_calculator));
5959

60-
let sim_cache_jh =
61-
sim.clone().spawn_cache_handler(tx_receiver, bundle_receiver, sim_items.clone());
60+
let (basefee_jh, sim_cache_jh) =
61+
sim.clone().spawn_cache_task(tx_receiver, bundle_receiver, sim_items.clone());
6262

6363
let build_jh = sim.clone().spawn_simulator_task(constants, sim_items.clone(), submit_channel);
6464

@@ -75,6 +75,9 @@ async fn main() -> eyre::Result<()> {
7575
_ = sim_cache_jh => {
7676
tracing::info!("sim cache task finished");
7777
}
78+
_ = basefee_jh => {
79+
tracing::info!("basefee task finished");
80+
}
7881
_ = submit_jh => {
7982
tracing::info!("submit finished");
8083
},

src/tasks/block.rs

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
};
610
use signet_sim::{BlockBuild, BuiltBlock, SimCache, SimItem};
711
use signet_types::{SlotCalculator, config::SignetSystemConstants};
812
use std::{
@@ -11,8 +15,12 @@ use std::{
1115
};
1216
use tokio::{
1317
select,
14-
sync::mpsc::{self},
18+
sync::{
19+
RwLock,
20+
mpsc::{self},
21+
},
1522
task::JoinHandle,
23+
time::sleep,
1624
};
1725
use 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

tests/block_builder_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ mod tests {
5757

5858
// Add two transactions from two senders to the sim cache
5959
let tx_1 = new_signed_tx(&test_key_0, 0, U256::from(1_f64), 11_000).unwrap();
60-
sim_items.add_item(SimItem::Tx(tx_1));
60+
sim_items.add_item(SimItem::Tx(tx_1), 0);
6161

6262
let tx_2 = new_signed_tx(&test_key_1, 0, U256::from(2_f64), 10_000).unwrap();
63-
sim_items.add_item(SimItem::Tx(tx_2));
63+
sim_items.add_item(SimItem::Tx(tx_2), 0);
6464

6565
let finish_by = Instant::now() + Duration::from_secs(2);
6666

@@ -113,7 +113,7 @@ mod tests {
113113
let sim_cache = SimCache::new();
114114

115115
// Create a sim cache and start filling it with items
116-
sim.clone().spawn_cache_handler(tx_receiver, bundle_receiver, sim_cache.clone());
116+
sim.clone().spawn_cache_task(tx_receiver, bundle_receiver, sim_cache.clone());
117117

118118
// Finally, Kick off the block builder task.
119119
sim.clone().spawn_simulator_task(constants, sim_cache.clone(), block_sender);

0 commit comments

Comments
 (0)