Skip to content

Commit 08f45dc

Browse files
committed
feat(router): upgrade to futures 0.3 and async/await
1 parent ced438b commit 08f45dc

File tree

3 files changed

+81
-63
lines changed

3 files changed

+81
-63
lines changed

Cargo.lock

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/interledger-router/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ edition = "2018"
88
repository = "https://github.com/interledger-rs/interledger-rs"
99

1010
[dependencies]
11-
futures = { version = "0.1.29", default-features = false }
1211
interledger-packet = { path = "../interledger-packet", version = "^0.4.0", default-features = false }
1312
interledger-service = { path = "../interledger-service", version = "^0.4.0", default-features = false }
1413
log = { version = "0.4.8", default-features = false }
1514
parking_lot = { version = "0.9.0", default-features = false }
1615
uuid = { version = "0.8.1", default-features = false, features = ["v4"]}
16+
async-trait = "0.1.22"
1717

1818
[dev-dependencies]
1919
lazy_static = { version = "1.4.0", default-features = false }
20+
tokio = { version = "0.2.6", features = ["rt-core", "macros"]}

crates/interledger-router/src/router.rs

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::RouterStore;
2-
use futures::{future::err, Future};
2+
use async_trait::async_trait;
33
use interledger_packet::{ErrorCode, RejectBuilder};
44
use interledger_service::*;
55
use log::{error, trace};
@@ -38,19 +38,18 @@ where
3838
}
3939
}
4040

41+
#[async_trait]
4142
impl<S, O> IncomingService<S::Account> for Router<S, O>
4243
where
4344
S: AddressStore + RouterStore,
4445
O: OutgoingService<S::Account> + Clone + Send + 'static,
4546
{
46-
type Future = BoxedIlpFuture;
47-
4847
/// Figures out the next node to pass the received Prepare packet to.
4948
///
5049
/// Firstly, it checks if there is a direct path for that account and uses that.
5150
/// If not it scans through the routing table and checks if the route prefix matches
5251
/// 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 {
5453
let destination = request.prepare.destination();
5554
let mut next_hop = None;
5655
let routing_table = self.store.routing_table();
@@ -92,24 +91,22 @@ where
9291

9392
if let Some(account_id) = next_hop {
9493
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+
}
113110
} else {
114111
error!(
115112
"No route found for request {}: {:?}",
@@ -129,21 +126,20 @@ where
129126
},
130127
request
131128
);
132-
Box::new(err(RejectBuilder {
129+
Err(RejectBuilder {
133130
code: ErrorCode::F02_UNREACHABLE,
134131
message: &[],
135132
triggered_by: Some(&ilp_address),
136133
data: &[],
137134
}
138-
.build()))
135+
.build())
139136
}
140137
}
141138
}
142139

143140
#[cfg(test)]
144141
mod tests {
145142
use super::*;
146-
use futures::future::ok;
147143
use interledger_packet::{Address, FulfillBuilder, PrepareBuilder};
148144
use interledger_service::outgoing_service_fn;
149145
use lazy_static::lazy_static;
@@ -190,36 +186,29 @@ mod tests {
190186
routes: HashMap<String, Uuid>,
191187
}
192188

189+
#[async_trait]
193190
impl AccountStore for TestStore {
194191
type Account = TestAccount;
195192

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())
201195
}
202196

203197
// 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())
209200
}
210201
}
211202

203+
#[async_trait]
212204
impl AddressStore for TestStore {
213205
/// 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(())
219208
}
220209

221-
fn clear_ilp_address(&self) -> Box<dyn Future<Item = (), Error = ()> + Send> {
222-
unimplemented!()
210+
async fn clear_ilp_address(&self) -> Result<(), ()> {
211+
Ok(())
223212
}
224213

225214
/// Get's the store's ilp address from memory
@@ -234,8 +223,8 @@ mod tests {
234223
}
235224
}
236225

237-
#[test]
238-
fn empty_routing_table() {
226+
#[tokio::test]
227+
async fn empty_routing_table() {
239228
let mut router = Router::new(
240229
TestStore {
241230
routes: HashMap::new(),
@@ -261,12 +250,12 @@ mod tests {
261250
}
262251
.build(),
263252
})
264-
.wait();
253+
.await;
265254
assert!(result.is_err());
266255
}
267256

268-
#[test]
269-
fn no_route() {
257+
#[tokio::test]
258+
async fn no_route() {
270259
let mut router = Router::new(
271260
TestStore {
272261
routes: HashMap::from_iter(
@@ -294,12 +283,12 @@ mod tests {
294283
}
295284
.build(),
296285
})
297-
.wait();
286+
.await;
298287
assert!(result.is_err());
299288
}
300289

301-
#[test]
302-
fn finds_exact_route() {
290+
#[tokio::test]
291+
async fn finds_exact_route() {
303292
let mut router = Router::new(
304293
TestStore {
305294
routes: HashMap::from_iter(
@@ -327,12 +316,12 @@ mod tests {
327316
}
328317
.build(),
329318
})
330-
.wait();
319+
.await;
331320
assert!(result.is_ok());
332321
}
333322

334-
#[test]
335-
fn catch_all_route() {
323+
#[tokio::test]
324+
async fn catch_all_route() {
336325
let mut router = Router::new(
337326
TestStore {
338327
routes: HashMap::from_iter(vec![(String::new(), Uuid::new_v4())].into_iter()),
@@ -358,12 +347,12 @@ mod tests {
358347
}
359348
.build(),
360349
})
361-
.wait();
350+
.await;
362351
assert!(result.is_ok());
363352
}
364353

365-
#[test]
366-
fn finds_matching_prefix() {
354+
#[tokio::test]
355+
async fn finds_matching_prefix() {
367356
let mut router = Router::new(
368357
TestStore {
369358
routes: HashMap::from_iter(
@@ -391,12 +380,12 @@ mod tests {
391380
}
392381
.build(),
393382
})
394-
.wait();
383+
.await;
395384
assert!(result.is_ok());
396385
}
397386

398-
#[test]
399-
fn finds_longest_matching_prefix() {
387+
#[tokio::test]
388+
async fn finds_longest_matching_prefix() {
400389
let id0 = Uuid::from_slice(&[0; 16]).unwrap();
401390
let id1 = Uuid::from_slice(&[1; 16]).unwrap();
402391
let id2 = Uuid::from_slice(&[2; 16]).unwrap();
@@ -414,7 +403,7 @@ mod tests {
414403
),
415404
},
416405
outgoing_service_fn(move |request: OutgoingRequest<TestAccount>| {
417-
*to_clone.lock() = Some(request.to.clone());
406+
*to_clone.lock() = Some(request.to);
418407

419408
Ok(FulfillBuilder {
420409
fulfillment: &[0; 32],
@@ -436,7 +425,7 @@ mod tests {
436425
}
437426
.build(),
438427
})
439-
.wait();
428+
.await;
440429
assert!(result.is_ok());
441430
assert_eq!(to.lock().take().unwrap().0, id2);
442431
}

0 commit comments

Comments
 (0)