Test invalid tokens and fees

This commit is contained in:
Greg Fitzgerald
2018-05-29 20:35:14 -06:00
committed by Grimes
parent 38623785f9
commit 290dde60a0
2 changed files with 24 additions and 7 deletions

View File

@ -25,7 +25,8 @@ pub const MAX_ENTRY_IDS: usize = 1024 * 4;
pub enum BankError { pub enum BankError {
AccountNotFound(PublicKey), AccountNotFound(PublicKey),
InsufficientFunds(PublicKey), InsufficientFunds(PublicKey),
InvalidTransferSignature(Signature), InvalidTxSignature(Signature),
InvalidTxTokens,
} }
pub type Result<T> = result::Result<T, BankError>; pub type Result<T> = result::Result<T, BankError>;
@ -161,6 +162,9 @@ impl Bank {
fn apply_debits(&self, tx: &Transaction) -> Result<()> { fn apply_debits(&self, tx: &Transaction) -> Result<()> {
if let Instruction::NewContract(contract) = &tx.instruction { if let Instruction::NewContract(contract) = &tx.instruction {
trace!("Transaction {}", contract.tokens); trace!("Transaction {}", contract.tokens);
if contract.tokens < 0 {
return Err(BankError::InvalidTxTokens);
}
} }
let bals = self.balances let bals = self.balances
.read() .read()
@ -172,7 +176,7 @@ impl Bank {
} }
if !self.reserve_signature_with_last_id(&tx.sig, &tx.last_id) { if !self.reserve_signature_with_last_id(&tx.sig, &tx.last_id) {
return Err(BankError::InvalidTransferSignature(tx.sig)); return Err(BankError::InvalidTxSignature(tx.sig));
} }
loop { loop {
@ -403,6 +407,18 @@ mod tests {
assert_eq!(bank.transaction_count(), 2); assert_eq!(bank.transaction_count(), 2);
} }
#[test]
fn test_invalid_tokens() {
let mint = Mint::new(1);
let pubkey = KeyPair::new().pubkey();
let bank = Bank::new(&mint);
assert_eq!(
bank.transfer(-1, &mint.keypair(), pubkey, mint.last_id()),
Err(BankError::InvalidTxTokens)
);
assert_eq!(bank.transaction_count(), 0);
}
#[test] #[test]
fn test_account_not_found() { fn test_account_not_found() {
let mint = Mint::new(1); let mint = Mint::new(1);

View File

@ -154,7 +154,8 @@ impl Transaction {
pub fn verify_plan(&self) -> bool { pub fn verify_plan(&self) -> bool {
if let Instruction::NewContract(contract) = &self.instruction { if let Instruction::NewContract(contract) = &self.instruction {
contract.plan.verify(contract.tokens - self.fee) self.fee >= 0 && self.fee <= contract.tokens
&& contract.plan.verify(contract.tokens - self.fee)
} else { } else {
true true
} }
@ -208,10 +209,10 @@ mod tests {
fn test_transfer_with_fee() { fn test_transfer_with_fee() {
let zero = Hash::default(); let zero = Hash::default();
let keypair0 = KeyPair::new(); let keypair0 = KeyPair::new();
let keypair1 = KeyPair::new(); let pubkey1 = KeyPair::new().pubkey();
let pubkey1 = keypair1.pubkey(); assert!(Transaction::new_taxed(&keypair0, pubkey1, 1, 1, zero).verify_plan());
let tx0 = Transaction::new_taxed(&keypair0, pubkey1, 42, 1, zero); assert!(!Transaction::new_taxed(&keypair0, pubkey1, 1, 2, zero).verify_plan());
assert!(tx0.verify_plan()); assert!(!Transaction::new_taxed(&keypair0, pubkey1, 1, -1, zero).verify_plan());
} }
#[test] #[test]