Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-stones committed Mar 18, 2024
1 parent 0e45085 commit 43b8180
Showing 1 changed file with 77 additions and 18 deletions.
95 changes: 77 additions & 18 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13797,20 +13797,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,
Expand All @@ -13830,28 +13839,28 @@ 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,
SystemError::ResultWithNegativeLamports.into(),
)),
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!(
Expand All @@ -13864,3 +13873,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()));
}
});
}

0 comments on commit 43b8180

Please sign in to comment.