diff --git a/src/accountant.rs b/src/accountant.rs index 9a04f6b9d..1cc220727 100644 --- a/src/accountant.rs +++ b/src/accountant.rs @@ -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 { 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 { 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 { @@ -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); diff --git a/src/transaction.rs b/src/transaction.rs index c88a1eea9..be43b8837 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -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();