Add FutureResult to return a Future that immediately resolves

This commit is contained in:
Robert Kelly 2018-04-24 13:15:08 -04:00 committed by Greg Fitzgerald
parent 753eaa8266
commit 34d1805b54
5 changed files with 33 additions and 18 deletions

View File

@ -63,3 +63,4 @@ byteorder = "^1.2.1"
libc = "^0.2.1" libc = "^0.2.1"
getopts = "^0.2" getopts = "^0.2"
isatty = "0.1" isatty = "0.1"
futures = "0.1"

View File

@ -326,6 +326,7 @@ mod tests {
use accountant_skel::AccountantSkel; use accountant_skel::AccountantSkel;
use accountant_stub::AccountantStub; use accountant_stub::AccountantStub;
use entry::Entry; use entry::Entry;
use futures::Future;
use historian::Historian; use historian::Historian;
use mint::Mint; use mint::Mint;
use plan::Plan; use plan::Plan;
@ -426,20 +427,20 @@ mod tests {
socket.set_read_timeout(Some(Duration::new(5, 0))).unwrap(); socket.set_read_timeout(Some(Duration::new(5, 0))).unwrap();
let acc = AccountantStub::new(&addr, socket); 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 tr = Transaction::new(&alice.keypair(), bob_pubkey, 500, last_id);
let _sig = acc.transfer_signed(tr).unwrap(); 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); let mut tr2 = Transaction::new(&alice.keypair(), bob_pubkey, 501, last_id);
tr2.data.tokens = 502; tr2.data.tokens = 502;
tr2.data.plan = Plan::new_payment(502, bob_pubkey); tr2.data.plan = Plan::new_payment(502, bob_pubkey);
let _sig = acc.transfer_signed(tr2).unwrap(); 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); exit.store(true, Ordering::Relaxed);
} }

View File

@ -5,6 +5,7 @@
use accountant_skel::{Request, Response}; use accountant_skel::{Request, Response};
use bincode::{deserialize, serialize}; use bincode::{deserialize, serialize};
use futures::future::{err, ok, FutureResult};
use hash::Hash; use hash::Hash;
use signature::{KeyPair, PublicKey, Signature}; use signature::{KeyPair, PublicKey, Signature};
use std::io; use std::io;
@ -51,35 +52,42 @@ impl AccountantStub {
/// Request the balance of the user holding `pubkey`. This method blocks /// Request the balance of the user holding `pubkey`. This method blocks
/// until the server sends a response. If the response packet is dropped /// until the server sends a response. If the response packet is dropped
/// by the network, this method will hang indefinitely. /// by the network, this method will hang indefinitely.
pub fn get_balance(&self, pubkey: &PublicKey) -> io::Result<Option<i64>> { pub fn get_balance(&self, pubkey: &PublicKey) -> FutureResult<i64, i64> {
let req = Request::GetBalance { key: *pubkey }; let req = Request::GetBalance { key: *pubkey };
let data = serialize(&req).expect("serialize GetBalance"); 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]; 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"); let resp = deserialize(&buf).expect("deserialize balance");
if let Response::Balance { key, val } = resp { if let Response::Balance { key, val } = resp {
assert_eq!(key, *pubkey); 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 /// Request the last Entry ID from the server. This method blocks
/// until the server sends a response. At the time of this writing, /// 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 /// it also has the side-effect of causing the server to log any
/// entries that have been published by the Historian. /// entries that have been published by the Historian.
pub fn get_last_id(&self) -> io::Result<Hash> { pub fn get_last_id(&self) -> FutureResult<Hash, ()> {
let req = Request::GetLastId; let req = Request::GetLastId;
let data = serialize(&req).expect("serialize GetId"); 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]; 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"); let resp = deserialize(&buf).expect("deserialize Id");
if let Response::LastId { id } = resp { 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 super::*;
use accountant::Accountant; use accountant::Accountant;
use accountant_skel::AccountantSkel; use accountant_skel::AccountantSkel;
use futures::Future;
use historian::Historian; use historian::Historian;
use mint::Mint; use mint::Mint;
use signature::{KeyPair, KeyPairUtil}; use signature::{KeyPair, KeyPairUtil};
@ -120,10 +129,10 @@ mod tests {
socket.set_read_timeout(Some(Duration::new(5, 0))).unwrap(); socket.set_read_timeout(Some(Duration::new(5, 0))).unwrap();
let acc = AccountantStub::new(addr, socket); 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) let _sig = acc.transfer(500, &alice.keypair(), bob_pubkey, &last_id)
.unwrap(); .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); exit.store(true, Ordering::Relaxed);
} }
} }

View File

@ -1,9 +1,11 @@
extern crate futures;
extern crate getopts; extern crate getopts;
extern crate isatty; extern crate isatty;
extern crate rayon; extern crate rayon;
extern crate serde_json; extern crate serde_json;
extern crate solana; extern crate solana;
use futures::Future;
use getopts::Options; use getopts::Options;
use isatty::stdin_isatty; use isatty::stdin_isatty;
use rayon::prelude::*; use rayon::prelude::*;
@ -84,10 +86,10 @@ fn main() {
println!("Stub new"); println!("Stub new");
let acc = AccountantStub::new(&addr, socket); let acc = AccountantStub::new(&addr, socket);
println!("Get last id"); println!("Get last id");
let last_id = acc.get_last_id().unwrap(); let last_id = acc.get_last_id().wait().unwrap();
println!("Get Balance"); 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!("Mint's Initial Balance {}", mint_balance);
println!("Signing transactions..."); println!("Signing transactions...");
@ -133,7 +135,7 @@ fn main() {
while val != prev { while val != prev {
sleep(Duration::from_millis(20)); sleep(Duration::from_millis(20));
prev = val; prev = val;
val = acc.get_balance(&mint_pubkey).unwrap().unwrap(); val = acc.get_balance(&mint_pubkey).wait().unwrap();
} }
println!("Mint's Final Balance {}", val); println!("Mint's Final Balance {}", val);
let txs = mint_balance - val; let txs = mint_balance - val;

View File

@ -36,6 +36,8 @@ extern crate serde_json;
extern crate sha2; extern crate sha2;
extern crate untrusted; extern crate untrusted;
extern crate futures;
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]
extern crate matches; extern crate matches;