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-05 14:34:15 -08:00
|
|
|
use log::{Entry, Sha256Hash};
|
2018-03-06 10:54:45 -08:00
|
|
|
use event::{get_pubkey, sign_transaction_data, verify_transaction, Event, PublicKey, Signature,
|
|
|
|
Transaction};
|
2018-03-03 23:13:40 -08:00
|
|
|
use genesis::Genesis;
|
2018-03-04 21:26:46 -08:00
|
|
|
use historian::{reserve_signature, Historian};
|
2018-02-23 13:08:19 -08:00
|
|
|
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,
|
2018-03-06 10:43:53 -08:00
|
|
|
InvalidTransfer,
|
|
|
|
InvalidTransferSignature,
|
2018-03-02 09:16:39 -08:00
|
|
|
SendError,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, AccountingError>;
|
2018-02-23 13:08:19 -08:00
|
|
|
|
|
|
|
pub struct Accountant {
|
2018-03-05 16:29:32 -08:00
|
|
|
pub historian: Historian<i64>,
|
|
|
|
pub balances: HashMap<PublicKey, i64>,
|
2018-03-05 11:48:09 -08:00
|
|
|
pub first_id: Sha256Hash,
|
2018-03-04 06:34:38 -08:00
|
|
|
pub last_id: Sha256Hash,
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Accountant {
|
2018-03-05 14:34:15 -08:00
|
|
|
pub fn new_from_entries<I>(entries: I, ms_per_tick: Option<u64>) -> Self
|
|
|
|
where
|
2018-03-05 16:29:32 -08:00
|
|
|
I: IntoIterator<Item = Entry<i64>>,
|
2018-03-05 14:34:15 -08:00
|
|
|
{
|
|
|
|
let mut entries = entries.into_iter();
|
|
|
|
|
|
|
|
// The first item in the log is required to be an entry with zero num_hashes,
|
|
|
|
// which implies its id can be used as the log's seed.
|
|
|
|
let entry0 = entries.next().unwrap();
|
|
|
|
let start_hash = entry0.id;
|
|
|
|
|
2018-03-05 16:29:32 -08:00
|
|
|
let hist = Historian::<i64>::new(&start_hash, ms_per_tick);
|
2018-03-03 23:13:40 -08:00
|
|
|
let mut acc = Accountant {
|
2018-02-23 13:08:19 -08:00
|
|
|
historian: hist,
|
|
|
|
balances: HashMap::new(),
|
2018-03-05 11:48:09 -08:00
|
|
|
first_id: start_hash,
|
2018-03-04 06:34:38 -08:00
|
|
|
last_id: start_hash,
|
2018-03-03 23:13:40 -08:00
|
|
|
};
|
2018-03-05 14:34:15 -08:00
|
|
|
|
|
|
|
// The second item in the log is a special transaction where the to and from
|
|
|
|
// fields are the same. That entry should be treated as a deposit, not a
|
|
|
|
// transfer to oneself.
|
|
|
|
let entry1 = entries.next().unwrap();
|
|
|
|
acc.process_verified_event(&entry1.event, true).unwrap();
|
|
|
|
|
|
|
|
for entry in entries {
|
|
|
|
acc.process_verified_event(&entry.event, false).unwrap();
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
2018-03-03 23:13:40 -08:00
|
|
|
acc
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-05 14:34:15 -08:00
|
|
|
pub fn new(gen: &Genesis, ms_per_tick: Option<u64>) -> Self {
|
|
|
|
Self::new_from_entries(gen.create_entries(), ms_per_tick)
|
|
|
|
}
|
|
|
|
|
2018-03-05 11:48:09 -08:00
|
|
|
pub fn sync(self: &mut Self) -> Sha256Hash {
|
2018-02-23 13:08:19 -08:00
|
|
|
while let Ok(entry) = self.historian.receiver.try_recv() {
|
2018-03-05 11:48:09 -08:00
|
|
|
self.last_id = entry.id;
|
2018-02-28 17:04:35 -08:00
|
|
|
}
|
2018-03-05 11:48:09 -08:00
|
|
|
self.last_id
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-03 21:25:37 -08:00
|
|
|
fn is_deposit(allow_deposits: bool, from: &PublicKey, to: &PublicKey) -> bool {
|
|
|
|
allow_deposits && from == to
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|
|
|
|
|
2018-03-06 10:54:45 -08:00
|
|
|
pub fn process_transaction(self: &mut Self, tr: Transaction<i64>) -> Result<()> {
|
|
|
|
if !verify_transaction(&tr) {
|
2018-03-06 10:43:53 -08:00
|
|
|
return Err(AccountingError::InvalidTransfer);
|
2018-03-02 08:10:10 -08:00
|
|
|
}
|
2018-03-04 21:26:46 -08:00
|
|
|
|
2018-03-06 10:43:53 -08:00
|
|
|
if self.get_balance(&tr.from).unwrap_or(0) < tr.data {
|
|
|
|
return Err(AccountingError::InsufficientFunds);
|
2018-03-04 21:26:46 -08:00
|
|
|
}
|
|
|
|
|
2018-03-06 10:54:45 -08:00
|
|
|
self.process_verified_transaction(&tr, false)?;
|
2018-03-06 10:43:53 -08:00
|
|
|
if let Err(SendError(_)) = self.historian.sender.send(Event::Transaction(tr)) {
|
2018-03-04 21:26:46 -08:00
|
|
|
return Err(AccountingError::SendError);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2018-03-03 21:25:37 -08:00
|
|
|
}
|
2018-03-02 08:10:10 -08:00
|
|
|
|
2018-03-06 10:54:45 -08:00
|
|
|
fn process_verified_transaction(
|
2018-03-03 21:25:37 -08:00
|
|
|
self: &mut Self,
|
2018-03-06 10:54:45 -08:00
|
|
|
tr: &Transaction<i64>,
|
2018-03-03 21:25:37 -08:00
|
|
|
allow_deposits: bool,
|
|
|
|
) -> Result<()> {
|
2018-03-06 10:43:53 -08:00
|
|
|
if !reserve_signature(&mut self.historian.signatures, &tr.sig) {
|
|
|
|
return Err(AccountingError::InvalidTransferSignature);
|
2018-03-04 21:26:46 -08:00
|
|
|
}
|
2018-03-03 21:25:37 -08:00
|
|
|
|
2018-03-06 10:43:53 -08:00
|
|
|
if !Self::is_deposit(allow_deposits, &tr.from, &tr.to) {
|
|
|
|
if let Some(x) = self.balances.get_mut(&tr.from) {
|
|
|
|
*x -= tr.data;
|
2018-03-04 21:26:46 -08:00
|
|
|
}
|
2018-03-06 10:43:53 -08:00
|
|
|
}
|
2018-03-03 21:25:37 -08:00
|
|
|
|
2018-03-06 10:43:53 -08:00
|
|
|
if self.balances.contains_key(&tr.to) {
|
|
|
|
if let Some(x) = self.balances.get_mut(&tr.to) {
|
|
|
|
*x += tr.data;
|
2018-03-02 08:10:10 -08:00
|
|
|
}
|
2018-03-06 10:43:53 -08:00
|
|
|
} else {
|
|
|
|
self.balances.insert(tr.to, tr.data);
|
2018-03-02 08:10:10 -08:00
|
|
|
}
|
2018-03-06 10:43:53 -08:00
|
|
|
|
2018-03-04 21:26:46 -08:00
|
|
|
Ok(())
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-06 10:43:53 -08:00
|
|
|
fn process_verified_event(
|
|
|
|
self: &mut Self,
|
|
|
|
event: &Event<i64>,
|
|
|
|
allow_deposits: bool,
|
|
|
|
) -> Result<()> {
|
|
|
|
match *event {
|
|
|
|
Event::Tick => Ok(()),
|
2018-03-06 10:54:45 -08:00
|
|
|
Event::Transaction(ref tr) => self.process_verified_transaction(tr, allow_deposits),
|
2018-03-06 10:43:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:19 -08:00
|
|
|
pub fn transfer(
|
|
|
|
self: &mut Self,
|
2018-03-05 16:29:32 -08:00
|
|
|
n: i64,
|
2018-02-23 13:08:19 -08:00
|
|
|
keypair: &Ed25519KeyPair,
|
2018-02-28 09:07:54 -08:00
|
|
|
to: PublicKey,
|
2018-03-02 09:16:39 -08:00
|
|
|
) -> Result<Signature> {
|
2018-02-28 09:07:54 -08:00
|
|
|
let from = get_pubkey(keypair);
|
2018-03-05 11:48:09 -08:00
|
|
|
let last_id = self.last_id;
|
|
|
|
let sig = sign_transaction_data(&n, keypair, &to, &last_id);
|
2018-03-06 10:54:45 -08:00
|
|
|
let tr = Transaction {
|
2018-03-03 21:25:37 -08:00
|
|
|
from,
|
|
|
|
to,
|
|
|
|
data: n,
|
2018-03-05 11:48:09 -08:00
|
|
|
last_id,
|
2018-03-03 21:25:37 -08:00
|
|
|
sig,
|
2018-03-06 10:43:53 -08:00
|
|
|
};
|
2018-03-06 10:54:45 -08:00
|
|
|
self.process_transaction(tr).map(|_| sig)
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-05 16:29:32 -08:00
|
|
|
pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<i64> {
|
2018-03-02 08:10:10 -08:00
|
|
|
self.balances.get(pubkey).map(|x| *x)
|
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-03-03 13:24:32 -08:00
|
|
|
use logger::ExitReason;
|
2018-03-03 23:13:40 -08:00
|
|
|
use genesis::Creator;
|
2018-02-23 13:08:19 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accountant() {
|
2018-03-04 00:21:40 -08:00
|
|
|
let bob = Creator::new(1_000);
|
2018-03-03 23:13:40 -08:00
|
|
|
let bob_pubkey = bob.pubkey;
|
|
|
|
let alice = Genesis::new(10_000, vec![bob]);
|
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-03-05 09:45:11 -08:00
|
|
|
acc.transfer(500, &alice.get_keypair(), bob_pubkey).unwrap();
|
2018-02-23 13:08:19 -08:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_500);
|
|
|
|
|
|
|
|
drop(acc.historian.sender);
|
|
|
|
assert_eq!(
|
2018-03-05 09:30:05 -08:00
|
|
|
acc.historian.thread_hdl.join().unwrap(),
|
2018-02-23 13:08:19 -08:00
|
|
|
ExitReason::RecvDisconnected
|
|
|
|
);
|
|
|
|
}
|
2018-02-27 10:28:10 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_transfer() {
|
2018-03-04 00:21:40 -08:00
|
|
|
let bob = Creator::new(1_000);
|
2018-03-03 23:13:40 -08:00
|
|
|
let bob_pubkey = bob.pubkey;
|
|
|
|
let alice = Genesis::new(11_000, vec![bob]);
|
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
2018-03-02 09:16:39 -08:00
|
|
|
assert_eq!(
|
2018-03-03 23:13:40 -08:00
|
|
|
acc.transfer(10_001, &alice.get_keypair(), bob_pubkey),
|
2018-03-02 09:16:39 -08:00
|
|
|
Err(AccountingError::InsufficientFunds)
|
|
|
|
);
|
2018-03-01 11:23:27 -08:00
|
|
|
|
2018-03-03 23:13:40 -08:00
|
|
|
let alice_pubkey = get_pubkey(&alice.get_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!(
|
2018-03-05 09:30:05 -08:00
|
|
|
acc.historian.thread_hdl.join().unwrap(),
|
2018-02-27 10:28:10 -08:00
|
|
|
ExitReason::RecvDisconnected
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_to_newb() {
|
2018-03-03 23:13:40 -08:00
|
|
|
let alice = Genesis::new(10_000, vec![]);
|
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
|
|
|
let alice_keypair = alice.get_keypair();
|
2018-02-27 10:28:10 -08:00
|
|
|
let bob_keypair = generate_keypair();
|
2018-02-28 09:07:54 -08:00
|
|
|
let bob_pubkey = get_pubkey(&bob_keypair);
|
2018-03-05 09:45:11 -08:00
|
|
|
acc.transfer(500, &alice_keypair, bob_pubkey).unwrap();
|
2018-02-27 10:28:10 -08:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 500);
|
|
|
|
|
|
|
|
drop(acc.historian.sender);
|
|
|
|
assert_eq!(
|
2018-03-05 09:30:05 -08:00
|
|
|
acc.historian.thread_hdl.join().unwrap(),
|
2018-02-27 10:28:10 -08:00
|
|
|
ExitReason::RecvDisconnected
|
|
|
|
);
|
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|