|
| 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 | +} |
0 commit comments