Skip to content

Commit 1d10cb6

Browse files
authored
chore(cache): move cache system out of config (#118)
There's no reason for this to be contained here, and feels wrong when every other task has its own file / spawn pattern.
1 parent 7dbea81 commit 1d10cb6

File tree

5 files changed

+84
-50
lines changed

5 files changed

+84
-50
lines changed

bin/builder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use builder::{
22
config::BuilderConfig,
33
service::serve_builder,
4-
tasks::{block::sim::Simulator, metrics::MetricsTask, submit::SubmitTask},
4+
tasks::{block::sim::Simulator, cache::CacheTasks, metrics::MetricsTask, submit::SubmitTask},
55
};
66
use init4_bin_base::{
77
deps::tracing::{info, info_span},
@@ -26,7 +26,8 @@ async fn main() -> eyre::Result<()> {
2626
let (block_env, env_jh) = env_task.spawn();
2727

2828
// Spawn the cache system
29-
let cache_system = config.spawn_cache_system(block_env.clone());
29+
let cache_tasks = CacheTasks::new(config.clone(), block_env.clone());
30+
let cache_system = cache_tasks.spawn();
3031

3132
// Prep providers and contracts
3233
let (host_provider, quincey) =

src/config.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use crate::{
22
quincey::Quincey,
3-
tasks::{
4-
block::cfg::SignetCfgEnv,
5-
cache::{BundlePoller, CacheSystem, CacheTask, TxPoller},
6-
env::{EnvTask, SimEnv},
7-
},
3+
tasks::{block::cfg::SignetCfgEnv, env::EnvTask},
84
};
95
use alloy::{
106
network::{Ethereum, EthereumWallet},
@@ -28,7 +24,6 @@ use init4_bin_base::{
2824
};
2925
use signet_zenith::Zenith;
3026
use std::borrow::Cow;
31-
use tokio::sync::watch;
3227

3328
/// Type alias for the provider used to simulate against rollup state.
3429
pub type RuProvider = RootProvider<Ethereum>;
@@ -255,27 +250,6 @@ impl BuilderConfig {
255250
EnvTask::new(self.clone(), ru_provider)
256251
}
257252

258-
/// Spawn a new [`CacheSystem`] using this config. This contains the
259-
/// joinhandles for [`TxPoller`] and [`BundlePoller`] and [`CacheTask`], as
260-
/// well as the [`SimCache`] and the block env watcher.
261-
///
262-
/// [`SimCache`]: signet_sim::SimCache
263-
pub fn spawn_cache_system(&self, block_env: watch::Receiver<Option<SimEnv>>) -> CacheSystem {
264-
// Tx Poller pulls transactions from the cache
265-
let tx_poller = TxPoller::new(self);
266-
let (tx_receiver, tx_poller) = tx_poller.spawn();
267-
268-
// Bundle Poller pulls bundles from the cache
269-
let bundle_poller = BundlePoller::new(self, self.oauth_token());
270-
let (bundle_receiver, bundle_poller) = bundle_poller.spawn();
271-
272-
// Set up the cache task
273-
let cache_task = CacheTask::new(block_env.clone(), bundle_receiver, tx_receiver);
274-
let (sim_cache, cache_task) = cache_task.spawn();
275-
276-
CacheSystem { cache_task, tx_poller, bundle_poller, sim_cache }
277-
}
278-
279253
/// Create a [`SignetCfgEnv`] using this config.
280254
pub const fn cfg_env(&self) -> SignetCfgEnv {
281255
SignetCfgEnv { chain_id: self.ru_chain_id }

src/tasks/cache/mod.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,5 @@ pub use tx::TxPoller;
77
mod bundle;
88
pub use bundle::BundlePoller;
99

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-
}
10+
mod system;
11+
pub use system::CacheTasks;

src/tasks/cache/system.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use signet_sim::SimCache;
2+
use tokio::{sync::watch, task::JoinHandle};
3+
4+
use crate::{
5+
config::BuilderConfig,
6+
tasks::{
7+
cache::{BundlePoller, CacheTask, TxPoller},
8+
env::SimEnv,
9+
},
10+
};
11+
12+
/// The block builder's cache system.
13+
#[derive(Debug)]
14+
pub struct CacheTasks {
15+
/// The builder config.
16+
pub config: BuilderConfig,
17+
/// The block environment receiver.
18+
pub block_env: watch::Receiver<Option<SimEnv>>,
19+
}
20+
21+
impl CacheTasks {
22+
/// Create a new [`CacheSystem`] with the given components.
23+
pub const fn new(config: BuilderConfig, block_env: watch::Receiver<Option<SimEnv>>) -> Self {
24+
Self { config, block_env }
25+
}
26+
27+
/// Spawn a new [`CacheSystem`], which starts the
28+
/// [`CacheTask`], [`TxPoller`], and [`BundlePoller`] internally and yields their [`JoinHandle`]s.
29+
pub fn spawn(&self) -> CacheSystem {
30+
// Tx Poller pulls transactions from the cache
31+
let tx_poller = TxPoller::new(&self.config);
32+
let (tx_receiver, tx_poller) = tx_poller.spawn();
33+
34+
// Bundle Poller pulls bundles from the cache
35+
let bundle_poller = BundlePoller::new(&self.config, self.config.oauth_token());
36+
let (bundle_receiver, bundle_poller) = bundle_poller.spawn();
37+
38+
// Set up the cache task
39+
let cache_task = CacheTask::new(self.block_env.clone(), bundle_receiver, tx_receiver);
40+
let (sim_cache, cache_task) = cache_task.spawn();
41+
42+
CacheSystem::new(sim_cache, tx_poller, bundle_poller, cache_task)
43+
}
44+
}
45+
46+
/// The tasks that the cache system spawns.
47+
////// This struct contains the cache and the task handles for the
48+
/// [`CacheTask`], [`TxPoller`], and [`BundlePoller`].
49+
#[derive(Debug)]
50+
pub struct CacheSystem {
51+
/// The cache for the block builder.
52+
pub sim_cache: SimCache,
53+
/// The task handle for the transaction poller.
54+
pub tx_poller: JoinHandle<()>,
55+
/// The task handle for the bundle poller.
56+
pub bundle_poller: JoinHandle<()>,
57+
/// The task handle for the cache task.
58+
pub cache_task: JoinHandle<()>,
59+
}
60+
61+
impl CacheSystem {
62+
/// Create a new [`CacheTasks`] instance.
63+
pub const fn new(
64+
sim_cache: SimCache,
65+
tx_poller: JoinHandle<()>,
66+
bundle_poller: JoinHandle<()>,
67+
cache_task: JoinHandle<()>,
68+
) -> Self {
69+
Self { sim_cache, tx_poller, bundle_poller, cache_task }
70+
}
71+
}

tests/cache.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use builder::test_utils::{setup_logging, setup_test_config};
1+
use builder::{
2+
tasks::cache::CacheTasks,
3+
test_utils::{setup_logging, setup_test_config},
4+
};
25
use init4_bin_base::deps::tracing::warn;
36
use std::time::Duration;
47

@@ -10,11 +13,12 @@ async fn test_bundle_poller_roundtrip() -> eyre::Result<()> {
1013
let config = setup_test_config().unwrap();
1114

1215
let (block_env, _jh) = config.env_task().spawn();
13-
let cache = config.spawn_cache_system(block_env);
16+
let cache_tasks = CacheTasks::new(config.clone(), block_env);
17+
let cache_system = cache_tasks.spawn();
1418

1519
tokio::time::sleep(Duration::from_secs(12)).await;
1620

17-
warn!(txns = ?cache.sim_cache.read_best(5));
21+
warn!(txns = ?cache_system.sim_cache.read_best(5));
1822

1923
Ok(())
2024
}

0 commit comments

Comments
 (0)