From f154c8c490e73e16e753546222257eb07f6000ed Mon Sep 17 00:00:00 2001 From: Greg Fitzgerald Date: Wed, 23 May 2018 12:26:32 -0600 Subject: [PATCH] Add data to errors to ease debugging --- src/bank.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/bank.rs b/src/bank.rs index 8fee843eb6602a..3a7c7540e8f5c3 100644 --- a/src/bank.rs +++ b/src/bank.rs @@ -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 = result::Result; @@ -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 { @@ -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( @@ -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); } @@ -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);