Better names

This commit is contained in:
Greg Fitzgerald
2018-05-29 10:01:40 -06:00
parent 0df6541d5e
commit 71cb7d5c97

View File

@ -158,13 +158,13 @@ impl Bank {
/// Deduct tokens from the 'from' address the account has sufficient /// Deduct tokens from the 'from' address the account has sufficient
/// funds and isn't a duplicate. /// funds and isn't a duplicate.
fn process_transaction_debits(&self, tx: &Transaction) -> Result<()> { fn apply_debits(&self, tx: &Transaction) -> Result<()> {
if let Instruction::NewContract(contract) = &tx.instruction { if let Instruction::NewContract(contract) = &tx.instruction {
trace!("Transaction {}", contract.tokens); trace!("Transaction {}", contract.tokens);
} }
let bals = self.balances let bals = self.balances
.read() .read()
.expect("'balances' read lock in process_transaction_debits"); .expect("'balances' read lock in apply_debits");
let option = bals.get(&tx.from); let option = bals.get(&tx.from);
if option.is_none() { if option.is_none() {
@ -205,36 +205,36 @@ impl Bank {
} }
} }
fn process_transaction_credits(&self, tx: &Transaction) { fn apply_credits(&self, tx: &Transaction) {
match &tx.instruction { match &tx.instruction {
Instruction::NewContract(contract) => { Instruction::NewContract(contract) => {
let mut plan = contract.plan.clone(); let mut plan = contract.plan.clone();
plan.apply_witness(&Witness::Timestamp(*self.last_time plan.apply_witness(&Witness::Timestamp(*self.last_time
.read() .read()
.expect("timestamp creation in process_transaction_credits"))); .expect("timestamp creation in apply_credits")));
if let Some(ref payment) = plan.final_payment() { if let Some(ref payment) = plan.final_payment() {
apply_payment(&self.balances, payment); apply_payment(&self.balances, payment);
} else { } else {
let mut pending = self.pending let mut pending = self.pending
.write() .write()
.expect("'pending' write lock in process_transaction_credits"); .expect("'pending' write lock in apply_credits");
pending.insert(tx.sig, plan); pending.insert(tx.sig, plan);
} }
} }
Instruction::ApplyTimestamp(dt) => { Instruction::ApplyTimestamp(dt) => {
let _ = self.process_timestamp(tx.from, *dt); let _ = self.apply_timestamp(tx.from, *dt);
} }
Instruction::ApplySignature(tx_sig) => { Instruction::ApplySignature(tx_sig) => {
let _ = self.process_signature(tx.from, *tx_sig); let _ = self.apply_signature(tx.from, *tx_sig);
} }
} }
} }
/// Process a Transaction. /// Process a Transaction.
fn process_transaction(&self, tx: &Transaction) -> Result<()> { fn process_transaction(&self, tx: &Transaction) -> Result<()> {
self.process_transaction_debits(tx)?; self.apply_debits(tx)?;
self.process_transaction_credits(tx); self.apply_credits(tx);
Ok(()) Ok(())
} }
@ -244,14 +244,14 @@ impl Bank {
// in parallel deterministically. // in parallel deterministically.
info!("processing Transactions {}", trs.len()); info!("processing Transactions {}", trs.len());
let results: Vec<_> = trs.into_par_iter() let results: Vec<_> = trs.into_par_iter()
.map(|tx| self.process_transaction_debits(&tx).map(|_| tx)) .map(|tx| self.apply_debits(&tx).map(|_| tx))
.collect(); // Calling collect() here forces all debits to complete before moving on. .collect(); // Calling collect() here forces all debits to complete before moving on.
results results
.into_par_iter() .into_par_iter()
.map(|result| { .map(|result| {
result.map(|tx| { result.map(|tx| {
self.process_transaction_credits(&tx); self.apply_credits(&tx);
tx tx
}) })
}) })
@ -269,10 +269,10 @@ impl Bank {
} }
/// Process a Witness Signature. /// Process a Witness Signature.
fn process_signature(&self, from: PublicKey, tx_sig: Signature) -> Result<()> { fn apply_signature(&self, from: PublicKey, tx_sig: Signature) -> Result<()> {
if let Occupied(mut e) = self.pending if let Occupied(mut e) = self.pending
.write() .write()
.expect("write() in process_signature") .expect("write() in apply_signature")
.entry(tx_sig) .entry(tx_sig)
{ {
e.get_mut().apply_witness(&Witness::Signature(from)); e.get_mut().apply_witness(&Witness::Signature(from));
@ -286,7 +286,7 @@ impl Bank {
} }
/// Process a Witness Timestamp. /// Process a Witness Timestamp.
fn process_timestamp(&self, from: PublicKey, dt: DateTime<Utc>) -> Result<()> { fn apply_timestamp(&self, from: PublicKey, dt: DateTime<Utc>) -> Result<()> {
// If this is the first timestamp we've seen, it probably came from the genesis block, // If this is the first timestamp we've seen, it probably came from the genesis block,
// so we'll trust it. // so we'll trust it.
if *self.last_time if *self.last_time
@ -319,7 +319,7 @@ impl Bank {
// double-spend if it enters before the modified plan is removed from 'pending'. // double-spend if it enters before the modified plan is removed from 'pending'.
let mut pending = self.pending let mut pending = self.pending
.write() .write()
.expect("'pending' write lock in process_timestamp"); .expect("'pending' write lock in apply_timestamp");
for (key, plan) in pending.iter_mut() { for (key, plan) in pending.iter_mut() {
plan.apply_witness(&Witness::Timestamp(*self.last_time plan.apply_witness(&Witness::Timestamp(*self.last_time
.read() .read()
@ -465,14 +465,14 @@ mod tests {
// Now, acknowledge the time in the condition occurred and // Now, acknowledge the time in the condition occurred and
// that pubkey's funds are now available. // that pubkey's funds are now available.
bank.process_timestamp(mint.pubkey(), dt).unwrap(); bank.apply_timestamp(mint.pubkey(), dt).unwrap();
assert_eq!(bank.get_balance(&pubkey), Some(1)); assert_eq!(bank.get_balance(&pubkey), Some(1));
// tx count is still 1, because we chose not to count timestamp transactions // tx count is still 1, because we chose not to count timestamp transactions
// tx count. // tx count.
assert_eq!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
bank.process_timestamp(mint.pubkey(), dt).unwrap(); // <-- Attack! Attempt to process completed transaction. bank.apply_timestamp(mint.pubkey(), dt).unwrap(); // <-- Attack! Attempt to process completed transaction.
assert_ne!(bank.get_balance(&pubkey), Some(2)); assert_ne!(bank.get_balance(&pubkey), Some(2));
} }
@ -482,7 +482,7 @@ mod tests {
let bank = Bank::new(&mint); let bank = Bank::new(&mint);
let pubkey = KeyPair::new().pubkey(); let pubkey = KeyPair::new().pubkey();
let dt = Utc::now(); let dt = Utc::now();
bank.process_timestamp(mint.pubkey(), dt).unwrap(); bank.apply_timestamp(mint.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.
bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id()) bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id())
@ -512,14 +512,14 @@ mod tests {
assert_eq!(bank.get_balance(&pubkey), None); assert_eq!(bank.get_balance(&pubkey), None);
// Now, cancel the trancaction. Mint gets her funds back, pubkey never sees them. // Now, cancel the trancaction. Mint gets her funds back, pubkey never sees them.
bank.process_signature(mint.pubkey(), sig).unwrap(); bank.apply_signature(mint.pubkey(), sig).unwrap();
assert_eq!(bank.get_balance(&mint.pubkey()), Some(1)); assert_eq!(bank.get_balance(&mint.pubkey()), Some(1));
assert_eq!(bank.get_balance(&pubkey), None); assert_eq!(bank.get_balance(&pubkey), None);
// Assert cancel doesn't cause count to go backward. // Assert cancel doesn't cause count to go backward.
assert_eq!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
bank.process_signature(mint.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction. bank.apply_signature(mint.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction.
assert_ne!(bank.get_balance(&mint.pubkey()), Some(2)); assert_ne!(bank.get_balance(&mint.pubkey()), Some(2));
} }