From d30049b8ebd6b9b39e0b22942dfd70bcd1f00e1a Mon Sep 17 00:00:00 2001 From: Rob Walker Date: Fri, 5 Apr 2019 16:55:58 -0700 Subject: [PATCH] test for debit of TX fees on full process_transaction() (#3643) * fix double debit of TX fees * add test that fails when removing that line * put that line back in * comments --- runtime/src/bank.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index bcdc281f92..2b4ca68813 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -711,7 +711,9 @@ impl Bank { let message = tx.message(); match *res { Err(TransactionError::InstructionError(_, _)) => { - // Charge the transaction fee even in case of InstructionError + // credit the transaction fee even in case of InstructionError + // necessary to withdraw from account[0] here because previous + // work of doing so (in accounts.load()) is ignored by store() self.withdraw(&message.account_keys[0], fee)?; fees += fee; Ok(()) @@ -1283,11 +1285,23 @@ mod tests { bank.fee_calculator.lamports_per_signature = 1; let tx = system_transaction::transfer(&key1, &key2.pubkey(), 1, genesis_block.hash(), 0); + assert_eq!(bank.process_transaction(&tx), Ok(())); assert_eq!(bank.get_balance(&leader), initial_balance + 4); assert_eq!(bank.get_balance(&key1.pubkey()), 0); assert_eq!(bank.get_balance(&key2.pubkey()), 1); assert_eq!(bank.get_balance(&mint_keypair.pubkey()), 100 - 5 - 3); + + // verify that an InstructionError collects fees, too + let mut tx = + system_transaction::transfer(&mint_keypair, &key2.pubkey(), 1, genesis_block.hash(), 0); + // send a bogus instruction to system_program, cause an instruction error + tx.message.instructions[0].data[0] = 40; + + bank.process_transaction(&tx).is_err(); // fails with an instruction error + assert_eq!(bank.get_balance(&leader), initial_balance + 5); // gots our bucks + assert_eq!(bank.get_balance(&key2.pubkey()), 1); // our fee ------V + assert_eq!(bank.get_balance(&mint_keypair.pubkey()), 100 - 5 - 3 - 1); } #[test]