1
1
use super :: RouterStore ;
2
- use futures :: { future :: err , Future } ;
2
+ use async_trait :: async_trait ;
3
3
use interledger_packet:: { ErrorCode , RejectBuilder } ;
4
4
use interledger_service:: * ;
5
5
use log:: { error, trace} ;
@@ -38,19 +38,18 @@ where
38
38
}
39
39
}
40
40
41
+ #[ async_trait]
41
42
impl < S , O > IncomingService < S :: Account > for Router < S , O >
42
43
where
43
44
S : AddressStore + RouterStore ,
44
45
O : OutgoingService < S :: Account > + Clone + Send + ' static ,
45
46
{
46
- type Future = BoxedIlpFuture ;
47
-
48
47
/// Figures out the next node to pass the received Prepare packet to.
49
48
///
50
49
/// Firstly, it checks if there is a direct path for that account and uses that.
51
50
/// If not it scans through the routing table and checks if the route prefix matches
52
51
/// the prepare packet's destination or if it's a catch-all address (i.e. empty prefix)
53
- fn handle_request ( & mut self , request : IncomingRequest < S :: Account > ) -> Self :: Future {
52
+ async fn handle_request ( & mut self , request : IncomingRequest < S :: Account > ) -> IlpResult {
54
53
let destination = request. prepare . destination ( ) ;
55
54
let mut next_hop = None ;
56
55
let routing_table = self . store . routing_table ( ) ;
@@ -92,24 +91,22 @@ where
92
91
93
92
if let Some ( account_id) = next_hop {
94
93
let mut next = self . next . clone ( ) ;
95
- Box :: new (
96
- self . store
97
- . get_accounts ( vec ! [ account_id] )
98
- . map_err ( move |_| {
99
- error ! ( "No record found for account: {}" , account_id) ;
100
- RejectBuilder {
101
- code : ErrorCode :: F02_UNREACHABLE ,
102
- message : & [ ] ,
103
- triggered_by : Some ( & ilp_address) ,
104
- data : & [ ] ,
105
- }
106
- . build ( )
107
- } )
108
- . and_then ( move |mut accounts| {
109
- let request = request. into_outgoing ( accounts. remove ( 0 ) ) ;
110
- next. send_request ( request)
111
- } ) ,
112
- )
94
+ match self . store . get_accounts ( vec ! [ account_id] ) . await {
95
+ Ok ( mut accounts) => {
96
+ let request = request. into_outgoing ( accounts. remove ( 0 ) ) ;
97
+ next. send_request ( request) . await
98
+ }
99
+ Err ( _) => {
100
+ error ! ( "No record found for account: {}" , account_id) ;
101
+ Err ( RejectBuilder {
102
+ code : ErrorCode :: F02_UNREACHABLE ,
103
+ message : & [ ] ,
104
+ triggered_by : Some ( & ilp_address) ,
105
+ data : & [ ] ,
106
+ }
107
+ . build ( ) )
108
+ }
109
+ }
113
110
} else {
114
111
error ! (
115
112
"No route found for request {}: {:?}" ,
@@ -129,21 +126,20 @@ where
129
126
} ,
130
127
request
131
128
) ;
132
- Box :: new ( err ( RejectBuilder {
129
+ Err ( RejectBuilder {
133
130
code : ErrorCode :: F02_UNREACHABLE ,
134
131
message : & [ ] ,
135
132
triggered_by : Some ( & ilp_address) ,
136
133
data : & [ ] ,
137
134
}
138
- . build ( ) ) )
135
+ . build ( ) )
139
136
}
140
137
}
141
138
}
142
139
143
140
#[ cfg( test) ]
144
141
mod tests {
145
142
use super :: * ;
146
- use futures:: future:: ok;
147
143
use interledger_packet:: { Address , FulfillBuilder , PrepareBuilder } ;
148
144
use interledger_service:: outgoing_service_fn;
149
145
use lazy_static:: lazy_static;
@@ -190,36 +186,29 @@ mod tests {
190
186
routes : HashMap < String , Uuid > ,
191
187
}
192
188
189
+ #[ async_trait]
193
190
impl AccountStore for TestStore {
194
191
type Account = TestAccount ;
195
192
196
- fn get_accounts (
197
- & self ,
198
- account_ids : Vec < Uuid > ,
199
- ) -> Box < dyn Future < Item = Vec < TestAccount > , Error = ( ) > + Send > {
200
- Box :: new ( ok ( account_ids. into_iter ( ) . map ( TestAccount ) . collect ( ) ) )
193
+ async fn get_accounts ( & self , account_ids : Vec < Uuid > ) -> Result < Vec < TestAccount > , ( ) > {
194
+ Ok ( account_ids. into_iter ( ) . map ( TestAccount ) . collect ( ) )
201
195
}
202
196
203
197
// stub implementation (not used in these tests)
204
- fn get_account_id_from_username (
205
- & self ,
206
- _username : & Username ,
207
- ) -> Box < dyn Future < Item = Uuid , Error = ( ) > + Send > {
208
- Box :: new ( ok ( Uuid :: new_v4 ( ) ) )
198
+ async fn get_account_id_from_username ( & self , _username : & Username ) -> Result < Uuid , ( ) > {
199
+ Ok ( Uuid :: new_v4 ( ) )
209
200
}
210
201
}
211
202
203
+ #[ async_trait]
212
204
impl AddressStore for TestStore {
213
205
/// Saves the ILP Address in the store's memory and database
214
- fn set_ilp_address (
215
- & self ,
216
- _ilp_address : Address ,
217
- ) -> Box < dyn Future < Item = ( ) , Error = ( ) > + Send > {
218
- unimplemented ! ( )
206
+ async fn set_ilp_address ( & self , _ilp_address : Address ) -> Result < ( ) , ( ) > {
207
+ Ok ( ( ) )
219
208
}
220
209
221
- fn clear_ilp_address ( & self ) -> Box < dyn Future < Item = ( ) , Error = ( ) > + Send > {
222
- unimplemented ! ( )
210
+ async fn clear_ilp_address ( & self ) -> Result < ( ) , ( ) > {
211
+ Ok ( ( ) )
223
212
}
224
213
225
214
/// Get's the store's ilp address from memory
@@ -234,8 +223,8 @@ mod tests {
234
223
}
235
224
}
236
225
237
- #[ test]
238
- fn empty_routing_table ( ) {
226
+ #[ tokio :: test]
227
+ async fn empty_routing_table ( ) {
239
228
let mut router = Router :: new (
240
229
TestStore {
241
230
routes : HashMap :: new ( ) ,
@@ -261,12 +250,12 @@ mod tests {
261
250
}
262
251
. build ( ) ,
263
252
} )
264
- . wait ( ) ;
253
+ . await ;
265
254
assert ! ( result. is_err( ) ) ;
266
255
}
267
256
268
- #[ test]
269
- fn no_route ( ) {
257
+ #[ tokio :: test]
258
+ async fn no_route ( ) {
270
259
let mut router = Router :: new (
271
260
TestStore {
272
261
routes : HashMap :: from_iter (
@@ -294,12 +283,12 @@ mod tests {
294
283
}
295
284
. build ( ) ,
296
285
} )
297
- . wait ( ) ;
286
+ . await ;
298
287
assert ! ( result. is_err( ) ) ;
299
288
}
300
289
301
- #[ test]
302
- fn finds_exact_route ( ) {
290
+ #[ tokio :: test]
291
+ async fn finds_exact_route ( ) {
303
292
let mut router = Router :: new (
304
293
TestStore {
305
294
routes : HashMap :: from_iter (
@@ -327,12 +316,12 @@ mod tests {
327
316
}
328
317
. build ( ) ,
329
318
} )
330
- . wait ( ) ;
319
+ . await ;
331
320
assert ! ( result. is_ok( ) ) ;
332
321
}
333
322
334
- #[ test]
335
- fn catch_all_route ( ) {
323
+ #[ tokio :: test]
324
+ async fn catch_all_route ( ) {
336
325
let mut router = Router :: new (
337
326
TestStore {
338
327
routes : HashMap :: from_iter ( vec ! [ ( String :: new( ) , Uuid :: new_v4( ) ) ] . into_iter ( ) ) ,
@@ -358,12 +347,12 @@ mod tests {
358
347
}
359
348
. build ( ) ,
360
349
} )
361
- . wait ( ) ;
350
+ . await ;
362
351
assert ! ( result. is_ok( ) ) ;
363
352
}
364
353
365
- #[ test]
366
- fn finds_matching_prefix ( ) {
354
+ #[ tokio :: test]
355
+ async fn finds_matching_prefix ( ) {
367
356
let mut router = Router :: new (
368
357
TestStore {
369
358
routes : HashMap :: from_iter (
@@ -391,12 +380,12 @@ mod tests {
391
380
}
392
381
. build ( ) ,
393
382
} )
394
- . wait ( ) ;
383
+ . await ;
395
384
assert ! ( result. is_ok( ) ) ;
396
385
}
397
386
398
- #[ test]
399
- fn finds_longest_matching_prefix ( ) {
387
+ #[ tokio :: test]
388
+ async fn finds_longest_matching_prefix ( ) {
400
389
let id0 = Uuid :: from_slice ( & [ 0 ; 16 ] ) . unwrap ( ) ;
401
390
let id1 = Uuid :: from_slice ( & [ 1 ; 16 ] ) . unwrap ( ) ;
402
391
let id2 = Uuid :: from_slice ( & [ 2 ; 16 ] ) . unwrap ( ) ;
@@ -414,7 +403,7 @@ mod tests {
414
403
) ,
415
404
} ,
416
405
outgoing_service_fn ( move |request : OutgoingRequest < TestAccount > | {
417
- * to_clone. lock ( ) = Some ( request. to . clone ( ) ) ;
406
+ * to_clone. lock ( ) = Some ( request. to ) ;
418
407
419
408
Ok ( FulfillBuilder {
420
409
fulfillment : & [ 0 ; 32 ] ,
@@ -436,7 +425,7 @@ mod tests {
436
425
}
437
426
. build ( ) ,
438
427
} )
439
- . wait ( ) ;
428
+ . await ;
440
429
assert ! ( result. is_ok( ) ) ;
441
430
assert_eq ! ( to. lock( ) . take( ) . unwrap( ) . 0 , id2) ;
442
431
}
0 commit comments