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-06 16:36:45 -08:00
|
|
|
use hash::Hash;
|
2018-03-06 16:31:17 -08:00
|
|
|
use entry::Entry;
|
2018-03-06 11:18:17 -08:00
|
|
|
use event::Event;
|
2018-03-10 16:57:16 -08:00
|
|
|
use transaction::{Action, Condition, Plan, Transaction};
|
2018-03-07 10:05:06 -08:00
|
|
|
use signature::{KeyPair, PublicKey, Signature};
|
2018-03-07 16:08:12 -08:00
|
|
|
use mint::Mint;
|
2018-03-04 21:26:46 -08:00
|
|
|
use historian::{reserve_signature, Historian};
|
2018-03-09 16:22:14 -08:00
|
|
|
use logger::Signal;
|
2018-03-02 08:10:10 -08:00
|
|
|
use std::sync::mpsc::SendError;
|
2018-03-08 09:05:00 -08:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2018-03-02 09:16:39 -08:00
|
|
|
use std::result;
|
2018-03-07 20:55:49 -08:00
|
|
|
use chrono::prelude::*;
|
2018-03-02 09:16:39 -08:00
|
|
|
|
|
|
|
#[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-06 19:22:30 -08:00
|
|
|
pub historian: Historian,
|
2018-03-05 16:29:32 -08:00
|
|
|
pub balances: HashMap<PublicKey, i64>,
|
2018-03-06 16:36:45 -08:00
|
|
|
pub first_id: Hash,
|
|
|
|
pub last_id: Hash,
|
2018-03-10 16:57:16 -08:00
|
|
|
pending: HashMap<Signature, Plan<i64>>,
|
2018-03-08 09:05:00 -08:00
|
|
|
time_sources: HashSet<PublicKey>,
|
|
|
|
last_time: DateTime<Utc>,
|
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-06 19:22:30 -08:00
|
|
|
I: IntoIterator<Item = Entry>,
|
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-06 19:22:30 -08:00
|
|
|
let hist = Historian::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-07 21:25:45 -08:00
|
|
|
pending: HashMap::new(),
|
2018-03-08 09:05:00 -08:00
|
|
|
time_sources: HashSet::new(),
|
|
|
|
last_time: Utc.timestamp(0, 0),
|
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();
|
2018-03-09 15:16:29 -08:00
|
|
|
acc.process_verified_event(&entry1.events[0], true).unwrap();
|
2018-03-05 14:34:15 -08:00
|
|
|
|
|
|
|
for entry in entries {
|
2018-03-09 15:16:29 -08:00
|
|
|
for event in entry.events {
|
|
|
|
acc.process_verified_event(&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-07 16:08:12 -08:00
|
|
|
pub fn new(mint: &Mint, ms_per_tick: Option<u64>) -> Self {
|
|
|
|
Self::new_from_entries(mint.create_entries(), ms_per_tick)
|
2018-03-05 14:34:15 -08:00
|
|
|
}
|
|
|
|
|
2018-03-06 16:36:45 -08:00
|
|
|
pub fn sync(self: &mut Self) -> Hash {
|
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-10 21:00:27 -08:00
|
|
|
fn is_deposit(allow_deposits: bool, from: &PublicKey, plan: &Plan<i64>) -> bool {
|
|
|
|
if let Plan::Action(Action::Pay(ref payment)) = *plan {
|
|
|
|
allow_deposits && *from == payment.to
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
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<()> {
|
2018-03-06 11:26:39 -08:00
|
|
|
if !tr.verify() {
|
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 13:37:08 -08:00
|
|
|
if self.get_balance(&tr.from).unwrap_or(0) < tr.asset {
|
2018-03-06 10:43:53 -08:00
|
|
|
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-09 16:22:14 -08:00
|
|
|
if let Err(SendError(_)) = self.historian
|
|
|
|
.sender
|
|
|
|
.send(Signal::Event(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-08 10:06:52 -08:00
|
|
|
/// Commit funds to the 'to' party.
|
2018-03-10 16:57:16 -08:00
|
|
|
fn complete_transaction(self: &mut Self, plan: &Plan<i64>) {
|
2018-03-10 21:00:27 -08:00
|
|
|
let payment = match *plan {
|
|
|
|
Plan::Action(Action::Pay(ref payment)) => Some(payment),
|
|
|
|
Plan::After(_, Action::Pay(ref payment)) => Some(payment),
|
|
|
|
Plan::Race(ref plan_a, _) => {
|
|
|
|
if let Plan::After(_, Action::Pay(ref payment)) = **plan_a {
|
|
|
|
Some(payment)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(payment) = payment {
|
|
|
|
if self.balances.contains_key(&payment.to) {
|
|
|
|
if let Some(x) = self.balances.get_mut(&payment.to) {
|
|
|
|
*x += payment.asset;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.balances.insert(payment.to, payment.asset);
|
2018-03-08 09:05:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 10:06:52 -08:00
|
|
|
/// Return funds to the 'from' party.
|
2018-03-10 16:57:16 -08:00
|
|
|
fn cancel_transaction(self: &mut Self, plan: &Plan<i64>) {
|
2018-03-10 21:00:27 -08:00
|
|
|
if let Plan::Race(_, ref cancel_plan) = *plan {
|
|
|
|
if let Plan::After(_, Action::Pay(ref payment)) = **cancel_plan {
|
|
|
|
if let Some(x) = self.balances.get_mut(&payment.to) {
|
|
|
|
*x += payment.asset;
|
|
|
|
}
|
|
|
|
}
|
2018-03-08 10:06:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 14:39:03 -08:00
|
|
|
// TODO: Move this to transaction.rs
|
2018-03-10 21:00:27 -08:00
|
|
|
fn all_satisfied(&self, plan: &Plan<i64>) -> bool {
|
|
|
|
match *plan {
|
|
|
|
Plan::Action(_) => true,
|
|
|
|
Plan::After(Condition::Timestamp(dt), _) => dt <= self.last_time,
|
|
|
|
Plan::After(Condition::Signature(_), _) => false,
|
|
|
|
Plan::Race(ref plan_a, ref plan_b) => {
|
|
|
|
self.all_satisfied(plan_a) || self.all_satisfied(plan_b)
|
2018-03-08 14:39:03 -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-10 21:00:27 -08:00
|
|
|
if !Self::is_deposit(allow_deposits, &tr.from, &tr.plan) {
|
2018-03-06 10:43:53 -08:00
|
|
|
if let Some(x) = self.balances.get_mut(&tr.from) {
|
2018-03-06 13:37:08 -08:00
|
|
|
*x -= tr.asset;
|
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-10 21:00:27 -08:00
|
|
|
if !self.all_satisfied(&tr.plan) {
|
2018-03-10 16:57:16 -08:00
|
|
|
self.pending.insert(tr.sig, tr.plan.clone());
|
2018-03-07 22:37:16 -08:00
|
|
|
return Ok(());
|
2018-03-07 21:25:45 -08:00
|
|
|
}
|
|
|
|
|
2018-03-10 16:57:16 -08:00
|
|
|
self.complete_transaction(&tr.plan);
|
2018-03-04 21:26:46 -08:00
|
|
|
Ok(())
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-08 10:06:52 -08:00
|
|
|
fn process_verified_sig(&mut self, from: PublicKey, tx_sig: Signature) -> Result<()> {
|
|
|
|
let mut cancel = false;
|
2018-03-10 16:57:16 -08:00
|
|
|
if let Some(plan) = self.pending.get(&tx_sig) {
|
2018-03-08 10:06:52 -08:00
|
|
|
// Cancel:
|
|
|
|
// if Signature(from) is in unless_any, return funds to tx.from, and remove the tx from this map.
|
|
|
|
|
|
|
|
// TODO: Use find().
|
2018-03-10 21:00:27 -08:00
|
|
|
if let Plan::Race(_, ref plan_b) = *plan {
|
|
|
|
if let Plan::After(Condition::Signature(pubkey), _) = **plan_b {
|
2018-03-08 10:06:52 -08:00
|
|
|
if from == pubkey {
|
|
|
|
cancel = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 21:25:45 -08:00
|
|
|
|
2018-03-08 10:06:52 -08:00
|
|
|
if cancel {
|
2018-03-10 16:57:16 -08:00
|
|
|
if let Some(plan) = self.pending.remove(&tx_sig) {
|
|
|
|
self.cancel_transaction(&plan);
|
2018-03-07 21:25:45 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-08 10:06:52 -08:00
|
|
|
|
|
|
|
// Process Multisig:
|
|
|
|
// otherwise, if "Signature(from) is in if_all, remove it. If that causes that list
|
|
|
|
// to be empty, add the asset to to, and remove the tx from this map.
|
2018-03-07 21:25:45 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-03-08 09:05:00 -08:00
|
|
|
fn process_verified_timestamp(&mut self, from: PublicKey, dt: DateTime<Utc>) -> Result<()> {
|
|
|
|
// If this is the first timestamp we've seen, it probably came from the genesis block,
|
|
|
|
// so we'll trust it.
|
|
|
|
if self.last_time == Utc.timestamp(0, 0) {
|
|
|
|
self.time_sources.insert(from);
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.time_sources.contains(&from) {
|
|
|
|
if dt > self.last_time {
|
|
|
|
self.last_time = dt;
|
|
|
|
}
|
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
|
|
|
// TODO: Lookup pending Transaction waiting on time, signed by a whitelisted PublicKey.
|
|
|
|
|
|
|
|
// Expire:
|
|
|
|
// if a Timestamp after this DateTime is in unless_any, return funds to tx.from,
|
|
|
|
// and remove the tx from this map.
|
|
|
|
|
2018-03-08 10:06:52 -08:00
|
|
|
// Check to see if any timelocked transactions can be completed.
|
|
|
|
let mut completed = vec![];
|
2018-03-10 16:57:16 -08:00
|
|
|
for (key, plan) in &self.pending {
|
2018-03-10 21:00:27 -08:00
|
|
|
if let Plan::After(Condition::Timestamp(dt), _) = *plan {
|
|
|
|
if self.last_time >= dt {
|
|
|
|
completed.push(*key);
|
|
|
|
}
|
|
|
|
} else if let Plan::Race(ref plan_a, _) = *plan {
|
|
|
|
if let Plan::After(Condition::Timestamp(dt), _) = **plan_a {
|
2018-03-08 10:06:52 -08:00
|
|
|
if self.last_time >= dt {
|
2018-03-10 21:00:27 -08:00
|
|
|
completed.push(*key);
|
2018-03-08 10:06:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Add this in once we start removing constraints
|
2018-03-10 16:11:12 -08:00
|
|
|
//if tr.plan.if_all.0.is_empty() {
|
2018-03-08 10:06:52 -08:00
|
|
|
// // TODO: Remove tr from pending
|
2018-03-10 16:57:16 -08:00
|
|
|
// self.complete_transaction(plan);
|
2018-03-08 10:06:52 -08:00
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key in completed {
|
2018-03-10 16:57:16 -08:00
|
|
|
if let Some(plan) = self.pending.remove(&key) {
|
|
|
|
self.complete_transaction(&plan);
|
2018-03-08 10:06:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:25:45 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-03-06 19:22:30 -08:00
|
|
|
fn process_verified_event(self: &mut Self, event: &Event, allow_deposits: bool) -> Result<()> {
|
2018-03-06 10:43:53 -08:00
|
|
|
match *event {
|
2018-03-06 10:54:45 -08:00
|
|
|
Event::Transaction(ref tr) => self.process_verified_transaction(tr, allow_deposits),
|
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-02-23 13:08:19 -08:00
|
|
|
pub fn transfer(
|
|
|
|
self: &mut 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-02 09:16:39 -08:00
|
|
|
) -> Result<Signature> {
|
2018-03-06 15:34:14 -08:00
|
|
|
let tr = Transaction::new(keypair, to, n, self.last_id);
|
|
|
|
let sig = tr.sig;
|
2018-03-06 10:54:45 -08:00
|
|
|
self.process_transaction(tr).map(|_| sig)
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-03-07 20:55:49 -08:00
|
|
|
pub fn transfer_on_date(
|
|
|
|
self: &mut Self,
|
|
|
|
n: i64,
|
|
|
|
keypair: &KeyPair,
|
|
|
|
to: PublicKey,
|
|
|
|
dt: DateTime<Utc>,
|
|
|
|
) -> Result<Signature> {
|
|
|
|
let tr = Transaction::new_on_date(keypair, to, dt, n, self.last_id);
|
|
|
|
let sig = tr.sig;
|
|
|
|
self.process_transaction(tr).map(|_| sig)
|
|
|
|
}
|
|
|
|
|
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-07 14:32:22 -08:00
|
|
|
use signature::KeyPairUtil;
|
2018-03-03 13:24:32 -08:00
|
|
|
use logger::ExitReason;
|
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-03-03 23:13:40 -08:00
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
2018-03-07 14:32:22 -08:00
|
|
|
acc.transfer(1_000, &alice.keypair(), bob_pubkey).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-03-07 14:32:22 -08:00
|
|
|
acc.transfer(500, &alice.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-07 16:08:12 -08:00
|
|
|
let alice = Mint::new(11_000);
|
2018-03-03 23:13:40 -08:00
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
2018-03-07 14:32:22 -08:00
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
acc.transfer(1_000, &alice.keypair(), bob_pubkey).unwrap();
|
2018-03-02 09:16:39 -08:00
|
|
|
assert_eq!(
|
2018-03-07 14:32:22 -08:00
|
|
|
acc.transfer(10_001, &alice.keypair(), bob_pubkey),
|
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);
|
|
|
|
|
|
|
|
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-07 16:08:12 -08:00
|
|
|
let alice = Mint::new(10_000);
|
2018-03-03 23:13:40 -08:00
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
2018-03-07 14:32:22 -08:00
|
|
|
let alice_keypair = alice.keypair();
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
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-03-08 07:58:01 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_on_date() {
|
|
|
|
let alice = Mint::new(1);
|
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
|
|
|
let alice_keypair = alice.keypair();
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
let dt = Utc::now();
|
|
|
|
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt)
|
|
|
|
.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);
|
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
|
|
|
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.
|
|
|
|
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt)
|
|
|
|
.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);
|
|
|
|
let mut acc = Accountant::new(&alice, Some(2));
|
|
|
|
let alice_keypair = alice.keypair();
|
|
|
|
let bob_pubkey = KeyPair::new().pubkey();
|
|
|
|
let dt = Utc::now();
|
|
|
|
let sig = acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt)
|
|
|
|
.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-02-23 13:08:19 -08:00
|
|
|
}
|