Boot Alice and Bob from the unit tests

This commit is contained in:
Greg Fitzgerald
2018-05-14 15:39:34 -06:00
parent d2dd005a59
commit 7736b9cac6
2 changed files with 75 additions and 79 deletions

View File

@ -407,18 +407,18 @@ mod tests {
#[test] #[test]
fn test_bank() { fn test_bank() {
let alice = Mint::new(10_000); let mint = Mint::new(10_000);
let bob_pubkey = KeyPair::new().pubkey(); let pubkey = KeyPair::new().pubkey();
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
assert_eq!(bank.last_id(), alice.last_id()); assert_eq!(bank.last_id(), mint.last_id());
bank.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id()) bank.transfer(1_000, &mint.keypair(), pubkey, mint.last_id())
.unwrap(); .unwrap();
assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_000); assert_eq!(bank.get_balance(&pubkey).unwrap(), 1_000);
bank.transfer(500, &alice.keypair(), bob_pubkey, alice.last_id()) bank.transfer(500, &mint.keypair(), pubkey, mint.last_id())
.unwrap(); .unwrap();
assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_500); assert_eq!(bank.get_balance(&pubkey).unwrap(), 1_500);
assert_eq!(bank.transaction_count(), 2); assert_eq!(bank.transaction_count(), 2);
} }
@ -435,155 +435,151 @@ mod tests {
#[test] #[test]
fn test_invalid_transfer() { fn test_invalid_transfer() {
let alice = Mint::new(11_000); let mint = Mint::new(11_000);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
let bob_pubkey = KeyPair::new().pubkey(); let pubkey = KeyPair::new().pubkey();
bank.transfer(1_000, &alice.keypair(), bob_pubkey, alice.last_id()) bank.transfer(1_000, &mint.keypair(), pubkey, mint.last_id())
.unwrap(); .unwrap();
assert_eq!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
assert_eq!( assert_eq!(
bank.transfer(10_001, &alice.keypair(), bob_pubkey, alice.last_id()), bank.transfer(10_001, &mint.keypair(), pubkey, mint.last_id()),
Err(BankError::InsufficientFunds) Err(BankError::InsufficientFunds)
); );
assert_eq!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
let alice_pubkey = alice.keypair().pubkey(); let mint_pubkey = mint.keypair().pubkey();
assert_eq!(bank.get_balance(&alice_pubkey).unwrap(), 10_000); assert_eq!(bank.get_balance(&mint_pubkey).unwrap(), 10_000);
assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 1_000); assert_eq!(bank.get_balance(&pubkey).unwrap(), 1_000);
} }
#[test] #[test]
fn test_transfer_to_newb() { fn test_transfer_to_newb() {
let alice = Mint::new(10_000); let mint = Mint::new(10_000);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
let alice_keypair = alice.keypair(); let pubkey = KeyPair::new().pubkey();
let bob_pubkey = KeyPair::new().pubkey(); bank.transfer(500, &mint.keypair(), pubkey, mint.last_id())
bank.transfer(500, &alice_keypair, bob_pubkey, alice.last_id())
.unwrap(); .unwrap();
assert_eq!(bank.get_balance(&bob_pubkey).unwrap(), 500); assert_eq!(bank.get_balance(&pubkey).unwrap(), 500);
} }
#[test] #[test]
fn test_transfer_on_date() { fn test_transfer_on_date() {
let alice = Mint::new(1); let mint = Mint::new(1);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
let alice_keypair = alice.keypair(); let pubkey = KeyPair::new().pubkey();
let bob_pubkey = KeyPair::new().pubkey();
let dt = Utc::now(); let dt = Utc::now();
bank.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id()) bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id())
.unwrap(); .unwrap();
// Alice's balance will be zero because all funds are locked up. // Mint's balance will be zero because all funds are locked up.
assert_eq!(bank.get_balance(&alice.pubkey()), Some(0)); assert_eq!(bank.get_balance(&mint.pubkey()), Some(0));
// tx count is 1, because debits were applied. // tx count is 1, because debits were applied.
assert_eq!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
// Bob's balance will be None because the funds have not been // pubkey's balance will be None because the funds have not been
// sent. // sent.
assert_eq!(bank.get_balance(&bob_pubkey), None); assert_eq!(bank.get_balance(&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 pubkey's funds are now available.
bank.process_verified_timestamp(alice.pubkey(), dt).unwrap(); bank.process_verified_timestamp(mint.pubkey(), dt).unwrap();
assert_eq!(bank.get_balance(&bob_pubkey), Some(1)); assert_eq!(bank.get_balance(&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!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
bank.process_verified_timestamp(alice.pubkey(), dt).unwrap(); // <-- Attack! Attempt to process completed transaction. bank.process_verified_timestamp(mint.pubkey(), dt).unwrap(); // <-- Attack! Attempt to process completed transaction.
assert_ne!(bank.get_balance(&bob_pubkey), Some(2)); assert_ne!(bank.get_balance(&pubkey), Some(2));
} }
#[test] #[test]
fn test_transfer_after_date() { fn test_transfer_after_date() {
let alice = Mint::new(1); let mint = Mint::new(1);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
let alice_keypair = alice.keypair(); let pubkey = KeyPair::new().pubkey();
let bob_pubkey = KeyPair::new().pubkey();
let dt = Utc::now(); let dt = Utc::now();
bank.process_verified_timestamp(alice.pubkey(), dt).unwrap(); bank.process_verified_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, &alice_keypair, bob_pubkey, dt, alice.last_id()) bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id())
.unwrap(); .unwrap();
assert_eq!(bank.get_balance(&alice.pubkey()), Some(0)); assert_eq!(bank.get_balance(&mint.pubkey()), Some(0));
assert_eq!(bank.get_balance(&bob_pubkey), Some(1)); assert_eq!(bank.get_balance(&pubkey), Some(1));
} }
#[test] #[test]
fn test_cancel_transfer() { fn test_cancel_transfer() {
let alice = Mint::new(1); let mint = Mint::new(1);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
let alice_keypair = alice.keypair(); let pubkey = KeyPair::new().pubkey();
let bob_pubkey = KeyPair::new().pubkey();
let dt = Utc::now(); let dt = Utc::now();
let sig = bank.transfer_on_date(1, &alice_keypair, bob_pubkey, dt, alice.last_id()) let sig = bank.transfer_on_date(1, &mint.keypair(), pubkey, dt, mint.last_id())
.unwrap(); .unwrap();
// Assert the debit counts as a transaction. // Assert the debit counts as a transaction.
assert_eq!(bank.transaction_count(), 1); assert_eq!(bank.transaction_count(), 1);
// Alice's balance will be zero because all funds are locked up. // Mint's balance will be zero because all funds are locked up.
assert_eq!(bank.get_balance(&alice.pubkey()), Some(0)); assert_eq!(bank.get_balance(&mint.pubkey()), Some(0));
// Bob's balance will be None because the funds have not been // pubkey's balance will be None because the funds have not been
// sent. // sent.
assert_eq!(bank.get_balance(&bob_pubkey), None); assert_eq!(bank.get_balance(&pubkey), None);
// Now, cancel the trancaction. Alice gets her funds back, Bob never sees them. // Now, cancel the trancaction. Mint gets her funds back, pubkey never sees them.
bank.process_verified_sig(alice.pubkey(), sig).unwrap(); bank.process_verified_sig(mint.pubkey(), sig).unwrap();
assert_eq!(bank.get_balance(&alice.pubkey()), Some(1)); assert_eq!(bank.get_balance(&mint.pubkey()), Some(1));
assert_eq!(bank.get_balance(&bob_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_verified_sig(alice.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction. bank.process_verified_sig(mint.pubkey(), sig).unwrap(); // <-- Attack! Attempt to cancel completed transaction.
assert_ne!(bank.get_balance(&alice.pubkey()), Some(2)); assert_ne!(bank.get_balance(&mint.pubkey()), Some(2));
} }
#[test] #[test]
fn test_duplicate_event_signature() { fn test_duplicate_event_signature() {
let alice = Mint::new(1); let mint = Mint::new(1);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
let sig = Signature::default(); let sig = Signature::default();
assert!(bank.reserve_signature_with_last_id(&sig, &alice.last_id())); assert!(bank.reserve_signature_with_last_id(&sig, &mint.last_id()));
assert!(!bank.reserve_signature_with_last_id(&sig, &alice.last_id())); assert!(!bank.reserve_signature_with_last_id(&sig, &mint.last_id()));
} }
#[test] #[test]
fn test_forget_signature() { fn test_forget_signature() {
let alice = Mint::new(1); let mint = Mint::new(1);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
let sig = Signature::default(); let sig = Signature::default();
bank.reserve_signature_with_last_id(&sig, &alice.last_id()); bank.reserve_signature_with_last_id(&sig, &mint.last_id());
assert!(bank.forget_signature_with_last_id(&sig, &alice.last_id())); assert!(bank.forget_signature_with_last_id(&sig, &mint.last_id()));
assert!(!bank.forget_signature_with_last_id(&sig, &alice.last_id())); assert!(!bank.forget_signature_with_last_id(&sig, &mint.last_id()));
} }
#[test] #[test]
fn test_max_entry_ids() { fn test_max_entry_ids() {
let alice = Mint::new(1); let mint = Mint::new(1);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
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
bank.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!(!bank.reserve_signature_with_last_id(&sig, &alice.last_id())); assert!(!bank.reserve_signature_with_last_id(&sig, &mint.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 bank = Bank::new(&mint); let bank = Bank::new(&mint);
let alice = KeyPair::new(); let keypair = KeyPair::new();
let tr0 = Transaction::new(&mint.keypair(), alice.pubkey(), 2, mint.last_id()); let tr0 = Transaction::new(&mint.keypair(), keypair.pubkey(), 2, mint.last_id());
let tr1 = Transaction::new(&alice, mint.pubkey(), 1, mint.last_id()); let tr1 = Transaction::new(&keypair, mint.pubkey(), 1, mint.last_id());
let trs = vec![tr0, tr1]; let trs = vec![tr0, tr1];
let results = bank.process_verified_transactions(trs); let results = bank.process_verified_transactions(trs);
assert!(results[1].is_err()); assert!(results[1].is_err());

View File

@ -307,11 +307,11 @@ mod tests {
); );
let starting_balance = 10_000; let starting_balance = 10_000;
let alice = Mint::new(starting_balance); let mint = Mint::new(starting_balance);
let bank = Bank::new(&alice); let bank = Bank::new(&mint);
let tvu = Arc::new(Tvu::new( let tvu = Arc::new(Tvu::new(
bank, bank,
alice.last_id(), mint.last_id(),
Some(Duration::from_millis(30)), Some(Duration::from_millis(30)),
)); ));
let replicate_addr = target1_data.replicate_addr; let replicate_addr = target1_data.replicate_addr;
@ -346,7 +346,7 @@ mod tests {
cur_hash = hash(&cur_hash); cur_hash = hash(&cur_hash);
let tr1 = Transaction::new( let tr1 = Transaction::new(
&alice.keypair(), &mint.keypair(),
bob_keypair.pubkey(), bob_keypair.pubkey(),
transfer_amount, transfer_amount,
cur_hash, cur_hash,
@ -381,7 +381,7 @@ mod tests {
} }
let bank = &tvu.bank; let bank = &tvu.bank;
let alice_balance = bank.get_balance(&alice.keypair().pubkey()).unwrap(); let alice_balance = bank.get_balance(&mint.keypair().pubkey()).unwrap();
assert_eq!(alice_balance, alice_ref_balance); assert_eq!(alice_balance, alice_ref_balance);
let bob_balance = bank.get_balance(&bob_keypair.pubkey()).unwrap(); let bob_balance = bank.get_balance(&bob_keypair.pubkey()).unwrap();