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 } ;
10
5
use serde_json:: from_slice;
11
6
12
7
pub use crate :: config:: BuilderConfig ;
13
8
14
- use metrics:: counter;
15
-
16
9
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
17
10
pub struct TxPoolResponse {
18
11
transactions : Vec < TxEnvelope > ,
@@ -24,63 +17,21 @@ pub struct TxPoller {
24
17
pub config : BuilderConfig ,
25
18
// Reqwest client for fetching transactions from the tx-pool
26
19
pub client : Client ,
27
- // Maintain a set of transaction hashes to their expiration times
28
- pub seen_txns : HashMap < TxHash , time:: Instant > ,
29
20
}
30
21
31
22
/// TxPoller implements a poller that fetches unique transactions from the transaction pool.
32
23
impl TxPoller {
33
24
/// returns a new TxPoller with the given config.
34
25
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 ( ) }
36
27
}
37
28
38
29
/// polls the tx-pool for unique transactions and evicts expired transactions.
39
30
/// unique transactions that haven't been seen before are sent into the builder pipeline.
40
31
pub async fn check_tx_cache ( & mut self ) -> Result < Vec < TxEnvelope > , Error > {
41
- let mut unique: Vec < TxEnvelope > = Vec :: new ( ) ;
42
-
43
32
let url: Url = Url :: parse ( & self . config . tx_pool_url ) ?. join ( "transactions" ) ?;
44
33
let result = self . client . get ( url) . send ( ) . await ?;
45
34
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 )
85
36
}
86
37
}
0 commit comments