solana/src/accountant.rs

217 lines
6.3 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.
2018-03-03 23:13:40 -08:00
use log::{hash, Entry, Sha256Hash};
use event::{get_pubkey, sign_transaction_data, verify_event, Event, PublicKey, Signature};
2018-03-03 23:13:40 -08:00
use genesis::Genesis;
use historian::{reserve_signature, Historian};
2018-02-23 13:08:19 -08:00
use ring::signature::Ed25519KeyPair;
use std::sync::mpsc::SendError;
2018-02-23 13:08:19 -08:00
use std::collections::HashMap;
use std::result;
2018-03-04 06:28:51 -08:00
use std::thread::sleep;
use std::time::Duration;
#[derive(Debug, PartialEq, Eq)]
pub enum AccountingError {
InsufficientFunds,
InvalidEvent,
SendError,
}
pub type Result<T> = result::Result<T, AccountingError>;
2018-02-23 13:08:19 -08:00
pub struct Accountant {
pub historian: Historian<u64>,
pub balances: HashMap<PublicKey, u64>,
2018-03-04 06:34:38 -08:00
pub last_id: Sha256Hash,
2018-02-23 13:08:19 -08:00
}
impl Accountant {
2018-03-03 23:13:40 -08:00
pub fn new(gen: &Genesis, ms_per_tick: Option<u64>) -> Self {
let start_hash = hash(&gen.pkcs8);
let hist = Historian::<u64>::new(&start_hash, ms_per_tick);
let mut acc = Accountant {
2018-02-23 13:08:19 -08:00
historian: hist,
balances: HashMap::new(),
2018-03-04 06:34:38 -08:00
last_id: start_hash,
2018-03-03 23:13:40 -08:00
};
for (i, event) in gen.create_events().iter().enumerate() {
2018-03-03 23:13:40 -08:00
acc.process_verified_event(event, i < 2).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 sync(self: &mut Self) -> Vec<Entry<u64>> {
2018-02-28 17:04:35 -08:00
let mut entries = vec![];
2018-02-23 13:08:19 -08:00
while let Ok(entry) = self.historian.receiver.try_recv() {
2018-02-28 17:04:35 -08:00
entries.push(entry);
}
2018-02-28 17:04:35 -08:00
if let Some(last_entry) = entries.last() {
2018-03-04 06:34:38 -08:00
self.last_id = last_entry.id;
2018-02-28 17:04:35 -08:00
}
entries
2018-02-23 13:08:19 -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
}
pub fn process_event(self: &mut Self, event: Event<u64>) -> Result<()> {
if !verify_event(&event) {
return Err(AccountingError::InvalidEvent);
}
if let Event::Transaction { from, data, .. } = event {
if self.get_balance(&from).unwrap_or(0) < data {
return Err(AccountingError::InsufficientFunds);
}
}
self.process_verified_event(&event, false)?;
if let Err(SendError(_)) = self.historian.sender.send(event) {
return Err(AccountingError::SendError);
}
Ok(())
}
fn process_verified_event(
self: &mut Self,
event: &Event<u64>,
allow_deposits: bool,
) -> Result<()> {
if !reserve_signature(&mut self.historian.signatures, event) {
return Err(AccountingError::InvalidEvent);
}
if let Event::Transaction { from, to, data, .. } = *event {
if !Self::is_deposit(allow_deposits, &from, &to) {
if let Some(x) = self.balances.get_mut(&from) {
*x -= data;
}
}
if self.balances.contains_key(&to) {
if let Some(x) = self.balances.get_mut(&to) {
*x += data;
}
} else {
self.balances.insert(to, data);
}
}
Ok(())
2018-02-23 13:08:19 -08:00
}
pub fn transfer(
self: &mut Self,
n: u64,
keypair: &Ed25519KeyPair,
2018-02-28 09:07:54 -08:00
to: PublicKey,
) -> Result<Signature> {
2018-02-28 09:07:54 -08:00
let from = get_pubkey(keypair);
let sig = sign_transaction_data(&n, keypair, &to);
let event = Event::Transaction {
from,
to,
data: n,
sig,
};
self.process_event(event).map(|_| sig)
2018-02-23 13:08:19 -08:00
}
pub fn get_balance(self: &Self, pubkey: &PublicKey) -> Option<u64> {
self.balances.get(pubkey).map(|x| *x)
2018-02-23 13:08:19 -08:00
}
pub fn wait_on_signature(self: &mut Self, wait_sig: &Signature) {
let mut entries = self.sync();
let mut found = false;
while !found {
found = entries.iter().any(|e| match e.event {
Event::Transaction { sig, .. } => sig == *wait_sig,
_ => false,
});
if !found {
sleep(Duration::from_millis(30));
entries = self.sync();
}
}
}
2018-02-23 13:08:19 -08:00
}
#[cfg(test)]
mod tests {
use super::*;
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-03-04 06:28:51 -08:00
use std::thread::sleep;
use std::time::Duration;
2018-02-23 13:08:19 -08:00
#[test]
fn test_accountant() {
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-03 23:13:40 -08:00
let sig = acc.transfer(500, &alice.get_keypair(), bob_pubkey).unwrap();
acc.wait_on_signature(&sig);
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 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-02-27 10:28:10 -08:00
assert_eq!(
2018-03-03 23:13:40 -08:00
acc.transfer(10_001, &alice.get_keypair(), bob_pubkey),
Err(AccountingError::InsufficientFunds)
);
2018-02-27 10:28:10 -08:00
sleep(Duration::from_millis(30));
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!(
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);
let sig = acc.transfer(500, &alice_keypair, bob_pubkey).unwrap();
acc.wait_on_signature(&sig);
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
);
}
2018-02-23 13:08:19 -08:00
}