accountant -> bank
This commit is contained in:
parent
6e8f99d9b2
commit
d2dd005a59
|
@ -1,4 +1,4 @@
|
||||||
//! The `accountant` module tracks client balances, and the progress of pending
|
//! The `bank` module tracks client balances, and the progress of pending
|
||||||
//! transactions. It offers a high-level public API that signs transactions
|
//! transactions. It offers a high-level public API that signs transactions
|
||||||
//! on behalf of the caller, and a private low-level API for when they have
|
//! on behalf of the caller, and a private low-level API for when they have
|
||||||
//! already been signed and verified.
|
//! already been signed and verified.
|
||||||
|
@ -23,13 +23,13 @@ use transaction::Transaction;
|
||||||
pub const MAX_ENTRY_IDS: usize = 1024 * 4;
|
pub const MAX_ENTRY_IDS: usize = 1024 * 4;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum AccountingError {
|
pub enum BankError {
|
||||||
AccountNotFound,
|
AccountNotFound,
|
||||||
InsufficientFunds,
|
InsufficientFunds,
|
||||||
InvalidTransferSignature,
|
InvalidTransferSignature,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T> = result::Result<T, AccountingError>;
|
pub type Result<T> = result::Result<T, BankError>;
|
||||||
|
|
||||||
/// Commit funds to the 'to' party.
|
/// Commit funds to the 'to' party.
|
||||||
fn apply_payment(balances: &RwLock<HashMap<PublicKey, AtomicIsize>>, payment: &Payment) {
|
fn apply_payment(balances: &RwLock<HashMap<PublicKey, AtomicIsize>>, payment: &Payment) {
|
||||||
|
@ -53,7 +53,7 @@ fn apply_payment(balances: &RwLock<HashMap<PublicKey, AtomicIsize>>, payment: &P
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Accountant {
|
pub struct Bank {
|
||||||
balances: RwLock<HashMap<PublicKey, AtomicIsize>>,
|
balances: RwLock<HashMap<PublicKey, AtomicIsize>>,
|
||||||
pending: RwLock<HashMap<Signature, Plan>>,
|
pending: RwLock<HashMap<Signature, Plan>>,
|
||||||
last_ids: RwLock<VecDeque<(Hash, RwLock<HashSet<Signature>>)>>,
|
last_ids: RwLock<VecDeque<(Hash, RwLock<HashSet<Signature>>)>>,
|
||||||
|
@ -62,12 +62,12 @@ pub struct Accountant {
|
||||||
transaction_count: AtomicUsize,
|
transaction_count: AtomicUsize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Accountant {
|
impl Bank {
|
||||||
/// Create an Accountant using a deposit.
|
/// Create an Bank using a deposit.
|
||||||
pub fn new_from_deposit(deposit: &Payment) -> Self {
|
pub fn new_from_deposit(deposit: &Payment) -> Self {
|
||||||
let balances = RwLock::new(HashMap::new());
|
let balances = RwLock::new(HashMap::new());
|
||||||
apply_payment(&balances, deposit);
|
apply_payment(&balances, deposit);
|
||||||
Accountant {
|
Bank {
|
||||||
balances,
|
balances,
|
||||||
pending: RwLock::new(HashMap::new()),
|
pending: RwLock::new(HashMap::new()),
|
||||||
last_ids: RwLock::new(VecDeque::new()),
|
last_ids: RwLock::new(VecDeque::new()),
|
||||||
|
@ -77,15 +77,15 @@ impl Accountant {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an Accountant with only a Mint. Typically used by unit tests.
|
/// Create an Bank with only a Mint. Typically used by unit tests.
|
||||||
pub fn new(mint: &Mint) -> Self {
|
pub fn new(mint: &Mint) -> Self {
|
||||||
let deposit = Payment {
|
let deposit = Payment {
|
||||||
to: mint.pubkey(),
|
to: mint.pubkey(),
|
||||||
tokens: mint.tokens,
|
tokens: mint.tokens,
|
||||||
};
|
};
|
||||||
let accountant = Self::new_from_deposit(&deposit);
|
let bank = Self::new_from_deposit(&deposit);
|
||||||
accountant.register_entry_id(&mint.last_id());
|
bank.register_entry_id(&mint.last_id());
|
||||||
accountant
|
bank
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the last entry ID registered
|
/// Return the last entry ID registered
|
||||||
|
@ -143,10 +143,10 @@ impl Accountant {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tell the accountant which Entry IDs exist on the ledger. This function
|
/// Tell the bank which Entry IDs exist on the ledger. This function
|
||||||
/// assumes subsequent calls correspond to later entries, and will boot
|
/// assumes subsequent calls correspond to later entries, and will boot
|
||||||
/// the oldest ones once its internal cache is full. Once boot, the
|
/// the oldest ones once its internal cache is full. Once boot, the
|
||||||
/// accountant will reject transactions using that `last_id`.
|
/// bank will reject transactions using that `last_id`.
|
||||||
pub fn register_entry_id(&self, last_id: &Hash) {
|
pub fn register_entry_id(&self, last_id: &Hash) {
|
||||||
let mut last_ids = self.last_ids
|
let mut last_ids = self.last_ids
|
||||||
.write()
|
.write()
|
||||||
|
@ -166,11 +166,11 @@ impl Accountant {
|
||||||
let option = bals.get(&tr.from);
|
let option = bals.get(&tr.from);
|
||||||
|
|
||||||
if option.is_none() {
|
if option.is_none() {
|
||||||
return Err(AccountingError::AccountNotFound);
|
return Err(BankError::AccountNotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.reserve_signature_with_last_id(&tr.sig, &tr.data.last_id) {
|
if !self.reserve_signature_with_last_id(&tr.sig, &tr.data.last_id) {
|
||||||
return Err(AccountingError::InvalidTransferSignature);
|
return Err(BankError::InvalidTransferSignature);
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -179,7 +179,7 @@ impl Accountant {
|
||||||
|
|
||||||
if current < tr.data.tokens {
|
if current < tr.data.tokens {
|
||||||
self.forget_signature_with_last_id(&tr.sig, &tr.data.last_id);
|
self.forget_signature_with_last_id(&tr.sig, &tr.data.last_id);
|
||||||
return Err(AccountingError::InsufficientFunds);
|
return Err(BankError::InsufficientFunds);
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = bal.compare_exchange(
|
let result = bal.compare_exchange(
|
||||||
|
@ -406,207 +406,190 @@ mod tests {
|
||||||
use signature::KeyPairUtil;
|
use signature::KeyPairUtil;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_accountant() {
|
fn test_bank() {
|
||||||
let alice = Mint::new(10_000);
|
let alice = Mint::new(10_000);
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
assert_eq!(accountant.last_id(), alice.last_id());
|
assert_eq!(bank.last_id(), alice.last_id());
|
||||||
|
|
||||||
accountant
|
bank.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id())
|
||||||
.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id())
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey).unwrap(), 1_000);
|
assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_000);
|
||||||
|
|
||||||
accountant
|
bank.transfer(500, &alice.keypair(), bob_pubkey, alice.last_id())
|
||||||
.transfer(500, &alice.keypair(), bob_pubkey, alice.last_id())
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey).unwrap(), 1_500);
|
assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_500);
|
||||||
assert_eq!(accountant.transaction_count(), 2);
|
assert_eq!(bank.transaction_count(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_account_not_found() {
|
fn test_account_not_found() {
|
||||||
let mint = Mint::new(1);
|
let mint = Mint::new(1);
|
||||||
let accountant = Accountant::new(&mint);
|
let bank = Bank::new(&mint);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
accountant.transfer(1, &KeyPair::new(), mint.pubkey(), mint.last_id()),
|
bank.transfer(1, &KeyPair::new(), mint.pubkey(), mint.last_id()),
|
||||||
Err(AccountingError::AccountNotFound)
|
Err(BankError::AccountNotFound)
|
||||||
);
|
);
|
||||||
assert_eq!(accountant.transaction_count(), 0);
|
assert_eq!(bank.transaction_count(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_transfer() {
|
fn test_invalid_transfer() {
|
||||||
let alice = Mint::new(11_000);
|
let alice = Mint::new(11_000);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
accountant
|
bank.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id())
|
||||||
.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id())
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(accountant.transaction_count(), 1);
|
assert_eq!(bank.transaction_count(), 1);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
accountant.transfer(10_001, &alice.keypair(), bob_pubkey, alice.last_id()),
|
bank.transfer(10_001, &alice.keypair(), bob_pubkey, alice.last_id()),
|
||||||
Err(AccountingError::InsufficientFunds)
|
Err(BankError::InsufficientFunds)
|
||||||
);
|
);
|
||||||
assert_eq!(accountant.transaction_count(), 1);
|
assert_eq!(bank.transaction_count(), 1);
|
||||||
|
|
||||||
let alice_pubkey = alice.keypair().pubkey();
|
let alice_pubkey = alice.keypair().pubkey();
|
||||||
assert_eq!(accountant.get_balance(&alice_pubkey).unwrap(), 10_000);
|
assert_eq!(bank.get_balance(&alice_pubkey).unwrap(), 10_000);
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey).unwrap(), 1_000);
|
assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer_to_newb() {
|
fn test_transfer_to_newb() {
|
||||||
let alice = Mint::new(10_000);
|
let alice = Mint::new(10_000);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let alice_keypair = alice.keypair();
|
let alice_keypair = alice.keypair();
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
accountant
|
bank.transfer(500, &alice_keypair, bob_pubkey, alice.last_id())
|
||||||
.transfer(500, &alice_keypair, bob_pubkey, alice.last_id())
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey).unwrap(), 500);
|
assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer_on_date() {
|
fn test_transfer_on_date() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let alice_keypair = alice.keypair();
|
let alice_keypair = alice.keypair();
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
accountant
|
bank.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
||||||
.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Alice's balance will be zero because all funds are locked up.
|
// Alice's balance will be zero because all funds are locked up.
|
||||||
assert_eq!(accountant.get_balance(&alice.pubkey()), Some(0));
|
assert_eq!(bank.get_balance(&alice.pubkey()), Some(0));
|
||||||
|
|
||||||
// tx count is 1, because debits were applied.
|
// tx count is 1, because debits were applied.
|
||||||
assert_eq!(accountant.transaction_count(), 1);
|
assert_eq!(bank.transaction_count(), 1);
|
||||||
|
|
||||||
// Bob's balance will be None because the funds have not been
|
// Bob's balance will be None because the funds have not been
|
||||||
// sent.
|
// sent.
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey), None);
|
assert_eq!(bank.get_balance(&bob_pubkey), None);
|
||||||
|
|
||||||
// Now, acknowledge the time in the condition occurred and
|
// Now, acknowledge the time in the condition occurred and
|
||||||
// that bob's funds are now available.
|
// that bob's funds are now available.
|
||||||
accountant
|
bank.process_verified_timestamp(alice.pubkey(), dt).unwrap();
|
||||||
.process_verified_timestamp(alice.pubkey(), dt)
|
assert_eq!(bank.get_balance(&bob_pubkey), Some(1));
|
||||||
.unwrap();
|
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey), Some(1));
|
|
||||||
|
|
||||||
// tx count is still 1, because we chose not to count timestamp events
|
// tx count is still 1, because we chose not to count timestamp events
|
||||||
// tx count.
|
// tx count.
|
||||||
assert_eq!(accountant.transaction_count(), 1);
|
assert_eq!(bank.transaction_count(), 1);
|
||||||
|
|
||||||
accountant
|
bank.process_verified_timestamp(alice.pubkey(), dt).unwrap(); // <-- Attack! Attempt to process completed transaction.
|
||||||
.process_verified_timestamp(alice.pubkey(), dt)
|
assert_ne!(bank.get_balance(&bob_pubkey), Some(2));
|
||||||
.unwrap(); // <-- Attack! Attempt to process completed transaction.
|
|
||||||
assert_ne!(accountant.get_balance(&bob_pubkey), Some(2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_transfer_after_date() {
|
fn test_transfer_after_date() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let alice_keypair = alice.keypair();
|
let alice_keypair = alice.keypair();
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
accountant
|
bank.process_verified_timestamp(alice.pubkey(), dt).unwrap();
|
||||||
.process_verified_timestamp(alice.pubkey(), dt)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// It's now past now, so this transfer should be processed immediately.
|
// It's now past now, so this transfer should be processed immediately.
|
||||||
accountant
|
bank.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
||||||
.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(accountant.get_balance(&alice.pubkey()), Some(0));
|
assert_eq!(bank.get_balance(&alice.pubkey()), Some(0));
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey), Some(1));
|
assert_eq!(bank.get_balance(&bob_pubkey), Some(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_cancel_transfer() {
|
fn test_cancel_transfer() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let alice_keypair = alice.keypair();
|
let alice_keypair = alice.keypair();
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let dt = Utc::now();
|
let dt = Utc::now();
|
||||||
let sig = accountant
|
let sig = bank.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
||||||
.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id())
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Assert the debit counts as a transaction.
|
// Assert the debit counts as a transaction.
|
||||||
assert_eq!(accountant.transaction_count(), 1);
|
assert_eq!(bank.transaction_count(), 1);
|
||||||
|
|
||||||
// Alice's balance will be zero because all funds are locked up.
|
// Alice's balance will be zero because all funds are locked up.
|
||||||
assert_eq!(accountant.get_balance(&alice.pubkey()), Some(0));
|
assert_eq!(bank.get_balance(&alice.pubkey()), Some(0));
|
||||||
|
|
||||||
// Bob's balance will be None because the funds have not been
|
// Bob's balance will be None because the funds have not been
|
||||||
// sent.
|
// sent.
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey), None);
|
assert_eq!(bank.get_balance(&bob_pubkey), None);
|
||||||
|
|
||||||
// Now, cancel the trancaction. Alice gets her funds back, Bob never sees them.
|
// Now, cancel the trancaction. Alice gets her funds back, Bob never sees them.
|
||||||
accountant
|
bank.process_verified_sig(alice.pubkey(), sig).unwrap();
|
||||||
.process_verified_sig(alice.pubkey(), sig)
|
assert_eq!(bank.get_balance(&alice.pubkey()), Some(1));
|
||||||
.unwrap();
|
assert_eq!(bank.get_balance(&bob_pubkey), None);
|
||||||
assert_eq!(accountant.get_balance(&alice.pubkey()), Some(1));
|
|
||||||
assert_eq!(accountant.get_balance(&bob_pubkey), None);
|
|
||||||
|
|
||||||
// Assert cancel doesn't cause count to go backward.
|
// Assert cancel doesn't cause count to go backward.
|
||||||
assert_eq!(accountant.transaction_count(), 1);
|
assert_eq!(bank.transaction_count(), 1);
|
||||||
|
|
||||||
accountant
|
bank.process_verified_sig(alice.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction.
|
||||||
.process_verified_sig(alice.pubkey(), sig)
|
assert_ne!(bank.get_balance(&alice.pubkey()), Some(2));
|
||||||
.unwrap(); // <-- Attack! Attempt to cancel completed transaction.
|
|
||||||
assert_ne!(accountant.get_balance(&alice.pubkey()), Some(2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_duplicate_event_signature() {
|
fn test_duplicate_event_signature() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let sig = Signature::default();
|
let sig = Signature::default();
|
||||||
assert!(accountant.reserve_signature_with_last_id(&sig, &alice.last_id()));
|
assert!(bank.reserve_signature_with_last_id(&sig, &alice.last_id()));
|
||||||
assert!(!accountant.reserve_signature_with_last_id(&sig, &alice.last_id()));
|
assert!(!bank.reserve_signature_with_last_id(&sig, &alice.last_id()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_forget_signature() {
|
fn test_forget_signature() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let sig = Signature::default();
|
let sig = Signature::default();
|
||||||
accountant.reserve_signature_with_last_id(&sig, &alice.last_id());
|
bank.reserve_signature_with_last_id(&sig, &alice.last_id());
|
||||||
assert!(accountant.forget_signature_with_last_id(&sig, &alice.last_id()));
|
assert!(bank.forget_signature_with_last_id(&sig, &alice.last_id()));
|
||||||
assert!(!accountant.forget_signature_with_last_id(&sig, &alice.last_id()));
|
assert!(!bank.forget_signature_with_last_id(&sig, &alice.last_id()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_max_entry_ids() {
|
fn test_max_entry_ids() {
|
||||||
let alice = Mint::new(1);
|
let alice = Mint::new(1);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let sig = Signature::default();
|
let sig = Signature::default();
|
||||||
for i in 0..MAX_ENTRY_IDS {
|
for i in 0..MAX_ENTRY_IDS {
|
||||||
let last_id = hash(&serialize(&i).unwrap()); // Unique hash
|
let last_id = hash(&serialize(&i).unwrap()); // Unique hash
|
||||||
accountant.register_entry_id(&last_id);
|
bank.register_entry_id(&last_id);
|
||||||
}
|
}
|
||||||
// Assert we're no longer able to use the oldest entry ID.
|
// Assert we're no longer able to use the oldest entry ID.
|
||||||
assert!(!accountant.reserve_signature_with_last_id(&sig, &alice.last_id()));
|
assert!(!bank.reserve_signature_with_last_id(&sig, &alice.last_id()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_debits_before_credits() {
|
fn test_debits_before_credits() {
|
||||||
let mint = Mint::new(2);
|
let mint = Mint::new(2);
|
||||||
let accountant = Accountant::new(&mint);
|
let bank = Bank::new(&mint);
|
||||||
let alice = KeyPair::new();
|
let alice = KeyPair::new();
|
||||||
let tr0 = Transaction::new(&mint.keypair(), alice.pubkey(), 2, mint.last_id());
|
let tr0 = Transaction::new(&mint.keypair(), alice.pubkey(), 2, mint.last_id());
|
||||||
let tr1 = Transaction::new(&alice, mint.pubkey(), 1, mint.last_id());
|
let tr1 = Transaction::new(&alice, mint.pubkey(), 1, mint.last_id());
|
||||||
let trs = vec![tr0, tr1];
|
let trs = vec![tr0, tr1];
|
||||||
let results = accountant.process_verified_transactions(trs);
|
let results = bank.process_verified_transactions(trs);
|
||||||
assert!(results[1].is_err());
|
assert!(results[1].is_err());
|
||||||
|
|
||||||
// Assert bad transactions aren't counted.
|
// Assert bad transactions aren't counted.
|
||||||
assert_eq!(accountant.transaction_count(), 1);
|
assert_eq!(bank.transaction_count(), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -614,7 +597,7 @@ mod tests {
|
||||||
mod bench {
|
mod bench {
|
||||||
extern crate test;
|
extern crate test;
|
||||||
use self::test::Bencher;
|
use self::test::Bencher;
|
||||||
use accountant::*;
|
use bank::*;
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
use hash::hash;
|
use hash::hash;
|
||||||
use signature::KeyPairUtil;
|
use signature::KeyPairUtil;
|
||||||
|
@ -622,7 +605,7 @@ mod bench {
|
||||||
#[bench]
|
#[bench]
|
||||||
fn process_verified_event_bench(bencher: &mut Bencher) {
|
fn process_verified_event_bench(bencher: &mut Bencher) {
|
||||||
let mint = Mint::new(100_000_000);
|
let mint = Mint::new(100_000_000);
|
||||||
let accountant = Accountant::new(&mint);
|
let bank = Bank::new(&mint);
|
||||||
// Create transactions between unrelated parties.
|
// Create transactions between unrelated parties.
|
||||||
let transactions: Vec<_> = (0..4096)
|
let transactions: Vec<_> = (0..4096)
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
|
@ -630,15 +613,15 @@ mod bench {
|
||||||
// Seed the 'from' account.
|
// Seed the 'from' account.
|
||||||
let rando0 = KeyPair::new();
|
let rando0 = KeyPair::new();
|
||||||
let tr = Transaction::new(&mint.keypair(), rando0.pubkey(), 1_000, mint.last_id());
|
let tr = Transaction::new(&mint.keypair(), rando0.pubkey(), 1_000, mint.last_id());
|
||||||
accountant.process_verified_transaction(&tr).unwrap();
|
bank.process_verified_transaction(&tr).unwrap();
|
||||||
|
|
||||||
// Seed the 'to' account and a cell for its signature.
|
// Seed the 'to' account and a cell for its signature.
|
||||||
let last_id = hash(&serialize(&i).unwrap()); // Unique hash
|
let last_id = hash(&serialize(&i).unwrap()); // Unique hash
|
||||||
accountant.register_entry_id(&last_id);
|
bank.register_entry_id(&last_id);
|
||||||
|
|
||||||
let rando1 = KeyPair::new();
|
let rando1 = KeyPair::new();
|
||||||
let tr = Transaction::new(&rando0, rando1.pubkey(), 1, last_id);
|
let tr = Transaction::new(&rando0, rando1.pubkey(), 1, last_id);
|
||||||
accountant.process_verified_transaction(&tr).unwrap();
|
bank.process_verified_transaction(&tr).unwrap();
|
||||||
|
|
||||||
// Finally, return a transaction that's unique
|
// Finally, return a transaction that's unique
|
||||||
Transaction::new(&rando0, rando1.pubkey(), 1, last_id)
|
Transaction::new(&rando0, rando1.pubkey(), 1, last_id)
|
||||||
|
@ -646,13 +629,12 @@ mod bench {
|
||||||
.collect();
|
.collect();
|
||||||
bencher.iter(|| {
|
bencher.iter(|| {
|
||||||
// Since benchmarker runs this multiple times, we need to clear the signatures.
|
// Since benchmarker runs this multiple times, we need to clear the signatures.
|
||||||
for sigs in accountant.last_ids.read().unwrap().iter() {
|
for sigs in bank.last_ids.read().unwrap().iter() {
|
||||||
sigs.1.write().unwrap().clear();
|
sigs.1.write().unwrap().clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
accountant
|
bank.process_verified_transactions(transactions.clone())
|
||||||
.process_verified_transactions(transactions.clone())
|
|
||||||
.iter()
|
.iter()
|
||||||
.all(|x| x.is_ok())
|
.all(|x| x.is_ok())
|
||||||
);
|
);
|
|
@ -7,7 +7,7 @@ extern crate untrusted;
|
||||||
|
|
||||||
use isatty::stdin_isatty;
|
use isatty::stdin_isatty;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use solana::accountant::MAX_ENTRY_IDS;
|
use solana::bank::MAX_ENTRY_IDS;
|
||||||
use solana::entry::{create_entry, next_entry};
|
use solana::entry::{create_entry, next_entry};
|
||||||
use solana::event::Event;
|
use solana::event::Event;
|
||||||
use solana::mint::MintDemo;
|
use solana::mint::MintDemo;
|
||||||
|
|
|
@ -6,7 +6,7 @@ extern crate solana;
|
||||||
|
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
use isatty::stdin_isatty;
|
use isatty::stdin_isatty;
|
||||||
use solana::accountant::Accountant;
|
use solana::bank::Bank;
|
||||||
use solana::crdt::ReplicatedData;
|
use solana::crdt::ReplicatedData;
|
||||||
use solana::entry::Entry;
|
use solana::entry::Entry;
|
||||||
use solana::event::Event;
|
use solana::event::Event;
|
||||||
|
@ -92,31 +92,31 @@ fn main() {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
eprintln!("creating accountant...");
|
eprintln!("creating bank...");
|
||||||
|
|
||||||
let accountant = Accountant::new_from_deposit(&deposit.unwrap());
|
let bank = Bank::new_from_deposit(&deposit.unwrap());
|
||||||
accountant.register_entry_id(&entry0.id);
|
bank.register_entry_id(&entry0.id);
|
||||||
accountant.register_entry_id(&entry1.id);
|
bank.register_entry_id(&entry1.id);
|
||||||
|
|
||||||
eprintln!("processing entries...");
|
eprintln!("processing entries...");
|
||||||
|
|
||||||
let mut last_id = entry1.id;
|
let mut last_id = entry1.id;
|
||||||
for entry in entries {
|
for entry in entries {
|
||||||
last_id = entry.id;
|
last_id = entry.id;
|
||||||
let results = accountant.process_verified_events(entry.events);
|
let results = bank.process_verified_events(entry.events);
|
||||||
for result in results {
|
for result in results {
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
eprintln!("failed to process event {:?}", e);
|
eprintln!("failed to process event {:?}", e);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
accountant.register_entry_id(&last_id);
|
bank.register_entry_id(&last_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("creating networking stack...");
|
eprintln!("creating networking stack...");
|
||||||
|
|
||||||
let exit = Arc::new(AtomicBool::new(false));
|
let exit = Arc::new(AtomicBool::new(false));
|
||||||
let rpu = Rpu::new(accountant, last_id, Some(Duration::from_millis(1000)));
|
let rpu = Rpu::new(bank, last_id, Some(Duration::from_millis(1000)));
|
||||||
let serve_sock = UdpSocket::bind(&serve_addr).unwrap();
|
let serve_sock = UdpSocket::bind(&serve_addr).unwrap();
|
||||||
let gossip_sock = UdpSocket::bind(&gossip_addr).unwrap();
|
let gossip_sock = UdpSocket::bind(&gossip_addr).unwrap();
|
||||||
let replicate_sock = UdpSocket::bind(&replicate_addr).unwrap();
|
let replicate_sock = UdpSocket::bind(&replicate_addr).unwrap();
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
//! * layer 1 - As many nodes as we can fit
|
//! * layer 1 - As many nodes as we can fit
|
||||||
//! * layer 2 - Everyone else, if layer 1 is `2^10`, layer 2 should be able to fit `2^20` number of nodes.
|
//! * layer 2 - Everyone else, if layer 1 is `2^10`, layer 2 should be able to fit `2^20` number of nodes.
|
||||||
//!
|
//!
|
||||||
//! Accountant needs to provide an interface for us to query the stake weight
|
//! Bank needs to provide an interface for us to query the stake weight
|
||||||
|
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! The `entry_writer` module helps implement the TPU's write stage.
|
//! The `entry_writer` module helps implement the TPU's write stage.
|
||||||
|
|
||||||
use accountant::Accountant;
|
use bank::Bank;
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use ledger;
|
use ledger;
|
||||||
use packet;
|
use packet;
|
||||||
|
@ -15,18 +15,18 @@ use std::time::Duration;
|
||||||
use streamer;
|
use streamer;
|
||||||
|
|
||||||
pub struct EntryWriter<'a> {
|
pub struct EntryWriter<'a> {
|
||||||
accountant: &'a Accountant,
|
bank: &'a Bank,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> EntryWriter<'a> {
|
impl<'a> EntryWriter<'a> {
|
||||||
/// Create a new Tpu that wraps the given Accountant.
|
/// Create a new Tpu that wraps the given Bank.
|
||||||
pub fn new(accountant: &'a Accountant) -> Self {
|
pub fn new(bank: &'a Bank) -> Self {
|
||||||
EntryWriter { accountant }
|
EntryWriter { bank }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_entry<W: Write>(&self, writer: &Mutex<W>, entry: &Entry) {
|
fn write_entry<W: Write>(&self, writer: &Mutex<W>, entry: &Entry) {
|
||||||
trace!("write_entry entry");
|
trace!("write_entry entry");
|
||||||
self.accountant.register_entry_id(&entry.id);
|
self.bank.register_entry_id(&entry.id);
|
||||||
writeln!(
|
writeln!(
|
||||||
writer.lock().expect("'writer' lock in fn fn write_entry"),
|
writer.lock().expect("'writer' lock in fn fn write_entry"),
|
||||||
"{}",
|
"{}",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#![cfg_attr(feature = "unstable", feature(test))]
|
#![cfg_attr(feature = "unstable", feature(test))]
|
||||||
pub mod accountant;
|
pub mod bank;
|
||||||
pub mod crdt;
|
pub mod crdt;
|
||||||
pub mod ecdsa;
|
pub mod ecdsa;
|
||||||
pub mod entry;
|
pub mod entry;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! The `request_stage` processes thin client Request messages.
|
//! The `request_stage` processes thin client Request messages.
|
||||||
|
|
||||||
use accountant::Accountant;
|
use bank::Bank;
|
||||||
use bincode::{deserialize, serialize};
|
use bincode::{deserialize, serialize};
|
||||||
use event::Event;
|
use event::Event;
|
||||||
use packet;
|
use packet;
|
||||||
|
@ -19,13 +19,13 @@ use streamer;
|
||||||
use timing;
|
use timing;
|
||||||
|
|
||||||
pub struct RequestProcessor {
|
pub struct RequestProcessor {
|
||||||
accountant: Arc<Accountant>,
|
bank: Arc<Bank>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RequestProcessor {
|
impl RequestProcessor {
|
||||||
/// Create a new Tpu that wraps the given Accountant.
|
/// Create a new Tpu that wraps the given Bank.
|
||||||
pub fn new(accountant: Arc<Accountant>) -> Self {
|
pub fn new(bank: Arc<Bank>) -> Self {
|
||||||
RequestProcessor { accountant }
|
RequestProcessor { bank }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Process Request items sent by clients.
|
/// Process Request items sent by clients.
|
||||||
|
@ -36,19 +36,19 @@ impl RequestProcessor {
|
||||||
) -> Option<(Response, SocketAddr)> {
|
) -> Option<(Response, SocketAddr)> {
|
||||||
match msg {
|
match msg {
|
||||||
Request::GetBalance { key } => {
|
Request::GetBalance { key } => {
|
||||||
let val = self.accountant.get_balance(&key);
|
let val = self.bank.get_balance(&key);
|
||||||
let rsp = (Response::Balance { key, val }, rsp_addr);
|
let rsp = (Response::Balance { key, val }, rsp_addr);
|
||||||
info!("Response::Balance {:?}", rsp);
|
info!("Response::Balance {:?}", rsp);
|
||||||
Some(rsp)
|
Some(rsp)
|
||||||
}
|
}
|
||||||
Request::GetLastId => {
|
Request::GetLastId => {
|
||||||
let id = self.accountant.last_id();
|
let id = self.bank.last_id();
|
||||||
let rsp = (Response::LastId { id }, rsp_addr);
|
let rsp = (Response::LastId { id }, rsp_addr);
|
||||||
info!("Response::LastId {:?}", rsp);
|
info!("Response::LastId {:?}", rsp);
|
||||||
Some(rsp)
|
Some(rsp)
|
||||||
}
|
}
|
||||||
Request::GetTransactionCount => {
|
Request::GetTransactionCount => {
|
||||||
let transaction_count = self.accountant.transaction_count() as u64;
|
let transaction_count = self.bank.transaction_count() as u64;
|
||||||
let rsp = (Response::TransactionCount { transaction_count }, rsp_addr);
|
let rsp = (Response::TransactionCount { transaction_count }, rsp_addr);
|
||||||
info!("Response::TransactionCount {:?}", rsp);
|
info!("Response::TransactionCount {:?}", rsp);
|
||||||
Some(rsp)
|
Some(rsp)
|
||||||
|
@ -174,7 +174,7 @@ impl RequestProcessor {
|
||||||
debug!("events: {} reqs: {}", events.len(), reqs.len());
|
debug!("events: {} reqs: {}", events.len(), reqs.len());
|
||||||
|
|
||||||
debug!("process_events");
|
debug!("process_events");
|
||||||
let results = self.accountant.process_verified_events(events);
|
let results = self.bank.process_verified_events(events);
|
||||||
let events = results.into_iter().filter_map(|x| x.ok()).collect();
|
let events = results.into_iter().filter_map(|x| x.ok()).collect();
|
||||||
signal_sender.send(Signal::Events(events))?;
|
signal_sender.send(Signal::Events(events))?;
|
||||||
debug!("done process_events");
|
debug!("done process_events");
|
||||||
|
|
|
@ -52,9 +52,9 @@ impl RequestStage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: When accounting is pulled out of RequestStage, add this test back in.
|
// TODO: When banking is pulled out of RequestStage, add this test back in.
|
||||||
|
|
||||||
//use accountant::Accountant;
|
//use bank::Bank;
|
||||||
//use entry::Entry;
|
//use entry::Entry;
|
||||||
//use event::Event;
|
//use event::Event;
|
||||||
//use hash::Hash;
|
//use hash::Hash;
|
||||||
|
@ -67,7 +67,7 @@ impl RequestStage {
|
||||||
//
|
//
|
||||||
//#[cfg(test)]
|
//#[cfg(test)]
|
||||||
//mod tests {
|
//mod tests {
|
||||||
// use accountant::Accountant;
|
// use bank::Bank;
|
||||||
// use event::Event;
|
// use event::Event;
|
||||||
// use event_processor::EventProcessor;
|
// use event_processor::EventProcessor;
|
||||||
// use mint::Mint;
|
// use mint::Mint;
|
||||||
|
@ -75,15 +75,15 @@ impl RequestStage {
|
||||||
// use transaction::Transaction;
|
// use transaction::Transaction;
|
||||||
//
|
//
|
||||||
// #[test]
|
// #[test]
|
||||||
// // TODO: Move this test accounting_stage. Calling process_events() directly
|
// // TODO: Move this test banking_stage. Calling process_events() directly
|
||||||
// // defeats the purpose of this test.
|
// // defeats the purpose of this test.
|
||||||
// fn test_accounting_sequential_consistency() {
|
// fn test_banking_sequential_consistency() {
|
||||||
// // In this attack we'll demonstrate that a verifier can interpret the ledger
|
// // In this attack we'll demonstrate that a verifier can interpret the ledger
|
||||||
// // differently if either the server doesn't signal the ledger to add an
|
// // differently if either the server doesn't signal the ledger to add an
|
||||||
// // Entry OR if the verifier tries to parallelize across multiple Entries.
|
// // Entry OR if the verifier tries to parallelize across multiple Entries.
|
||||||
// let mint = Mint::new(2);
|
// let mint = Mint::new(2);
|
||||||
// let accountant = Accountant::new(&mint);
|
// let bank = Bank::new(&mint);
|
||||||
// let event_processor = EventProcessor::new(accountant, &mint.last_id(), None);
|
// let event_processor = EventProcessor::new(bank, &mint.last_id(), None);
|
||||||
//
|
//
|
||||||
// // Process a batch that includes a transaction that receives two tokens.
|
// // Process a batch that includes a transaction that receives two tokens.
|
||||||
// let alice = KeyPair::new();
|
// let alice = KeyPair::new();
|
||||||
|
@ -96,22 +96,22 @@ impl RequestStage {
|
||||||
// let events = vec![Event::Transaction(tr)];
|
// let events = vec![Event::Transaction(tr)];
|
||||||
// let entry1 = event_processor.process_events(events).unwrap();
|
// let entry1 = event_processor.process_events(events).unwrap();
|
||||||
//
|
//
|
||||||
// // Collect the ledger and feed it to a new accountant.
|
// // Collect the ledger and feed it to a new bank.
|
||||||
// let entries = vec![entry0, entry1];
|
// let entries = vec![entry0, entry1];
|
||||||
//
|
//
|
||||||
// // Assert the user holds one token, not two. If the server only output one
|
// // Assert the user holds one token, not two. If the server only output one
|
||||||
// // entry, then the second transaction will be rejected, because it drives
|
// // entry, then the second transaction will be rejected, because it drives
|
||||||
// // the account balance below zero before the credit is added.
|
// // the account balance below zero before the credit is added.
|
||||||
// let accountant = Accountant::new(&mint);
|
// let bank = Bank::new(&mint);
|
||||||
// for entry in entries {
|
// for entry in entries {
|
||||||
// assert!(
|
// assert!(
|
||||||
// accountant
|
// bank
|
||||||
// .process_verified_events(entry.events)
|
// .process_verified_events(entry.events)
|
||||||
// .into_iter()
|
// .into_iter()
|
||||||
// .all(|x| x.is_ok())
|
// .all(|x| x.is_ok())
|
||||||
// );
|
// );
|
||||||
// }
|
// }
|
||||||
// assert_eq!(accountant.get_balance(&alice.pubkey()), Some(1));
|
// assert_eq!(bank.get_balance(&alice.pubkey()), Some(1));
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
//
|
//
|
||||||
|
@ -119,7 +119,7 @@ impl RequestStage {
|
||||||
//mod bench {
|
//mod bench {
|
||||||
// extern crate test;
|
// extern crate test;
|
||||||
// use self::test::Bencher;
|
// use self::test::Bencher;
|
||||||
// use accountant::{Accountant, MAX_ENTRY_IDS};
|
// use bank::{Bank, MAX_ENTRY_IDS};
|
||||||
// use bincode::serialize;
|
// use bincode::serialize;
|
||||||
// use event_processor::*;
|
// use event_processor::*;
|
||||||
// use hash::hash;
|
// use hash::hash;
|
||||||
|
@ -133,7 +133,7 @@ impl RequestStage {
|
||||||
// #[bench]
|
// #[bench]
|
||||||
// fn process_events_bench(_bencher: &mut Bencher) {
|
// fn process_events_bench(_bencher: &mut Bencher) {
|
||||||
// let mint = Mint::new(100_000_000);
|
// let mint = Mint::new(100_000_000);
|
||||||
// let accountant = Accountant::new(&mint);
|
// let bank = Bank::new(&mint);
|
||||||
// // Create transactions between unrelated parties.
|
// // Create transactions between unrelated parties.
|
||||||
// let txs = 100_000;
|
// let txs = 100_000;
|
||||||
// let last_ids: Mutex<HashSet<Hash>> = Mutex::new(HashSet::new());
|
// let last_ids: Mutex<HashSet<Hash>> = Mutex::new(HashSet::new());
|
||||||
|
@ -147,18 +147,18 @@ impl RequestStage {
|
||||||
// let mut last_ids = last_ids.lock().unwrap();
|
// let mut last_ids = last_ids.lock().unwrap();
|
||||||
// if !last_ids.contains(&last_id) {
|
// if !last_ids.contains(&last_id) {
|
||||||
// last_ids.insert(last_id);
|
// last_ids.insert(last_id);
|
||||||
// accountant.register_entry_id(&last_id);
|
// bank.register_entry_id(&last_id);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// // Seed the 'from' account.
|
// // Seed the 'from' account.
|
||||||
// let rando0 = KeyPair::new();
|
// let rando0 = KeyPair::new();
|
||||||
// let tr = Transaction::new(&mint.keypair(), rando0.pubkey(), 1_000, last_id);
|
// let tr = Transaction::new(&mint.keypair(), rando0.pubkey(), 1_000, last_id);
|
||||||
// accountant.process_verified_transaction(&tr).unwrap();
|
// bank.process_verified_transaction(&tr).unwrap();
|
||||||
//
|
//
|
||||||
// let rando1 = KeyPair::new();
|
// let rando1 = KeyPair::new();
|
||||||
// let tr = Transaction::new(&rando0, rando1.pubkey(), 2, last_id);
|
// let tr = Transaction::new(&rando0, rando1.pubkey(), 2, last_id);
|
||||||
// accountant.process_verified_transaction(&tr).unwrap();
|
// bank.process_verified_transaction(&tr).unwrap();
|
||||||
//
|
//
|
||||||
// // Finally, return a transaction that's unique
|
// // Finally, return a transaction that's unique
|
||||||
// Transaction::new(&rando0, rando1.pubkey(), 1, last_id)
|
// Transaction::new(&rando0, rando1.pubkey(), 1, last_id)
|
||||||
|
@ -170,7 +170,7 @@ impl RequestStage {
|
||||||
// .map(|tr| Event::Transaction(tr))
|
// .map(|tr| Event::Transaction(tr))
|
||||||
// .collect();
|
// .collect();
|
||||||
//
|
//
|
||||||
// let event_processor = EventProcessor::new(accountant, &mint.last_id(), None);
|
// let event_processor = EventProcessor::new(bank, &mint.last_id(), None);
|
||||||
//
|
//
|
||||||
// let now = Instant::now();
|
// let now = Instant::now();
|
||||||
// assert!(event_processor.process_events(events).is_ok());
|
// assert!(event_processor.process_events(events).is_ok());
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! The `result` module exposes a Result type that propagates one of many different Error types.
|
//! The `result` module exposes a Result type that propagates one of many different Error types.
|
||||||
|
|
||||||
use accountant;
|
use bank;
|
||||||
use bincode;
|
use bincode;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use std;
|
use std;
|
||||||
|
@ -15,7 +15,7 @@ pub enum Error {
|
||||||
RecvError(std::sync::mpsc::RecvError),
|
RecvError(std::sync::mpsc::RecvError),
|
||||||
RecvTimeoutError(std::sync::mpsc::RecvTimeoutError),
|
RecvTimeoutError(std::sync::mpsc::RecvTimeoutError),
|
||||||
Serialize(std::boxed::Box<bincode::ErrorKind>),
|
Serialize(std::boxed::Box<bincode::ErrorKind>),
|
||||||
AccountingError(accountant::AccountingError),
|
BankError(bank::BankError),
|
||||||
SendError,
|
SendError,
|
||||||
Services,
|
Services,
|
||||||
GeneralError,
|
GeneralError,
|
||||||
|
@ -33,9 +33,9 @@ impl std::convert::From<std::sync::mpsc::RecvTimeoutError> for Error {
|
||||||
Error::RecvTimeoutError(e)
|
Error::RecvTimeoutError(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl std::convert::From<accountant::AccountingError> for Error {
|
impl std::convert::From<bank::BankError> for Error {
|
||||||
fn from(e: accountant::AccountingError) -> Error {
|
fn from(e: bank::BankError) -> Error {
|
||||||
Error::AccountingError(e)
|
Error::BankError(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T> std::convert::From<std::sync::mpsc::SendError<T>> for Error {
|
impl<T> std::convert::From<std::sync::mpsc::SendError<T>> for Error {
|
||||||
|
|
18
src/rpu.rs
18
src/rpu.rs
|
@ -1,7 +1,7 @@
|
||||||
//! The `rpu` module implements the Request Processing Unit, a
|
//! The `rpu` module implements the Request Processing Unit, a
|
||||||
//! 5-stage transaction processing pipeline in software.
|
//! 5-stage transaction processing pipeline in software.
|
||||||
|
|
||||||
use accountant::Accountant;
|
use bank::Bank;
|
||||||
use crdt::{Crdt, ReplicatedData};
|
use crdt::{Crdt, ReplicatedData};
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use entry_writer::EntryWriter;
|
use entry_writer::EntryWriter;
|
||||||
|
@ -22,23 +22,23 @@ use std::time::Duration;
|
||||||
use streamer;
|
use streamer;
|
||||||
|
|
||||||
pub struct Rpu {
|
pub struct Rpu {
|
||||||
accountant: Arc<Accountant>,
|
bank: Arc<Bank>,
|
||||||
start_hash: Hash,
|
start_hash: Hash,
|
||||||
tick_duration: Option<Duration>,
|
tick_duration: Option<Duration>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rpu {
|
impl Rpu {
|
||||||
/// Create a new Rpu that wraps the given Accountant.
|
/// Create a new Rpu that wraps the given Bank.
|
||||||
pub fn new(accountant: Accountant, start_hash: Hash, tick_duration: Option<Duration>) -> Self {
|
pub fn new(bank: Bank, start_hash: Hash, tick_duration: Option<Duration>) -> Self {
|
||||||
Rpu {
|
Rpu {
|
||||||
accountant: Arc::new(accountant),
|
bank: Arc::new(bank),
|
||||||
start_hash,
|
start_hash,
|
||||||
tick_duration,
|
tick_duration,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_service<W: Write + Send + 'static>(
|
fn write_service<W: Write + Send + 'static>(
|
||||||
accountant: Arc<Accountant>,
|
bank: Arc<Bank>,
|
||||||
exit: Arc<AtomicBool>,
|
exit: Arc<AtomicBool>,
|
||||||
broadcast: streamer::BlobSender,
|
broadcast: streamer::BlobSender,
|
||||||
blob_recycler: packet::BlobRecycler,
|
blob_recycler: packet::BlobRecycler,
|
||||||
|
@ -46,7 +46,7 @@ impl Rpu {
|
||||||
entry_receiver: Receiver<Entry>,
|
entry_receiver: Receiver<Entry>,
|
||||||
) -> JoinHandle<()> {
|
) -> JoinHandle<()> {
|
||||||
spawn(move || loop {
|
spawn(move || loop {
|
||||||
let entry_writer = EntryWriter::new(&accountant);
|
let entry_writer = EntryWriter::new(&bank);
|
||||||
let _ = entry_writer.write_and_send_entries(
|
let _ = entry_writer.write_and_send_entries(
|
||||||
&broadcast,
|
&broadcast,
|
||||||
&blob_recycler,
|
&blob_recycler,
|
||||||
|
@ -91,7 +91,7 @@ impl Rpu {
|
||||||
let sig_verify_stage = SigVerifyStage::new(exit.clone(), packet_receiver);
|
let sig_verify_stage = SigVerifyStage::new(exit.clone(), packet_receiver);
|
||||||
|
|
||||||
let blob_recycler = packet::BlobRecycler::default();
|
let blob_recycler = packet::BlobRecycler::default();
|
||||||
let request_processor = RequestProcessor::new(self.accountant.clone());
|
let request_processor = RequestProcessor::new(self.bank.clone());
|
||||||
let request_stage = RequestStage::new(
|
let request_stage = RequestStage::new(
|
||||||
request_processor,
|
request_processor,
|
||||||
exit.clone(),
|
exit.clone(),
|
||||||
|
@ -108,7 +108,7 @@ impl Rpu {
|
||||||
|
|
||||||
let (broadcast_sender, broadcast_receiver) = channel();
|
let (broadcast_sender, broadcast_receiver) = channel();
|
||||||
let t_write = Self::write_service(
|
let t_write = Self::write_service(
|
||||||
self.accountant.clone(),
|
self.bank.clone(),
|
||||||
exit.clone(),
|
exit.clone(),
|
||||||
broadcast_sender,
|
broadcast_sender,
|
||||||
blob_recycler.clone(),
|
blob_recycler.clone(),
|
||||||
|
|
|
@ -322,7 +322,7 @@ fn retransmit(
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `sock` - Socket to read from. Read timeout is set to 1.
|
/// * `sock` - Socket to read from. Read timeout is set to 1.
|
||||||
/// * `exit` - Boolean to signal system exit.
|
/// * `exit` - Boolean to signal system exit.
|
||||||
/// * `crdt` - This structure needs to be updated and populated by the accountant and via gossip.
|
/// * `crdt` - This structure needs to be updated and populated by the bank and via gossip.
|
||||||
/// * `recycler` - Blob recycler.
|
/// * `recycler` - Blob recycler.
|
||||||
/// * `r` - Receive channel for blobs to be retransmitted to all the layer 1 nodes.
|
/// * `r` - Receive channel for blobs to be retransmitted to all the layer 1 nodes.
|
||||||
pub fn retransmitter(
|
pub fn retransmitter(
|
||||||
|
|
|
@ -154,7 +154,7 @@ impl ThinClient {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use accountant::Accountant;
|
use bank::Bank;
|
||||||
use crdt::{Crdt, ReplicatedData};
|
use crdt::{Crdt, ReplicatedData};
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use logger;
|
use logger;
|
||||||
|
@ -187,10 +187,10 @@ mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
let alice = Mint::new(10_000);
|
let alice = Mint::new(10_000);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let exit = Arc::new(AtomicBool::new(false));
|
let exit = Arc::new(AtomicBool::new(false));
|
||||||
let rpu = Rpu::new(accountant, alice.last_id(), Some(Duration::from_millis(30)));
|
let rpu = Rpu::new(bank, alice.last_id(), Some(Duration::from_millis(30)));
|
||||||
let threads = rpu.serve(d, serve, gossip, exit.clone(), sink()).unwrap();
|
let threads = rpu.serve(d, serve, gossip, exit.clone(), sink()).unwrap();
|
||||||
sleep(Duration::from_millis(900));
|
sleep(Duration::from_millis(900));
|
||||||
|
|
||||||
|
@ -225,10 +225,10 @@ mod tests {
|
||||||
fn test_bad_sig() {
|
fn test_bad_sig() {
|
||||||
let (leader_data, leader_gossip, _, leader_serve, _leader_events) = tvu::test_node();
|
let (leader_data, leader_gossip, _, leader_serve, _leader_events) = tvu::test_node();
|
||||||
let alice = Mint::new(10_000);
|
let alice = Mint::new(10_000);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let exit = Arc::new(AtomicBool::new(false));
|
let exit = Arc::new(AtomicBool::new(false));
|
||||||
let rpu = Rpu::new(accountant, alice.last_id(), Some(Duration::from_millis(30)));
|
let rpu = Rpu::new(bank, alice.last_id(), Some(Duration::from_millis(30)));
|
||||||
let serve_addr = leader_serve.local_addr().unwrap();
|
let serve_addr = leader_serve.local_addr().unwrap();
|
||||||
let threads = rpu.serve(
|
let threads = rpu.serve(
|
||||||
leader_data,
|
leader_data,
|
||||||
|
@ -295,25 +295,25 @@ mod tests {
|
||||||
let bob_pubkey = KeyPair::new().pubkey();
|
let bob_pubkey = KeyPair::new().pubkey();
|
||||||
let exit = Arc::new(AtomicBool::new(false));
|
let exit = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
let leader_acc = {
|
let leader_bank = {
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
Rpu::new(accountant, alice.last_id(), Some(Duration::from_millis(30)))
|
Rpu::new(bank, alice.last_id(), Some(Duration::from_millis(30)))
|
||||||
};
|
};
|
||||||
|
|
||||||
let replicant_acc = {
|
let replicant_bank = {
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
Arc::new(Tvu::new(
|
Arc::new(Tvu::new(
|
||||||
accountant,
|
bank,
|
||||||
alice.last_id(),
|
alice.last_id(),
|
||||||
Some(Duration::from_millis(30)),
|
Some(Duration::from_millis(30)),
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
let leader_threads = leader_acc
|
let leader_threads = leader_bank
|
||||||
.serve(leader.0.clone(), leader.2, leader.1, exit.clone(), sink())
|
.serve(leader.0.clone(), leader.2, leader.1, exit.clone(), sink())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let replicant_threads = Tvu::serve(
|
let replicant_threads = Tvu::serve(
|
||||||
&replicant_acc,
|
&replicant_bank,
|
||||||
replicant.0.clone(),
|
replicant.0.clone(),
|
||||||
replicant.1,
|
replicant.1,
|
||||||
replicant.2,
|
replicant.2,
|
||||||
|
|
49
src/tvu.rs
49
src/tvu.rs
|
@ -1,7 +1,7 @@
|
||||||
//! The `tvu` module implements the Transaction Validation Unit, a
|
//! The `tvu` module implements the Transaction Validation Unit, a
|
||||||
//! 5-stage transaction validation pipeline in software.
|
//! 5-stage transaction validation pipeline in software.
|
||||||
|
|
||||||
use accountant::Accountant;
|
use bank::Bank;
|
||||||
use crdt::{Crdt, ReplicatedData};
|
use crdt::{Crdt, ReplicatedData};
|
||||||
use entry::Entry;
|
use entry::Entry;
|
||||||
use entry_writer::EntryWriter;
|
use entry_writer::EntryWriter;
|
||||||
|
@ -22,28 +22,28 @@ use std::time::Duration;
|
||||||
use streamer;
|
use streamer;
|
||||||
|
|
||||||
pub struct Tvu {
|
pub struct Tvu {
|
||||||
accountant: Arc<Accountant>,
|
bank: Arc<Bank>,
|
||||||
start_hash: Hash,
|
start_hash: Hash,
|
||||||
tick_duration: Option<Duration>,
|
tick_duration: Option<Duration>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tvu {
|
impl Tvu {
|
||||||
/// Create a new Tvu that wraps the given Accountant.
|
/// Create a new Tvu that wraps the given Bank.
|
||||||
pub fn new(accountant: Accountant, start_hash: Hash, tick_duration: Option<Duration>) -> Self {
|
pub fn new(bank: Bank, start_hash: Hash, tick_duration: Option<Duration>) -> Self {
|
||||||
Tvu {
|
Tvu {
|
||||||
accountant: Arc::new(accountant),
|
bank: Arc::new(bank),
|
||||||
start_hash,
|
start_hash,
|
||||||
tick_duration,
|
tick_duration,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn drain_service(
|
fn drain_service(
|
||||||
accountant: Arc<Accountant>,
|
bank: Arc<Bank>,
|
||||||
exit: Arc<AtomicBool>,
|
exit: Arc<AtomicBool>,
|
||||||
entry_receiver: Receiver<Entry>,
|
entry_receiver: Receiver<Entry>,
|
||||||
) -> JoinHandle<()> {
|
) -> JoinHandle<()> {
|
||||||
spawn(move || {
|
spawn(move || {
|
||||||
let entry_writer = EntryWriter::new(&accountant);
|
let entry_writer = EntryWriter::new(&bank);
|
||||||
loop {
|
loop {
|
||||||
let _ = entry_writer.drain_entries(&entry_receiver);
|
let _ = entry_writer.drain_entries(&entry_receiver);
|
||||||
if exit.load(Ordering::Relaxed) {
|
if exit.load(Ordering::Relaxed) {
|
||||||
|
@ -65,7 +65,7 @@ impl Tvu {
|
||||||
let blobs = verified_receiver.recv_timeout(timer)?;
|
let blobs = verified_receiver.recv_timeout(timer)?;
|
||||||
trace!("replicating blobs {}", blobs.len());
|
trace!("replicating blobs {}", blobs.len());
|
||||||
let entries = ledger::reconstruct_entries_from_blobs(&blobs);
|
let entries = ledger::reconstruct_entries_from_blobs(&blobs);
|
||||||
obj.accountant.process_verified_entries(entries)?;
|
obj.bank.process_verified_entries(entries)?;
|
||||||
for blob in blobs {
|
for blob in blobs {
|
||||||
blob_recycler.recycle(blob);
|
blob_recycler.recycle(blob);
|
||||||
}
|
}
|
||||||
|
@ -73,9 +73,9 @@ impl Tvu {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This service receives messages from a leader in the network and processes the transactions
|
/// This service receives messages from a leader in the network and processes the transactions
|
||||||
/// on the accountant state.
|
/// on the bank state.
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `obj` - The accountant state.
|
/// * `obj` - The bank state.
|
||||||
/// * `me` - my configuration
|
/// * `me` - my configuration
|
||||||
/// * `leader` - leader configuration
|
/// * `leader` - leader configuration
|
||||||
/// * `exit` - The exit signal.
|
/// * `exit` - The exit signal.
|
||||||
|
@ -173,7 +173,7 @@ impl Tvu {
|
||||||
|
|
||||||
let sig_verify_stage = SigVerifyStage::new(exit.clone(), packet_receiver);
|
let sig_verify_stage = SigVerifyStage::new(exit.clone(), packet_receiver);
|
||||||
|
|
||||||
let request_processor = RequestProcessor::new(obj.accountant.clone());
|
let request_processor = RequestProcessor::new(obj.bank.clone());
|
||||||
let request_stage = RequestStage::new(
|
let request_stage = RequestStage::new(
|
||||||
request_processor,
|
request_processor,
|
||||||
exit.clone(),
|
exit.clone(),
|
||||||
|
@ -188,11 +188,8 @@ impl Tvu {
|
||||||
obj.tick_duration,
|
obj.tick_duration,
|
||||||
);
|
);
|
||||||
|
|
||||||
let t_write = Self::drain_service(
|
let t_write =
|
||||||
obj.accountant.clone(),
|
Self::drain_service(obj.bank.clone(), exit.clone(), record_stage.entry_receiver);
|
||||||
exit.clone(),
|
|
||||||
record_stage.entry_receiver,
|
|
||||||
);
|
|
||||||
|
|
||||||
let t_responder = streamer::responder(
|
let t_responder = streamer::responder(
|
||||||
respond_socket,
|
respond_socket,
|
||||||
|
@ -240,7 +237,7 @@ pub fn test_node() -> (ReplicatedData, UdpSocket, UdpSocket, UdpSocket, UdpSocke
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use accountant::Accountant;
|
use bank::Bank;
|
||||||
use bincode::serialize;
|
use bincode::serialize;
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use crdt::Crdt;
|
use crdt::Crdt;
|
||||||
|
@ -311,9 +308,9 @@ mod tests {
|
||||||
|
|
||||||
let starting_balance = 10_000;
|
let starting_balance = 10_000;
|
||||||
let alice = Mint::new(starting_balance);
|
let alice = Mint::new(starting_balance);
|
||||||
let accountant = Accountant::new(&alice);
|
let bank = Bank::new(&alice);
|
||||||
let tvu = Arc::new(Tvu::new(
|
let tvu = Arc::new(Tvu::new(
|
||||||
accountant,
|
bank,
|
||||||
alice.last_id(),
|
alice.last_id(),
|
||||||
Some(Duration::from_millis(30)),
|
Some(Duration::from_millis(30)),
|
||||||
));
|
));
|
||||||
|
@ -341,11 +338,11 @@ mod tests {
|
||||||
w.set_index(i).unwrap();
|
w.set_index(i).unwrap();
|
||||||
w.set_id(leader_id).unwrap();
|
w.set_id(leader_id).unwrap();
|
||||||
|
|
||||||
let accountant = &tvu.accountant;
|
let bank = &tvu.bank;
|
||||||
|
|
||||||
let tr0 = Event::new_timestamp(&bob_keypair, Utc::now());
|
let tr0 = Event::new_timestamp(&bob_keypair, Utc::now());
|
||||||
let entry0 = entry::create_entry(&cur_hash, i, vec![tr0]);
|
let entry0 = entry::create_entry(&cur_hash, i, vec![tr0]);
|
||||||
accountant.register_entry_id(&cur_hash);
|
bank.register_entry_id(&cur_hash);
|
||||||
cur_hash = hash(&cur_hash);
|
cur_hash = hash(&cur_hash);
|
||||||
|
|
||||||
let tr1 = Transaction::new(
|
let tr1 = Transaction::new(
|
||||||
|
@ -354,11 +351,11 @@ mod tests {
|
||||||
transfer_amount,
|
transfer_amount,
|
||||||
cur_hash,
|
cur_hash,
|
||||||
);
|
);
|
||||||
accountant.register_entry_id(&cur_hash);
|
bank.register_entry_id(&cur_hash);
|
||||||
cur_hash = hash(&cur_hash);
|
cur_hash = hash(&cur_hash);
|
||||||
let entry1 =
|
let entry1 =
|
||||||
entry::create_entry(&cur_hash, i + num_blobs, vec![Event::Transaction(tr1)]);
|
entry::create_entry(&cur_hash, i + num_blobs, vec![Event::Transaction(tr1)]);
|
||||||
accountant.register_entry_id(&cur_hash);
|
bank.register_entry_id(&cur_hash);
|
||||||
cur_hash = hash(&cur_hash);
|
cur_hash = hash(&cur_hash);
|
||||||
|
|
||||||
alice_ref_balance -= transfer_amount;
|
alice_ref_balance -= transfer_amount;
|
||||||
|
@ -383,11 +380,11 @@ mod tests {
|
||||||
msgs.push(msg);
|
msgs.push(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
let accountant = &tvu.accountant;
|
let bank = &tvu.bank;
|
||||||
let alice_balance = accountant.get_balance(&alice.keypair().pubkey()).unwrap();
|
let alice_balance = bank.get_balance(&alice.keypair().pubkey()).unwrap();
|
||||||
assert_eq!(alice_balance, alice_ref_balance);
|
assert_eq!(alice_balance, alice_ref_balance);
|
||||||
|
|
||||||
let bob_balance = accountant.get_balance(&bob_keypair.pubkey()).unwrap();
|
let bob_balance = bank.get_balance(&bob_keypair.pubkey()).unwrap();
|
||||||
assert_eq!(bob_balance, starting_balance - alice_ref_balance);
|
assert_eq!(bob_balance, starting_balance - alice_ref_balance);
|
||||||
|
|
||||||
exit.store(true, Ordering::Relaxed);
|
exit.store(true, Ordering::Relaxed);
|
||||||
|
|
Loading…
Reference in New Issue