Skip to content

Commit

Permalink
Add data to errors to ease debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
garious committed May 23, 2018
1 parent d4959bc commit f154c8c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ pub const MAX_ENTRY_IDS: usize = 1024 * 4;

#[derive(Debug, PartialEq, Eq)]
pub enum BankError {
AccountNotFound,
InsufficientFunds,
InvalidTransferSignature,
AccountNotFound(PublicKey),
InsufficientFunds(PublicKey),
InvalidTransferSignature(Signature),
}

pub type Result<T> = result::Result<T, BankError>;
Expand Down Expand Up @@ -167,11 +167,11 @@ impl Bank {
let option = bals.get(&tr.from);

if option.is_none() {
return Err(BankError::AccountNotFound);
return Err(BankError::AccountNotFound(tr.from));
}

if !self.reserve_signature_with_last_id(&tr.sig, &tr.last_id) {
return Err(BankError::InvalidTransferSignature);
return Err(BankError::InvalidTransferSignature(tr.sig));
}

loop {
Expand All @@ -180,7 +180,7 @@ impl Bank {

if current < tr.contract.tokens {
self.forget_signature_with_last_id(&tr.sig, &tr.last_id);
return Err(BankError::InsufficientFunds);
return Err(BankError::InsufficientFunds(tr.from));
}

let result = bal.compare_exchange(
Expand Down Expand Up @@ -427,9 +427,10 @@ mod tests {
fn test_account_not_found() {
let mint = Mint::new(1);
let bank = Bank::new(&mint);
let keypair = KeyPair::new();
assert_eq!(
bank.transfer(1, &KeyPair::new(), mint.pubkey(), mint.last_id()),
Err(BankError::AccountNotFound)
bank.transfer(1, &keypair, mint.pubkey(), mint.last_id()),
Err(BankError::AccountNotFound(keypair.pubkey()))
);
assert_eq!(bank.transaction_count(), 0);
}
Expand All @@ -444,7 +445,7 @@ mod tests {
assert_eq!(bank.transaction_count(), 1);
assert_eq!(
bank.transfer(10_001, &mint.keypair(), pubkey, mint.last_id()),
Err(BankError::InsufficientFunds)
Err(BankError::InsufficientFunds(mint.pubkey()))
);
assert_eq!(bank.transaction_count(), 1);

Expand Down

0 comments on commit f154c8c

Please sign in to comment.