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-06 12:48:26 -07:00
|
|
|
use transaction::{sign_transaction_data, Transaction};
|
|
|
|
use signature::{get_pubkey, PublicKey, Signature};
|
2018-03-05 11:11:00 -07:00
|
|
|
use log::{Entry, Sha256Hash};
|
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-03-05 11:11:00 -07:00
|
|
|
pub last_id: Option<Sha256Hash>,
|
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-03-05 11:11:00 -07:00
|
|
|
last_id: None,
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transfer_signed(
|
2018-03-05 11:11:00 -07:00
|
|
|
&self,
|
2018-02-28 14:16:50 -07:00
|
|
|
from: PublicKey,
|
|
|
|
to: PublicKey,
|
2018-03-05 17:29:32 -07:00
|
|
|
val: i64,
|
2018-03-05 12:48:09 -07:00
|
|
|
last_id: Sha256Hash,
|
2018-02-28 14:16:50 -07:00
|
|
|
sig: Signature,
|
|
|
|
) -> io::Result<usize> {
|
2018-03-06 11:54:45 -07:00
|
|
|
let req = Request::Transaction(Transaction {
|
2018-03-05 12:48:09 -07:00
|
|
|
from,
|
|
|
|
to,
|
2018-03-06 14:37:08 -07:00
|
|
|
asset: val,
|
2018-03-05 12:48:09 -07:00
|
|
|
last_id,
|
|
|
|
sig,
|
2018-03-06 11:03:41 -07:00
|
|
|
});
|
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-02-28 14:16:50 -07:00
|
|
|
keypair: &Ed25519KeyPair,
|
|
|
|
to: PublicKey,
|
2018-03-05 12:48:09 -07:00
|
|
|
last_id: &Sha256Hash,
|
2018-03-01 12:23:27 -07:00
|
|
|
) -> io::Result<Signature> {
|
2018-02-28 14:16:50 -07:00
|
|
|
let from = get_pubkey(keypair);
|
2018-03-05 12:48:09 -07:00
|
|
|
let sig = sign_transaction_data(&n, keypair, &to, last_id);
|
|
|
|
self.transfer_signed(from, to, n, *last_id, sig)
|
|
|
|
.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-05 12:48:09 -07:00
|
|
|
fn get_id(&self, is_last: bool) -> io::Result<Sha256Hash> {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_last_id(&self) -> io::Result<Sha256Hash> {
|
|
|
|
self.get_id(true)
|
|
|
|
}
|
|
|
|
|
2018-03-05 11:11:00 -07:00
|
|
|
pub fn wait_on_signature(&mut self, wait_sig: &Signature) -> io::Result<()> {
|
2018-03-05 12:48:09 -07:00
|
|
|
let last_id = match self.last_id {
|
|
|
|
None => {
|
|
|
|
let first_id = self.get_id(false)?;
|
|
|
|
self.last_id = Some(first_id);
|
|
|
|
first_id
|
|
|
|
}
|
|
|
|
Some(last_id) => last_id,
|
2018-03-05 11:11:00 -07:00
|
|
|
};
|
2018-03-05 12:48:09 -07:00
|
|
|
|
|
|
|
let req = Request::GetEntries { last_id };
|
2018-03-01 12:23:27 -07:00
|
|
|
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");
|
2018-03-05 11:11:00 -07:00
|
|
|
if let Response::Entries { entries } = resp {
|
|
|
|
for Entry { id, event, .. } in entries {
|
|
|
|
self.last_id = Some(id);
|
2018-03-06 12:26:39 -07:00
|
|
|
if let Some(sig) = event.get_signature() {
|
2018-03-05 11:11:00 -07:00
|
|
|
if sig == *wait_sig {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 12:23:27 -07:00
|
|
|
}
|
2018-03-05 11:11:00 -07:00
|
|
|
|
|
|
|
// TODO: Loop until we found it.
|
2018-03-01 12:23:27 -07:00
|
|
|
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();
|
2018-03-05 11:11:00 -07:00
|
|
|
let mut acc = AccountantStub::new(addr, socket);
|
2018-03-05 12:48:09 -07:00
|
|
|
let last_id = acc.get_last_id().unwrap();
|
|
|
|
let sig = acc.transfer(500, &alice.get_keypair(), bob_pubkey, &last_id)
|
|
|
|
.unwrap();
|
2018-03-01 12:23:27 -07:00
|
|
|
acc.wait_on_signature(&sig).unwrap();
|
2018-03-05 15:34:15 -07:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap().unwrap(), 1_500);
|
2018-02-28 14:16:50 -07:00
|
|
|
}
|
|
|
|
}
|