From d2dd005a596507ae2653734d51a7417c82b80804 Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Mon, 14 May 2018 15:33:11 -0600 Subject: [PATCH] accountant -> bank --- src/{accountant.rs => bank.rs} | 188 +++++++++++++++------------------ src/bin/genesis-demo.rs | 2 +- src/bin/testnode.rs | 16 +-- src/crdt.rs | 2 +- src/entry_writer.rs | 12 +-- src/lib.rs | 2 +- src/request_processor.rs | 18 ++-- src/request_stage.rs | 34 +++--- src/result.rs | 10 +- src/rpu.rs | 18 ++-- src/streamer.rs | 2 +- src/thin_client.rs | 26 ++--- src/tvu.rs | 49 ++++----- 13 files changed, 179 insertions(+), 200 deletions(-) rename src/{accountant.rs => bank.rs} (76%) diff --git a/src/accountant.rs b/src/bank.rs similarity index 76% rename from src/accountant.rs rename to src/bank.rs index 857dca6063..7989f138dd 100644 --- a/src/accountant.rs +++ b/src/bank.rs @@ -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 //! on behalf of the caller, and a private low-level API for when they have //! already been signed and verified. @@ -23,13 +23,13 @@ use transaction::Transaction; pub const MAX_ENTRY_IDS: usize = 1024 * 4; #[derive(Debug, PartialEq, Eq)] -pub enum AccountingError { +pub enum BankError { AccountNotFound, InsufficientFunds, InvalidTransferSignature, } -pub type Result = result::Result; +pub type Result = result::Result; /// Commit funds to the 'to' party. fn apply_payment(balances: &RwLock>, payment: &Payment) { @@ -53,7 +53,7 @@ fn apply_payment(balances: &RwLock>, payment: &P } } -pub struct Accountant { +pub struct Bank { balances: RwLock>, pending: RwLock>, last_ids: RwLock>)>>, @@ -62,12 +62,12 @@ pub struct Accountant { transaction_count: AtomicUsize, } -impl Accountant { - /// Create an Accountant using a deposit. +impl Bank { + /// Create an Bank using a deposit. pub fn new_from_deposit(deposit: &Payment) -> Self { let balances = RwLock::new(HashMap::new()); apply_payment(&balances, deposit); - Accountant { + Bank { balances, pending: RwLock::new(HashMap::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 { let deposit = Payment { to: mint.pubkey(), tokens: mint.tokens, }; - let accountant = Self::new_from_deposit(&deposit); - accountant.register_entry_id(&mint.last_id()); - accountant + let bank = Self::new_from_deposit(&deposit); + bank.register_entry_id(&mint.last_id()); + bank } /// Return the last entry ID registered @@ -143,10 +143,10 @@ impl Accountant { 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 /// 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) { let mut last_ids = self.last_ids .write() @@ -166,11 +166,11 @@ impl Accountant { let option = bals.get(&tr.from); 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) { - return Err(AccountingError::InvalidTransferSignature); + return Err(BankError::InvalidTransferSignature); } loop { @@ -179,7 +179,7 @@ impl Accountant { if current < tr.data.tokens { 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( @@ -406,207 +406,190 @@ mod tests { use signature::KeyPairUtil; #[test] - fn test_accountant() { + fn test_bank() { let alice = Mint::new(10_000); let bob_pubkey = KeyPair::new().pubkey(); - let accountant = Accountant::new(&alice); - assert_eq!(accountant.last_id(), alice.last_id()); + let bank = Bank::new(&alice); + assert_eq!(bank.last_id(), alice.last_id()); - accountant - .transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id()) + bank.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id()) .unwrap(); - assert_eq!(accountant.get_balance(&bob_pubkey).unwrap(), 1_000); + assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_000); - accountant - .transfer(500, &alice.keypair(), bob_pubkey, alice.last_id()) + bank.transfer(500, &alice.keypair(), bob_pubkey, alice.last_id()) .unwrap(); - assert_eq!(accountant.get_balance(&bob_pubkey).unwrap(), 1_500); - assert_eq!(accountant.transaction_count(), 2); + assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_500); + assert_eq!(bank.transaction_count(), 2); } #[test] fn test_account_not_found() { let mint = Mint::new(1); - let accountant = Accountant::new(&mint); + let bank = Bank::new(&mint); assert_eq!( - accountant.transfer(1, &KeyPair::new(), mint.pubkey(), mint.last_id()), - Err(AccountingError::AccountNotFound) + bank.transfer(1, &KeyPair::new(), mint.pubkey(), mint.last_id()), + Err(BankError::AccountNotFound) ); - assert_eq!(accountant.transaction_count(), 0); + assert_eq!(bank.transaction_count(), 0); } #[test] fn test_invalid_transfer() { let alice = Mint::new(11_000); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let bob_pubkey = KeyPair::new().pubkey(); - accountant - .transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id()) + bank.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id()) .unwrap(); - assert_eq!(accountant.transaction_count(), 1); + assert_eq!(bank.transaction_count(), 1); assert_eq!( - accountant.transfer(10_001, &alice.keypair(), bob_pubkey, alice.last_id()), - Err(AccountingError::InsufficientFunds) + bank.transfer(10_001, &alice.keypair(), bob_pubkey, alice.last_id()), + Err(BankError::InsufficientFunds) ); - assert_eq!(accountant.transaction_count(), 1); + assert_eq!(bank.transaction_count(), 1); let alice_pubkey = alice.keypair().pubkey(); - assert_eq!(accountant.get_balance(&alice_pubkey).unwrap(), 10_000); - assert_eq!(accountant.get_balance(&bob_pubkey).unwrap(), 1_000); + assert_eq!(bank.get_balance(&alice_pubkey).unwrap(), 10_000); + assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_000); } #[test] fn test_transfer_to_newb() { let alice = Mint::new(10_000); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let alice_keypair = alice.keypair(); let bob_pubkey = KeyPair::new().pubkey(); - accountant - .transfer(500, &alice_keypair, bob_pubkey, alice.last_id()) + bank.transfer(500, &alice_keypair, bob_pubkey, alice.last_id()) .unwrap(); - assert_eq!(accountant.get_balance(&bob_pubkey).unwrap(), 500); + assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 500); } #[test] fn test_transfer_on_date() { let alice = Mint::new(1); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let alice_keypair = alice.keypair(); let bob_pubkey = KeyPair::new().pubkey(); let dt = Utc::now(); - accountant - .transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id()) + bank.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id()) .unwrap(); // 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. - assert_eq!(accountant.transaction_count(), 1); + assert_eq!(bank.transaction_count(), 1); // Bob's balance will be None because the funds have not been // 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 // that bob's funds are now available. - accountant - .process_verified_timestamp(alice.pubkey(), dt) - .unwrap(); - assert_eq!(accountant.get_balance(&bob_pubkey), Some(1)); + bank.process_verified_timestamp(alice.pubkey(), dt).unwrap(); + assert_eq!(bank.get_balance(&bob_pubkey), Some(1)); // tx count is still 1, because we chose not to count timestamp events // tx count. - assert_eq!(accountant.transaction_count(), 1); + assert_eq!(bank.transaction_count(), 1); - accountant - .process_verified_timestamp(alice.pubkey(), dt) - .unwrap(); // <-- Attack! Attempt to process completed transaction. - assert_ne!(accountant.get_balance(&bob_pubkey), Some(2)); + bank.process_verified_timestamp(alice.pubkey(), dt).unwrap(); // <-- Attack! Attempt to process completed transaction. + assert_ne!(bank.get_balance(&bob_pubkey), Some(2)); } #[test] fn test_transfer_after_date() { let alice = Mint::new(1); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let alice_keypair = alice.keypair(); let bob_pubkey = KeyPair::new().pubkey(); let dt = Utc::now(); - accountant - .process_verified_timestamp(alice.pubkey(), dt) - .unwrap(); + bank.process_verified_timestamp(alice.pubkey(), dt).unwrap(); // It's now past now, so this transfer should be processed immediately. - accountant - .transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id()) + bank.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id()) .unwrap(); - assert_eq!(accountant.get_balance(&alice.pubkey()), Some(0)); - assert_eq!(accountant.get_balance(&bob_pubkey), Some(1)); + assert_eq!(bank.get_balance(&alice.pubkey()), Some(0)); + assert_eq!(bank.get_balance(&bob_pubkey), Some(1)); } #[test] fn test_cancel_transfer() { let alice = Mint::new(1); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let alice_keypair = alice.keypair(); let bob_pubkey = KeyPair::new().pubkey(); let dt = Utc::now(); - let sig = accountant - .transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id()) + let sig = bank.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id()) .unwrap(); // 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. - 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 // 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. - accountant - .process_verified_sig(alice.pubkey(), sig) - .unwrap(); - assert_eq!(accountant.get_balance(&alice.pubkey()), Some(1)); - assert_eq!(accountant.get_balance(&bob_pubkey), None); + bank.process_verified_sig(alice.pubkey(), sig).unwrap(); + assert_eq!(bank.get_balance(&alice.pubkey()), Some(1)); + assert_eq!(bank.get_balance(&bob_pubkey), None); // Assert cancel doesn't cause count to go backward. - assert_eq!(accountant.transaction_count(), 1); + assert_eq!(bank.transaction_count(), 1); - accountant - .process_verified_sig(alice.pubkey(), sig) - .unwrap(); // <-- Attack! Attempt to cancel completed transaction. - assert_ne!(accountant.get_balance(&alice.pubkey()), Some(2)); + bank.process_verified_sig(alice.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction. + assert_ne!(bank.get_balance(&alice.pubkey()), Some(2)); } #[test] fn test_duplicate_event_signature() { let alice = Mint::new(1); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let sig = Signature::default(); - assert!(accountant.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())); + assert!(!bank.reserve_signature_with_last_id(&sig, &alice.last_id())); } #[test] fn test_forget_signature() { let alice = Mint::new(1); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let sig = Signature::default(); - accountant.reserve_signature_with_last_id(&sig, &alice.last_id()); - assert!(accountant.forget_signature_with_last_id(&sig, &alice.last_id())); - assert!(!accountant.forget_signature_with_last_id(&sig, &alice.last_id())); + bank.reserve_signature_with_last_id(&sig, &alice.last_id()); + assert!(bank.forget_signature_with_last_id(&sig, &alice.last_id())); + assert!(!bank.forget_signature_with_last_id(&sig, &alice.last_id())); } #[test] fn test_max_entry_ids() { let alice = Mint::new(1); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let sig = Signature::default(); for i in 0..MAX_ENTRY_IDS { 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!(!accountant.reserve_signature_with_last_id(&sig, &alice.last_id())); + assert!(!bank.reserve_signature_with_last_id(&sig, &alice.last_id())); } #[test] fn test_debits_before_credits() { let mint = Mint::new(2); - let accountant = Accountant::new(&mint); + let bank = Bank::new(&mint); let alice = KeyPair::new(); let tr0 = Transaction::new(&mint.keypair(), alice.pubkey(), 2, mint.last_id()); let tr1 = Transaction::new(&alice, mint.pubkey(), 1, mint.last_id()); 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 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 { extern crate test; use self::test::Bencher; - use accountant::*; + use bank::*; use bincode::serialize; use hash::hash; use signature::KeyPairUtil; @@ -622,7 +605,7 @@ mod bench { #[bench] fn process_verified_event_bench(bencher: &mut Bencher) { let mint = Mint::new(100_000_000); - let accountant = Accountant::new(&mint); + let bank = Bank::new(&mint); // Create transactions between unrelated parties. let transactions: Vec<_> = (0..4096) .into_par_iter() @@ -630,15 +613,15 @@ mod bench { // Seed the 'from' account. let rando0 = KeyPair::new(); 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. 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 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 Transaction::new(&rando0, rando1.pubkey(), 1, last_id) @@ -646,13 +629,12 @@ mod bench { .collect(); bencher.iter(|| { // 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(); } assert!( - accountant - .process_verified_transactions(transactions.clone()) + bank.process_verified_transactions(transactions.clone()) .iter() .all(|x| x.is_ok()) ); diff --git a/src/bin/genesis-demo.rs b/src/bin/genesis-demo.rs index 926a695f14..0e408c11ec 100644 --- a/src/bin/genesis-demo.rs +++ b/src/bin/genesis-demo.rs @@ -7,7 +7,7 @@ extern crate untrusted; use isatty::stdin_isatty; use rayon::prelude::*; -use solana::accountant::MAX_ENTRY_IDS; +use solana::bank::MAX_ENTRY_IDS; use solana::entry::{create_entry, next_entry}; use solana::event::Event; use solana::mint::MintDemo; diff --git a/src/bin/testnode.rs b/src/bin/testnode.rs index ac8b74d10a..51e4e4527b 100644 --- a/src/bin/testnode.rs +++ b/src/bin/testnode.rs @@ -6,7 +6,7 @@ extern crate solana; use getopts::Options; use isatty::stdin_isatty; -use solana::accountant::Accountant; +use solana::bank::Bank; use solana::crdt::ReplicatedData; use solana::entry::Entry; use solana::event::Event; @@ -92,31 +92,31 @@ fn main() { None }; - eprintln!("creating accountant..."); + eprintln!("creating bank..."); - let accountant = Accountant::new_from_deposit(&deposit.unwrap()); - accountant.register_entry_id(&entry0.id); - accountant.register_entry_id(&entry1.id); + let bank = Bank::new_from_deposit(&deposit.unwrap()); + bank.register_entry_id(&entry0.id); + bank.register_entry_id(&entry1.id); eprintln!("processing entries..."); let mut last_id = entry1.id; for entry in entries { last_id = entry.id; - let results = accountant.process_verified_events(entry.events); + let results = bank.process_verified_events(entry.events); for result in results { if let Err(e) = result { eprintln!("failed to process event {:?}", e); exit(1); } } - accountant.register_entry_id(&last_id); + bank.register_entry_id(&last_id); } eprintln!("creating networking stack..."); 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 gossip_sock = UdpSocket::bind(&gossip_addr).unwrap(); let replicate_sock = UdpSocket::bind(&replicate_addr).unwrap(); diff --git a/src/crdt.rs b/src/crdt.rs index 965dac08fe..d5aa7a0f42 100644 --- a/src/crdt.rs +++ b/src/crdt.rs @@ -11,7 +11,7 @@ //! * 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. //! -//! 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 byteorder::{LittleEndian, ReadBytesExt}; diff --git a/src/entry_writer.rs b/src/entry_writer.rs index ab973ba3bc..69e62f0f32 100644 --- a/src/entry_writer.rs +++ b/src/entry_writer.rs @@ -1,6 +1,6 @@ //! The `entry_writer` module helps implement the TPU's write stage. -use accountant::Accountant; +use bank::Bank; use entry::Entry; use ledger; use packet; @@ -15,18 +15,18 @@ use std::time::Duration; use streamer; pub struct EntryWriter<'a> { - accountant: &'a Accountant, + bank: &'a Bank, } impl<'a> EntryWriter<'a> { - /// Create a new Tpu that wraps the given Accountant. - pub fn new(accountant: &'a Accountant) -> Self { - EntryWriter { accountant } + /// Create a new Tpu that wraps the given Bank. + pub fn new(bank: &'a Bank) -> Self { + EntryWriter { bank } } fn write_entry(&self, writer: &Mutex, entry: &Entry) { trace!("write_entry entry"); - self.accountant.register_entry_id(&entry.id); + self.bank.register_entry_id(&entry.id); writeln!( writer.lock().expect("'writer' lock in fn fn write_entry"), "{}", diff --git a/src/lib.rs b/src/lib.rs index d31c950b70..e61b59f889 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![cfg_attr(feature = "unstable", feature(test))] -pub mod accountant; +pub mod bank; pub mod crdt; pub mod ecdsa; pub mod entry; diff --git a/src/request_processor.rs b/src/request_processor.rs index 47692e1ef6..f070bbfe3d 100644 --- a/src/request_processor.rs +++ b/src/request_processor.rs @@ -1,6 +1,6 @@ //! The `request_stage` processes thin client Request messages. -use accountant::Accountant; +use bank::Bank; use bincode::{deserialize, serialize}; use event::Event; use packet; @@ -19,13 +19,13 @@ use streamer; use timing; pub struct RequestProcessor { - accountant: Arc, + bank: Arc, } impl RequestProcessor { - /// Create a new Tpu that wraps the given Accountant. - pub fn new(accountant: Arc) -> Self { - RequestProcessor { accountant } + /// Create a new Tpu that wraps the given Bank. + pub fn new(bank: Arc) -> Self { + RequestProcessor { bank } } /// Process Request items sent by clients. @@ -36,19 +36,19 @@ impl RequestProcessor { ) -> Option<(Response, SocketAddr)> { match msg { 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); info!("Response::Balance {:?}", rsp); Some(rsp) } Request::GetLastId => { - let id = self.accountant.last_id(); + let id = self.bank.last_id(); let rsp = (Response::LastId { id }, rsp_addr); info!("Response::LastId {:?}", rsp); Some(rsp) } 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); info!("Response::TransactionCount {:?}", rsp); Some(rsp) @@ -174,7 +174,7 @@ impl RequestProcessor { debug!("events: {} reqs: {}", events.len(), reqs.len()); 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(); signal_sender.send(Signal::Events(events))?; debug!("done process_events"); diff --git a/src/request_stage.rs b/src/request_stage.rs index e5ef5ef20d..8efc4f8237 100644 --- a/src/request_stage.rs +++ b/src/request_stage.rs @@ -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 event::Event; //use hash::Hash; @@ -67,7 +67,7 @@ impl RequestStage { // //#[cfg(test)] //mod tests { -// use accountant::Accountant; +// use bank::Bank; // use event::Event; // use event_processor::EventProcessor; // use mint::Mint; @@ -75,15 +75,15 @@ impl RequestStage { // use transaction::Transaction; // // #[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. -// fn test_accounting_sequential_consistency() { +// fn test_banking_sequential_consistency() { // // 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 // // Entry OR if the verifier tries to parallelize across multiple Entries. // let mint = Mint::new(2); -// let accountant = Accountant::new(&mint); -// let event_processor = EventProcessor::new(accountant, &mint.last_id(), None); +// let bank = Bank::new(&mint); +// let event_processor = EventProcessor::new(bank, &mint.last_id(), None); // // // Process a batch that includes a transaction that receives two tokens. // let alice = KeyPair::new(); @@ -96,22 +96,22 @@ impl RequestStage { // let events = vec![Event::Transaction(tr)]; // 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]; // // // 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 // // the account balance below zero before the credit is added. -// let accountant = Accountant::new(&mint); +// let bank = Bank::new(&mint); // for entry in entries { // assert!( -// accountant +// bank // .process_verified_events(entry.events) // .into_iter() // .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 { // extern crate test; // use self::test::Bencher; -// use accountant::{Accountant, MAX_ENTRY_IDS}; +// use bank::{Bank, MAX_ENTRY_IDS}; // use bincode::serialize; // use event_processor::*; // use hash::hash; @@ -133,7 +133,7 @@ impl RequestStage { // #[bench] // fn process_events_bench(_bencher: &mut Bencher) { // let mint = Mint::new(100_000_000); -// let accountant = Accountant::new(&mint); +// let bank = Bank::new(&mint); // // Create transactions between unrelated parties. // let txs = 100_000; // let last_ids: Mutex> = Mutex::new(HashSet::new()); @@ -147,18 +147,18 @@ impl RequestStage { // let mut last_ids = last_ids.lock().unwrap(); // if !last_ids.contains(&last_id) { // last_ids.insert(last_id); -// accountant.register_entry_id(&last_id); +// bank.register_entry_id(&last_id); // } // } // // // Seed the 'from' account. // let rando0 = KeyPair::new(); // 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 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 // Transaction::new(&rando0, rando1.pubkey(), 1, last_id) @@ -170,7 +170,7 @@ impl RequestStage { // .map(|tr| Event::Transaction(tr)) // .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(); // assert!(event_processor.process_events(events).is_ok()); diff --git a/src/result.rs b/src/result.rs index 3e2d2ac549..2c08058f75 100644 --- a/src/result.rs +++ b/src/result.rs @@ -1,6 +1,6 @@ //! The `result` module exposes a Result type that propagates one of many different Error types. -use accountant; +use bank; use bincode; use serde_json; use std; @@ -15,7 +15,7 @@ pub enum Error { RecvError(std::sync::mpsc::RecvError), RecvTimeoutError(std::sync::mpsc::RecvTimeoutError), Serialize(std::boxed::Box), - AccountingError(accountant::AccountingError), + BankError(bank::BankError), SendError, Services, GeneralError, @@ -33,9 +33,9 @@ impl std::convert::From for Error { Error::RecvTimeoutError(e) } } -impl std::convert::From for Error { - fn from(e: accountant::AccountingError) -> Error { - Error::AccountingError(e) +impl std::convert::From for Error { + fn from(e: bank::BankError) -> Error { + Error::BankError(e) } } impl std::convert::From> for Error { diff --git a/src/rpu.rs b/src/rpu.rs index 6bc7aa5226..cf43869d8d 100644 --- a/src/rpu.rs +++ b/src/rpu.rs @@ -1,7 +1,7 @@ //! The `rpu` module implements the Request Processing Unit, a //! 5-stage transaction processing pipeline in software. -use accountant::Accountant; +use bank::Bank; use crdt::{Crdt, ReplicatedData}; use entry::Entry; use entry_writer::EntryWriter; @@ -22,23 +22,23 @@ use std::time::Duration; use streamer; pub struct Rpu { - accountant: Arc, + bank: Arc, start_hash: Hash, tick_duration: Option, } impl Rpu { - /// Create a new Rpu that wraps the given Accountant. - pub fn new(accountant: Accountant, start_hash: Hash, tick_duration: Option) -> Self { + /// Create a new Rpu that wraps the given Bank. + pub fn new(bank: Bank, start_hash: Hash, tick_duration: Option) -> Self { Rpu { - accountant: Arc::new(accountant), + bank: Arc::new(bank), start_hash, tick_duration, } } fn write_service( - accountant: Arc, + bank: Arc, exit: Arc, broadcast: streamer::BlobSender, blob_recycler: packet::BlobRecycler, @@ -46,7 +46,7 @@ impl Rpu { entry_receiver: Receiver, ) -> JoinHandle<()> { spawn(move || loop { - let entry_writer = EntryWriter::new(&accountant); + let entry_writer = EntryWriter::new(&bank); let _ = entry_writer.write_and_send_entries( &broadcast, &blob_recycler, @@ -91,7 +91,7 @@ impl Rpu { let sig_verify_stage = SigVerifyStage::new(exit.clone(), packet_receiver); 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( request_processor, exit.clone(), @@ -108,7 +108,7 @@ impl Rpu { let (broadcast_sender, broadcast_receiver) = channel(); let t_write = Self::write_service( - self.accountant.clone(), + self.bank.clone(), exit.clone(), broadcast_sender, blob_recycler.clone(), diff --git a/src/streamer.rs b/src/streamer.rs index 77aab25dcb..c03b7c36cb 100644 --- a/src/streamer.rs +++ b/src/streamer.rs @@ -322,7 +322,7 @@ fn retransmit( /// # Arguments /// * `sock` - Socket to read from. Read timeout is set to 1. /// * `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. /// * `r` - Receive channel for blobs to be retransmitted to all the layer 1 nodes. pub fn retransmitter( diff --git a/src/thin_client.rs b/src/thin_client.rs index 4f45018f11..411e17afc4 100644 --- a/src/thin_client.rs +++ b/src/thin_client.rs @@ -154,7 +154,7 @@ impl ThinClient { #[cfg(test)] mod tests { use super::*; - use accountant::Accountant; + use bank::Bank; use crdt::{Crdt, ReplicatedData}; use futures::Future; use logger; @@ -187,10 +187,10 @@ mod tests { ); let alice = Mint::new(10_000); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let bob_pubkey = KeyPair::new().pubkey(); 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(); sleep(Duration::from_millis(900)); @@ -225,10 +225,10 @@ mod tests { fn test_bad_sig() { let (leader_data, leader_gossip, _, leader_serve, _leader_events) = tvu::test_node(); let alice = Mint::new(10_000); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let bob_pubkey = KeyPair::new().pubkey(); 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 threads = rpu.serve( leader_data, @@ -295,25 +295,25 @@ mod tests { let bob_pubkey = KeyPair::new().pubkey(); let exit = Arc::new(AtomicBool::new(false)); - let leader_acc = { - let accountant = Accountant::new(&alice); - Rpu::new(accountant, alice.last_id(), Some(Duration::from_millis(30))) + let leader_bank = { + let bank = Bank::new(&alice); + Rpu::new(bank, alice.last_id(), Some(Duration::from_millis(30))) }; - let replicant_acc = { - let accountant = Accountant::new(&alice); + let replicant_bank = { + let bank = Bank::new(&alice); Arc::new(Tvu::new( - accountant, + bank, alice.last_id(), 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()) .unwrap(); let replicant_threads = Tvu::serve( - &replicant_acc, + &replicant_bank, replicant.0.clone(), replicant.1, replicant.2, diff --git a/src/tvu.rs b/src/tvu.rs index df7a8989a1..62de4d839d 100644 --- a/src/tvu.rs +++ b/src/tvu.rs @@ -1,7 +1,7 @@ //! The `tvu` module implements the Transaction Validation Unit, a //! 5-stage transaction validation pipeline in software. -use accountant::Accountant; +use bank::Bank; use crdt::{Crdt, ReplicatedData}; use entry::Entry; use entry_writer::EntryWriter; @@ -22,28 +22,28 @@ use std::time::Duration; use streamer; pub struct Tvu { - accountant: Arc, + bank: Arc, start_hash: Hash, tick_duration: Option, } impl Tvu { - /// Create a new Tvu that wraps the given Accountant. - pub fn new(accountant: Accountant, start_hash: Hash, tick_duration: Option) -> Self { + /// Create a new Tvu that wraps the given Bank. + pub fn new(bank: Bank, start_hash: Hash, tick_duration: Option) -> Self { Tvu { - accountant: Arc::new(accountant), + bank: Arc::new(bank), start_hash, tick_duration, } } fn drain_service( - accountant: Arc, + bank: Arc, exit: Arc, entry_receiver: Receiver, ) -> JoinHandle<()> { spawn(move || { - let entry_writer = EntryWriter::new(&accountant); + let entry_writer = EntryWriter::new(&bank); loop { let _ = entry_writer.drain_entries(&entry_receiver); if exit.load(Ordering::Relaxed) { @@ -65,7 +65,7 @@ impl Tvu { let blobs = verified_receiver.recv_timeout(timer)?; trace!("replicating blobs {}", blobs.len()); let entries = ledger::reconstruct_entries_from_blobs(&blobs); - obj.accountant.process_verified_entries(entries)?; + obj.bank.process_verified_entries(entries)?; for blob in blobs { blob_recycler.recycle(blob); } @@ -73,9 +73,9 @@ impl Tvu { } /// This service receives messages from a leader in the network and processes the transactions - /// on the accountant state. + /// on the bank state. /// # Arguments - /// * `obj` - The accountant state. + /// * `obj` - The bank state. /// * `me` - my configuration /// * `leader` - leader configuration /// * `exit` - The exit signal. @@ -173,7 +173,7 @@ impl Tvu { 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( request_processor, exit.clone(), @@ -188,11 +188,8 @@ impl Tvu { obj.tick_duration, ); - let t_write = Self::drain_service( - obj.accountant.clone(), - exit.clone(), - record_stage.entry_receiver, - ); + let t_write = + Self::drain_service(obj.bank.clone(), exit.clone(), record_stage.entry_receiver); let t_responder = streamer::responder( respond_socket, @@ -240,7 +237,7 @@ pub fn test_node() -> (ReplicatedData, UdpSocket, UdpSocket, UdpSocket, UdpSocke #[cfg(test)] mod tests { - use accountant::Accountant; + use bank::Bank; use bincode::serialize; use chrono::prelude::*; use crdt::Crdt; @@ -311,9 +308,9 @@ mod tests { let starting_balance = 10_000; let alice = Mint::new(starting_balance); - let accountant = Accountant::new(&alice); + let bank = Bank::new(&alice); let tvu = Arc::new(Tvu::new( - accountant, + bank, alice.last_id(), Some(Duration::from_millis(30)), )); @@ -341,11 +338,11 @@ mod tests { w.set_index(i).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 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); let tr1 = Transaction::new( @@ -354,11 +351,11 @@ mod tests { transfer_amount, cur_hash, ); - accountant.register_entry_id(&cur_hash); + bank.register_entry_id(&cur_hash); cur_hash = hash(&cur_hash); let entry1 = 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); alice_ref_balance -= transfer_amount; @@ -383,11 +380,11 @@ mod tests { msgs.push(msg); } - let accountant = &tvu.accountant; - let alice_balance = accountant.get_balance(&alice.keypair().pubkey()).unwrap(); + let bank = &tvu.bank; + let alice_balance = bank.get_balance(&alice.keypair().pubkey()).unwrap(); 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); exit.store(true, Ordering::Relaxed);