2018-02-28 14:16:50 -07:00
|
|
|
//! The `accountant` is a client of the `historian`. It uses the historian's
|
|
|
|
//! event log to record transactions. Its users can deposit funds and
|
|
|
|
//! transfer funds to other users.
|
|
|
|
|
2018-03-26 22:03:26 -06:00
|
|
|
use accountant_skel::{Request, Response};
|
2018-03-27 14:45:04 -06:00
|
|
|
use bincode::{deserialize, serialize, serialized_size};
|
2018-03-06 17:31:17 -07:00
|
|
|
use entry::Entry;
|
2018-03-26 22:03:26 -06:00
|
|
|
use hash::Hash;
|
|
|
|
use signature::{KeyPair, PublicKey, Signature};
|
2018-03-27 14:45:04 -06:00
|
|
|
use std::io::{self, Read};
|
|
|
|
use std::net::{TcpStream, 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-03-27 14:45:04 -06:00
|
|
|
pub stream: TcpStream,
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountantStub {
|
2018-03-27 14:45:04 -06:00
|
|
|
pub fn new(addr: &str, socket: UdpSocket, stream: TcpStream) -> Self {
|
2018-02-28 14:16:50 -07:00
|
|
|
AccountantStub {
|
|
|
|
addr: addr.to_string(),
|
2018-03-01 12:23:27 -07:00
|
|
|
socket,
|
2018-03-27 14:45:04 -06:00
|
|
|
stream,
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-05 17:29:32 -07:00
|
|
|
pub fn get_balance(&self, pubkey: &PublicKey) -> io::Result<Option<i64>> {
|
2018-02-28 14:16:50 -07:00
|
|
|
let req = Request::GetBalance { key: *pubkey };
|
|
|
|
let data = serialize(&req).expect("serialize GetBalance");
|
2018-03-01 12:23:27 -07:00
|
|
|
self.socket.send_to(&data, &self.addr)?;
|
2018-02-28 14:16:50 -07:00
|
|
|
let mut buf = vec![0u8; 1024];
|
2018-03-01 12:23:27 -07:00
|
|
|
self.socket.recv_from(&mut buf)?;
|
2018-02-28 14:16:50 -07:00
|
|
|
let resp = deserialize(&buf).expect("deserialize balance");
|
2018-03-01 12:23:27 -07:00
|
|
|
if let Response::Balance { key, val } = resp {
|
|
|
|
assert_eq!(key, *pubkey);
|
|
|
|
return Ok(val);
|
|
|
|
}
|
2018-03-05 15:34:15 -07:00
|
|
|
Ok(None)
|
2018-03-01 12:23:27 -07:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:36:45 -07:00
|
|
|
fn get_id(&self, is_last: bool) -> io::Result<Hash> {
|
2018-03-05 12:48:09 -07:00
|
|
|
let req = Request::GetId { is_last };
|
|
|
|
let data = serialize(&req).expect("serialize GetId");
|
|
|
|
self.socket.send_to(&data, &self.addr)?;
|
|
|
|
let mut buf = vec![0u8; 1024];
|
|
|
|
self.socket.recv_from(&mut buf)?;
|
|
|
|
let resp = deserialize(&buf).expect("deserialize Id");
|
|
|
|
if let Response::Id { id, .. } = resp {
|
|
|
|
return Ok(id);
|
|
|
|
}
|
|
|
|
Ok(Default::default())
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:36:45 -07:00
|
|
|
pub fn get_last_id(&self) -> io::Result<Hash> {
|
2018-03-05 12:48:09 -07:00
|
|
|
self.get_id(true)
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:15:32 -06:00
|
|
|
pub fn check_on_signature(
|
|
|
|
&mut self,
|
|
|
|
wait_sig: &Signature,
|
|
|
|
last_id: &Hash,
|
|
|
|
) -> io::Result<(bool, Hash)> {
|
2018-03-21 15:43:39 -06:00
|
|
|
let mut last_id = *last_id;
|
2018-03-21 17:15:32 -06:00
|
|
|
let mut buf = vec![0u8; 65_535];
|
2018-03-27 14:45:04 -06:00
|
|
|
let mut buf_offset = 0;
|
2018-03-21 17:15:32 -06:00
|
|
|
let mut found = false;
|
2018-03-27 14:45:04 -06:00
|
|
|
if let Ok(bytes) = self.stream.read(&mut buf) {
|
|
|
|
loop {
|
|
|
|
match deserialize::<Entry>(&buf[buf_offset..]) {
|
|
|
|
Ok(entry) => {
|
|
|
|
buf_offset += serialized_size(&entry).unwrap() as usize;
|
|
|
|
last_id = entry.id;
|
|
|
|
if !found {
|
|
|
|
for event in entry.events {
|
|
|
|
if let Some(sig) = event.get_signature() {
|
|
|
|
if sig == *wait_sig {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 17:15:32 -06:00
|
|
|
}
|
2018-03-09 16:16:29 -07:00
|
|
|
}
|
2018-03-05 11:11:00 -07:00
|
|
|
}
|
2018-03-27 14:45:04 -06:00
|
|
|
Err(_) => {
|
|
|
|
println!("read {} of {} in buf", buf_offset, bytes);
|
|
|
|
break;
|
|
|
|
}
|
2018-03-05 11:11:00 -07:00
|
|
|
}
|
|
|
|
}
|
2018-03-01 12:23:27 -07:00
|
|
|
}
|
2018-03-05 11:11:00 -07:00
|
|
|
|
2018-03-21 17:15:32 -06:00
|
|
|
Ok((found, last_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wait_on_signature(&mut self, wait_sig: &Signature, last_id: &Hash) -> io::Result<Hash> {
|
|
|
|
let mut found = false;
|
|
|
|
let mut last_id = *last_id;
|
|
|
|
while !found {
|
|
|
|
let ret = self.check_on_signature(wait_sig, &last_id)?;
|
|
|
|
found = ret.0;
|
|
|
|
last_id = ret.1;
|
2018-03-27 14:45:04 -06:00
|
|
|
|
|
|
|
// Clunky way to force a sync in the skel.
|
|
|
|
self.get_last_id()?;
|
2018-03-21 17:15:32 -06:00
|
|
|
}
|
2018-03-21 15:43:39 -06:00
|
|
|
Ok(last_id)
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use accountant::Accountant;
|
|
|
|
use accountant_skel::AccountantSkel;
|
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
|
|
|
|
|
|
|
#[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-03-21 17:15:32 -06:00
|
|
|
let acc = Accountant::new(&alice, Some(30));
|
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-03-26 11:17:19 -07:00
|
|
|
let acc = Arc::new(Mutex::new(AccountantSkel::new(acc, sink())));
|
2018-03-27 14:45:04 -06: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-03-27 14:45:04 -06:00
|
|
|
let stream = TcpStream::connect(addr).expect("tcp connect");
|
|
|
|
stream.set_nonblocking(true).expect("nonblocking");
|
|
|
|
|
|
|
|
let mut acc = AccountantStub::new(addr, socket, stream);
|
2018-03-26 22:02:05 -06:00
|
|
|
let last_id = acc.get_last_id().unwrap();
|
|
|
|
let sig = acc.transfer(500, &alice.keypair(), bob_pubkey, &last_id)
|
|
|
|
.unwrap();
|
|
|
|
acc.wait_on_signature(&sig, &last_id).unwrap();
|
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap().unwrap(), 500);
|
2018-03-22 14:05:23 -06:00
|
|
|
exit.store(true, Ordering::Relaxed);
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|