2018-05-14 14:33:11 -07:00
|
|
|
//! The `bank` 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:07:11 -07:00
|
|
|
extern crate libc;
|
|
|
|
|
2018-03-26 21:03:26 -07:00
|
|
|
use chrono::prelude::*;
|
2018-05-10 13:01:42 -07:00
|
|
|
use entry::Entry;
|
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-04-05 21:13:54 -07:00
|
|
|
use rayon::prelude::*;
|
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-04-04 16:03:18 -07:00
|
|
|
use std::collections::{HashMap, HashSet, VecDeque};
|
2018-03-02 09:16:39 -08:00
|
|
|
use std::result;
|
2018-05-11 11:38:52 -07:00
|
|
|
use std::sync::RwLock;
|
2018-05-14 05:49:48 -07:00
|
|
|
use std::sync::atomic::{AtomicIsize, AtomicUsize, Ordering};
|
2018-03-26 21:03:26 -07:00
|
|
|
use transaction::Transaction;
|
2018-03-02 09:16:39 -08:00
|
|
|
|
2018-04-11 13:05:29 -07:00
|
|
|
pub const MAX_ENTRY_IDS: usize = 1024 * 4;
|
2018-04-05 09:26:43 -07:00
|
|
|
|
2018-03-02 09:16:39 -08:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2018-05-14 14:33:11 -07:00
|
|
|
pub enum BankError {
|
2018-05-23 11:26:32 -07:00
|
|
|
AccountNotFound(PublicKey),
|
|
|
|
InsufficientFunds(PublicKey),
|
|
|
|
InvalidTransferSignature(Signature),
|
2018-03-02 09:16:39 -08:00
|
|
|
}
|
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
pub type Result<T> = result::Result<T, BankError>;
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-03-20 16:47:55 -07:00
|
|
|
/// Commit funds to the 'to' party.
|
2018-05-01 13:22:33 -07:00
|
|
|
fn apply_payment(balances: &RwLock<HashMap<PublicKey, AtomicIsize>>, payment: &Payment) {
|
2018-05-10 18:07:12 -07:00
|
|
|
// First we check balances with a read lock to maximize potential parallelization.
|
2018-05-11 11:38:52 -07:00
|
|
|
if balances
|
|
|
|
.read()
|
|
|
|
.expect("'balances' read lock in apply_payment")
|
|
|
|
.contains_key(&payment.to)
|
|
|
|
{
|
2018-05-09 18:16:37 -07:00
|
|
|
let bals = balances.read().expect("'balances' read lock");
|
2018-05-01 13:22:33 -07:00
|
|
|
bals[&payment.to].fetch_add(payment.tokens as isize, Ordering::Relaxed);
|
2018-04-04 15:31:11 -07:00
|
|
|
} else {
|
2018-05-10 18:07:12 -07:00
|
|
|
// Now we know the key wasn't present a nanosecond ago, but it might be there
|
|
|
|
// by the time we aquire a write lock, so we'll have to check again.
|
2018-05-09 18:16:37 -07:00
|
|
|
let mut bals = balances.write().expect("'balances' write lock");
|
2018-05-10 18:07:12 -07:00
|
|
|
if bals.contains_key(&payment.to) {
|
|
|
|
bals[&payment.to].fetch_add(payment.tokens as isize, Ordering::Relaxed);
|
|
|
|
} else {
|
|
|
|
bals.insert(payment.to, AtomicIsize::new(payment.tokens as isize));
|
|
|
|
}
|
2018-04-04 15:31:11 -07:00
|
|
|
}
|
2018-03-20 16:47:55 -07:00
|
|
|
}
|
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
pub struct Bank {
|
2018-05-01 13:22:33 -07:00
|
|
|
balances: RwLock<HashMap<PublicKey, AtomicIsize>>,
|
2018-04-04 11:33:00 -07:00
|
|
|
pending: RwLock<HashMap<Signature, Plan>>,
|
2018-04-04 16:03:18 -07:00
|
|
|
last_ids: RwLock<VecDeque<(Hash, RwLock<HashSet<Signature>>)>>,
|
2018-04-04 11:33:00 -07:00
|
|
|
time_sources: RwLock<HashSet<PublicKey>>,
|
|
|
|
last_time: RwLock<DateTime<Utc>>,
|
2018-05-14 05:49:48 -07:00
|
|
|
transaction_count: AtomicUsize,
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
impl Bank {
|
|
|
|
/// Create an Bank using a deposit.
|
2018-04-02 13:41:07 -07:00
|
|
|
pub fn new_from_deposit(deposit: &Payment) -> Self {
|
2018-04-04 15:31:11 -07:00
|
|
|
let balances = RwLock::new(HashMap::new());
|
|
|
|
apply_payment(&balances, deposit);
|
2018-05-14 14:33:11 -07:00
|
|
|
Bank {
|
2018-04-04 15:31:11 -07:00
|
|
|
balances,
|
2018-04-04 11:33:00 -07:00
|
|
|
pending: RwLock::new(HashMap::new()),
|
2018-04-04 16:03:18 -07:00
|
|
|
last_ids: RwLock::new(VecDeque::new()),
|
2018-04-04 11:33:00 -07:00
|
|
|
time_sources: RwLock::new(HashSet::new()),
|
|
|
|
last_time: RwLock::new(Utc.timestamp(0, 0)),
|
2018-05-14 05:49:48 -07:00
|
|
|
transaction_count: AtomicUsize::new(0),
|
2018-04-02 12:51:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
/// Create an Bank 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-05-14 14:33:11 -07:00
|
|
|
let bank = Self::new_from_deposit(&deposit);
|
|
|
|
bank.register_entry_id(&mint.last_id());
|
|
|
|
bank
|
2018-04-02 12:51:44 -07:00
|
|
|
}
|
|
|
|
|
2018-05-03 12:24:35 -07:00
|
|
|
/// Return the last entry ID registered
|
|
|
|
pub fn last_id(&self) -> Hash {
|
2018-05-09 18:16:37 -07:00
|
|
|
let last_ids = self.last_ids.read().expect("'last_ids' read lock");
|
2018-05-09 17:19:12 -07:00
|
|
|
let last_item = last_ids.iter().last().expect("empty 'last_ids' list");
|
2018-05-03 12:24:35 -07:00
|
|
|
last_item.0
|
|
|
|
}
|
|
|
|
|
2018-04-04 16:03:18 -07:00
|
|
|
fn reserve_signature(signatures: &RwLock<HashSet<Signature>>, sig: &Signature) -> bool {
|
2018-05-11 11:38:52 -07:00
|
|
|
if signatures
|
|
|
|
.read()
|
|
|
|
.expect("'signatures' read lock")
|
|
|
|
.contains(sig)
|
|
|
|
{
|
2018-04-02 08:36:22 -07:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-11 11:38:52 -07:00
|
|
|
signatures
|
|
|
|
.write()
|
|
|
|
.expect("'signatures' write lock")
|
|
|
|
.insert(*sig);
|
2018-04-04 16:03:18 -07:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2018-04-26 04:22:11 -07:00
|
|
|
fn forget_signature(signatures: &RwLock<HashSet<Signature>>, sig: &Signature) -> bool {
|
2018-05-11 11:38:52 -07:00
|
|
|
signatures
|
|
|
|
.write()
|
|
|
|
.expect("'signatures' write lock in forget_signature")
|
|
|
|
.remove(sig)
|
2018-04-26 04:22:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn forget_signature_with_last_id(&self, sig: &Signature, last_id: &Hash) -> bool {
|
|
|
|
if let Some(entry) = self.last_ids
|
|
|
|
.read()
|
2018-05-09 18:16:37 -07:00
|
|
|
.expect("'last_ids' read lock in forget_signature_with_last_id")
|
2018-04-26 04:22:11 -07:00
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find(|x| x.0 == *last_id)
|
|
|
|
{
|
|
|
|
return Self::forget_signature(&entry.1, sig);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-04 16:03:18 -07:00
|
|
|
fn reserve_signature_with_last_id(&self, sig: &Signature, last_id: &Hash) -> bool {
|
|
|
|
if let Some(entry) = self.last_ids
|
|
|
|
.read()
|
2018-05-09 18:16:37 -07:00
|
|
|
.expect("'last_ids' read lock in reserve_signature_with_last_id")
|
2018-04-04 16:03:18 -07:00
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find(|x| x.0 == *last_id)
|
|
|
|
{
|
|
|
|
return Self::reserve_signature(&entry.1, sig);
|
|
|
|
}
|
2018-04-05 08:53:58 -07:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
/// Tell the bank which Entry IDs exist on the ledger. This function
|
2018-04-05 08:53:58 -07:00
|
|
|
/// assumes subsequent calls correspond to later entries, and will boot
|
|
|
|
/// the oldest ones once its internal cache is full. Once boot, the
|
2018-05-14 14:33:11 -07:00
|
|
|
/// bank will reject transactions using that `last_id`.
|
2018-04-05 08:53:58 -07:00
|
|
|
pub fn register_entry_id(&self, last_id: &Hash) {
|
2018-05-11 11:38:52 -07:00
|
|
|
let mut last_ids = self.last_ids
|
|
|
|
.write()
|
|
|
|
.expect("'last_ids' write lock in register_entry_id");
|
2018-04-05 09:26:43 -07:00
|
|
|
if last_ids.len() >= MAX_ENTRY_IDS {
|
|
|
|
last_ids.pop_front();
|
|
|
|
}
|
|
|
|
last_ids.push_back((*last_id, RwLock::new(HashSet::new())));
|
2018-04-02 08:36:22 -07:00
|
|
|
}
|
|
|
|
|
2018-04-05 19:05:37 -07:00
|
|
|
/// Deduct tokens from the 'from' address the account has sufficient
|
|
|
|
/// funds and isn't a duplicate.
|
|
|
|
pub fn process_verified_transaction_debits(&self, tr: &Transaction) -> Result<()> {
|
2018-05-22 15:09:38 -07:00
|
|
|
info!("Transaction {}", tr.contract.tokens);
|
2018-05-11 11:38:52 -07:00
|
|
|
let bals = self.balances
|
|
|
|
.read()
|
|
|
|
.expect("'balances' read lock in process_verified_transaction_debits");
|
2018-04-05 20:00:08 -07:00
|
|
|
let option = bals.get(&tr.from);
|
2018-05-01 13:22:33 -07:00
|
|
|
|
2018-04-05 20:00:08 -07:00
|
|
|
if option.is_none() {
|
2018-05-23 11:26:32 -07:00
|
|
|
return Err(BankError::AccountNotFound(tr.from));
|
2018-04-05 20:00:08 -07:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:05:21 -07:00
|
|
|
if !self.reserve_signature_with_last_id(&tr.sig, &tr.last_id) {
|
2018-05-23 11:26:32 -07:00
|
|
|
return Err(BankError::InvalidTransferSignature(tr.sig));
|
2018-03-04 21:26:46 -08:00
|
|
|
}
|
2018-03-03 21:25:37 -08:00
|
|
|
|
2018-05-02 07:15:08 -07:00
|
|
|
loop {
|
2018-05-09 18:16:37 -07:00
|
|
|
let bal = option.expect("assignment of option to bal");
|
2018-05-02 07:15:08 -07:00
|
|
|
let current = bal.load(Ordering::Relaxed) as i64;
|
2018-05-01 13:22:33 -07:00
|
|
|
|
2018-05-22 15:09:38 -07:00
|
|
|
if current < tr.contract.tokens {
|
2018-05-22 15:05:21 -07:00
|
|
|
self.forget_signature_with_last_id(&tr.sig, &tr.last_id);
|
2018-05-23 11:26:32 -07:00
|
|
|
return Err(BankError::InsufficientFunds(tr.from));
|
2018-05-02 07:15:08 -07:00
|
|
|
}
|
2018-03-03 21:25:37 -08:00
|
|
|
|
2018-05-02 07:15:08 -07:00
|
|
|
let result = bal.compare_exchange(
|
|
|
|
current as isize,
|
2018-05-22 15:09:38 -07:00
|
|
|
(current - tr.contract.tokens) as isize,
|
2018-05-02 07:15:08 -07:00
|
|
|
Ordering::Relaxed,
|
|
|
|
Ordering::Relaxed,
|
|
|
|
);
|
|
|
|
|
|
|
|
match result {
|
2018-05-14 05:49:48 -07:00
|
|
|
Ok(_) => {
|
|
|
|
self.transaction_count.fetch_add(1, Ordering::Relaxed);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-05-02 07:15:08 -07:00
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
}
|
2018-04-05 19:05:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_verified_transaction_credits(&self, tr: &Transaction) {
|
2018-05-22 15:09:38 -07:00
|
|
|
let mut plan = tr.contract.plan.clone();
|
2018-05-11 11:38:52 -07:00
|
|
|
plan.apply_witness(&Witness::Timestamp(*self.last_time
|
|
|
|
.read()
|
2018-05-09 18:16:37 -07:00
|
|
|
.expect("timestamp creation in process_verified_transaction_credits")));
|
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 15:31:11 -07:00
|
|
|
apply_payment(&self.balances, payment);
|
2018-03-20 15:52:47 -07:00
|
|
|
} else {
|
2018-05-11 11:38:52 -07:00
|
|
|
let mut pending = self.pending
|
|
|
|
.write()
|
|
|
|
.expect("'pending' write lock in process_verified_transaction_credits");
|
2018-04-05 19:05:37 -07:00
|
|
|
pending.insert(tr.sig, plan);
|
2018-03-07 21:25:45 -08:00
|
|
|
}
|
2018-04-05 19:05:37 -07:00
|
|
|
}
|
2018-03-07 21:25:45 -08:00
|
|
|
|
2018-04-05 19:05:37 -07:00
|
|
|
/// Process a Transaction that has already been verified.
|
|
|
|
pub fn process_verified_transaction(&self, tr: &Transaction) -> Result<()> {
|
2018-05-02 07:44:41 -07:00
|
|
|
self.process_verified_transaction_debits(tr)?;
|
|
|
|
self.process_verified_transaction_credits(tr);
|
|
|
|
Ok(())
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
2018-04-05 21:13:54 -07:00
|
|
|
/// Process a batch of verified transactions.
|
2018-04-11 10:17:00 -07:00
|
|
|
pub fn process_verified_transactions(&self, trs: Vec<Transaction>) -> Vec<Result<Transaction>> {
|
2018-04-05 21:13:54 -07:00
|
|
|
// Run all debits first to filter out any transactions that can't be processed
|
|
|
|
// in parallel deterministically.
|
2018-04-12 19:53:34 -07:00
|
|
|
let results: Vec<_> = trs.into_par_iter()
|
2018-05-02 07:44:41 -07:00
|
|
|
.map(|tr| self.process_verified_transaction_debits(&tr).map(|_| tr))
|
2018-04-12 19:53:34 -07:00
|
|
|
.collect(); // Calling collect() here forces all debits to complete before moving on.
|
|
|
|
|
|
|
|
results
|
|
|
|
.into_par_iter()
|
2018-04-11 10:17:00 -07:00
|
|
|
.map(|result| {
|
|
|
|
result.map(|tr| {
|
|
|
|
self.process_verified_transaction_credits(&tr);
|
|
|
|
tr
|
|
|
|
})
|
|
|
|
})
|
2018-04-05 21:13:54 -07:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2018-04-12 19:53:34 -07:00
|
|
|
fn partition_events(events: Vec<Event>) -> (Vec<Transaction>, Vec<Event>) {
|
|
|
|
let mut trs = vec![];
|
|
|
|
let mut rest = vec![];
|
|
|
|
for event in events {
|
|
|
|
match event {
|
|
|
|
Event::Transaction(tr) => trs.push(tr),
|
|
|
|
_ => rest.push(event),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(trs, rest)
|
|
|
|
}
|
|
|
|
|
2018-05-07 13:51:08 -07:00
|
|
|
pub fn process_verified_events(&self, events: Vec<Event>) -> Vec<Result<Event>> {
|
2018-04-12 19:53:34 -07:00
|
|
|
let (trs, rest) = Self::partition_events(events);
|
2018-05-07 13:51:08 -07:00
|
|
|
let mut results: Vec<_> = self.process_verified_transactions(trs)
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| x.map(Event::Transaction))
|
|
|
|
.collect();
|
|
|
|
|
2018-04-12 19:53:34 -07:00
|
|
|
for event in rest {
|
2018-05-07 13:51:08 -07:00
|
|
|
results.push(self.process_verified_event(event));
|
2018-04-12 19:53:34 -07:00
|
|
|
}
|
2018-05-07 13:51:08 -07:00
|
|
|
|
|
|
|
results
|
2018-04-12 19:53:34 -07:00
|
|
|
}
|
|
|
|
|
2018-05-10 13:01:42 -07:00
|
|
|
pub fn process_verified_entries(&self, entries: Vec<Entry>) -> Result<()> {
|
|
|
|
for entry in entries {
|
|
|
|
self.register_entry_id(&entry.id);
|
|
|
|
for result in self.process_verified_events(entry.events) {
|
|
|
|
result?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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<()> {
|
2018-05-11 11:38:52 -07:00
|
|
|
if let Occupied(mut e) = self.pending
|
|
|
|
.write()
|
|
|
|
.expect("write() in process_verified_sig")
|
|
|
|
.entry(tx_sig)
|
|
|
|
{
|
2018-03-22 13:38:06 -07:00
|
|
|
e.get_mut().apply_witness(&Witness::Signature(from));
|
2018-05-15 04:35:41 -07:00
|
|
|
if let Some(payment) = e.get().final_payment() {
|
|
|
|
apply_payment(&self.balances, &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-05-11 11:38:52 -07:00
|
|
|
if *self.last_time
|
|
|
|
.read()
|
|
|
|
.expect("'last_time' read lock on first timestamp check")
|
|
|
|
== Utc.timestamp(0, 0)
|
|
|
|
{
|
|
|
|
self.time_sources
|
|
|
|
.write()
|
|
|
|
.expect("'time_sources' write lock on first timestamp")
|
|
|
|
.insert(from);
|
2018-03-08 09:05:00 -08:00
|
|
|
}
|
|
|
|
|
2018-05-11 11:38:52 -07:00
|
|
|
if self.time_sources
|
|
|
|
.read()
|
|
|
|
.expect("'time_sources' read lock")
|
|
|
|
.contains(&from)
|
|
|
|
{
|
2018-05-09 18:16:37 -07:00
|
|
|
if dt > *self.last_time.read().expect("'last_time' read lock") {
|
|
|
|
*self.last_time.write().expect("'last_time' write lock") = 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'.
|
2018-05-11 11:38:52 -07:00
|
|
|
let mut pending = self.pending
|
|
|
|
.write()
|
|
|
|
.expect("'pending' write lock in process_verified_timestamp");
|
2018-04-04 11:33:00 -07:00
|
|
|
for (key, plan) in pending.iter_mut() {
|
2018-05-11 11:38:52 -07:00
|
|
|
plan.apply_witness(&Witness::Timestamp(*self.last_time
|
|
|
|
.read()
|
|
|
|
.expect("'last_time' read lock when creating timestamp")));
|
2018-04-02 12:51:44 -07:00
|
|
|
if let Some(ref payment) = plan.final_payment() {
|
2018-04-04 15:31:11 -07:00
|
|
|
apply_payment(&self.balances, 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-05-07 13:51:08 -07:00
|
|
|
pub fn process_verified_event(&self, event: Event) -> Result<Event> {
|
|
|
|
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-05-07 13:51:08 -07:00
|
|
|
}?;
|
|
|
|
Ok(event)
|
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-05-11 11:38:52 -07:00
|
|
|
let bals = self.balances
|
|
|
|
.read()
|
|
|
|
.expect("'balances' read lock in get_balance");
|
2018-05-01 13:22:33 -07:00
|
|
|
bals.get(pubkey).map(|x| x.load(Ordering::Relaxed) as i64)
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
2018-05-14 05:49:48 -07:00
|
|
|
|
|
|
|
pub fn transaction_count(&self) -> usize {
|
|
|
|
self.transaction_count.load(Ordering::Relaxed)
|
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-04-05 09:26:43 -07:00
|
|
|
use bincode::serialize;
|
2018-04-05 21:39:07 -07:00
|
|
|
use hash::hash;
|
|
|
|
use signature::KeyPairUtil;
|
2018-02-23 13:08:19 -08:00
|
|
|
|
|
|
|
#[test]
|
2018-05-14 14:33:11 -07:00
|
|
|
fn test_bank() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(10_000);
|
|
|
|
let pubkey = KeyPair::new().pubkey();
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
assert_eq!(bank.last_id(), mint.last_id());
|
2018-05-03 12:24:35 -07:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer(1_000, &mint.keypair(), pubkey, mint.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-05-14 14:39:34 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey).unwrap(), 1_000);
|
2018-02-23 13:08:19 -08:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer(500, &mint.keypair(), pubkey, mint.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-05-14 14:39:34 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey).unwrap(), 1_500);
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 2);
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
2018-02-27 10:28:10 -08:00
|
|
|
|
2018-04-05 20:00:08 -07:00
|
|
|
#[test]
|
|
|
|
fn test_account_not_found() {
|
|
|
|
let mint = Mint::new(1);
|
2018-05-14 14:33:11 -07:00
|
|
|
let bank = Bank::new(&mint);
|
2018-05-23 11:26:32 -07:00
|
|
|
let keypair = KeyPair::new();
|
2018-04-05 20:00:08 -07:00
|
|
|
assert_eq!(
|
2018-05-23 11:26:32 -07:00
|
|
|
bank.transfer(1, &keypair, mint.pubkey(), mint.last_id()),
|
|
|
|
Err(BankError::AccountNotFound(keypair.pubkey()))
|
2018-04-05 20:00:08 -07:00
|
|
|
);
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 0);
|
2018-04-05 20:00:08 -07:00
|
|
|
}
|
|
|
|
|
2018-02-27 10:28:10 -08:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_transfer() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(11_000);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
let pubkey = KeyPair::new().pubkey();
|
|
|
|
bank.transfer(1_000, &mint.keypair(), pubkey, mint.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-03-02 09:16:39 -08:00
|
|
|
assert_eq!(
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer(10_001, &mint.keypair(), pubkey, mint.last_id()),
|
2018-05-23 11:26:32 -07:00
|
|
|
Err(BankError::InsufficientFunds(mint.pubkey()))
|
2018-03-02 09:16:39 -08:00
|
|
|
);
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-03-01 11:23:27 -08:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint_pubkey = mint.keypair().pubkey();
|
|
|
|
assert_eq!(bank.get_balance(&mint_pubkey).unwrap(), 10_000);
|
|
|
|
assert_eq!(bank.get_balance(&pubkey).unwrap(), 1_000);
|
2018-02-27 10:28:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_to_newb() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(10_000);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
let pubkey = KeyPair::new().pubkey();
|
|
|
|
bank.transfer(500, &mint.keypair(), pubkey, mint.last_id())
|
2018-03-20 22:15:44 -07:00
|
|
|
.unwrap();
|
2018-05-14 14:39:34 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey).unwrap(), 500);
|
2018-02-27 10:28:10 -08:00
|
|
|
}
|
2018-03-08 07:58:01 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_on_date() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
let pubkey = KeyPair::new().pubkey();
|
2018-03-08 07:58:01 -08:00
|
|
|
let dt = Utc::now();
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id())
|
2018-03-08 07:58:01 -08:00
|
|
|
.unwrap();
|
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
// Mint's balance will be zero because all funds are locked up.
|
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), Some(0));
|
2018-03-08 07:58:01 -08:00
|
|
|
|
2018-05-14 05:49:48 -07:00
|
|
|
// tx count is 1, because debits were applied.
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-05-14 05:49:48 -07:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
// pubkey's balance will be None because the funds have not been
|
2018-03-08 07:58:01 -08:00
|
|
|
// sent.
|
2018-05-14 14:39:34 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), None);
|
2018-03-08 07:58:01 -08:00
|
|
|
|
|
|
|
// Now, acknowledge the time in the condition occurred and
|
2018-05-14 14:39:34 -07:00
|
|
|
// that pubkey's funds are now available.
|
|
|
|
bank.process_verified_timestamp(mint.pubkey(), dt).unwrap();
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), Some(1));
|
2018-03-08 10:06:52 -08:00
|
|
|
|
2018-05-14 05:49:48 -07:00
|
|
|
// tx count is still 1, because we chose not to count timestamp events
|
|
|
|
// tx count.
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-05-14 05:49:48 -07:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.process_verified_timestamp(mint.pubkey(), dt).unwrap(); // <-- Attack! Attempt to process completed transaction.
|
|
|
|
assert_ne!(bank.get_balance(&pubkey), Some(2));
|
2018-03-08 10:06:52 -08:00
|
|
|
}
|
|
|
|
|
2018-03-08 14:39:03 -08:00
|
|
|
#[test]
|
|
|
|
fn test_transfer_after_date() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
let pubkey = KeyPair::new().pubkey();
|
2018-03-08 14:39:03 -08:00
|
|
|
let dt = Utc::now();
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.process_verified_timestamp(mint.pubkey(), dt).unwrap();
|
2018-03-08 14:39:03 -08:00
|
|
|
|
|
|
|
// It's now past now, so this transfer should be processed immediately.
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id())
|
2018-03-08 14:39:03 -08:00
|
|
|
.unwrap();
|
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), Some(0));
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), Some(1));
|
2018-03-08 14:39:03 -08:00
|
|
|
}
|
|
|
|
|
2018-03-08 10:06:52 -08:00
|
|
|
#[test]
|
|
|
|
fn test_cancel_transfer() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
|
|
|
let pubkey = KeyPair::new().pubkey();
|
2018-03-08 10:06:52 -08:00
|
|
|
let dt = Utc::now();
|
2018-05-14 14:39:34 -07:00
|
|
|
let sig = bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id())
|
2018-03-08 10:06:52 -08:00
|
|
|
.unwrap();
|
|
|
|
|
2018-05-14 05:49:48 -07:00
|
|
|
// Assert the debit counts as a transaction.
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-05-14 05:49:48 -07:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
// Mint's balance will be zero because all funds are locked up.
|
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), Some(0));
|
2018-03-08 10:06:52 -08:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
// pubkey's balance will be None because the funds have not been
|
2018-03-08 10:06:52 -08:00
|
|
|
// sent.
|
2018-05-14 14:39:34 -07:00
|
|
|
assert_eq!(bank.get_balance(&pubkey), None);
|
2018-03-08 10:06:52 -08:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
// Now, cancel the trancaction. Mint gets her funds back, pubkey never sees them.
|
|
|
|
bank.process_verified_sig(mint.pubkey(), sig).unwrap();
|
|
|
|
assert_eq!(bank.get_balance(&mint.pubkey()), Some(1));
|
|
|
|
assert_eq!(bank.get_balance(&pubkey), None);
|
2018-03-08 07:58:01 -08:00
|
|
|
|
2018-05-14 05:49:48 -07:00
|
|
|
// Assert cancel doesn't cause count to go backward.
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-05-14 05:49:48 -07:00
|
|
|
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.process_verified_sig(mint.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction.
|
|
|
|
assert_ne!(bank.get_balance(&mint.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() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-04-02 08:36:22 -07:00
|
|
|
let sig = Signature::default();
|
2018-05-14 14:39:34 -07:00
|
|
|
assert!(bank.reserve_signature_with_last_id(&sig, &mint.last_id()));
|
|
|
|
assert!(!bank.reserve_signature_with_last_id(&sig, &mint.last_id()));
|
2018-04-02 08:36:22 -07:00
|
|
|
}
|
2018-04-05 09:26:43 -07:00
|
|
|
|
2018-04-26 04:22:11 -07:00
|
|
|
#[test]
|
|
|
|
fn test_forget_signature() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-04-26 04:22:11 -07:00
|
|
|
let sig = Signature::default();
|
2018-05-14 14:39:34 -07:00
|
|
|
bank.reserve_signature_with_last_id(&sig, &mint.last_id());
|
|
|
|
assert!(bank.forget_signature_with_last_id(&sig, &mint.last_id()));
|
|
|
|
assert!(!bank.forget_signature_with_last_id(&sig, &mint.last_id()));
|
2018-04-26 04:22:11 -07:00
|
|
|
}
|
|
|
|
|
2018-04-05 09:26:43 -07:00
|
|
|
#[test]
|
|
|
|
fn test_max_entry_ids() {
|
2018-05-14 14:39:34 -07:00
|
|
|
let mint = Mint::new(1);
|
|
|
|
let bank = Bank::new(&mint);
|
2018-04-05 09:26:43 -07:00
|
|
|
let sig = Signature::default();
|
|
|
|
for i in 0..MAX_ENTRY_IDS {
|
|
|
|
let last_id = hash(&serialize(&i).unwrap()); // Unique hash
|
2018-05-14 14:33:11 -07:00
|
|
|
bank.register_entry_id(&last_id);
|
2018-04-05 09:26:43 -07:00
|
|
|
}
|
|
|
|
// Assert we're no longer able to use the oldest entry ID.
|
2018-05-14 14:39:34 -07:00
|
|
|
assert!(!bank.reserve_signature_with_last_id(&sig, &mint.last_id()));
|
2018-04-05 09:26:43 -07:00
|
|
|
}
|
2018-04-12 19:53:34 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_debits_before_credits() {
|
|
|
|
let mint = Mint::new(2);
|
2018-05-14 14:33:11 -07:00
|
|
|
let bank = Bank::new(&mint);
|
2018-05-14 14:39:34 -07:00
|
|
|
let keypair = KeyPair::new();
|
|
|
|
let tr0 = Transaction::new(&mint.keypair(), keypair.pubkey(), 2, mint.last_id());
|
|
|
|
let tr1 = Transaction::new(&keypair, mint.pubkey(), 1, mint.last_id());
|
2018-04-12 19:53:34 -07:00
|
|
|
let trs = vec![tr0, tr1];
|
2018-05-14 14:33:11 -07:00
|
|
|
let results = bank.process_verified_transactions(trs);
|
2018-05-14 05:49:48 -07:00
|
|
|
assert!(results[1].is_err());
|
|
|
|
|
|
|
|
// Assert bad transactions aren't counted.
|
2018-05-14 14:33:11 -07:00
|
|
|
assert_eq!(bank.transaction_count(), 1);
|
2018-04-12 19:53:34 -07:00
|
|
|
}
|
2018-02-23 13:08:19 -08:00
|
|
|
}
|
2018-04-04 09:28:12 -07:00
|
|
|
|
|
|
|
#[cfg(all(feature = "unstable", test))]
|
|
|
|
mod bench {
|
|
|
|
extern crate test;
|
|
|
|
use self::test::Bencher;
|
2018-05-14 14:33:11 -07:00
|
|
|
use bank::*;
|
2018-04-04 16:29:22 -07:00
|
|
|
use bincode::serialize;
|
2018-04-05 21:39:07 -07:00
|
|
|
use hash::hash;
|
|
|
|
use signature::KeyPairUtil;
|
2018-04-04 09:28:12 -07:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn process_verified_event_bench(bencher: &mut Bencher) {
|
2018-04-04 15:01:43 -07:00
|
|
|
let mint = Mint::new(100_000_000);
|
2018-05-14 14:33:11 -07:00
|
|
|
let bank = Bank::new(&mint);
|
2018-04-04 15:01:43 -07:00
|
|
|
// Create transactions between unrelated parties.
|
2018-04-04 09:28:12 -07:00
|
|
|
let transactions: Vec<_> = (0..4096)
|
|
|
|
.into_par_iter()
|
2018-04-04 16:29:22 -07:00
|
|
|
.map(|i| {
|
2018-04-04 15:31:11 -07:00
|
|
|
// Seed the 'from' account.
|
2018-04-04 15:01:43 -07:00
|
|
|
let rando0 = KeyPair::new();
|
|
|
|
let tr = Transaction::new(&mint.keypair(), rando0.pubkey(), 1_000, mint.last_id());
|
2018-05-14 14:33:11 -07:00
|
|
|
bank.process_verified_transaction(&tr).unwrap();
|
2018-04-04 15:31:11 -07:00
|
|
|
|
2018-04-04 16:29:22 -07:00
|
|
|
// Seed the 'to' account and a cell for its signature.
|
|
|
|
let last_id = hash(&serialize(&i).unwrap()); // Unique hash
|
2018-05-14 14:33:11 -07:00
|
|
|
bank.register_entry_id(&last_id);
|
2018-04-05 08:53:58 -07:00
|
|
|
|
2018-04-04 15:01:43 -07:00
|
|
|
let rando1 = KeyPair::new();
|
2018-04-04 16:29:22 -07:00
|
|
|
let tr = Transaction::new(&rando0, rando1.pubkey(), 1, last_id);
|
2018-05-14 14:33:11 -07:00
|
|
|
bank.process_verified_transaction(&tr).unwrap();
|
2018-04-04 15:31:11 -07:00
|
|
|
|
|
|
|
// Finally, return a transaction that's unique
|
2018-04-04 16:29:22 -07:00
|
|
|
Transaction::new(&rando0, rando1.pubkey(), 1, last_id)
|
2018-04-04 09:28:12 -07:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
bencher.iter(|| {
|
2018-04-04 15:31:11 -07:00
|
|
|
// Since benchmarker runs this multiple times, we need to clear the signatures.
|
2018-05-14 14:33:11 -07:00
|
|
|
for sigs in bank.last_ids.read().unwrap().iter() {
|
2018-04-11 15:40:25 -07:00
|
|
|
sigs.1.write().unwrap().clear();
|
2018-04-04 16:29:22 -07:00
|
|
|
}
|
2018-04-04 15:31:11 -07:00
|
|
|
|
2018-04-05 21:13:54 -07:00
|
|
|
assert!(
|
2018-05-14 14:33:11 -07:00
|
|
|
bank.process_verified_transactions(transactions.clone())
|
2018-04-05 21:13:54 -07:00
|
|
|
.iter()
|
|
|
|
.all(|x| x.is_ok())
|
|
|
|
);
|
2018-04-04 09:28:12 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|