@@ -13,7 +13,7 @@ use thiserror::Error;
1313use tracing:: { error, info} ;
1414
1515#[ derive( Error , Debug , Clone ) ]
16- pub enum SafeProviderError {
16+ pub enum RobustProviderError {
1717 #[ error( "RPC error: {0}" ) ]
1818 RpcError ( Arc < RpcError < TransportErrorKind > > ) ,
1919 #[ error( "Operation timed out" ) ]
@@ -22,9 +22,9 @@ pub enum SafeProviderError {
2222 RetryFail ( usize ) ,
2323}
2424
25- impl From < RpcError < TransportErrorKind > > for SafeProviderError {
25+ impl From < RpcError < TransportErrorKind > > for RobustProviderError {
2626 fn from ( err : RpcError < TransportErrorKind > ) -> Self {
27- SafeProviderError :: RpcError ( Arc :: new ( err) )
27+ RobustProviderError :: RpcError ( Arc :: new ( err) )
2828 }
2929}
3030
@@ -33,23 +33,23 @@ impl From<RpcError<TransportErrorKind>> for SafeProviderError {
3333/// This wrapper around Alloy providers automatically handles retries,
3434/// timeouts, and error logging for RPC calls.
3535#[ derive( Clone ) ]
36- pub struct SafeProvider < N : Network > {
36+ pub struct RobustProvider < N : Network > {
3737 provider : RootProvider < N > ,
3838 max_timeout : Duration ,
3939 max_retries : usize ,
4040 retry_interval : Duration ,
4141}
4242
4343// RPC retry and timeout settings
44- /// Default timeout used by `SafeProvider `
44+ /// Default timeout used by `RobustProvider `
4545pub const DEFAULT_MAX_TIMEOUT : Duration = Duration :: from_secs ( 30 ) ;
4646/// Default maximum number of retry attempts.
4747pub const DEFAULT_MAX_RETRIES : usize = 5 ;
4848/// Default base delay between retries.
4949pub const DEFAULT_RETRY_INTERVAL : Duration = Duration :: from_secs ( 1 ) ;
5050
51- impl < N : Network > SafeProvider < N > {
52- /// Create a new `SafeProvider ` with default settings.
51+ impl < N : Network > RobustProvider < N > {
52+ /// Create a new `RobustProvider ` with default settings.
5353 #[ must_use]
5454 pub fn new ( provider : RootProvider < N > ) -> Self {
5555 Self {
@@ -87,10 +87,10 @@ impl<N: Network> SafeProvider<N> {
8787 pub async fn get_block_by_number (
8888 & self ,
8989 number : BlockNumberOrTag ,
90- ) -> Result < Option < N :: BlockResponse > , SafeProviderError > {
90+ ) -> Result < Option < N :: BlockResponse > , RobustProviderError > {
9191 info ! ( "eth_getBlockByNumber called" ) ;
9292 let operation = async || {
93- self . provider . get_block_by_number ( number) . await . map_err ( SafeProviderError :: from)
93+ self . provider . get_block_by_number ( number) . await . map_err ( RobustProviderError :: from)
9494 } ;
9595 let result = self . retry_with_total_timeout ( operation) . await ;
9696 if let Err ( e) = & result {
@@ -105,10 +105,10 @@ impl<N: Network> SafeProvider<N> {
105105 ///
106106 /// Returns an error if RPC call fails repeatedly even
107107 /// after exhausting retries or if the call times out.
108- pub async fn get_block_number ( & self ) -> Result < u64 , SafeProviderError > {
108+ pub async fn get_block_number ( & self ) -> Result < u64 , RobustProviderError > {
109109 info ! ( "eth_getBlockNumber called" ) ;
110110 let operation =
111- async || self . provider . get_block_number ( ) . await . map_err ( SafeProviderError :: from) ;
111+ async || self . provider . get_block_number ( ) . await . map_err ( RobustProviderError :: from) ;
112112 let result = self . retry_with_total_timeout ( operation) . await ;
113113 if let Err ( e) = & result {
114114 error ! ( error = %e, "eth_getBlockNumber failed" ) ;
@@ -125,10 +125,10 @@ impl<N: Network> SafeProvider<N> {
125125 pub async fn get_block_by_hash (
126126 & self ,
127127 hash : alloy:: primitives:: BlockHash ,
128- ) -> Result < Option < N :: BlockResponse > , SafeProviderError > {
128+ ) -> Result < Option < N :: BlockResponse > , RobustProviderError > {
129129 info ! ( "eth_getBlockByHash called" ) ;
130130 let operation =
131- async || self . provider . get_block_by_hash ( hash) . await . map_err ( SafeProviderError :: from) ;
131+ async || self . provider . get_block_by_hash ( hash) . await . map_err ( RobustProviderError :: from) ;
132132 let result = self . retry_with_total_timeout ( operation) . await ;
133133 if let Err ( e) = & result {
134134 error ! ( error = %e, "eth_getBlockByHash failed" ) ;
@@ -142,10 +142,10 @@ impl<N: Network> SafeProvider<N> {
142142 ///
143143 /// Returns an error if RPC call fails repeatedly even
144144 /// after exhausting retries or if the call times out.
145- pub async fn get_logs ( & self , filter : & Filter ) -> Result < Vec < Log > , SafeProviderError > {
145+ pub async fn get_logs ( & self , filter : & Filter ) -> Result < Vec < Log > , RobustProviderError > {
146146 info ! ( "eth_getLogs called" ) ;
147147 let operation =
148- async || self . provider . get_logs ( filter) . await . map_err ( SafeProviderError :: from) ;
148+ async || self . provider . get_logs ( filter) . await . map_err ( RobustProviderError :: from) ;
149149 let result = self . retry_with_total_timeout ( operation) . await ;
150150 if let Err ( e) = & result {
151151 error ! ( error = %e, "eth_getLogs failed" ) ;
@@ -161,12 +161,12 @@ impl<N: Network> SafeProvider<N> {
161161 /// after exhausting retries or if the call times out.
162162 pub async fn subscribe_blocks (
163163 & self ,
164- ) -> Result < Subscription < N :: HeaderResponse > , SafeProviderError > {
164+ ) -> Result < Subscription < N :: HeaderResponse > , RobustProviderError > {
165165 info ! ( "eth_subscribe called" ) ;
166166 let provider = self . provider . clone ( ) ;
167167 let result = self
168168 . retry_with_total_timeout ( || async {
169- provider. subscribe_blocks ( ) . await . map_err ( SafeProviderError :: from)
169+ provider. subscribe_blocks ( ) . await . map_err ( RobustProviderError :: from)
170170 } )
171171 . await ;
172172 if let Err ( e) = & result {
@@ -189,10 +189,10 @@ impl<N: Network> SafeProvider<N> {
189189 async fn retry_with_total_timeout < T , F , Fut > (
190190 & self ,
191191 operation : F ,
192- ) -> Result < T , SafeProviderError >
192+ ) -> Result < T , RobustProviderError >
193193 where
194194 F : Fn ( ) -> Fut ,
195- Fut : Future < Output = Result < T , SafeProviderError > > ,
195+ Fut : Future < Output = Result < T , RobustProviderError > > ,
196196 {
197197 let retry_strategy = ExponentialBuilder :: default ( )
198198 . with_max_times ( self . max_retries )
@@ -205,8 +205,8 @@ impl<N: Network> SafeProvider<N> {
205205 . await
206206 {
207207 Ok ( Ok ( res) ) => Ok ( res) ,
208- Ok ( Err ( _) ) => Err ( SafeProviderError :: RetryFail ( self . max_retries + 1 ) ) ,
209- Err ( _) => Err ( SafeProviderError :: Timeout ) ,
208+ Ok ( Err ( _) ) => Err ( RobustProviderError :: RetryFail ( self . max_retries + 1 ) ) ,
209+ Err ( _) => Err ( RobustProviderError :: Timeout ) ,
210210 }
211211 }
212212}
@@ -222,8 +222,8 @@ mod tests {
222222 timeout : u64 ,
223223 max_retries : usize ,
224224 retry_interval : u64 ,
225- ) -> SafeProvider < Ethereum > {
226- SafeProvider {
225+ ) -> RobustProvider < Ethereum > {
226+ RobustProvider {
227227 provider : RootProvider :: new_http ( "http://localhost:8545" . parse ( ) . unwrap ( ) ) ,
228228 max_timeout : Duration :: from_millis ( timeout) ,
229229 max_retries,
@@ -257,7 +257,7 @@ mod tests {
257257 . retry_with_total_timeout ( || async {
258258 call_count. fetch_add ( 1 , Ordering :: SeqCst ) ;
259259 if call_count. load ( Ordering :: SeqCst ) < 3 {
260- Err ( SafeProviderError :: RpcError ( Arc :: new ( TransportErrorKind :: custom_str (
260+ Err ( RobustProviderError :: RpcError ( Arc :: new ( TransportErrorKind :: custom_str (
261261 "temp error" ,
262262 ) ) ) )
263263 } else {
@@ -279,12 +279,12 @@ mod tests {
279279 . retry_with_total_timeout ( || async {
280280 call_count. fetch_add ( 1 , Ordering :: SeqCst ) ;
281281 // permanent error
282- Err :: < i32 , SafeProviderError > ( SafeProviderError :: Timeout )
282+ Err :: < i32 , RobustProviderError > ( RobustProviderError :: Timeout )
283283 } )
284284 . await ;
285285
286286 let err = result. unwrap_err ( ) ;
287- assert ! ( matches!( err, SafeProviderError :: RetryFail ( 3 ) ) ) ;
287+ assert ! ( matches!( err, RobustProviderError :: RetryFail ( 3 ) ) ) ;
288288 assert_eq ! ( call_count. load( Ordering :: SeqCst ) , 3 ) ;
289289 }
290290
@@ -301,6 +301,6 @@ mod tests {
301301 . await ;
302302
303303 let err = result. unwrap_err ( ) ;
304- assert ! ( matches!( err, SafeProviderError :: Timeout ) ) ;
304+ assert ! ( matches!( err, RobustProviderError :: Timeout ) ) ;
305305 }
306306}
0 commit comments