2018-03-30 13:10:27 -07:00
|
|
|
//! The `accountant_stub` module is a client-side object that interfaces with a server-side Accountant
|
2018-03-29 12:20:54 -06:00
|
|
|
//! object via the network interface exposed by AccountantSkel. Client code should use
|
|
|
|
//! this object instead of writing messages to the network directly. The binary
|
|
|
|
//! encoding of its messages are unstable and may change in future releases.
|
2018-02-28 14:16:50 -07:00
|
|
|
|
2018-04-17 18:14:53 -04:00
|
|
|
use accountant_skel::{Request, Response, Subscription};
|
2018-03-28 20:13:10 -06:00
|
|
|
use bincode::{deserialize, serialize};
|
2018-04-17 18:30:41 -04:00
|
|
|
use futures::future::{ok, FutureResult};
|
2018-03-26 22:03:26 -06:00
|
|
|
use hash::Hash;
|
|
|
|
use signature::{KeyPair, PublicKey, Signature};
|
2018-04-17 18:14:53 -04:00
|
|
|
use std::collections::HashMap;
|
2018-03-28 20:13:10 -06:00
|
|
|
use std::io;
|
|
|
|
use std::net::UdpSocket;
|
2018-03-26 22:03:26 -06:00
|
|
|
use transaction::Transaction;
|
2018-02-28 14:16:50 -07:00
|
|
|
|
|
|
|
pub struct AccountantStub {
|
|
|
|
pub addr: String,
|
2018-03-01 12:23:27 -07:00
|
|
|
pub socket: UdpSocket,
|
2018-04-17 18:14:53 -04:00
|
|
|
last_id: Option<Hash>,
|
|
|
|
num_events: u64,
|
2018-04-17 18:30:41 -04:00
|
|
|
balances: HashMap<PublicKey, Option<i64>>,
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountantStub {
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Create a new AccountantStub that will interface with AccountantSkel
|
|
|
|
/// over `socket`. To receive responses, the caller must bind `socket`
|
|
|
|
/// to a public address before invoking AccountantStub methods.
|
2018-03-28 20:13:10 -06:00
|
|
|
pub fn new(addr: &str, socket: UdpSocket) -> Self {
|
2018-04-17 18:41:58 -04:00
|
|
|
let stub = AccountantStub {
|
2018-02-28 14:16:50 -07:00
|
|
|
addr: addr.to_string(),
|
2018-03-01 12:23:27 -07:00
|
|
|
socket,
|
2018-04-17 18:14:53 -04:00
|
|
|
last_id: None,
|
|
|
|
num_events: 0,
|
|
|
|
balances: HashMap::new(),
|
2018-04-17 18:41:58 -04:00
|
|
|
};
|
|
|
|
stub.init();
|
|
|
|
stub
|
2018-04-17 18:14:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init(&self) {
|
|
|
|
let subscriptions = vec![Subscription::EntryInfo];
|
|
|
|
let req = Request::Subscribe { subscriptions };
|
2018-04-17 18:41:58 -04:00
|
|
|
let data = serialize(&req).expect("serialize Subscribe");
|
2018-04-17 18:14:53 -04:00
|
|
|
let _res = self.socket.send_to(&data, &self.addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recv_response(&self) -> io::Result<Response> {
|
|
|
|
let mut buf = vec![0u8; 1024];
|
|
|
|
self.socket.recv_from(&mut buf)?;
|
|
|
|
let resp = deserialize(&buf).expect("deserialize balance");
|
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_response(&mut self, resp: Response) {
|
|
|
|
match resp {
|
|
|
|
Response::Balance { key, val } => {
|
2018-04-17 18:30:41 -04:00
|
|
|
self.balances.insert(key, val);
|
2018-04-17 18:14:53 -04:00
|
|
|
}
|
|
|
|
Response::LastId { id } => {
|
|
|
|
self.last_id = Some(id);
|
|
|
|
}
|
|
|
|
Response::EntryInfo(entry_info) => {
|
|
|
|
self.last_id = Some(entry_info.id);
|
|
|
|
self.num_events += entry_info.num_events;
|
|
|
|
}
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Send a signed Transaction to the server for processing. This method
|
|
|
|
/// does not wait for a response.
|
2018-03-17 14:42:50 -06:00
|
|
|
pub fn transfer_signed(&self, tr: Transaction) -> io::Result<usize> {
|
2018-03-06 16:34:14 -07:00
|
|
|
let req = Request::Transaction(tr);
|
2018-02-28 14:16:50 -07:00
|
|
|
let data = serialize(&req).unwrap();
|
2018-03-01 12:23:27 -07:00
|
|
|
self.socket.send_to(&data, &self.addr)
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// Creates, signs, and processes a Transaction. Useful for writing unit-tests.
|
2018-02-28 14:16:50 -07:00
|
|
|
pub fn transfer(
|
2018-03-05 11:11:00 -07:00
|
|
|
&self,
|
2018-03-05 17:29:32 -07:00
|
|
|
n: i64,
|
2018-03-07 11:05:06 -07:00
|
|
|
keypair: &KeyPair,
|
2018-02-28 14:16:50 -07:00
|
|
|
to: PublicKey,
|
2018-03-06 17:36:45 -07:00
|
|
|
last_id: &Hash,
|
2018-03-01 12:23:27 -07:00
|
|
|
) -> io::Result<Signature> {
|
2018-03-06 16:34:14 -07:00
|
|
|
let tr = Transaction::new(keypair, to, n, *last_id);
|
|
|
|
let sig = tr.sig;
|
|
|
|
self.transfer_signed(tr).map(|_| sig)
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
2018-03-29 12:20:54 -06:00
|
|
|
/// 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.
|
2018-04-17 18:30:41 -04:00
|
|
|
pub fn get_balance(&mut self, pubkey: &PublicKey) -> FutureResult<i64, i64> {
|
2018-02-28 14:16:50 -07:00
|
|
|
let req = Request::GetBalance { key: *pubkey };
|
|
|
|
let data = serialize(&req).expect("serialize GetBalance");
|
2018-04-24 13:15:08 -04:00
|
|
|
self.socket
|
|
|
|
.send_to(&data, &self.addr)
|
|
|
|
.expect("buffer error");
|
2018-04-17 18:41:58 -04:00
|
|
|
let mut done = false;
|
|
|
|
while !done {
|
|
|
|
let resp = self.recv_response().expect("recv response");
|
|
|
|
if let &Response::Balance { ref key, .. } = &resp {
|
|
|
|
done = key == pubkey;
|
|
|
|
}
|
|
|
|
self.process_response(resp);
|
|
|
|
}
|
2018-04-17 18:30:41 -04:00
|
|
|
ok(self.balances[pubkey].unwrap())
|
2018-03-01 12:23:27 -07:00
|
|
|
}
|
|
|
|
|
2018-04-02 09:30:10 -06:00
|
|
|
/// 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.
|
2018-04-17 18:30:41 -04:00
|
|
|
pub fn get_last_id(&mut self) -> FutureResult<Hash, ()> {
|
2018-04-02 09:30:10 -06:00
|
|
|
let req = Request::GetLastId;
|
2018-03-05 12:48:09 -07:00
|
|
|
let data = serialize(&req).expect("serialize GetId");
|
2018-04-24 13:15:08 -04:00
|
|
|
self.socket
|
|
|
|
.send_to(&data, &self.addr)
|
|
|
|
.expect("buffer error");
|
2018-04-20 23:28:55 -06:00
|
|
|
let mut done = false;
|
|
|
|
while !done {
|
|
|
|
let resp = self.recv_response().expect("recv response");
|
|
|
|
if let &Response::LastId { .. } = &resp {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
self.process_response(resp);
|
|
|
|
}
|
2018-04-17 18:30:41 -04:00
|
|
|
ok(self.last_id.unwrap_or(Hash::default()))
|
2018-03-05 12:48:09 -07:00
|
|
|
}
|
2018-04-17 18:14:53 -04:00
|
|
|
|
|
|
|
/// Return the number of transactions the server processed since creating
|
|
|
|
/// this stub instance.
|
2018-04-20 23:28:55 -06:00
|
|
|
pub fn transaction_count(&mut self) -> u64 {
|
|
|
|
self.socket.set_nonblocking(true).expect("set nonblocking");
|
|
|
|
loop {
|
|
|
|
match self.recv_response() {
|
|
|
|
Err(_) => break,
|
|
|
|
Ok(resp) => self.process_response(resp),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.socket.set_nonblocking(false).expect("set blocking");
|
2018-04-17 18:14:53 -04:00
|
|
|
self.num_events
|
|
|
|
}
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use accountant::Accountant;
|
|
|
|
use accountant_skel::AccountantSkel;
|
2018-04-24 13:15:08 -04:00
|
|
|
use futures::Future;
|
2018-04-02 21:15:21 -06:00
|
|
|
use historian::Historian;
|
2018-03-07 17:08:12 -07:00
|
|
|
use mint::Mint;
|
2018-03-07 15:32:22 -07:00
|
|
|
use signature::{KeyPair, KeyPairUtil};
|
2018-03-26 11:17:19 -07:00
|
|
|
use std::io::sink;
|
2018-03-26 22:03:26 -06:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
2018-02-28 14:16:50 -07:00
|
|
|
|
2018-03-28 10:04:04 -06:00
|
|
|
// TODO: Figure out why this test sometimes hangs on TravisCI.
|
2018-02-28 14:16:50 -07:00
|
|
|
#[test]
|
|
|
|
fn test_accountant_stub() {
|
2018-03-01 12:23:27 -07:00
|
|
|
let addr = "127.0.0.1:9000";
|
|
|
|
let send_addr = "127.0.0.1:9001";
|
2018-03-07 17:08:12 -07:00
|
|
|
let alice = Mint::new(10_000);
|
2018-04-02 14:41:07 -06:00
|
|
|
let acc = Accountant::new(&alice);
|
2018-03-07 15:32:22 -07:00
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
2018-03-22 14:05:23 -06:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2018-04-02 15:02:23 -06:00
|
|
|
let historian = Historian::new(&alice.last_id(), Some(30));
|
2018-04-02 14:41:07 -06:00
|
|
|
let acc = Arc::new(Mutex::new(AccountantSkel::new(
|
|
|
|
acc,
|
2018-04-02 15:02:23 -06:00
|
|
|
alice.last_id(),
|
2018-04-02 14:41:07 -06:00
|
|
|
sink(),
|
|
|
|
historian,
|
|
|
|
)));
|
2018-04-02 19:32:58 -07:00
|
|
|
let _threads = AccountantSkel::serve(&acc, addr, exit.clone()).unwrap();
|
2018-03-26 22:02:05 -06:00
|
|
|
sleep(Duration::from_millis(300));
|
2018-02-28 14:16:50 -07:00
|
|
|
|
2018-03-03 21:15:42 -07:00
|
|
|
let socket = UdpSocket::bind(send_addr).unwrap();
|
2018-04-02 19:32:58 -07:00
|
|
|
socket.set_read_timeout(Some(Duration::new(5, 0))).unwrap();
|
2018-03-27 14:45:04 -06:00
|
|
|
|
2018-04-17 18:30:41 -04:00
|
|
|
let mut acc = AccountantStub::new(addr, socket);
|
2018-04-24 13:15:08 -04:00
|
|
|
let last_id = acc.get_last_id().wait().unwrap();
|
2018-03-28 10:25:16 -06:00
|
|
|
let _sig = acc.transfer(500, &alice.keypair(), bob_pubkey, &last_id)
|
2018-03-26 22:02:05 -06:00
|
|
|
.unwrap();
|
2018-04-24 13:15:08 -04:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).wait().unwrap(), 500);
|
2018-03-22 14:05:23 -06:00
|
|
|
exit.store(true, Ordering::Relaxed);
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|