From 34d1805b549659928ba1378bde36b49f5cf932ef Mon Sep 17 00:00:00 2001 From: Robert Kelly Date: Tue, 24 Apr 2018 13:15:08 -0400 Subject: [PATCH] Add FutureResult to return a Future that immediately resolves --- Cargo.toml | 1 + src/accountant_skel.rs | 7 ++++--- src/accountant_stub.rs | 33 +++++++++++++++++++++------------ src/bin/client-demo.rs | 8 +++++--- src/lib.rs | 2 ++ 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 73e4022c86..433712ae68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,3 +63,4 @@ byteorder = "^1.2.1" libc = "^0.2.1" getopts = "^0.2" isatty = "0.1" +futures = "0.1" diff --git a/src/accountant_skel.rs b/src/accountant_skel.rs index 41d8f90ede..b4bf805c63 100644 --- a/src/accountant_skel.rs +++ b/src/accountant_skel.rs @@ -326,6 +326,7 @@ mod tests { use accountant_skel::AccountantSkel; use accountant_stub::AccountantStub; use entry::Entry; + use futures::Future; use historian::Historian; use mint::Mint; use plan::Plan; @@ -426,20 +427,20 @@ mod tests { socket.set_read_timeout(Some(Duration::new(5, 0))).unwrap(); let acc = AccountantStub::new(&addr, socket); - let last_id = acc.get_last_id().unwrap(); + let last_id = acc.get_last_id().wait().unwrap(); let tr = Transaction::new(&alice.keypair(), bob_pubkey, 500, last_id); let _sig = acc.transfer_signed(tr).unwrap(); - let last_id = acc.get_last_id().unwrap(); + let last_id = acc.get_last_id().wait().unwrap(); let mut tr2 = Transaction::new(&alice.keypair(), bob_pubkey, 501, last_id); tr2.data.tokens = 502; tr2.data.plan = Plan::new_payment(502, bob_pubkey); let _sig = acc.transfer_signed(tr2).unwrap(); - assert_eq!(acc.get_balance(&bob_pubkey).unwrap().unwrap(), 500); + assert_eq!(acc.get_balance(&bob_pubkey).wait().unwrap(), 500); exit.store(true, Ordering::Relaxed); } diff --git a/src/accountant_stub.rs b/src/accountant_stub.rs index 69d2f95989..5edfa6a053 100644 --- a/src/accountant_stub.rs +++ b/src/accountant_stub.rs @@ -5,6 +5,7 @@ use accountant_skel::{Request, Response}; use bincode::{deserialize, serialize}; +use futures::future::{err, ok, FutureResult}; use hash::Hash; use signature::{KeyPair, PublicKey, Signature}; use std::io; @@ -51,35 +52,42 @@ impl AccountantStub { /// Request the balance of the user holding `pubkey`. This method blocks /// until the server sends a response. If the response packet is dropped /// by the network, this method will hang indefinitely. - pub fn get_balance(&self, pubkey: &PublicKey) -> io::Result> { + pub fn get_balance(&self, pubkey: &PublicKey) -> FutureResult { let req = Request::GetBalance { key: *pubkey }; let data = serialize(&req).expect("serialize GetBalance"); - self.socket.send_to(&data, &self.addr)?; + self.socket + .send_to(&data, &self.addr) + .expect("buffer error"); let mut buf = vec![0u8; 1024]; - self.socket.recv_from(&mut buf)?; + self.socket.recv_from(&mut buf).expect("buffer error"); let resp = deserialize(&buf).expect("deserialize balance"); if let Response::Balance { key, val } = resp { assert_eq!(key, *pubkey); - return Ok(val); + return match val { + Some(x) => ok(x), + _ => err(0), + }; } - Ok(None) + err(0) } /// Request the last Entry ID from the server. This method blocks /// until the server sends a response. At the time of this writing, /// it also has the side-effect of causing the server to log any /// entries that have been published by the Historian. - pub fn get_last_id(&self) -> io::Result { + pub fn get_last_id(&self) -> FutureResult { let req = Request::GetLastId; let data = serialize(&req).expect("serialize GetId"); - self.socket.send_to(&data, &self.addr)?; + self.socket + .send_to(&data, &self.addr) + .expect("buffer error"); let mut buf = vec![0u8; 1024]; - self.socket.recv_from(&mut buf)?; + self.socket.recv_from(&mut buf).expect("buffer error"); let resp = deserialize(&buf).expect("deserialize Id"); if let Response::LastId { id } = resp { - return Ok(id); + return ok(id); } - Ok(Default::default()) + ok(Default::default()) } } @@ -88,6 +96,7 @@ mod tests { use super::*; use accountant::Accountant; use accountant_skel::AccountantSkel; + use futures::Future; use historian::Historian; use mint::Mint; use signature::{KeyPair, KeyPairUtil}; @@ -120,10 +129,10 @@ mod tests { socket.set_read_timeout(Some(Duration::new(5, 0))).unwrap(); let acc = AccountantStub::new(addr, socket); - let last_id = acc.get_last_id().unwrap(); + let last_id = acc.get_last_id().wait().unwrap(); let _sig = acc.transfer(500, &alice.keypair(), bob_pubkey, &last_id) .unwrap(); - assert_eq!(acc.get_balance(&bob_pubkey).unwrap().unwrap(), 500); + assert_eq!(acc.get_balance(&bob_pubkey).wait().unwrap(), 500); exit.store(true, Ordering::Relaxed); } } diff --git a/src/bin/client-demo.rs b/src/bin/client-demo.rs index 962141720b..e4ca981597 100644 --- a/src/bin/client-demo.rs +++ b/src/bin/client-demo.rs @@ -1,9 +1,11 @@ +extern crate futures; extern crate getopts; extern crate isatty; extern crate rayon; extern crate serde_json; extern crate solana; +use futures::Future; use getopts::Options; use isatty::stdin_isatty; use rayon::prelude::*; @@ -84,10 +86,10 @@ fn main() { println!("Stub new"); let acc = AccountantStub::new(&addr, socket); println!("Get last id"); - let last_id = acc.get_last_id().unwrap(); + let last_id = acc.get_last_id().wait().unwrap(); println!("Get Balance"); - let mint_balance = acc.get_balance(&mint_pubkey).unwrap().unwrap(); + let mint_balance = acc.get_balance(&mint_pubkey).wait().unwrap(); println!("Mint's Initial Balance {}", mint_balance); println!("Signing transactions..."); @@ -133,7 +135,7 @@ fn main() { while val != prev { sleep(Duration::from_millis(20)); prev = val; - val = acc.get_balance(&mint_pubkey).unwrap().unwrap(); + val = acc.get_balance(&mint_pubkey).wait().unwrap(); } println!("Mint's Final Balance {}", val); let txs = mint_balance - val; diff --git a/src/lib.rs b/src/lib.rs index 90200b5b84..7a316f9ade 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,8 @@ extern crate serde_json; extern crate sha2; extern crate untrusted; +extern crate futures; + #[cfg(test)] #[macro_use] extern crate matches;