2018-02-23 13:08:19 -08: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-02 07:43:54 -08:00
|
|
|
use log::{Entry, Sha256Hash};
|
|
|
|
use event::{Event, PublicKey, Signature};
|
2018-02-23 13:08:19 -08:00
|
|
|
use historian::Historian;
|
|
|
|
use ring::signature::Ed25519KeyPair;
|
2018-03-02 08:10:10 -08:00
|
|
|
use std::sync::mpsc::SendError;
|
2018-02-23 13:08:19 -08:00
|
|
|
use std::collections::HashMap;
|
2018-03-02 09:16:39 -08:00
|
|
|
use std::result;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum AccountingError {
|
|
|
|
InsufficientFunds,
|
|
|
|
InvalidEvent,
|
|
|
|
SendError,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, AccountingError>;
|
2018-02-23 13:08:19 -08:00
|
|
|
|
|
|
|
pub struct Accountant {
|
|
|
|
pub historian: Historian<u64>,
|
|
|
|
pub balances: HashMap<PublicKey, u64>,
|
|
|
|
pub end_hash: Sha256Hash,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Accountant {
|
|
|
|
pub fn new(start_hash: &Sha256Hash, ms_per_tick: Option<u64>) -> Self {
|
|
|
|
let hist = Historian::<u64>::new(start_hash, ms_per_tick);
|
|
|
|
Accountant {
|
|
|
|
historian: hist,
|
|
|
|
balances: HashMap::new(),
|
|
|
|
end_hash: *start_hash,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:23:27 -08:00
|
|
|
pub fn sync(self: &mut Self) -> Vec<Entry<u64>> {
|
2018-02-28 17:04:35 -08:00
|
|
|
let mut entries = vec![];
|
2018-02-23 13:08:19 -08:00
|
|
|
while let Ok(entry) = self.historian.receiver.try_recv() {
|
2018-02-28 17:04:35 -08:00
|
|
|
entries.push(entry);
|
|
|
|
}
|
2018-03-02 07:51:27 -08:00
|
|
|
|
2018-02-28 17:04:35 -08:00
|
|
|
if let Some(last_entry) = entries.last() {
|
|
|
|
self.end_hash = last_entry.end_hash;
|
|
|
|
}
|
2018-03-02 07:51:27 -08:00
|
|
|
|
2018-03-01 11:23:27 -08:00
|
|
|
entries
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:47:21 -08:00
|
|
|
pub fn deposit_signed(self: &mut Self, to: PublicKey, data: u64, sig: Signature) -> Result<()> {
|
2018-03-02 09:58:43 -08:00
|
|
|
let event = Event::new_claim(to, data, sig);
|
2018-03-02 08:10:10 -08:00
|
|
|
if !self.historian.verify_event(&event) {
|
2018-03-02 09:16:39 -08:00
|
|
|
return Err(AccountingError::InvalidEvent);
|
|
|
|
}
|
|
|
|
if let Err(SendError(_)) = self.historian.sender.send(event) {
|
|
|
|
return Err(AccountingError::SendError);
|
2018-03-02 08:10:10 -08:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:47:21 -08:00
|
|
|
if self.balances.contains_key(&to) {
|
|
|
|
if let Some(x) = self.balances.get_mut(&to) {
|
2018-03-02 08:10:10 -08:00
|
|
|
*x += data;
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-02 09:47:21 -08:00
|
|
|
self.balances.insert(to, data);
|
2018-03-02 08:10:10 -08:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:16:39 -08:00
|
|
|
Ok(())
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:16:39 -08:00
|
|
|
pub fn deposit(self: &mut Self, n: u64, keypair: &Ed25519KeyPair) -> Result<Signature> {
|
2018-03-02 10:46:19 -08:00
|
|
|
use event::{get_pubkey, sign_claim_data};
|
2018-02-28 09:07:54 -08:00
|
|
|
let key = get_pubkey(keypair);
|
2018-03-02 10:46:19 -08:00
|
|
|
let sig = sign_claim_data(&n, keypair);
|
2018-03-01 11:23:27 -08:00
|
|
|
self.deposit_signed(key, n, sig).map(|_| sig)
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transfer_signed(
|
|
|
|
self: &mut Self,
|
|
|
|
from: PublicKey,
|
|
|
|
to: PublicKey,
|
|
|
|
data: u64,
|
|
|
|
sig: Signature,
|
2018-03-02 09:16:39 -08:00
|
|
|
) -> Result<()> {
|
|
|
|
if self.get_balance(&from).unwrap_or(0) < data {
|
|
|
|
return Err(AccountingError::InsufficientFunds);
|
|
|
|
}
|
|
|
|
|
2018-02-28 09:07:54 -08:00
|
|
|
let event = Event::Transaction {
|
2018-03-02 09:41:13 -08:00
|
|
|
from: Some(from),
|
2018-02-28 09:07:54 -08:00
|
|
|
to,
|
|
|
|
data,
|
|
|
|
sig,
|
|
|
|
};
|
2018-03-02 08:10:10 -08:00
|
|
|
if !self.historian.verify_event(&event) {
|
2018-03-02 09:16:39 -08:00
|
|
|
return Err(AccountingError::InvalidEvent);
|
2018-03-02 08:10:10 -08:00
|
|
|
}
|
2018-03-02 09:16:39 -08:00
|
|
|
if let Err(SendError(_)) = self.historian.sender.send(event) {
|
|
|
|
return Err(AccountingError::SendError);
|
2018-03-02 08:10:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(x) = self.balances.get_mut(&from) {
|
|
|
|
*x -= data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.balances.contains_key(&to) {
|
|
|
|
if let Some(x) = self.balances.get_mut(&to) {
|
|
|
|
*x += data;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.balances.insert(to, data);
|
|
|
|
}
|
|
|
|
|
2018-03-02 09:16:39 -08:00
|
|
|
Ok(())
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transfer(
|
|
|
|
self: &mut Self,
|
|
|
|
n: u64,
|
|
|
|
keypair: &Ed25519KeyPair,
|
2018-02-28 09:07:54 -08:00
|
|
|
to: PublicKey,
|
2018-03-02 09:16:39 -08:00
|
|
|
) -> Result<Signature> {
|
2018-03-02 07:43:54 -08:00
|
|
|
use event::{get_pubkey, sign_transaction_data};
|
2018-02-28 09:07:54 -08:00
|
|
|
let from = get_pubkey(keypair);
|
|
|
|
let sig = sign_transaction_data(&n, keypair, &to);
|
2018-03-01 11:23:27 -08:00
|
|
|
self.transfer_signed(from, to, n, sig).map(|_| sig)
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-02 08:10:10 -08:00
|
|
|
pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<u64> {
|
|
|
|
self.balances.get(pubkey).map(|x| *x)
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
2018-03-01 11:23:27 -08:00
|
|
|
|
|
|
|
pub fn wait_on_signature(self: &mut Self, wait_sig: &Signature) {
|
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
|
|
|
let mut entries = self.sync();
|
|
|
|
let mut found = false;
|
|
|
|
while !found {
|
|
|
|
found = entries.iter().any(|e| match e.event {
|
|
|
|
Event::Transaction { sig, .. } => sig == *wait_sig,
|
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
if !found {
|
|
|
|
sleep(Duration::from_millis(30));
|
|
|
|
entries = self.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-03-02 07:43:54 -08:00
|
|
|
use event::{generate_keypair, get_pubkey};
|
2018-02-23 13:08:19 -08:00
|
|
|
use historian::ExitReason;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accountant() {
|
|
|
|
let zero = Sha256Hash::default();
|
|
|
|
let mut acc = Accountant::new(&zero, Some(2));
|
|
|
|
let alice_keypair = generate_keypair();
|
|
|
|
let bob_keypair = generate_keypair();
|
|
|
|
acc.deposit(10_000, &alice_keypair).unwrap();
|
2018-03-01 11:23:27 -08:00
|
|
|
let sig = acc.deposit(1_000, &bob_keypair).unwrap();
|
|
|
|
acc.wait_on_signature(&sig);
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-02-28 09:07:54 -08:00
|
|
|
let bob_pubkey = get_pubkey(&bob_keypair);
|
2018-03-01 11:23:27 -08:00
|
|
|
let sig = acc.transfer(500, &alice_keypair, bob_pubkey).unwrap();
|
|
|
|
acc.wait_on_signature(&sig);
|
2018-02-23 13:08:19 -08:00
|
|
|
|
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_500);
|
|
|
|
|
|
|
|
drop(acc.historian.sender);
|
|
|
|
assert_eq!(
|
|
|
|
acc.historian.thread_hdl.join().unwrap().1,
|
|
|
|
ExitReason::RecvDisconnected
|
|
|
|
);
|
|
|
|
}
|
2018-02-27 10:28:10 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_transfer() {
|
2018-03-01 11:23:27 -08:00
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
2018-02-27 10:28:10 -08:00
|
|
|
let zero = Sha256Hash::default();
|
|
|
|
let mut acc = Accountant::new(&zero, Some(2));
|
|
|
|
let alice_keypair = generate_keypair();
|
|
|
|
let bob_keypair = generate_keypair();
|
|
|
|
acc.deposit(10_000, &alice_keypair).unwrap();
|
2018-03-01 11:23:27 -08:00
|
|
|
let sig = acc.deposit(1_000, &bob_keypair).unwrap();
|
|
|
|
acc.wait_on_signature(&sig);
|
2018-02-27 10:28:10 -08:00
|
|
|
|
2018-02-28 09:07:54 -08:00
|
|
|
let bob_pubkey = get_pubkey(&bob_keypair);
|
2018-03-02 09:16:39 -08:00
|
|
|
assert_eq!(
|
|
|
|
acc.transfer(10_001, &alice_keypair, bob_pubkey),
|
|
|
|
Err(AccountingError::InsufficientFunds)
|
|
|
|
);
|
2018-02-27 10:28:10 -08:00
|
|
|
sleep(Duration::from_millis(30));
|
2018-03-01 11:23:27 -08:00
|
|
|
|
2018-02-28 09:07:54 -08:00
|
|
|
let alice_pubkey = get_pubkey(&alice_keypair);
|
2018-02-27 10:28:10 -08:00
|
|
|
assert_eq!(acc.get_balance(&alice_pubkey).unwrap(), 10_000);
|
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_000);
|
|
|
|
|
|
|
|
drop(acc.historian.sender);
|
|
|
|
assert_eq!(
|
|
|
|
acc.historian.thread_hdl.join().unwrap().1,
|
|
|
|
ExitReason::RecvDisconnected
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-02-28 09:07:54 -08:00
|
|
|
fn test_multiple_claims() {
|
2018-02-27 10:28:10 -08:00
|
|
|
let zero = Sha256Hash::default();
|
|
|
|
let mut acc = Accountant::new(&zero, Some(2));
|
|
|
|
let keypair = generate_keypair();
|
|
|
|
acc.deposit(1, &keypair).unwrap();
|
2018-03-01 11:23:27 -08:00
|
|
|
let sig = acc.deposit(2, &keypair).unwrap();
|
|
|
|
acc.wait_on_signature(&sig);
|
2018-02-27 10:28:10 -08:00
|
|
|
|
2018-02-28 09:07:54 -08:00
|
|
|
let pubkey = get_pubkey(&keypair);
|
2018-02-27 10:28:10 -08:00
|
|
|
assert_eq!(acc.get_balance(&pubkey).unwrap(), 3);
|
|
|
|
|
|
|
|
drop(acc.historian.sender);
|
|
|
|
assert_eq!(
|
|
|
|
acc.historian.thread_hdl.join().unwrap().1,
|
|
|
|
ExitReason::RecvDisconnected
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_to_newb() {
|
|
|
|
let zero = Sha256Hash::default();
|
|
|
|
let mut acc = Accountant::new(&zero, Some(2));
|
|
|
|
let alice_keypair = generate_keypair();
|
|
|
|
let bob_keypair = generate_keypair();
|
2018-03-01 11:23:27 -08:00
|
|
|
let sig = acc.deposit(10_000, &alice_keypair).unwrap();
|
|
|
|
acc.wait_on_signature(&sig);
|
2018-02-27 10:28:10 -08:00
|
|
|
|
2018-02-28 09:07:54 -08:00
|
|
|
let bob_pubkey = get_pubkey(&bob_keypair);
|
2018-03-01 11:23:27 -08:00
|
|
|
let sig = acc.transfer(500, &alice_keypair, bob_pubkey).unwrap();
|
|
|
|
acc.wait_on_signature(&sig);
|
2018-02-27 10:28:10 -08:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 500);
|
|
|
|
|
|
|
|
drop(acc.historian.sender);
|
|
|
|
assert_eq!(
|
|
|
|
acc.historian.thread_hdl.join().unwrap().1,
|
|
|
|
ExitReason::RecvDisconnected
|
|
|
|
);
|
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|