11use {
22 super :: { BadTokenDetecting , TokenQuality } ,
3+ alloy:: primitives:: Address ,
34 anyhow:: Result ,
45 dashmap:: DashMap ,
56 futures:: future:: join_all,
6- primitive_types:: H160 ,
77 std:: {
88 ops:: Div ,
99 sync:: Arc ,
@@ -14,15 +14,15 @@ use {
1414
1515pub struct CachingDetector {
1616 inner : Box < dyn BadTokenDetecting > ,
17- cache : DashMap < H160 , ( Instant , TokenQuality ) > ,
17+ cache : DashMap < Address , ( Instant , TokenQuality ) > ,
1818 cache_expiry : Duration ,
1919 prefetch_time : Duration ,
2020}
2121
2222#[ async_trait:: async_trait]
2323impl BadTokenDetecting for CachingDetector {
2424 #[ instrument( skip_all) ]
25- async fn detect ( & self , token : H160 ) -> Result < TokenQuality > {
25+ async fn detect ( & self , token : Address ) -> Result < TokenQuality > {
2626 if let Some ( quality) = self . get_from_cache ( & token, Instant :: now ( ) ) {
2727 return Ok ( quality) ;
2828 }
@@ -53,13 +53,13 @@ impl CachingDetector {
5353 detector
5454 }
5555
56- fn get_from_cache ( & self , token : & H160 , now : Instant ) -> Option < TokenQuality > {
56+ fn get_from_cache ( & self , token : & Address , now : Instant ) -> Option < TokenQuality > {
5757 let ( instant, quality) = self . cache . get ( token) ?. value ( ) . clone ( ) ;
5858 let still_valid = now. saturating_duration_since ( instant) < self . cache_expiry ;
5959 still_valid. then_some ( quality)
6060 }
6161
62- fn insert_many_into_cache ( & self , tokens : impl Iterator < Item = ( H160 , TokenQuality ) > ) {
62+ fn insert_many_into_cache ( & self , tokens : impl Iterator < Item = ( Address , TokenQuality ) > ) {
6363 let now = Instant :: now ( ) ;
6464 tokens. into_iter ( ) . for_each ( |( token, quality) | {
6565 self . cache . insert ( token, ( now, quality) ) ;
@@ -134,7 +134,7 @@ mod tests {
134134
135135 for _ in 0 ..2 {
136136 let result = detector
137- . detect ( H160 :: from_low_u64_le ( 0 ) )
137+ . detect ( Address :: with_last_byte ( 0 ) )
138138 . now_or_never ( )
139139 . unwrap ( ) ;
140140 assert ! ( result. unwrap( ) . is_good( ) ) ;
@@ -144,7 +144,7 @@ mod tests {
144144 #[ tokio:: test]
145145 async fn cache_expires ( ) {
146146 let inner = MockBadTokenDetecting :: new ( ) ;
147- let token = H160 :: from_low_u64_le ( 0 ) ;
147+ let token = Address :: with_last_byte ( 0 ) ;
148148 let detector = CachingDetector :: new (
149149 Box :: new ( inner) ,
150150 Duration :: from_secs ( 2 ) ,
@@ -193,22 +193,22 @@ mod tests {
193193 ) ;
194194
195195 let result = detector
196- . detect ( H160 :: from_low_u64_le ( 0 ) )
196+ . detect ( Address :: with_last_byte ( 0 ) )
197197 . now_or_never ( )
198198 . unwrap ( ) ;
199199 assert ! ( result. unwrap( ) . is_good( ) ) ;
200200 // Check that the result is the same because we haven't reached the prefetch
201201 // time yet
202202 tokio:: time:: sleep ( Duration :: from_millis ( 100 ) ) . await ;
203203 let result = detector
204- . detect ( H160 :: from_low_u64_le ( 0 ) )
204+ . detect ( Address :: with_last_byte ( 0 ) )
205205 . now_or_never ( )
206206 . unwrap ( ) ;
207207 assert ! ( result. unwrap( ) . is_good( ) ) ;
208208 // We wait so the prefetch fetches the data
209209 tokio:: time:: sleep ( Duration :: from_millis ( 70 ) ) . await ;
210210 let result = detector
211- . detect ( H160 :: from_low_u64_le ( 0 ) )
211+ . detect ( Address :: with_last_byte ( 0 ) )
212212 . now_or_never ( )
213213 . unwrap ( ) ;
214214 assert ! ( !result. unwrap( ) . is_good( ) ) ;
0 commit comments