Skip to content

Commit effaf4a

Browse files
committed
feat: cache task
1 parent 7a84ca9 commit effaf4a

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ path = "bin/submit_transaction.rs"
2424
[dependencies]
2525
init4-bin-base = "0.3"
2626

27-
signet-zenith = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
28-
signet-constants = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
29-
signet-types = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
3027
signet-bundle = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
28+
signet-constants = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
3129
signet-sim = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
30+
signet-tx-cache = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
31+
signet-types = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
32+
signet-zenith = { git = "https://github.com/init4tech/signet-sdk", branch = "main" }
3233

3334
trevm = { version = "0.20.10", features = ["concurrent-db", "test-utils"] }
3435

src/tasks/cache.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use alloy::consensus::TxEnvelope;
2+
use init4_bin_base::deps::tracing::{debug, info};
3+
use signet_sim::SimCache;
4+
use signet_tx_cache::types::TxCacheBundle;
5+
use tokio::{
6+
sync::{mpsc, watch},
7+
task::JoinHandle,
8+
};
9+
use trevm::revm::context::BlockEnv;
10+
11+
/// Cache task for the block builder.
12+
///
13+
/// This tasks handles the ingestion of transactions and bundles into the cache.
14+
/// It keeps a receiver for the block environment and cleans the cache when
15+
/// the environment changes.
16+
#[derive(Debug)]
17+
pub struct CacheTask {
18+
/// The shared sim cache to populate.
19+
cache: SimCache,
20+
21+
/// The channel to receive the block environment.
22+
env: watch::Receiver<Option<BlockEnv>>,
23+
24+
/// The channel to receive the transaction bundles.
25+
bundles: mpsc::UnboundedReceiver<TxCacheBundle>,
26+
/// The channel to receive the transactions.
27+
txns: mpsc::UnboundedReceiver<TxEnvelope>,
28+
}
29+
30+
impl CacheTask {
31+
async fn task_future(mut self) {
32+
loop {
33+
let mut basefee = 0;
34+
tokio::select! {
35+
res = self.env.changed() => {
36+
if res.is_err() {
37+
debug!("Cache task: env channel closed, exiting");
38+
break;
39+
}
40+
if let Some(env) = self.env.borrow_and_update().as_ref() {
41+
basefee = env.basefee;
42+
info!(basefee, number = env.number, timestamp = env.timestamp, "block env changed, clearing cache");
43+
self.cache.clean(
44+
env.number, env.timestamp
45+
);
46+
}
47+
}
48+
Some(bundle) = self.bundles.recv() => {
49+
self.cache.add_item(bundle.bundle, basefee);
50+
}
51+
Some(txn) = self.txns.recv() => {
52+
self.cache.add_item(txn, basefee);
53+
}
54+
}
55+
}
56+
}
57+
58+
/// Spawn the cache task.
59+
pub fn spawn(self) -> JoinHandle<()> {
60+
let fut = self.task_future();
61+
tokio::spawn(fut)
62+
}
63+
}

src/tasks/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/// Block creation task
22
pub mod block;
33

4+
/// Cache ingestion task
5+
pub mod cache;
6+
47
/// Bundle poller task
58
pub mod bundler;
69

0 commit comments

Comments
 (0)