solana/src/accountant.rs

250 lines
7.4 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 log::{Entry, Sha256Hash};
use event::{Event, PublicKey, Signature};
2018-02-23 13:08:19 -08:00
use historian::Historian;
use ring::signature::Ed25519KeyPair;
use std::sync::mpsc::SendError;
2018-02-23 13:08:19 -08:00
use std::collections::HashMap;
use std::result;
#[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>,
pub end_hash: Sha256Hash,
}
impl Accountant {
pub fn new(start_hash: &Sha256Hash, ms_per_tick: Option<u64>) -> Self {
let hist = Historian::<u64>::new(start_hash, ms_per_tick);
Accountant {
historian: hist,
balances: HashMap::new(),
end_hash: *start_hash,
}
}
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() {
self.end_hash = last_entry.end_hash;
}
entries
2018-02-23 13:08:19 -08:00
}
pub fn deposit_signed(self: &mut Self, to: PublicKey, data: u64, sig: Signature) -> Result<()> {
let event = Event::new_claim(to, data, sig);
if !self.historian.verify_event(&event) {
return Err(AccountingError::InvalidEvent);
}
if let Err(SendError(_)) = self.historian.sender.send(event) {
return Err(AccountingError::SendError);
}
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-28 09:07:54 -08:00
}
pub fn deposit(self: &mut Self, n: u64, keypair: &Ed25519KeyPair) -> Result<Signature> {
use event::{get_pubkey, sign_claim_data};
2018-02-28 09:07:54 -08:00
let key = get_pubkey(keypair);
let sig = sign_claim_data(&n, keypair);
self.deposit_signed(key, n, sig).map(|_| sig)
2018-02-28 09:07:54 -08:00
}
pub fn transfer_signed(
self: &mut Self,
from: PublicKey,
to: PublicKey,
data: u64,
sig: Signature,
) -> Result<()> {
if self.get_balance(&from).unwrap_or(0) < data {
return Err(AccountingError::InsufficientFunds);
}
2018-02-28 09:07:54 -08:00
let event = Event::Transaction {
2018-03-03 19:41:05 -08:00
from,
2018-02-28 09:07:54 -08:00
to,
data,
sig,
};
if !self.historian.verify_event(&event) {
return Err(AccountingError::InvalidEvent);
}
if let Err(SendError(_)) = self.historian.sender.send(event) {
return Err(AccountingError::SendError);
}
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> {
use event::{get_pubkey, sign_transaction_data};
2018-02-28 09:07:54 -08:00
let from = get_pubkey(keypair);
let sig = sign_transaction_data(&n, keypair, &to);
self.transfer_signed(from, to, n, sig).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) {
use std::thread::sleep;
use std::time::Duration;
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-02-23 13:08:19 -08:00
#[test]
fn test_accountant() {
let zero = Sha256Hash::default();
let mut acc = Accountant::new(&zero, Some(2));
let alice_keypair = generate_keypair();
let bob_keypair = generate_keypair();
acc.deposit(10_000, &alice_keypair).unwrap();
let sig = acc.deposit(1_000, &bob_keypair).unwrap();
acc.wait_on_signature(&sig);
2018-02-23 13:08:19 -08:00
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-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().1,
ExitReason::RecvDisconnected
);
}
2018-02-27 10:28:10 -08:00
#[test]
fn test_invalid_transfer() {
use std::thread::sleep;
use std::time::Duration;
2018-02-27 10:28:10 -08:00
let zero = Sha256Hash::default();
let mut acc = Accountant::new(&zero, Some(2));
let alice_keypair = generate_keypair();
let bob_keypair = generate_keypair();
acc.deposit(10_000, &alice_keypair).unwrap();
let sig = acc.deposit(1_000, &bob_keypair).unwrap();
acc.wait_on_signature(&sig);
2018-02-27 10:28:10 -08:00
2018-02-28 09:07:54 -08:00
let bob_pubkey = get_pubkey(&bob_keypair);
assert_eq!(
acc.transfer(10_001, &alice_keypair, bob_pubkey),
Err(AccountingError::InsufficientFunds)
);
2018-02-27 10:28:10 -08:00
sleep(Duration::from_millis(30));
2018-02-28 09:07:54 -08:00
let alice_pubkey = get_pubkey(&alice_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().1,
ExitReason::RecvDisconnected
);
}
#[test]
2018-02-28 09:07:54 -08:00
fn test_multiple_claims() {
2018-02-27 10:28:10 -08:00
let zero = Sha256Hash::default();
let mut acc = Accountant::new(&zero, Some(2));
let keypair = generate_keypair();
acc.deposit(1, &keypair).unwrap();
let sig = acc.deposit(2, &keypair).unwrap();
acc.wait_on_signature(&sig);
2018-02-27 10:28:10 -08:00
2018-02-28 09:07:54 -08:00
let pubkey = get_pubkey(&keypair);
2018-02-27 10:28:10 -08:00
assert_eq!(acc.get_balance(&pubkey).unwrap(), 3);
drop(acc.historian.sender);
assert_eq!(
acc.historian.thread_hdl.join().unwrap().1,
ExitReason::RecvDisconnected
);
}
#[test]
fn test_transfer_to_newb() {
let zero = Sha256Hash::default();
let mut acc = Accountant::new(&zero, Some(2));
let alice_keypair = generate_keypair();
let bob_keypair = generate_keypair();
let sig = acc.deposit(10_000, &alice_keypair).unwrap();
acc.wait_on_signature(&sig);
2018-02-27 10:28:10 -08:00
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().1,
ExitReason::RecvDisconnected
);
}
2018-02-23 13:08:19 -08:00
}