@@ -10,6 +10,7 @@ use api::ElectrumApi;
10
10
use batch:: Batch ;
11
11
use config:: Config ;
12
12
use raw_client:: * ;
13
+ use std:: convert:: TryFrom ;
13
14
use types:: * ;
14
15
15
16
/// Generalized Electrum client that supports multiple backends. This wraps
@@ -52,9 +53,9 @@ macro_rules! impl_inner_call {
52
53
return res;
53
54
} ,
54
55
Err ( e) => {
55
- let failed_attempts = ( errors. len( ) + 1 ) as u8 ;
56
+ let failed_attempts = errors. len( ) + 1 ;
56
57
57
- if failed_attempts > $self. config. retry( ) {
58
+ if retries_exhausted ( failed_attempts, $self. config. retry( ) ) {
58
59
warn!( "call '{}' failed after {} attempts" , stringify!( $name) , failed_attempts) ;
59
60
return Err ( Error :: AllAttemptsErrored ( errors) ) ;
60
61
}
@@ -91,6 +92,13 @@ macro_rules! impl_inner_call {
91
92
}
92
93
}
93
94
95
+ fn retries_exhausted ( failed_attempts : usize , configured_retries : u8 ) -> bool {
96
+ match u8:: try_from ( failed_attempts) {
97
+ Ok ( failed_attempts) => failed_attempts > configured_retries,
98
+ Err ( _) => true , // if the usize doesn't fit into a u8, we definitely exhausted our retries
99
+ }
100
+ }
101
+
94
102
impl ClientType {
95
103
/// Constructor that supports multiple backends and allows configuration through
96
104
/// the [Config]
@@ -294,3 +302,38 @@ impl ElectrumApi for Client {
294
302
impl_inner_call ! ( self , calls_made)
295
303
}
296
304
}
305
+
306
+ #[ cfg( test) ]
307
+ mod tests {
308
+ use super :: * ;
309
+
310
+ #[ test]
311
+ fn more_failed_attempts_than_retries_means_exhausted ( ) {
312
+ let exhausted = retries_exhausted ( 10 , 5 ) ;
313
+
314
+ assert_eq ! ( exhausted, true )
315
+ }
316
+
317
+ #[ test]
318
+ fn failed_attempts_bigger_than_u8_means_exhausted ( ) {
319
+ let failed_attempts = u8:: MAX as usize + 1 ;
320
+
321
+ let exhausted = retries_exhausted ( failed_attempts, u8:: MAX ) ;
322
+
323
+ assert_eq ! ( exhausted, true )
324
+ }
325
+
326
+ #[ test]
327
+ fn less_failed_attempts_means_not_exhausted ( ) {
328
+ let exhausted = retries_exhausted ( 2 , 5 ) ;
329
+
330
+ assert_eq ! ( exhausted, false )
331
+ }
332
+
333
+ #[ test]
334
+ fn attempts_equals_retries_means_not_exhausted_yet ( ) {
335
+ let exhausted = retries_exhausted ( 2 , 2 ) ;
336
+
337
+ assert_eq ! ( exhausted, false )
338
+ }
339
+ }
0 commit comments