Skip to content

Commit

Permalink
Merge pull request #101 from garious/rollback
Browse files Browse the repository at this point in the history
Move tests
  • Loading branch information
garious authored Apr 3, 2018
2 parents 95749ed + 2f80747 commit 5c672ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
38 changes: 2 additions & 36 deletions src/accountant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use transaction::Transaction;
#[derive(Debug, PartialEq, Eq)]
pub enum AccountingError {
InsufficientFunds,
InvalidTransfer,
InvalidTransferSignature,
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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`
Expand All @@ -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> {
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 5c672ad

Please sign in to comment.