solana/src/accountant.rs

382 lines
12 KiB
Rust
Raw Normal View History

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.
use chrono::prelude::*;
use entry::Entry;
2018-03-06 11:18:17 -08:00
use event::Event;
use hash::Hash;
2018-03-22 13:50:24 -07:00
use historian::Historian;
use mint::Mint;
use plan::{Plan, Witness};
use recorder::Signal;
use signature::{KeyPair, PublicKey, Signature};
use std::collections::hash_map::Entry::Occupied;
use std::collections::{HashMap, HashSet};
use std::result;
use std::sync::mpsc::SendError;
use transaction::Transaction;
#[derive(Debug, PartialEq, Eq)]
pub enum AccountingError {
InsufficientFunds,
InvalidTransfer,
InvalidTransferSignature,
SendError,
}
pub type Result<T> = result::Result<T, AccountingError>;
2018-02-23 13:08:19 -08:00
/// Commit funds to the 'to' party.
fn complete_transaction(balances: &mut HashMap<PublicKey, i64>, plan: &Plan) {
if let Plan::Pay(ref payment) = *plan {
2018-03-22 13:15:29 -07:00
*balances.entry(payment.to).or_insert(0) += payment.tokens;
}
}
2018-02-23 13:08:19 -08:00
pub struct Accountant {
pub historian: Historian,
pub balances: HashMap<PublicKey, i64>,
pub first_id: Hash,
pending: HashMap<Signature, Plan>,
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 {
pub fn new_from_entries<I>(entries: I, ms_per_tick: Option<u64>) -> Self
where
I: IntoIterator<Item = Entry>,
{
let mut entries = entries.into_iter();
// The first item in the ledger is required to be an entry with zero num_hashes,
// which implies its id can be used as the ledger's seed.
let entry0 = entries.next().unwrap();
let start_hash = entry0.id;
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(),
first_id: start_hash,
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
};
// The second item in the ledger 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.events[0], true).unwrap();
for entry in entries {
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
}
pub fn new(mint: &Mint, ms_per_tick: Option<u64>) -> Self {
Self::new_from_entries(mint.create_entries(), ms_per_tick)
}
fn is_deposit(allow_deposits: bool, from: &PublicKey, plan: &Plan) -> bool {
2018-03-20 14:43:04 -07:00
if let Plan::Pay(ref payment) = *plan {
allow_deposits && *from == payment.to
} else {
false
}
2018-02-28 09:07:54 -08:00
}
pub fn process_transaction(self: &mut Self, tr: Transaction) -> Result<()> {
if !tr.verify() {
return Err(AccountingError::InvalidTransfer);
}
2018-03-19 09:03:41 -07:00
if self.get_balance(&tr.from).unwrap_or(0) < tr.tokens {
return Err(AccountingError::InsufficientFunds);
}
self.process_verified_transaction(&tr, false)?;
if let Err(SendError(_)) = self.historian
.sender
.send(Signal::Event(Event::Transaction(tr)))
{
return Err(AccountingError::SendError);
}
Ok(())
}
fn process_verified_transaction(
self: &mut Self,
tr: &Transaction,
allow_deposits: bool,
) -> Result<()> {
2018-03-22 13:50:24 -07:00
if !self.historian.reserve_signature(&tr.sig) {
return Err(AccountingError::InvalidTransferSignature);
}
if !Self::is_deposit(allow_deposits, &tr.from, &tr.plan) {
if let Some(x) = self.balances.get_mut(&tr.from) {
2018-03-19 09:03:41 -07:00
*x -= tr.tokens;
}
}
2018-03-10 23:30:01 -08:00
let mut plan = tr.plan.clone();
2018-03-22 13:38:06 -07:00
plan.apply_witness(&Witness::Timestamp(self.last_time));
2018-03-10 23:30:01 -08:00
if plan.is_complete() {
complete_transaction(&mut self.balances, &plan);
} else {
2018-03-10 23:30:01 -08:00
self.pending.insert(tr.sig, plan);
}
Ok(())
2018-02-23 13:08:19 -08:00
}
fn process_verified_sig(&mut self, from: PublicKey, tx_sig: Signature) -> Result<()> {
if let Occupied(mut e) = self.pending.entry(tx_sig) {
2018-03-22 13:38:06 -07:00
e.get_mut().apply_witness(&Witness::Signature(from));
if e.get().is_complete() {
complete_transaction(&mut self.balances, e.get());
e.remove_entry();
}
2018-03-10 23:11:08 -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;
}
} else {
return Ok(());
2018-03-08 09:05:00 -08:00
}
// Check to see if any timelocked transactions can be completed.
let mut completed = vec![];
2018-03-10 23:30:01 -08:00
for (key, plan) in &mut self.pending {
2018-03-22 13:38:06 -07:00
plan.apply_witness(&Witness::Timestamp(self.last_time));
if plan.is_complete() {
2018-03-22 13:15:29 -07:00
complete_transaction(&mut self.balances, plan);
2018-03-10 23:30:01 -08:00
completed.push(key.clone());
}
}
for key in completed {
self.pending.remove(&key);
}
Ok(())
}
fn process_verified_event(self: &mut Self, event: &Event, allow_deposits: bool) -> Result<()> {
match *event {
Event::Transaction(ref tr) => self.process_verified_transaction(tr, allow_deposits),
Event::Signature { from, tx_sig, .. } => self.process_verified_sig(from, tx_sig),
Event::Timestamp { from, dt, .. } => self.process_verified_timestamp(from, dt),
}
}
2018-02-23 13:08:19 -08:00
pub fn transfer(
self: &mut Self,
n: i64,
keypair: &KeyPair,
2018-02-28 09:07:54 -08:00
to: PublicKey,
2018-03-20 22:15:44 -07:00
last_id: Hash,
) -> Result<Signature> {
2018-03-20 22:15:44 -07:00
let tr = Transaction::new(keypair, to, n, last_id);
let sig = tr.sig;
self.process_transaction(tr).map(|_| sig)
2018-02-23 13:08:19 -08:00
}
pub fn transfer_on_date(
self: &mut Self,
n: i64,
keypair: &KeyPair,
to: PublicKey,
dt: DateTime<Utc>,
2018-03-20 22:15:44 -07:00
last_id: Hash,
) -> Result<Signature> {
2018-03-20 22:15:44 -07:00
let tr = Transaction::new_on_date(keypair, to, dt, n, last_id);
let sig = tr.sig;
self.process_transaction(tr).map(|_| sig)
}
pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<i64> {
2018-03-22 13:15:29 -07:00
self.balances.get(pubkey).cloned()
2018-02-23 13:08:19 -08:00
}
}
#[cfg(test)]
mod tests {
use super::*;
use recorder::ExitReason;
use signature::KeyPairUtil;
2018-02-23 13:08:19 -08:00
#[test]
fn test_accountant() {
let alice = Mint::new(10_000);
let bob_pubkey = KeyPair::new().pubkey();
2018-03-03 23:13:40 -08:00
let mut acc = Accountant::new(&alice, Some(2));
2018-03-20 22:15:44 -07:00
acc.transfer(1_000, &alice.keypair(), bob_pubkey, alice.seed())
.unwrap();
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_000);
2018-02-23 13:08:19 -08:00
2018-03-20 22:15:44 -07:00
acc.transfer(500, &alice.keypair(), bob_pubkey, alice.seed())
.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!(
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() {
let alice = Mint::new(11_000);
2018-03-03 23:13:40 -08:00
let mut acc = Accountant::new(&alice, Some(2));
let bob_pubkey = KeyPair::new().pubkey();
2018-03-20 22:15:44 -07:00
acc.transfer(1_000, &alice.keypair(), bob_pubkey, alice.seed())
.unwrap();
assert_eq!(
2018-03-20 22:15:44 -07:00
acc.transfer(10_001, &alice.keypair(), bob_pubkey, alice.seed()),
Err(AccountingError::InsufficientFunds)
);
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!(
acc.historian.thread_hdl.join().unwrap(),
2018-02-27 10:28:10 -08:00
ExitReason::RecvDisconnected
);
}
#[test]
fn test_overspend_attack() {
let alice = Mint::new(1);
let mut acc = Accountant::new(&alice, None);
let bob_pubkey = KeyPair::new().pubkey();
let mut tr = Transaction::new(&alice.keypair(), bob_pubkey, 1, alice.seed());
2018-03-20 14:43:04 -07:00
if let Plan::Pay(ref mut payment) = tr.plan {
2018-03-19 09:03:41 -07:00
payment.tokens = 2; // <-- attack!
}
assert_eq!(
acc.process_transaction(tr.clone()),
Err(AccountingError::InvalidTransfer)
);
2018-03-19 09:03:41 -07:00
// Also, ensure all branchs of the plan spend all tokens
2018-03-20 14:43:04 -07:00
if let Plan::Pay(ref mut payment) = tr.plan {
2018-03-19 09:03:41 -07:00
payment.tokens = 0; // <-- whoops!
}
assert_eq!(
acc.process_transaction(tr.clone()),
Err(AccountingError::InvalidTransfer)
);
}
2018-02-27 10:28:10 -08:00
#[test]
fn test_transfer_to_newb() {
let alice = Mint::new(10_000);
2018-03-03 23:13:40 -08:00
let mut acc = Accountant::new(&alice, Some(2));
let alice_keypair = alice.keypair();
let bob_pubkey = KeyPair::new().pubkey();
2018-03-20 22:15:44 -07:00
acc.transfer(500, &alice_keypair, bob_pubkey, alice.seed())
.unwrap();
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(),
2018-02-27 10:28:10 -08:00
ExitReason::RecvDisconnected
);
}
#[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();
2018-03-20 22:15:44 -07:00
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.seed())
.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();
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.
2018-03-20 22:15:44 -07:00
acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.seed())
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));
}
#[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();
2018-03-20 22:15:44 -07:00
let sig = acc.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.seed())
.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);
acc.process_verified_sig(alice.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction.
assert_ne!(acc.get_balance(&alice.pubkey()), Some(2));
}
2018-02-23 13:08:19 -08:00
}