diff --git a/Cargo.toml b/Cargo.toml index 73e4022c86f578..4bbccdfbc5b278 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,4 +62,6 @@ matches = "^0.1.6" byteorder = "^1.2.1" libc = "^0.2.1" getopts = "^0.2" +futures = "0.1" + isatty = "0.1" diff --git a/src/accountant.rs b/src/accountant.rs index 682fa7635bd038..cc5cc377c4309d 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -81,6 +81,12 @@ impl Accountant { true } + // fn forget_signature_with_last_id(&self, last_id: &Hash) -> bool { + // let mut last_ids = self.last_ids.write().unwrap(); + // last_ids.pop_back(); + // true + // } + fn reserve_signature_with_last_id(&self, sig: &Signature, last_id: &Hash) -> bool { if let Some(entry) = self.last_ids .read() diff --git a/src/accountant_skel.rs b/src/accountant_skel.rs index 41d8f90ede1292..97f8e3ec344675 100644 --- a/src/accountant_skel.rs +++ b/src/accountant_skel.rs @@ -28,6 +28,7 @@ use std::time::Duration; use streamer; use transaction::Transaction; + pub struct AccountantSkel { acc: Accountant, last_id: Hash, @@ -326,6 +327,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 +428,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 69d2f959897444..92e4e9253ee2ec 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,38 @@ 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 +92,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 +125,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 962141720bb861..0744b890d87cf5 100644 --- a/src/bin/client-demo.rs +++ b/src/bin/client-demo.rs @@ -3,6 +3,7 @@ extern crate isatty; extern crate rayon; extern crate serde_json; extern crate solana; +extern crate futures; use getopts::Options; use isatty::stdin_isatty; @@ -17,6 +18,7 @@ use std::net::UdpSocket; use std::process::exit; use std::thread::sleep; use std::time::{Duration, Instant}; +use futures::Future; fn print_usage(program: &str, opts: Options) { let mut brief = format!("Usage: cat | {} [options]\n\n", program); @@ -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 80a0f607790a4f..b0a4796d95eef3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,8 @@ extern crate serde_json; extern crate sha2; extern crate untrusted; +extern crate futures; + #[cfg(test)] #[macro_use] extern crate matches;