@@ -5,7 +5,7 @@ use eyre::Error;
5
5
use init4_bin_base:: deps:: tracing:: { Instrument , debug, debug_span, trace} ;
6
6
use reqwest:: { Client , Url } ;
7
7
use serde:: { Deserialize , Serialize } ;
8
- use serde_json :: from_slice ;
8
+ use std :: time :: Duration ;
9
9
use tokio:: { sync:: mpsc, task:: JoinHandle , time} ;
10
10
11
11
/// Models a response from the transaction pool.
@@ -40,12 +40,22 @@ impl TxPoller {
40
40
Self { config : config. clone ( ) , client : Client :: new ( ) , poll_interval_ms }
41
41
}
42
42
43
+ /// Returns the poll duration as a [`Duration`].
44
+ const fn poll_duration ( & self ) -> Duration {
45
+ Duration :: from_millis ( self . poll_interval_ms )
46
+ }
47
+
43
48
/// Polls the transaction cache for transactions.
44
49
pub async fn check_tx_cache ( & mut self ) -> Result < Vec < TxEnvelope > , Error > {
45
50
let url: Url = Url :: parse ( & self . config . tx_pool_url ) ?. join ( "transactions" ) ?;
46
- let result = self . client . get ( url) . send ( ) . await ?;
47
- let response: TxPoolResponse = from_slice ( result. text ( ) . await ?. as_bytes ( ) ) ?;
48
- Ok ( response. transactions )
51
+ self . client
52
+ . get ( url)
53
+ . send ( )
54
+ . await ?
55
+ . json ( )
56
+ . await
57
+ . map ( |resp : TxPoolResponse | resp. transactions )
58
+ . map_err ( Into :: into)
49
59
}
50
60
51
61
async fn task_future ( mut self , outbound : mpsc:: UnboundedSender < TxEnvelope > ) {
@@ -64,25 +74,23 @@ impl TxPoller {
64
74
// exit the span after the check.
65
75
drop ( _guard) ;
66
76
67
- match self . check_tx_cache ( ) . instrument ( span. clone ( ) ) . await {
68
- Ok ( transactions) => {
69
- let _guard = span. entered ( ) ;
70
- debug ! ( count = ?transactions. len( ) , "found transactions" ) ;
71
- for tx in transactions. into_iter ( ) {
72
- if outbound. send ( tx) . is_err ( ) {
73
- // If there are no receivers, we can shut down
74
- trace ! ( "No receivers left, shutting down" ) ;
75
- break ;
76
- }
77
+ if let Ok ( transactions) =
78
+ self . check_tx_cache ( ) . instrument ( span. clone ( ) ) . await . inspect_err ( |err| {
79
+ debug ! ( %err, "Error fetching transactions" ) ;
80
+ } )
81
+ {
82
+ let _guard = span. entered ( ) ;
83
+ debug ! ( count = ?transactions. len( ) , "found transactions" ) ;
84
+ for tx in transactions. into_iter ( ) {
85
+ if outbound. send ( tx) . is_err ( ) {
86
+ // If there are no receivers, we can shut down
87
+ trace ! ( "No receivers left, shutting down" ) ;
88
+ break ;
77
89
}
78
90
}
79
- // If fetching was an error, we log and continue. We expect
80
- // these to be transient network issues.
81
- Err ( e) => {
82
- debug ! ( error = %e, "Error fetching transactions" ) ;
83
- }
84
91
}
85
- time:: sleep ( time:: Duration :: from_millis ( self . poll_interval_ms ) ) . await ;
92
+
93
+ time:: sleep ( self . poll_duration ( ) ) . await ;
86
94
}
87
95
}
88
96
0 commit comments