Skip to content

Commit 42a66e2

Browse files
committed
feat: cache task
1 parent 33a9d37 commit 42a66e2

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
biased;
36+
res = self.env.changed() => {
37+
if res.is_err() {
38+
debug!("Cache task: env channel closed, exiting");
39+
break;
40+
}
41+
if let Some(env) = self.env.borrow_and_update().as_ref() {
42+
basefee = env.basefee;
43+
info!(basefee, number = env.number, timestamp = env.timestamp, "block env changed, clearing cache");
44+
self.cache.clean(
45+
env.number, env.timestamp
46+
);
47+
}
48+
}
49+
Some(bundle) = self.bundles.recv() => {
50+
self.cache.add_item(bundle.bundle, basefee);
51+
}
52+
Some(txn) = self.txns.recv() => {
53+
self.cache.add_item(txn, basefee);
54+
}
55+
}
56+
}
57+
}
58+
59+
/// Spawn the cache task.
60+
pub fn spawn(self) -> JoinHandle<()> {
61+
let fut = self.task_future();
62+
tokio::spawn(fut)
63+
}
64+
}

src/tasks/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/// Block creation task
2+
pub mod block;
3+
4+
/// Cache ingestion task
5+
pub mod cache;
6+
17
/// Bundle poller task
28
pub mod bundler;
39

@@ -13,8 +19,5 @@ pub mod submit;
1319
/// Tx polling task
1420
pub mod tx_poller;
1521

16-
/// Block simulation and environment
17-
pub mod block;
18-
1922
/// Constructs the simualtion environment.
2023
pub mod env;

0 commit comments

Comments
 (0)