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-01 12:23:27 -07:00
|
|
|
use std::net::UdpSocket;
|
2018-02-28 14:16:50 -07:00
|
|
|
use std::io;
|
|
|
|
use bincode::{deserialize, serialize};
|
2018-03-04 07:28:51 -07:00
|
|
|
use event::{get_pubkey, sign_transaction_data, PublicKey, Signature};
|
2018-02-28 14:16:50 -07:00
|
|
|
use ring::signature::Ed25519KeyPair;
|
|
|
|
use accountant_skel::{Request, Response};
|
|
|
|
|
|
|
|
pub struct AccountantStub {
|
|
|
|
pub addr: String,
|
2018-03-01 12:23:27 -07:00
|
|
|
pub socket: UdpSocket,
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountantStub {
|
2018-03-01 12:23:27 -07:00
|
|
|
pub fn new(addr: &str, socket: UdpSocket) -> Self {
|
2018-02-28 14:16:50 -07:00
|
|
|
AccountantStub {
|
|
|
|
addr: addr.to_string(),
|
2018-03-01 12:23:27 -07:00
|
|
|
socket,
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transfer_signed(
|
2018-03-03 10:23:31 -07:00
|
|
|
self: &Self,
|
2018-02-28 14:16:50 -07:00
|
|
|
from: PublicKey,
|
|
|
|
to: PublicKey,
|
|
|
|
val: u64,
|
|
|
|
sig: Signature,
|
|
|
|
) -> io::Result<usize> {
|
|
|
|
let req = Request::Transfer { from, to, val, sig };
|
|
|
|
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-03 10:23:31 -07:00
|
|
|
self: &Self,
|
2018-02-28 14:16:50 -07:00
|
|
|
n: u64,
|
|
|
|
keypair: &Ed25519KeyPair,
|
|
|
|
to: PublicKey,
|
2018-03-01 12:23:27 -07:00
|
|
|
) -> io::Result<Signature> {
|
2018-02-28 14:16:50 -07:00
|
|
|
let from = get_pubkey(keypair);
|
|
|
|
let sig = sign_transaction_data(&n, keypair, &to);
|
2018-03-01 12:23:27 -07:00
|
|
|
self.transfer_signed(from, to, n, sig).map(|_| sig)
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
|
2018-03-03 10:23:31 -07:00
|
|
|
pub fn get_balance(self: &Self, pubkey: &PublicKey) -> io::Result<u64> {
|
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);
|
|
|
|
}
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
|
2018-03-03 10:23:31 -07:00
|
|
|
pub fn wait_on_signature(self: &Self, wait_sig: &Signature) -> io::Result<()> {
|
2018-03-01 12:23:27 -07:00
|
|
|
let req = Request::Wait { sig: *wait_sig };
|
|
|
|
let data = serialize(&req).unwrap();
|
|
|
|
self.socket.send_to(&data, &self.addr).map(|_| ())?;
|
|
|
|
|
|
|
|
let mut buf = vec![0u8; 1024];
|
|
|
|
self.socket.recv_from(&mut buf)?;
|
|
|
|
let resp = deserialize(&buf).expect("deserialize signature");
|
|
|
|
if let Response::Confirmed { sig } = resp {
|
|
|
|
assert_eq!(sig, *wait_sig);
|
|
|
|
}
|
|
|
|
Ok(())
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use accountant::Accountant;
|
|
|
|
use accountant_skel::AccountantSkel;
|
|
|
|
use std::thread::{sleep, spawn};
|
|
|
|
use std::time::Duration;
|
2018-03-04 00:13:40 -07:00
|
|
|
use genesis::{Creator, Genesis};
|
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-04 01:21:40 -07:00
|
|
|
let bob = Creator::new(1_000);
|
2018-03-04 00:13:40 -07:00
|
|
|
let bob_pubkey = bob.pubkey;
|
|
|
|
let alice = Genesis::new(10_000, vec![bob]);
|
|
|
|
let acc = Accountant::new(&alice, None);
|
2018-03-03 21:15:42 -07:00
|
|
|
spawn(move || AccountantSkel::new(acc).serve(addr).unwrap());
|
|
|
|
sleep(Duration::from_millis(30));
|
2018-02-28 14:16:50 -07:00
|
|
|
|
2018-03-03 21:15:42 -07:00
|
|
|
let socket = UdpSocket::bind(send_addr).unwrap();
|
|
|
|
let acc = AccountantStub::new(addr, socket);
|
2018-03-04 00:13:40 -07:00
|
|
|
let sig = acc.transfer(500, &alice.get_keypair(), bob_pubkey).unwrap();
|
2018-03-01 12:23:27 -07:00
|
|
|
acc.wait_on_signature(&sig).unwrap();
|
2018-02-28 14:16:50 -07:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_500);
|
|
|
|
}
|
|
|
|
}
|