Skip to content

Commit 59ba745

Browse files
authored
fix: remove naive seen_txns cache (#32)
1 parent 32a6998 commit 59ba745

File tree

2 files changed

+2
-52
lines changed

2 files changed

+2
-52
lines changed

src/tasks/block.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ impl BlockBuilder {
153153
tracing::error!(error = %e, "error polling transactions");
154154
}
155155
}
156-
self.tx_poller.evict();
157156
}
158157

159158
async fn _get_bundles(&mut self, in_progress: &mut InProgressBlock) {

src/tasks/tx_poller.rs

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
use std::time::Duration;
2-
use std::{collections::HashMap, time};
3-
41
use alloy::consensus::TxEnvelope;
5-
use alloy_primitives::TxHash;
6-
72
use eyre::Error;
83
use reqwest::{Client, Url};
94
use serde::{Deserialize, Serialize};
105
use serde_json::from_slice;
116

127
pub use crate::config::BuilderConfig;
138

14-
use metrics::counter;
15-
169
#[derive(Debug, Clone, Serialize, Deserialize)]
1710
pub struct TxPoolResponse {
1811
transactions: Vec<TxEnvelope>,
@@ -24,63 +17,21 @@ pub struct TxPoller {
2417
pub config: BuilderConfig,
2518
// Reqwest client for fetching transactions from the tx-pool
2619
pub client: Client,
27-
// Maintain a set of transaction hashes to their expiration times
28-
pub seen_txns: HashMap<TxHash, time::Instant>,
2920
}
3021

3122
/// TxPoller implements a poller that fetches unique transactions from the transaction pool.
3223
impl TxPoller {
3324
/// returns a new TxPoller with the given config.
3425
pub fn new(config: &BuilderConfig) -> Self {
35-
Self { config: config.clone(), client: Client::new(), seen_txns: HashMap::new() }
26+
Self { config: config.clone(), client: Client::new() }
3627
}
3728

3829
/// polls the tx-pool for unique transactions and evicts expired transactions.
3930
/// unique transactions that haven't been seen before are sent into the builder pipeline.
4031
pub async fn check_tx_cache(&mut self) -> Result<Vec<TxEnvelope>, Error> {
41-
let mut unique: Vec<TxEnvelope> = Vec::new();
42-
4332
let url: Url = Url::parse(&self.config.tx_pool_url)?.join("transactions")?;
4433
let result = self.client.get(url).send().await?;
4534
let response: TxPoolResponse = from_slice(result.text().await?.as_bytes())?;
46-
47-
response.transactions.iter().for_each(|entry| {
48-
self.check_seen_txs(entry.clone(), &mut unique);
49-
});
50-
51-
Ok(unique)
52-
}
53-
54-
/// checks if the transaction has been seen before and if not, adds it to the unique transactions list.
55-
fn check_seen_txs(&mut self, tx: TxEnvelope, unique: &mut Vec<TxEnvelope>) {
56-
self.seen_txns.entry(*tx.tx_hash()).or_insert_with(|| {
57-
// add to unique transactions
58-
unique.push(tx.clone());
59-
counter!("builder.unique_txs").increment(1);
60-
// expiry is now + cache_duration
61-
time::Instant::now() + Duration::from_secs(self.config.tx_pool_cache_duration)
62-
});
63-
}
64-
65-
/// removes entries from seen_txns that have lived past expiry
66-
pub fn evict(&mut self) {
67-
let expired_keys: Vec<TxHash> = self
68-
.seen_txns
69-
.iter()
70-
.filter_map(
71-
|(key, &expiration)| {
72-
if !expiration.elapsed().is_zero() {
73-
Some(*key)
74-
} else {
75-
None
76-
}
77-
},
78-
)
79-
.collect();
80-
81-
for key in expired_keys {
82-
self.seen_txns.remove(&key);
83-
counter!("builder.evicted_txs").increment(1);
84-
}
35+
Ok(response.transactions)
8536
}
8637
}

0 commit comments

Comments
 (0)