Merge pull request #101 from garious/rollback

Move tests
This commit is contained in:
Greg Fitzgerald 2018-04-02 21:58:10 -06:00 committed by GitHub
commit 5c672adc21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 36 deletions

View File

@ -17,7 +17,6 @@ use transaction::Transaction;
#[derive(Debug, PartialEq, Eq)]
pub enum AccountingError {
InsufficientFunds,
InvalidTransfer,
InvalidTransferSignature,
}
@ -59,15 +58,6 @@ impl Accountant {
Self::new_from_deposit(&deposit)
}
/// Verify and process the given Transaction.
pub fn process_transaction(&mut self, tr: Transaction) -> Result<()> {
if !tr.verify() {
return Err(AccountingError::InvalidTransfer);
}
self.process_verified_transaction(&tr)
}
fn reserve_signature(&mut self, sig: &Signature) -> bool {
if self.signatures.contains(sig) {
return false;
@ -168,7 +158,7 @@ impl Accountant {
) -> Result<Signature> {
let tr = Transaction::new(keypair, to, n, last_id);
let sig = tr.sig;
self.process_transaction(tr).map(|_| sig)
self.process_verified_transaction(&tr).map(|_| sig)
}
/// Create, sign, and process a postdated Transaction from `keypair`
@ -184,7 +174,7 @@ impl Accountant {
) -> Result<Signature> {
let tr = Transaction::new_on_date(keypair, to, dt, n, last_id);
let sig = tr.sig;
self.process_transaction(tr).map(|_| sig)
self.process_verified_transaction(&tr).map(|_| sig)
}
pub fn get_balance(&self, pubkey: &PublicKey) -> Option<i64> {
@ -228,30 +218,6 @@ mod tests {
assert_eq!(acc.get_balance(&bob_pubkey).unwrap(), 1_000);
}
#[test]
fn test_overspend_attack() {
let alice = Mint::new(1);
let mut acc = Accountant::new(&alice);
let bob_pubkey = KeyPair::new().pubkey();
let mut tr = Transaction::new(&alice.keypair(), bob_pubkey, 1, alice.last_id());
if let Plan::Pay(ref mut payment) = tr.plan {
payment.tokens = 2; // <-- attack!
}
assert_eq!(
acc.process_transaction(tr.clone()),
Err(AccountingError::InvalidTransfer)
);
// Also, ensure all branchs of the plan spend all tokens
if let Plan::Pay(ref mut payment) = tr.plan {
payment.tokens = 0; // <-- whoops!
}
assert_eq!(
acc.process_transaction(tr.clone()),
Err(AccountingError::InvalidTransfer)
);
}
#[test]
fn test_transfer_to_newb() {
let alice = Mint::new(10_000);

View File

@ -154,6 +154,24 @@ mod tests {
assert!(!tr.verify());
}
#[test]
fn test_overspend_attack() {
let keypair0 = KeyPair::new();
let keypair1 = KeyPair::new();
let zero = Hash::default();
let mut tr = Transaction::new(&keypair0, keypair1.pubkey(), 1, zero);
if let Plan::Pay(ref mut payment) = tr.plan {
payment.tokens = 2; // <-- attack!
}
assert!(!tr.verify());
// Also, ensure all branchs of the plan spend all tokens
if let Plan::Pay(ref mut payment) = tr.plan {
payment.tokens = 0; // <-- whoops!
}
assert!(!tr.verify());
}
#[test]
fn test_verify_transactions() {
let alice_keypair = KeyPair::new();