Skip to content

Commit d3d3f0c

Browse files
committed
refactor: cache system
1 parent 9c6c948 commit d3d3f0c

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

bin/builder.rs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
use builder::{
22
config::BuilderConfig,
33
service::serve_builder,
4-
tasks::{
5-
block::sim::Simulator,
6-
cache::{BundlePoller, CacheTask, TxPoller},
7-
metrics::MetricsTask,
8-
submit::SubmitTask,
9-
},
4+
tasks::{block::sim::Simulator, metrics::MetricsTask, submit::SubmitTask},
105
};
116
use init4_bin_base::{
127
deps::tracing::{info, info_span},
@@ -26,12 +21,12 @@ async fn main() -> eyre::Result<()> {
2621
let config = BuilderConfig::from_env()?.clone();
2722
let constants = SignetSystemConstants::pecorino();
2823

29-
// Initialize the oauth token
30-
let token = config.oauth_token();
31-
32-
// Set up the EnvTask
24+
// Spawn the EnvTask
3325
let env_task = config.env_task();
34-
let (env_watcher, env_jh) = env_task.spawn();
26+
let (block_env, env_jh) = env_task.spawn();
27+
28+
// Spawn the cache system
29+
let cache_system = config.spawn_cache_system(block_env);
3530

3631
// Prep providers and contracts
3732
let (host_provider, quincey) =
@@ -47,24 +42,12 @@ async fn main() -> eyre::Result<()> {
4742
let submit =
4843
SubmitTask { zenith, quincey, config: config.clone(), outbound_tx_channel: tx_channel };
4944

50-
// Tx Poller pulls transactions from the cache
51-
let tx_poller = TxPoller::new(&config);
52-
let (tx_receiver, tx_poller_jh) = tx_poller.spawn();
53-
54-
// Bundle Poller pulls bundles from the cache
55-
let bundle_poller = BundlePoller::new(&config, token);
56-
let (bundle_receiver, bundle_poller_jh) = bundle_poller.spawn();
57-
58-
// Set up the cache task
59-
let cache_task = CacheTask::new(env_watcher.clone(), bundle_receiver, tx_receiver);
60-
let (sim_cache, cache_jh) = cache_task.spawn();
61-
6245
// Set up tx submission
6346
let (submit_channel, submit_jh) = submit.spawn();
6447

6548
// Set up the simulator
6649
let sim = Simulator::new(&config, ru_provider.clone());
67-
let build_jh = sim.spawn_simulator_task(constants, sim_cache, submit_channel);
50+
let build_jh = sim.spawn_simulator_task(constants, cache_system.sim_cache, submit_channel);
6851

6952
// Start the healthcheck server
7053
let server = serve_builder(([0, 0, 0, 0], config.builder_port));
@@ -74,16 +57,17 @@ async fn main() -> eyre::Result<()> {
7457
drop(init_span_guard);
7558

7659
select! {
77-
_ = cache_jh => {
78-
info!("cache task finished");
79-
},
60+
8061
_ = env_jh => {
8162
info!("env task finished");
8263
},
83-
_ = tx_poller_jh => {
64+
_ = cache_system.cache_task => {
65+
info!("cache task finished");
66+
},
67+
_ = cache_system.tx_poller => {
8468
info!("tx_poller finished");
8569
},
86-
_ = bundle_poller_jh => {
70+
_ = cache_system.bundle_poller => {
8771
info!("bundle_poller finished");
8872
},
8973
_ = submit_jh => {

src/config.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
quincey::Quincey,
33
signer::{LocalOrAws, SignerError},
44
tasks::{
5+
cache::{BundlePoller, CacheSystem, CacheTask, TxPoller},
56
env::EnvTask,
67
oauth::{Authenticator, SharedToken},
78
},
@@ -22,6 +23,8 @@ use init4_bin_base::utils::{calc::SlotCalculator, from_env::FromEnv};
2223
use oauth2::url;
2324
use signet_zenith::Zenith;
2425
use std::borrow::Cow;
26+
use tokio::sync::watch;
27+
use trevm::revm::context::BlockEnv;
2528

2629
/// Type alias for the provider used to simulate against rollup state.
2730
pub type RuProvider = RootProvider<Ethereum>;
@@ -254,4 +257,25 @@ impl BuilderConfig {
254257
let provider = self.connect_ru_provider();
255258
EnvTask::new(self.clone(), provider)
256259
}
260+
261+
/// Spawn a new [`CacheSystem`] using this config. This contains the
262+
/// joinhandles for [`TxPoller`] and [`BundlePoller`] and [`CacheTask`], as
263+
/// well as the [`SimCache`] and the block env watcher.
264+
///
265+
/// [`SimCache`]: signet_sim::SimCache
266+
pub fn spawn_cache_system(&self, block_env: watch::Receiver<Option<BlockEnv>>) -> CacheSystem {
267+
// Tx Poller pulls transactions from the cache
268+
let tx_poller = TxPoller::new(self);
269+
let (tx_receiver, tx_poller) = tx_poller.spawn();
270+
271+
// Bundle Poller pulls bundles from the cache
272+
let bundle_poller = BundlePoller::new(self, self.oauth_token());
273+
let (bundle_receiver, bundle_poller) = bundle_poller.spawn();
274+
275+
// Set up the cache task
276+
let cache_task = CacheTask::new(block_env.clone(), bundle_receiver, tx_receiver);
277+
let (sim_cache, cache_task) = cache_task.spawn();
278+
279+
CacheSystem { cache_task, tx_poller, bundle_poller, sim_cache }
280+
}
257281
}

src/tasks/cache/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,22 @@ pub use tx::TxPoller;
66

77
mod bundle;
88
pub use bundle::BundlePoller;
9+
10+
use signet_sim::SimCache;
11+
use tokio::task::JoinHandle;
12+
13+
/// Cache tasks for the block builder.
14+
#[derive(Debug)]
15+
pub struct CacheSystem {
16+
/// The cache task.
17+
pub cache_task: JoinHandle<()>,
18+
19+
/// The transaction poller task.
20+
pub tx_poller: JoinHandle<()>,
21+
22+
/// The bundle poller task.
23+
pub bundle_poller: JoinHandle<()>,
24+
25+
/// The sim cache.
26+
pub sim_cache: SimCache,
27+
}

0 commit comments

Comments
 (0)