Port blocking stub functions to new stateful ones
This commit is contained in:
parent
6badc98510
commit
d5d133353f
|
@ -468,7 +468,7 @@ mod tests {
|
||||||
let socket = UdpSocket::bind(send_addr).unwrap();
|
let socket = UdpSocket::bind(send_addr).unwrap();
|
||||||
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 mut acc = AccountantStub::new(&addr, socket);
|
||||||
let last_id = acc.get_last_id().wait().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);
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
use accountant_skel::{Request, Response, Subscription};
|
use accountant_skel::{Request, Response, Subscription};
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use futures::future::{err, ok, FutureResult};
|
use futures::future::{ok, FutureResult};
|
||||||
use hash::Hash;
|
use hash::Hash;
|
||||||
use signature::{KeyPair, PublicKey, Signature};
|
use signature::{KeyPair, PublicKey, Signature};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -18,7 +18,7 @@ pub struct AccountantStub {
|
||||||
pub socket: UdpSocket,
|
pub socket: UdpSocket,
|
||||||
last_id: Option<Hash>,
|
last_id: Option<Hash>,
|
||||||
num_events: u64,
|
num_events: u64,
|
||||||
balances: HashMap<PublicKey, i64>,
|
balances: HashMap<PublicKey, Option<i64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AccountantStub {
|
impl AccountantStub {
|
||||||
|
@ -52,9 +52,7 @@ impl AccountantStub {
|
||||||
pub fn process_response(&mut self, resp: Response) {
|
pub fn process_response(&mut self, resp: Response) {
|
||||||
match resp {
|
match resp {
|
||||||
Response::Balance { key, val } => {
|
Response::Balance { key, val } => {
|
||||||
if let Some(val) = val {
|
self.balances.insert(key, val);
|
||||||
self.balances.insert(key, val);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Response::LastId { id } => {
|
Response::LastId { id } => {
|
||||||
self.last_id = Some(id);
|
self.last_id = Some(id);
|
||||||
|
@ -90,42 +88,30 @@ 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) -> FutureResult<i64, i64> {
|
pub fn get_balance(&mut 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
|
self.socket
|
||||||
.send_to(&data, &self.addr)
|
.send_to(&data, &self.addr)
|
||||||
.expect("buffer error");
|
.expect("buffer error");
|
||||||
let mut buf = vec![0u8; 1024];
|
let resp = self.recv_response().expect("recv response");
|
||||||
self.socket.recv_from(&mut buf).expect("buffer error");
|
self.process_response(resp);
|
||||||
let resp = deserialize(&buf).expect("deserialize balance");
|
ok(self.balances[pubkey].unwrap())
|
||||||
if let Response::Balance { key, val } = resp {
|
|
||||||
assert_eq!(key, *pubkey);
|
|
||||||
return match val {
|
|
||||||
Some(x) => ok(x),
|
|
||||||
_ => err(0),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
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) -> FutureResult<Hash, ()> {
|
pub fn get_last_id(&mut 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
|
self.socket
|
||||||
.send_to(&data, &self.addr)
|
.send_to(&data, &self.addr)
|
||||||
.expect("buffer error");
|
.expect("buffer error");
|
||||||
let mut buf = vec![0u8; 1024];
|
let resp = self.recv_response().expect("recv response");
|
||||||
self.socket.recv_from(&mut buf).expect("buffer error");
|
self.process_response(resp);
|
||||||
let resp = deserialize(&buf).expect("deserialize Id");
|
ok(self.last_id.unwrap_or(Hash::default()))
|
||||||
if let Response::LastId { id } = resp {
|
|
||||||
return ok(id);
|
|
||||||
}
|
|
||||||
ok(Default::default())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the number of transactions the server processed since creating
|
/// Return the number of transactions the server processed since creating
|
||||||
|
@ -172,7 +158,7 @@ mod tests {
|
||||||
let socket = UdpSocket::bind(send_addr).unwrap();
|
let socket = UdpSocket::bind(send_addr).unwrap();
|
||||||
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 mut acc = AccountantStub::new(addr, socket);
|
||||||
let last_id = acc.get_last_id().wait().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();
|
||||||
|
|
|
@ -83,7 +83,7 @@ fn main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
let socket = UdpSocket::bind(&send_addr).unwrap();
|
let socket = UdpSocket::bind(&send_addr).unwrap();
|
||||||
let acc = AccountantStub::new(&addr, socket);
|
let mut acc = AccountantStub::new(&addr, socket);
|
||||||
println!("Get last id");
|
println!("Get last id");
|
||||||
let last_id = acc.get_last_id().wait().unwrap();
|
let last_id = acc.get_last_id().wait().unwrap();
|
||||||
|
|
||||||
|
|
|
@ -64,7 +64,7 @@ fn main() {
|
||||||
|
|
||||||
eprintln!("Initializing...");
|
eprintln!("Initializing...");
|
||||||
let mut entries = buffer.lines().map(|line| {
|
let mut entries = buffer.lines().map(|line| {
|
||||||
serde_json::from_str(&line.unwrap()).unwrap_or_else(|e| {
|
serde_json::from_str(&line).unwrap_or_else(|e| {
|
||||||
eprintln!("failed to parse json: {}", e);
|
eprintln!("failed to parse json: {}", e);
|
||||||
exit(1);
|
exit(1);
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue