2018-03-30 10:43:38 -07:00
|
|
|
//! The `accountant` module tracks client balances, and the progress of pending
|
2018-03-29 11:20:54 -07:00
|
|
|
//! transactions. It offers a high-level public API that signs transactions
|
2018-03-30 10:43:38 -07:00
|
|
|
//! on behalf of the caller, and a private low-level API for when they have
|
2018-03-29 11:20:54 -07:00
|
|
|
//! already been signed and verified.
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-03-26 21:03:26 -07:00
|
|
|
use chrono::prelude::*;
|
2018-03-06 11:18:17 -08:00
|
|
|
use event::Event;
|
2018-03-26 21:03:26 -07:00
|
|
|
use hash::Hash;
|
|
|
|
use mint::Mint;
|
2018-04-02 12:51:44 -07:00
|
|
|
use plan::{Payment, Plan, Witness};
|
2018-03-26 21:03:26 -07:00
|
|
|
use signature::{KeyPair, PublicKey, Signature};
|
2018-03-20 17:07:54 -07:00
|
|
|
use std::collections::hash_map::Entry::Occupied;
|
2018-03-26 21:03:26 -07:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2018-03-02 09:16:39 -08:00
|
|
|
use std::result;
|
2018-04-04 11:33:00 -07:00
|
|
|
use std::sync::RwLock;
|
2018-03-26 21:03:26 -07:00
|
|
|
use transaction::Transaction;
|
2018-03-02 09:16:39 -08:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum AccountingError {
|
|
|
|
InsufficientFunds,
|
2018-03-06 10:43:53 -08:00
|
|
|
InvalidTransferSignature,
|
2018-03-02 09:16:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, AccountingError>;
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-03-20 16:47:55 -07:00
|
|
|
/// Commit funds to the 'to' party.
|
2018-04-02 12:51:44 -07:00
|
|
|
fn apply_payment(balances: &mut HashMap<PublicKey, i64>, payment: &Payment) {
|
|
|
|
*balances.entry(payment.to).or_insert(0) += payment.tokens;
|
2018-03-20 16:47:55 -07:00
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:19 -08:00
|
|
|
pub struct Accountant {
|
2018-04-04 11:33:00 -07:00
|
|
|
balances: RwLock<HashMap<PublicKey, i64>>,
|
|
|
|
pending: RwLock<HashMap<Signature, Plan>>,
|
|
|
|
signatures: RwLock<HashSet<Signature>>,
|
|
|
|
time_sources: RwLock<HashSet<PublicKey>>,
|
|
|
|
last_time: RwLock<DateTime<Utc>>,
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Accountant {
|
2018-04-02 12:51:44 -07:00
|
|
|
/// Create an Accountant using a deposit.
|
2018-04-02 13:41:07 -07:00
|
|
|
pub fn new_from_deposit(deposit: &Payment) -> Self {
|
2018-04-02 12:51:44 -07:00
|
|
|
let mut balances = HashMap::new();
|
2018-04-03 08:55:33 -07:00
|
|
|
apply_payment(&mut balances, deposit);
|
2018-04-02 12:51:44 -07:00
|
|
|
Accountant {
|
2018-04-04 11:33:00 -07:00
|
|
|
balances: RwLock::new(balances),
|
|
|
|
pending: RwLock::new(HashMap::new()),
|
|
|
|
signatures: RwLock::new(HashSet::new()),
|
|
|
|
time_sources: RwLock::new(HashSet::new()),
|
|
|
|
last_time: RwLock::new(Utc.timestamp(0, 0)),
|
2018-04-02 12:51:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an Accountant with only a Mint. Typically used by unit tests.
|
2018-04-02 13:41:07 -07:00
|
|
|
pub fn new(mint: &Mint) -> Self {
|
2018-04-02 12:51:44 -07:00
|
|
|
let deposit = Payment {
|
|
|
|
to: mint.pubkey(),
|
|
|
|
tokens: mint.tokens,
|
|
|
|
};
|
2018-04-02 13:41:07 -07:00
|
|
|
Self::new_from_deposit(&deposit)
|
2018-04-02 12:51:44 -07:00
|
|
|
}
|
|
|
|
|
2018-04-04 11:33:00 -07:00
|
|
|
fn reserve_signature(&self, sig: &Signature) -> bool {
|
|
|
|
if self.signatures.read().unwrap().contains(sig) {
|
2018-04-02 08:36:22 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-04 11:33:00 -07:00
|
|
|
self.signatures.write().unwrap().insert(*sig);
|
2018-04-02 08:36:22 -07:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Process a Transaction that has already been verified.
|
2018-04-04 11:33:00 -07:00
|
|
|
pub fn process_verified_transaction(&self, tr: &Transaction) -> Result<()> {
|
2018-04-02 13:14:49 -07:00
|
|
|
if self.get_balance(&tr.from).unwrap_or(0) < tr.tokens {
|
|
|
|
return Err(AccountingError::InsufficientFunds);
|
|
|
|
}
|
|
|
|
|
2018-04-02 08:36:22 -07:00
|
|
|
if !self.reserve_signature(&tr.sig) {
|
2018-03-06 10:43:53 -08:00
|
|
|
return Err(AccountingError::InvalidTransferSignature);
|
2018-03-04 21:26:46 -08:00
|
|
|
}
|
2018-03-03 21:25:37 -08:00
|
|
|
|
2018-04-04 11:33:00 -07:00
|
|
|
if let Some(x) = self.balances.write().unwrap().get_mut(&tr.from) {
|
2018-04-02 13:00:42 -07:00
|
|
|
*x -= tr.tokens;
|
2018-03-06 10:43:53 -08:00
|
|
|
}
|
2018-03-03 21:25:37 -08:00
|
|
|
|
2018-03-10 23:30:01 -08:00
|
|
|
let mut plan = tr.plan.clone();
|
2018-04-04 11:33:00 -07:00
|
|
|
plan.apply_witness(&Witness::Timestamp(*self.last_time.read().unwrap()));
|
2018-03-10 23:30:01 -08:00
|
|
|
|
2018-04-02 12:51:44 -07:00
|
|
|
if let Some(ref payment) = plan.final_payment() {
|
2018-04-04 11:33:00 -07:00
|
|
|
apply_payment(&mut self.balances.write().unwrap(), payment);
|
2018-03-20 15:52:47 -07:00
|
|
|
} else {
|
2018-04-04 11:33:00 -07:00
|
|
|
self.pending.write().unwrap().insert(tr.sig, plan);
|
2018-03-07 21:25:45 -08:00
|
|
|
}
|
|
|
|
|
2018-03-04 21:26:46 -08:00
|
|
|
Ok(())
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Process a Witness Signature that has already been verified.
|
2018-04-04 11:33:00 -07:00
|
|
|
fn process_verified_sig(&self, from: PublicKey, tx_sig: Signature) -> Result<()> {
|
|
|
|
if let Occupied(mut e) = self.pending.write().unwrap().entry(tx_sig) {
|
2018-03-22 13:38:06 -07:00
|
|
|
e.get_mut().apply_witness(&Witness::Signature(from));
|
2018-04-02 12:51:44 -07:00
|
|
|
if let Some(ref payment) = e.get().final_payment() {
|
2018-04-04 11:33:00 -07:00
|
|
|
apply_payment(&mut self.balances.write().unwrap(), payment);
|
2018-03-20 17:07:54 -07:00
|
|
|
e.remove_entry();
|
2018-03-20 16:47:55 -07:00
|
|
|
}
|
2018-03-10 23:11:08 -08:00
|
|
|
};
|
2018-03-07 21:25:45 -08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Process a Witness Timestamp that has already been verified.
|
2018-04-04 11:33:00 -07:00
|
|
|
fn process_verified_timestamp(&self, from: PublicKey, dt: DateTime<Utc>) -> Result<()> {
|
2018-03-08 09:05:00 -08:00
|
|
|
// If this is the first timestamp we've seen, it probably came from the genesis block,
|
|
|
|
// so we'll trust it.
|
2018-04-04 11:33:00 -07:00
|
|
|
if *self.last_time.read().unwrap() == Utc.timestamp(0, 0) {
|
|
|
|
self.time_sources.write().unwrap().insert(from);
|
2018-03-08 09:05:00 -08:00
|
|
|
}
|
|
|
|
|
2018-04-04 11:33:00 -07:00
|
|
|
if self.time_sources.read().unwrap().contains(&from) {
|
|
|
|
if dt > *self.last_time.read().unwrap() {
|
|
|
|
*self.last_time.write().unwrap() = dt;
|
2018-03-08 09:05:00 -08:00
|
|
|
}
|
2018-03-08 10:06:52 -08:00
|
|
|
} else {
|
|
|
|
return Ok(());
|
2018-03-08 09:05:00 -08:00
|
|
|
}
|
2018-03-07 21:25:45 -08:00
|
|
|
|
2018-03-08 10:06:52 -08:00
|
|
|
// Check to see if any timelocked transactions can be completed.
|
|
|
|
let mut completed = vec![];
|
2018-04-04 11:33:00 -07:00
|
|
|
|
|
|
|
// Hold 'pending' write lock until the end of this function. Otherwise another thread can
|
|
|
|
// double-spend if it enters before the modified plan is removed from 'pending'.
|
|
|
|
let mut pending = self.pending.write().unwrap();
|
|
|
|
for (key, plan) in pending.iter_mut() {
|
|
|
|
plan.apply_witness(&Witness::Timestamp(*self.last_time.read().unwrap()));
|
2018-04-02 12:51:44 -07:00
|
|
|
if let Some(ref payment) = plan.final_payment() {
|
2018-04-04 11:33:00 -07:00
|
|
|
apply_payment(&mut self.balances.write().unwrap(), payment);
|
2018-03-10 23:30:01 -08:00
|
|
|
completed.push(key.clone());
|
2018-03-08 10:06:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key in completed {
|
2018-04-04 11:33:00 -07:00
|
|
|
pending.remove(&key);
|
2018-03-08 10:06:52 -08:00
|
|
|
}
|
|
|
|
|
2018-03-07 21:25:45 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Process an Transaction or Witness that has already been verified.
|
2018-04-04 11:33:00 -07:00
|
|
|
pub fn process_verified_event(&self, event: &Event) -> Result<()> {
|
2018-03-06 10:43:53 -08:00
|
|
|
match *event {
|
2018-04-02 13:00:42 -07:00
|
|
|
Event::Transaction(ref tr) => self.process_verified_transaction(tr),
|
2018-03-07 21:25:45 -08:00
|
|
|
Event::Signature { from, tx_sig, .. } => self.process_verified_sig(from, tx_sig),
|
|
|
|
Event::Timestamp { from, dt, .. } => self.process_verified_timestamp(from, dt),
|
2018-03-06 10:43:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Create, sign, and process a Transaction from `keypair` to `to` of
|
|
|
|
/// `n` tokens where `last_id` is the last Entry ID observed by the client.
|
2018-02-23 13:08:19 -08:00
|
|
|
pub fn transfer(
|
2018-04-04 11:33:00 -07:00
|
|
|
&self,
|
2018-03-05 16:29:32 -08:00
|
|
|
n: i64,
|
2018-03-07 10:05:06 -08:00
|
|
|
keypair: &KeyPair,
|
2018-02-28 09:07:54 -08:00
|
|
|
to: PublicKey,
|
2018-03-20 22:15:44 -07:00
|
|
|
last_id: Hash,
|
2018-03-02 09:16:39 -08:00
|
|
|
) -> Result<Signature> {
|
2018-03-20 22:15:44 -07:00
|
|
|
let tr = Transaction::new(keypair, to, n, last_id);
|
2018-03-06 15:34:14 -08:00
|
|
|
let sig = tr.sig;
|
2018-04-02 20:45:17 -07:00
|
|
|
self.process_verified_transaction(&tr).map(|_| sig)
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:54 -07:00
|
|
|
/// Create, sign, and process a postdated Transaction from `keypair`
|
|
|
|
/// to `to` of `n` tokens on `dt` where `last_id` is the last Entry ID
|
|
|
|
/// observed by the client.
|
2018-03-07 20:55:49 -08:00
|
|
|
pub fn transfer_on_date(
|
2018-04-04 11:33:00 -07:00
|
|
|
&self,
|
2018-03-07 20:55:49 -08:00
|
|
|
n: i64,
|
|
|
|
keypair: &KeyPair,
|
|
|
|
to: PublicKey,
|
|
|
|
dt: DateTime<Utc>,
|
2018-03-20 22:15:44 -07:00
|
|
|
last_id: Hash,
|
2018-03-07 20:55:49 -08:00
|
|
|
) -> Result<Signature> {
|
2018-03-20 22:15:44 -07:00
|
|
|
let tr = Transaction::new_on_date(keypair, to, dt, n, last_id);
|
2018-03-07 20:55:49 -08:00
|
|
|
let sig = tr.sig;
|
2018-04-02 20:45:17 -07:00
|
|
|
self.process_verified_transaction(&tr).map(|_| sig)
|
2018-03-07 20:55:49 -08:00
|
|
|
}
|
|
|
|
|
2018-04-02 13:00:42 -07:00
|
|
|
pub fn get_balance(&self, pubkey: &PublicKey) -> Option<i64> {
|
2018-04-04 11:33:00 -07:00
|
|
|
self.balances.read().unwrap().get(pubkey).cloned()
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-03-26 21:03:26 -07:00
|
|
|
use signature::KeyPairUtil;
|
2018-02-23 13:08:19 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_accountant() {
|
2018-03-07 16:08:12 -08:00
|
|
|
let alice = Mint::new(10_000);
|
2018-03-07 14:32:22 -08:00
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
2018-04-04 11:33:00 -07:00
|
|
|
let acc = Accountant::new(&alice);
|
2018-04-02 14:02:23 -07:00
|
|
|
acc.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-03-07 10:05:06 -08:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_000);
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-04-02 14:02:23 -07:00
|
|
|
acc.transfer(500, &alice.keypair(), bob_pubkey, alice.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-02-23 13:08:19 -08:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_500);
|
|
|
|
}
|
2018-02-27 10:28:10 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_transfer() {
|
2018-03-07 16:08:12 -08:00
|
|
|
let alice = Mint::new(11_000);
|
2018-04-04 11:33:00 -07:00
|
|
|
let acc = Accountant::new(&alice);
|
2018-03-07 14:32:22 -08:00
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
2018-04-02 14:02:23 -07:00
|
|
|
acc.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-03-02 09:16:39 -08:00
|
|
|
assert_eq!(
|
2018-04-02 14:02:23 -07:00
|
|
|
acc.transfer(10_001, &alice.keypair(), bob_pubkey, alice.last_id()),
|
2018-03-02 09:16:39 -08:00
|
|
|
Err(AccountingError::InsufficientFunds)
|
|
|
|
);
|
2018-03-01 11:23:27 -08:00
|
|
|
|
2018-03-07 14:32:22 -08:00
|
|
|
let alice_pubkey = alice.keypair().pubkey();
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_to_newb() {
|
2018-03-07 16:08:12 -08:00
|
|
|
let alice = Mint::new(10_000);
|
2018-04-04 11:33:00 -07:00
|
|
|
let acc = Accountant::new(&alice);
|
2018-03-07 14:32:22 -08:00
|
|
|
let alice_keypair = alice.keypair();
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
2018-04-02 14:02:23 -07:00
|
|
|
acc.transfer(500, &alice_keypair, bob_pubkey, alice.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-02-27 10:28:10 -08:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 500);
|
|
|
|
}
|
2018-03-08 07:58:01 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_on_date() {
|
|
|
|
let alice = Mint::new(1);
|
2018-04-04 11:33:00 -07:00
|
|
|
let acc = Accountant::new(&alice);
|
2018-03-08 07:58:01 -08:00
|
|
|
let alice_keypair = alice.keypair();
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
let dt = Utc::now();
|
2018-04-02 14:02:23 -07:00
|
|
|
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
2018-03-08 07:58:01 -08:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Alice's balance will be zero because all funds are locked up.
|
|
|
|
assert_eq!(acc.get_balance(&alice.pubkey()), Some(0));
|
|
|
|
|
|
|
|
// Bob's balance will be None because the funds have not been
|
|
|
|
// sent.
|
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey), None);
|
|
|
|
|
|
|
|
// Now, acknowledge the time in the condition occurred and
|
|
|
|
// that bob's funds are now available.
|
|
|
|
acc.process_verified_timestamp(alice.pubkey(), dt).unwrap();
|
2018-03-08 10:06:52 -08:00
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey), Some(1));
|
|
|
|
|
|
|
|
acc.process_verified_timestamp(alice.pubkey(), dt).unwrap(); // <-- Attack! Attempt to process completed transaction.
|
|
|
|
assert_ne!(acc.get_balance(&bob_pubkey), Some(2));
|
|
|
|
}
|
|
|
|
|
2018-03-08 14:39:03 -08:00
|
|
|
#[test]
|
|
|
|
fn test_transfer_after_date() {
|
|
|
|
let alice = Mint::new(1);
|
2018-04-04 11:33:00 -07:00
|
|
|
let acc = Accountant::new(&alice);
|
2018-03-08 14:39:03 -08:00
|
|
|
let alice_keypair = alice.keypair();
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
let dt = Utc::now();
|
|
|
|
acc.process_verified_timestamp(alice.pubkey(), dt).unwrap();
|
|
|
|
|
|
|
|
// It's now past now, so this transfer should be processed immediately.
|
2018-04-02 14:02:23 -07:00
|
|
|
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
2018-03-08 14:39:03 -08:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(acc.get_balance(&alice.pubkey()), Some(0));
|
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey), Some(1));
|
|
|
|
}
|
|
|
|
|
2018-03-08 10:06:52 -08:00
|
|
|
#[test]
|
|
|
|
fn test_cancel_transfer() {
|
|
|
|
let alice = Mint::new(1);
|
2018-04-04 11:33:00 -07:00
|
|
|
let acc = Accountant::new(&alice);
|
2018-03-08 10:06:52 -08:00
|
|
|
let alice_keypair = alice.keypair();
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
let dt = Utc::now();
|
2018-04-02 14:02:23 -07:00
|
|
|
let sig = acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
2018-03-08 10:06:52 -08:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Alice's balance will be zero because all funds are locked up.
|
|
|
|
assert_eq!(acc.get_balance(&alice.pubkey()), Some(0));
|
|
|
|
|
|
|
|
// Bob's balance will be None because the funds have not been
|
|
|
|
// sent.
|
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey), None);
|
|
|
|
|
|
|
|
// Now, cancel the trancaction. Alice gets her funds back, Bob never sees them.
|
|
|
|
acc.process_verified_sig(alice.pubkey(), sig).unwrap();
|
|
|
|
assert_eq!(acc.get_balance(&alice.pubkey()), Some(1));
|
|
|
|
assert_eq!(acc.get_balance(&bob_pubkey), None);
|
2018-03-08 07:58:01 -08:00
|
|
|
|
2018-03-08 10:06:52 -08:00
|
|
|
acc.process_verified_sig(alice.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction.
|
|
|
|
assert_ne!(acc.get_balance(&alice.pubkey()), Some(2));
|
2018-03-08 07:58:01 -08:00
|
|
|
}
|
2018-04-02 08:36:22 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_duplicate_event_signature() {
|
|
|
|
let alice = Mint::new(1);
|
2018-04-04 11:33:00 -07:00
|
|
|
let acc = Accountant::new(&alice);
|
2018-04-02 08:36:22 -07:00
|
|
|
let sig = Signature::default();
|
|
|
|
assert!(acc.reserve_signature(&sig));
|
|
|
|
assert!(!acc.reserve_signature(&sig));
|
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|