1
- use std:: time:: Duration ;
2
- use std:: { collections:: HashMap , time} ;
3
-
4
1
use alloy:: consensus:: TxEnvelope ;
5
- use alloy_primitives:: TxHash ;
6
-
7
2
use eyre:: Error ;
8
3
use reqwest:: { Client , Url } ;
9
4
use serde:: { Deserialize , Serialize } ;
@@ -22,61 +17,21 @@ pub struct TxPoller {
22
17
pub config : BuilderConfig ,
23
18
// Reqwest client for fetching transactions from the tx-pool
24
19
pub client : Client ,
25
- // Maintain a set of transaction hashes to their expiration times
26
- pub seen_txns : HashMap < TxHash , time:: Instant > ,
27
20
}
28
21
29
22
/// TxPoller implements a poller that fetches unique transactions from the transaction pool.
30
23
impl TxPoller {
31
24
/// returns a new TxPoller with the given config.
32
25
pub fn new ( config : & BuilderConfig ) -> Self {
33
- Self { config : config. clone ( ) , client : Client :: new ( ) , seen_txns : HashMap :: new ( ) }
26
+ Self { config : config. clone ( ) , client : Client :: new ( ) }
34
27
}
35
28
36
29
/// polls the tx-pool for unique transactions and evicts expired transactions.
37
30
/// unique transactions that haven't been seen before are sent into the builder pipeline.
38
31
pub async fn check_tx_cache ( & mut self ) -> Result < Vec < TxEnvelope > , Error > {
39
- let mut unique: Vec < TxEnvelope > = Vec :: new ( ) ;
40
-
41
32
let url: Url = Url :: parse ( & self . config . tx_pool_url ) ?. join ( "transactions" ) ?;
42
33
let result = self . client . get ( url) . send ( ) . await ?;
43
34
let response: TxPoolResponse = from_slice ( result. text ( ) . await ?. as_bytes ( ) ) ?;
44
-
45
- response. transactions . iter ( ) . for_each ( |entry| {
46
- self . check_seen_txs ( entry. clone ( ) , & mut unique) ;
47
- } ) ;
48
-
49
- Ok ( unique)
50
- }
51
-
52
- /// checks if the transaction has been seen before and if not, adds it to the unique transactions list.
53
- fn check_seen_txs ( & mut self , tx : TxEnvelope , unique : & mut Vec < TxEnvelope > ) {
54
- self . seen_txns . entry ( * tx. tx_hash ( ) ) . or_insert_with ( || {
55
- // add to unique transactions
56
- unique. push ( tx. clone ( ) ) ;
57
- // expiry is now + cache_duration
58
- time:: Instant :: now ( ) + Duration :: from_secs ( self . config . tx_pool_cache_duration )
59
- } ) ;
60
- }
61
-
62
- /// removes entries from seen_txns that have lived past expiry
63
- pub fn evict ( & mut self ) {
64
- let expired_keys: Vec < TxHash > = self
65
- . seen_txns
66
- . iter ( )
67
- . filter_map (
68
- |( key, & expiration) | {
69
- if !expiration. elapsed ( ) . is_zero ( ) {
70
- Some ( * key)
71
- } else {
72
- None
73
- }
74
- } ,
75
- )
76
- . collect ( ) ;
77
-
78
- for key in expired_keys {
79
- self . seen_txns . remove ( & key) ;
80
- }
35
+ Ok ( response. transactions )
81
36
}
82
37
}
0 commit comments