1- //! Transaction service responsible for fetching and sending trasnsactions to the simulator.
1+ //! Transaction service responsible for fetching and sending transactions to the simulator.
22use crate :: config:: BuilderConfig ;
33use alloy:: {
44 consensus:: { Transaction , TxEnvelope , transaction:: SignerRecoverable } ,
55 providers:: Provider ,
66} ;
7- use eyre:: Error ;
8- use reqwest:: { Client , Url } ;
9- use serde:: { Deserialize , Serialize } ;
7+ use futures_util:: TryStreamExt ;
8+ use signet_tx_cache:: { TxCache , TxCacheError } ;
109use std:: time:: Duration ;
1110use tokio:: { sync:: mpsc, task:: JoinHandle , time} ;
1211use tracing:: { Instrument , debug, debug_span, trace, trace_span} ;
1312
1413/// Poll interval for the transaction poller in milliseconds.
1514const POLL_INTERVAL_MS : u64 = 1000 ;
1615
17- /// Models a response from the transaction pool.
18- #[ derive( Debug , Clone , Serialize , Deserialize ) ]
19- struct TxPoolResponse {
20- /// Holds the transactions property as a list on the response.
21- transactions : Vec < TxEnvelope > ,
22- }
23-
2416/// Implements a poller for the block builder to pull transactions from the
2517/// transaction pool.
2618#[ derive( Debug , Clone ) ]
2719pub struct TxPoller {
2820 /// Config values from the Builder.
2921 config : & ' static BuilderConfig ,
30- /// Reqwest Client for fetching transactions from the cache.
31- client : Client ,
22+ /// Client for the tx cache.
23+ tx_cache : TxCache ,
3224 /// Defines the interval at which the service should poll the cache.
3325 poll_interval_ms : u64 ,
3426}
@@ -51,7 +43,8 @@ impl TxPoller {
5143 /// Returns a new [`TxPoller`] with the given config and cache polling interval in milliseconds.
5244 pub fn new_with_poll_interval_ms ( poll_interval_ms : u64 ) -> Self {
5345 let config = crate :: config ( ) ;
54- Self { config, client : Client :: new ( ) , poll_interval_ms }
46+ let tx_cache = TxCache :: new ( config. tx_pool_url . clone ( ) ) ;
47+ Self { config, tx_cache, poll_interval_ms }
5548 }
5649
5750 /// Returns the poll duration as a [`Duration`].
@@ -98,21 +91,12 @@ impl TxPoller {
9891 } ) ;
9992 }
10093
101- /// Polls the transaction cache for transactions.
102- pub async fn check_tx_cache ( & mut self ) -> Result < Vec < TxEnvelope > , Error > {
103- let url: Url = self . config . tx_pool_url . join ( "transactions" ) ?;
104- self . client
105- . get ( url)
106- . send ( )
107- . await ?
108- . error_for_status ( ) ?
109- . json ( )
110- . await
111- . map ( |resp : TxPoolResponse | resp. transactions )
112- . map_err ( Into :: into)
94+ /// Polls the transaction cache for transactions, paginating through all available pages.
95+ pub async fn check_tx_cache ( & self ) -> Result < Vec < TxEnvelope > , TxCacheError > {
96+ self . tx_cache . stream_transactions ( ) . try_collect ( ) . await
11397 }
11498
115- async fn task_future ( mut self , outbound : mpsc:: UnboundedSender < TxEnvelope > ) {
99+ async fn task_future ( self , outbound : mpsc:: UnboundedSender < TxEnvelope > ) {
116100 loop {
117101 let span = trace_span ! ( "TxPoller::loop" , url = %self . config. tx_pool_url) ;
118102
@@ -124,12 +108,12 @@ impl TxPoller {
124108 }
125109
126110 if let Ok ( transactions) =
127- self . check_tx_cache ( ) . instrument ( span. clone ( ) ) . await . inspect_err ( |err | {
128- debug ! ( %err , "Error fetching transactions" ) ;
111+ self . check_tx_cache ( ) . instrument ( span. clone ( ) ) . await . inspect_err ( |error | {
112+ debug ! ( %error , "Error fetching transactions" ) ;
129113 } )
130114 {
131115 let _guard = span. entered ( ) ;
132- trace ! ( count = ? transactions. len( ) , "found transactions" ) ;
116+ trace ! ( count = transactions. len( ) , "found transactions" ) ;
133117 for tx in transactions. into_iter ( ) {
134118 self . spawn_check_nonce ( tx, outbound. clone ( ) ) ;
135119 }
0 commit comments