Skip to content

Commit

Permalink
harden bank tests (#34821)
Browse files Browse the repository at this point in the history
* harden bank tests by specifying exact genesis config wrt the fee rate and rent

* rename to clarify test function, add comments
  • Loading branch information
tao-stones authored Jan 22, 2024
1 parent 9db4e84 commit a5c470d
Showing 1 changed file with 69 additions and 33 deletions.
102 changes: 69 additions & 33 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use {
feature_set::{self, FeatureSet},
fee::FeeStructure,
fee_calculator::FeeRateGovernor,
genesis_config::{create_genesis_config, ClusterType, GenesisConfig},
genesis_config::{ClusterType, GenesisConfig},
hash::{hash, Hash},
incinerator,
instruction::{AccountMeta, CompiledInstruction, Instruction, InstructionError},
Expand Down Expand Up @@ -170,6 +170,28 @@ fn new_bank_from_parent_with_bank_forks(
.clone_without_scheduler()
}

fn create_genesis_config_no_tx_fee_no_rent(lamports: u64) -> (GenesisConfig, Keypair) {
// genesis_util creates config with no tx fee and no rent
let genesis_config_info = solana_runtime::genesis_utils::create_genesis_config(lamports);
(
genesis_config_info.genesis_config,
genesis_config_info.mint_keypair,
)
}

fn create_genesis_config_no_tx_fee(lamports: u64) -> (GenesisConfig, Keypair) {
// genesis_config creates config with default fee rate and default rent
// override to set fee rate to zero.
let (mut genesis_config, mint_keypair) =
solana_sdk::genesis_config::create_genesis_config(lamports);
genesis_config.fee_rate_governor = FeeRateGovernor::new(0, 0);
(genesis_config, mint_keypair)
}

fn create_genesis_config(lamports: u64) -> (GenesisConfig, Keypair) {
solana_sdk::genesis_config::create_genesis_config(lamports)
}

#[test]
fn test_race_register_tick_freeze() {
solana_logger::setup();
Expand Down Expand Up @@ -428,7 +450,7 @@ fn test_credit_debit_rent_no_side_effect_on_hash() {
for set_exempt_rent_epoch_max in [false, true] {
solana_logger::setup();

let (mut genesis_config, _mint_keypair) = create_genesis_config(10);
let (mut genesis_config, _mint_keypair) = create_genesis_config_no_tx_fee(10);

genesis_config.rent = rent_with_exemption_threshold(21.0);

Expand Down Expand Up @@ -2128,7 +2150,7 @@ fn test_purge_empty_accounts() {
// so we have to stop at various points and restart to actively test.
for pass in 0..3 {
solana_logger::setup();
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.));
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(1.));
let amount = genesis_config.rent.minimum_balance(0);
let (mut bank, bank_forks) =
Bank::new_for_tests(&genesis_config).wrap_with_bank_forks_for_tests();
Expand Down Expand Up @@ -2238,7 +2260,7 @@ fn test_two_payments_to_one_party() {

#[test]
fn test_one_source_two_tx_one_batch() {
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.));
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(1.));
let key1 = solana_sdk::pubkey::new_rand();
let key2 = solana_sdk::pubkey::new_rand();
let bank = Bank::new_with_bank_forks_for_tests(&genesis_config).0;
Expand Down Expand Up @@ -2268,7 +2290,7 @@ fn test_one_source_two_tx_one_batch() {
#[test]
fn test_one_tx_two_out_atomic_fail() {
let amount = sol_to_lamports(1.);
let (genesis_config, mint_keypair) = create_genesis_config(amount);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(amount);
let key1 = solana_sdk::pubkey::new_rand();
let key2 = solana_sdk::pubkey::new_rand();
let bank = Bank::new_with_bank_forks_for_tests(&genesis_config).0;
Expand All @@ -2289,7 +2311,7 @@ fn test_one_tx_two_out_atomic_fail() {

#[test]
fn test_one_tx_two_out_atomic_pass() {
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.));
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(1.));
let key1 = solana_sdk::pubkey::new_rand();
let key2 = solana_sdk::pubkey::new_rand();
let bank = Bank::new_with_bank_forks_for_tests(&genesis_config).0;
Expand Down Expand Up @@ -2360,7 +2382,7 @@ fn test_account_not_found() {
#[test]
fn test_insufficient_funds() {
let mint_amount = sol_to_lamports(1.);
let (genesis_config, mint_keypair) = create_genesis_config(mint_amount);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(mint_amount);
let bank = Bank::new_with_bank_forks_for_tests(&genesis_config).0;
let pubkey = solana_sdk::pubkey::new_rand();
let amount = genesis_config.rent.minimum_balance(0);
Expand Down Expand Up @@ -2643,20 +2665,20 @@ fn test_bank_tx_compute_unit_fee() {
} = create_genesis_config_with_leader(mint, &leader, 3);
genesis_config.fee_rate_governor = FeeRateGovernor::new(4, 0); // something divisible by 2

let (bank, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);

let expected_fee_paid = calculate_test_fee(
&SanitizedMessage::try_from(Message::new(&[], Some(&Pubkey::new_unique()))).unwrap(),
genesis_config
.fee_rate_governor
.create_fee_calculator()
.lamports_per_signature,
&FeeStructure::default(),
&bank.fee_structure,
);

let (expected_fee_collected, expected_fee_burned) =
genesis_config.fee_rate_governor.burn(expected_fee_paid);

let (bank, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);

let capitalization = bank.capitalization();

let tx = system_transaction::transfer(
Expand Down Expand Up @@ -2773,9 +2795,14 @@ fn test_bank_blockhash_fee_structure() {
let tx = system_transaction::transfer(&mint_keypair, &key, 1, cheap_blockhash);
assert_eq!(bank.process_transaction(&tx), Ok(()));
assert_eq!(bank.get_balance(&key), 1);
let cheap_fee = calculate_test_fee(
&SanitizedMessage::try_from(Message::new(&[], Some(&Pubkey::new_unique()))).unwrap(),
cheap_lamports_per_signature,
&bank.fee_structure,
);
assert_eq!(
bank.get_balance(&mint_keypair.pubkey()),
initial_mint_balance - 1 - cheap_lamports_per_signature
initial_mint_balance - 1 - cheap_fee
);

// Send a transfer using expensive_blockhash
Expand All @@ -2784,9 +2811,14 @@ fn test_bank_blockhash_fee_structure() {
let tx = system_transaction::transfer(&mint_keypair, &key, 1, expensive_blockhash);
assert_eq!(bank.process_transaction(&tx), Ok(()));
assert_eq!(bank.get_balance(&key), 1);
let expensive_fee = calculate_test_fee(
&SanitizedMessage::try_from(Message::new(&[], Some(&Pubkey::new_unique()))).unwrap(),
expensive_lamports_per_signature,
&bank.fee_structure,
);
assert_eq!(
bank.get_balance(&mint_keypair.pubkey()),
initial_mint_balance - 1 - expensive_lamports_per_signature
initial_mint_balance - 1 - expensive_fee
);
}

Expand Down Expand Up @@ -2828,7 +2860,7 @@ fn test_bank_blockhash_compute_unit_fee_structure() {
let cheap_fee = calculate_test_fee(
&SanitizedMessage::try_from(Message::new(&[], Some(&Pubkey::new_unique()))).unwrap(),
cheap_lamports_per_signature,
&FeeStructure::default(),
&bank.fee_structure,
);
assert_eq!(
bank.get_balance(&mint_keypair.pubkey()),
Expand All @@ -2844,7 +2876,7 @@ fn test_bank_blockhash_compute_unit_fee_structure() {
let expensive_fee = calculate_test_fee(
&SanitizedMessage::try_from(Message::new(&[], Some(&Pubkey::new_unique()))).unwrap(),
expensive_lamports_per_signature,
&FeeStructure::default(),
&bank.fee_structure,
);
assert_eq!(
bank.get_balance(&mint_keypair.pubkey()),
Expand Down Expand Up @@ -2955,7 +2987,7 @@ fn test_filter_program_errors_and_collect_compute_unit_fee() {
.fee_rate_governor
.create_fee_calculator()
.lamports_per_signature,
&FeeStructure::default(),
&bank.fee_structure,
) * 2
)
.0
Expand All @@ -2966,7 +2998,8 @@ fn test_filter_program_errors_and_collect_compute_unit_fee() {

#[test]
fn test_debits_before_credits() {
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(2.));
let (genesis_config, mint_keypair) =
create_genesis_config_no_tx_fee_no_rent(sol_to_lamports(2.));
let bank = Bank::new_with_bank_forks_for_tests(&genesis_config).0;
let keypair = Keypair::new();
let tx0 = system_transaction::transfer(
Expand Down Expand Up @@ -3204,7 +3237,7 @@ fn test_bank_invalid_account_index() {

#[test]
fn test_bank_pay_to_self() {
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.));
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(1.));
let key1 = Keypair::new();
let bank = Bank::new_with_bank_forks_for_tests(&genesis_config).0;
let amount = genesis_config.rent.minimum_balance(0);
Expand Down Expand Up @@ -3297,11 +3330,12 @@ fn test_bank_parent_already_processed() {
/// Verifies that last ids and accounts are correctly referenced from parent
#[test]
fn test_bank_parent_account_spend() {
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.0));
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(1.0));
let key1 = Keypair::new();
let key2 = Keypair::new();
let (parent, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);
let amount = genesis_config.rent.minimum_balance(0);
println!("==== amount {}", amount);

let tx =
system_transaction::transfer(&mint_keypair, &key1.pubkey(), amount, genesis_config.hash());
Expand All @@ -3314,7 +3348,8 @@ fn test_bank_parent_account_spend() {

#[test]
fn test_bank_hash_internal_state() {
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.));
let (genesis_config, mint_keypair) =
create_genesis_config_no_tx_fee_no_rent(sol_to_lamports(1.));
let (bank0, _) = Bank::new_with_bank_forks_for_tests(&genesis_config);
let (bank1, bank_forks_1) = Bank::new_with_bank_forks_for_tests(&genesis_config);
let amount = genesis_config.rent.minimum_balance(0);
Expand Down Expand Up @@ -3345,7 +3380,8 @@ fn test_bank_hash_internal_state() {
fn test_bank_hash_internal_state_verify() {
for pass in 0..3 {
solana_logger::setup();
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.));
let (genesis_config, mint_keypair) =
create_genesis_config_no_tx_fee_no_rent(sol_to_lamports(1.));
let (bank0, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);
let amount = genesis_config.rent.minimum_balance(0);

Expand Down Expand Up @@ -3538,7 +3574,7 @@ fn test_bank_hash_internal_state_squash() {
#[test]
fn test_bank_squash() {
solana_logger::setup();
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(2.));
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(2.));
let key1 = Keypair::new();
let key2 = Keypair::new();
let (parent, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);
Expand Down Expand Up @@ -4544,7 +4580,7 @@ fn test_status_cache_ancestors() {

#[test]
fn test_add_builtin() {
let (genesis_config, mint_keypair) = create_genesis_config(500);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(500);
let mut bank = Bank::new_for_tests(&genesis_config);

fn mock_vote_program_id() -> Pubkey {
Expand Down Expand Up @@ -5968,7 +6004,7 @@ fn test_pre_post_transaction_balances() {

#[test]
fn test_transaction_with_duplicate_accounts_in_instruction() {
let (genesis_config, mint_keypair) = create_genesis_config(500);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(500);

let mock_program_id = Pubkey::from([2u8; 32]);
let bank =
Expand Down Expand Up @@ -6025,7 +6061,7 @@ fn test_transaction_with_duplicate_accounts_in_instruction() {

#[test]
fn test_transaction_with_program_ids_passed_to_programs() {
let (genesis_config, mint_keypair) = create_genesis_config(500);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(500);

let mock_program_id = Pubkey::from([2u8; 32]);
let bank =
Expand Down Expand Up @@ -6061,7 +6097,7 @@ fn test_transaction_with_program_ids_passed_to_programs() {
#[test]
fn test_account_ids_after_program_ids() {
solana_logger::setup();
let (genesis_config, mint_keypair) = create_genesis_config(500);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(500);
let (bank, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);

let from_pubkey = solana_sdk::pubkey::new_rand();
Expand Down Expand Up @@ -6100,7 +6136,7 @@ fn test_account_ids_after_program_ids() {

#[test]
fn test_incinerator() {
let (genesis_config, mint_keypair) = create_genesis_config(1_000_000_000_000);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(1_000_000_000_000);
let (bank0, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);

// Move to the first normal slot so normal rent behaviour applies
Expand Down Expand Up @@ -6241,7 +6277,7 @@ fn test_program_id_as_payer() {

#[test]
fn test_ref_account_key_after_program_id() {
let (genesis_config, mint_keypair) = create_genesis_config(500);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(500);
let (bank, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);

let from_pubkey = solana_sdk::pubkey::new_rand();
Expand Down Expand Up @@ -7145,7 +7181,7 @@ fn test_bank_load_program() {

#[test]
fn test_bpf_loader_upgradeable_deploy_with_max_len() {
let (genesis_config, mint_keypair) = create_genesis_config(1_000_000_000);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(1_000_000_000);
let mut bank = Bank::new_for_tests(&genesis_config);
bank.feature_set = Arc::new(FeatureSet::all_enabled());
let (bank, bank_forks) = bank.wrap_with_bank_forks_for_tests();
Expand Down Expand Up @@ -10190,7 +10226,7 @@ fn test_calculate_fee_secp256k1() {

#[test]
fn test_an_empty_instruction_without_program() {
let (genesis_config, mint_keypair) = create_genesis_config(1);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(1);
let destination = solana_sdk::pubkey::new_rand();
let mut ix = system_instruction::transfer(&mint_keypair.pubkey(), &destination, 0);
ix.program_id = native_loader::id(); // Empty executable account chain
Expand Down Expand Up @@ -13004,7 +13040,7 @@ fn test_store_vote_accounts_partitioned_empty() {

#[test]
fn test_system_instruction_allocate() {
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.0));
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(1.0));
let bank = Bank::new_with_bank_forks_for_tests(&genesis_config).0;
let bank_client = BankClient::new_shared(bank);
let data_len = 2;
Expand Down Expand Up @@ -13062,7 +13098,7 @@ where
let len2 = 456;

// create initial bank and fund the alice account
let (genesis_config, mint_keypair) = create_genesis_config(mint_lamports);
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee_no_rent(mint_lamports);
let (bank, bank_forks) = Bank::new_with_bank_forks_for_tests(&genesis_config);
let bank_client = BankClient::new_shared(bank.clone());
bank_client
Expand Down Expand Up @@ -13131,7 +13167,7 @@ fn test_create_zero_lamport_without_clean() {

#[test]
fn test_system_instruction_assign_with_seed() {
let (genesis_config, mint_keypair) = create_genesis_config(sol_to_lamports(1.0));
let (genesis_config, mint_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(1.0));
let bank = Bank::new_with_bank_forks_for_tests(&genesis_config).0;
let bank_client = BankClient::new_shared(bank);

Expand Down Expand Up @@ -13166,7 +13202,7 @@ fn test_system_instruction_assign_with_seed() {

#[test]
fn test_system_instruction_unsigned_transaction() {
let (genesis_config, alice_keypair) = create_genesis_config(sol_to_lamports(1.0));
let (genesis_config, alice_keypair) = create_genesis_config_no_tx_fee(sol_to_lamports(1.0));
let alice_pubkey = alice_keypair.pubkey();
let mallory_keypair = Keypair::new();
let mallory_pubkey = mallory_keypair.pubkey();
Expand Down

0 comments on commit a5c470d

Please sign in to comment.