From f59c93da6e0586a0a3e7896822b08fa68f53b712 Mon Sep 17 00:00:00 2001 From: Tao Zhu Date: Mon, 18 Mar 2024 16:42:14 +0000 Subject: [PATCH] more tests --- runtime/src/bank/tests.rs | 95 +++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 18 deletions(-) diff --git a/runtime/src/bank/tests.rs b/runtime/src/bank/tests.rs index 8525c9a01b27d2..ea2354ef8e3586 100644 --- a/runtime/src/bank/tests.rs +++ b/runtime/src/bank/tests.rs @@ -13795,20 +13795,29 @@ fn test_failed_simulation_compute_units() { #[test] fn test_filter_program_errors_and_collect_fee_details() { - // TX | EXECUTION RESULT | COLLECT | ADDITIONAL | COLLECT - // | (TX_FEE, PRIO_FEE) | WITHDRAW FROM PAYER | RESULT - // -------------------------------------------------------------------------------------------------- - // tx1 | executed and no error | (5_000, 1_000) | 0 | Ok - // tx2 | executed has error | (5_000, 1_000) | 6_000 | Ok - // tx3 | not executed | (0 , 0) | 0 | Original Err - // tx4 | executed error, payer insufficient fund | (0 , 0) | 0 | InsufficientFundsForFee + // TX | EXECUTION RESULT | is nonce | COLLECT | ADDITIONAL | COLLECT + // | | | (TX_FEE, PRIO_FEE) | WITHDRAW FROM PAYER | RESULT + // ------------------------------------------------------------------------------------------------------ + // tx1 | not executed | n/a | (0 , 0) | 0 | Original Err + // tx2 | executed and no error | n/a | (5_000, 1_000) | 0 | Ok + // tx3 | executed has error | true | (5_000, 1_000) | 0 | Ok + // tx4 | executed has error | false | (5_000, 1_000) | 6_000 | Ok + // tx5 | executed error, + // payer insufficient fund | false | (0 , 0) | 0 | InsufficientFundsForFee // let initial_payer_balance = 7_000; let additional_payer_withdraw = 6_000; let expected_collected_fee_details = CollectorFeeDetails { - transaction_fee: 10_000, - priority_fee: 2_000, + transaction_fee: 15_000, + priority_fee: 3_000, }; + let expected_collect_results = vec![ + Err(TransactionError::AccountNotFound), + Ok(()), + Ok(()), + Ok(()), + Err(TransactionError::InsufficientFundsForFee), + ]; let GenesisConfigInfo { mut genesis_config, @@ -13828,10 +13837,18 @@ fn test_filter_program_errors_and_collect_fee_details() { &[&mint_keypair], genesis_config.hash(), )); - let txs = vec![tx.clone(), tx.clone(), tx.clone(), tx]; + let txs = vec![tx.clone(), tx.clone(), tx.clone(), tx.clone(), tx]; let results = vec![ + TransactionExecutionResult::NotExecuted(TransactionError::AccountNotFound), new_execution_result(Ok(()), None), + new_execution_result( + Err(TransactionError::InstructionError( + 1, + SystemError::ResultWithNegativeLamports.into(), + )), + Some(&NonceFull::default()), + ), new_execution_result( Err(TransactionError::InstructionError( 1, @@ -13839,17 +13856,9 @@ fn test_filter_program_errors_and_collect_fee_details() { )), None, ), - TransactionExecutionResult::NotExecuted(TransactionError::AccountNotFound), new_execution_result(Err(TransactionError::AccountNotFound), None), ]; - let expected_collect_results = vec![ - Ok(()), - Ok(()), - Err(TransactionError::AccountNotFound), - Err(TransactionError::InsufficientFundsForFee), - ]; - let results = bank.filter_program_errors_and_collect_fee_details(&txs, &results); assert_eq!( @@ -13862,3 +13871,53 @@ fn test_filter_program_errors_and_collect_fee_details() { ); assert_eq!(expected_collect_results, results); } + +#[test] +fn test_check_execution_status_and_charge_fee() { + let fee = 5000; + let initial_balance = fee - 1000; + let tx_error = + TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature); + let GenesisConfigInfo { + mut genesis_config, + mint_keypair, + .. + } = create_genesis_config_with_leader(initial_balance, &Pubkey::new_unique(), 3); + genesis_config.fee_rate_governor = FeeRateGovernor::new(5000, 0); + let bank = Bank::new_for_tests(&genesis_config); + let message = new_sanitized_message(Message::new( + &[system_instruction::transfer( + &mint_keypair.pubkey(), + &Pubkey::new_unique(), + 1, + )], + Some(&mint_keypair.pubkey()), + )); + + [Ok(()), Err(tx_error)] + .iter() + .flat_map(|result| [true, false].iter().map(move |is_nonce| (result, is_nonce))) + .for_each(|(result, is_nonce)| { + if result.is_err() && !is_nonce { + assert_eq!( + Err(TransactionError::InsufficientFundsForFee), + bank.check_execution_status_and_charge_fee(&message, result, *is_nonce, fee) + ); + assert_eq!(initial_balance, bank.get_balance(&mint_keypair.pubkey())); + + let small_fee = 1; + assert!(bank + .check_execution_status_and_charge_fee(&message, result, *is_nonce, small_fee) + .is_ok()); + assert_eq!( + initial_balance - small_fee, + bank.get_balance(&mint_keypair.pubkey()) + ); + } else { + assert!(bank + .check_execution_status_and_charge_fee(&message, result, *is_nonce, fee) + .is_ok()); + assert_eq!(initial_balance, bank.get_balance(&mint_keypair.pubkey())); + } + }); +}