Skip to content

Commit

Permalink
update tests for added feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-stones committed Jun 28, 2024
1 parent a5c9c97 commit 7ba56f4
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 53 deletions.
50 changes: 35 additions & 15 deletions compute-budget/src/compute_budget_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ mod tests {
},
};

impl Default for ComputeBudgetLimits {
fn default() -> Self {
ComputeBudgetLimits {
updated_heap_bytes: u32::try_from(MIN_HEAP_FRAME_BYTES).unwrap(),
compute_unit_limit: MAX_COMPUTE_UNIT_LIMIT,
compute_unit_price: 0,
loaded_accounts_bytes: DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES,
}
}
}

macro_rules! test {
( $instructions: expr, $expected_result: expr, $use_default_loaded_accounts_data_szie: expr) => {
let payer_keypair = Keypair::new();
Expand Down Expand Up @@ -468,9 +479,10 @@ mod tests {
for use_default_loaded_accounts_data_size in [true, false] {
let expected_result = Ok(ComputeBudgetLimits {
compute_unit_limit: DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT,
loaded_accounts_bytes: Self::get_default_loaded_accounts_data_size_bytes(
use_default_loaded_accounts_data_size,
),
loaded_accounts_bytes:
ComputeBudgetLimits::get_default_loaded_accounts_data_size_bytes(
use_default_loaded_accounts_data_size,
),
..ComputeBudgetLimits::default()
});

Expand Down Expand Up @@ -515,18 +527,26 @@ mod tests {
Hash::default(),
));

let result =
process_compute_budget_instructions(transaction.message().program_instructions_iter());
for use_default_loaded_accounts_data_size in [true, false] {
let result = process_compute_budget_instructions(
transaction.message().program_instructions_iter(),
use_default_loaded_accounts_data_size,
);

// assert process_instructions will be successful with default,
// and the default compute_unit_limit is 2 times default: one for bpf ix, one for
// builtin ix.
assert_eq!(
result,
Ok(ComputeBudgetLimits {
compute_unit_limit: 2 * DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT,
..ComputeBudgetLimits::default()
})
);
// assert process_instructions will be successful with default,
// and the default compute_unit_limit is 2 times default: one for bpf ix, one for
// builtin ix.
assert_eq!(
result,
Ok(ComputeBudgetLimits {
compute_unit_limit: 2 * DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT,
loaded_accounts_bytes:
ComputeBudgetLimits::get_default_loaded_accounts_data_size_bytes(
use_default_loaded_accounts_data_size
),
..ComputeBudgetLimits::default()
})
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ mod tests {
let transaction = Transaction::new_unsigned(Message::new(
&[
ComputeBudgetInstruction::set_compute_unit_price(priority),
ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(64 * 1024 * 1024),
system_instruction::transfer(&from_account, write_to_account, 2),
],
Some(&from_account),
Expand Down
4 changes: 2 additions & 2 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ impl CostModel {
mod tests {
use {
super::*,
solana_compute_budget::compute_budget_processor::ComputeBudgetLimits,
solana_sdk::{
compute_budget::{self, ComputeBudgetInstruction},
fee::ACCOUNT_DATA_COST_PAGE_SIZE,
Expand Down Expand Up @@ -617,8 +618,7 @@ mod tests {
.unwrap();
const DEFAULT_PAGE_COST: u64 = 8;
let expected_loaded_accounts_data_size_cost =
solana_program_runtime::compute_budget_processor::DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES
as u64
ComputeBudgetLimits::get_default_loaded_accounts_data_size_bytes(true) as u64
/ ACCOUNT_DATA_COST_PAGE_SIZE
* DEFAULT_PAGE_COST;

Expand Down
4 changes: 2 additions & 2 deletions cost-model/src/transaction_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ mod tests {
use {
super::*,
crate::cost_model::CostModel,
solana_compute_budget::compute_budget_processor::ComputeBudgetLimits,
solana_sdk::{
feature_set::FeatureSet,
fee::ACCOUNT_DATA_COST_PAGE_SIZE,
Expand Down Expand Up @@ -252,8 +253,7 @@ mod tests {
// expected non-vote tx cost would include default loaded accounts size cost additionally
const DEFAULT_PAGE_COST: u64 = 8;
let expected_loaded_accounts_data_size_cost =
solana_program_runtime::compute_budget_processor::DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES
as u64
ComputeBudgetLimits::get_default_loaded_accounts_data_size_bytes(true) as u64
/ ACCOUNT_DATA_COST_PAGE_SIZE
* DEFAULT_PAGE_COST;
let min_none_vote_cost = SIMPLE_VOTE_USAGE_COST + expected_loaded_accounts_data_size_cost;
Expand Down
6 changes: 5 additions & 1 deletion programs/bpf-loader-tests/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use {
account::AccountSharedData,
account_utils::StateMut,
bpf_loader_upgradeable::{id, UpgradeableLoaderState},
compute_budget::ComputeBudgetInstruction,
instruction::{Instruction, InstructionError},
pubkey::Pubkey,
signature::{Keypair, Signer},
Expand Down Expand Up @@ -35,7 +36,10 @@ pub async fn assert_ix_error(
}

let transaction = Transaction::new_signed_with_payer(
&[ix],
&[
ix,
ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(64 * 1024 * 1024),
],
Some(&fee_payer.pubkey()),
&signers,
recent_blockhash,
Expand Down
23 changes: 16 additions & 7 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use {
},
solana_compute_budget::{
compute_budget::ComputeBudget,
compute_budget_processor::process_compute_budget_instructions,
compute_budget_processor::{process_compute_budget_instructions, ComputeBudgetLimits},
},
solana_ledger::token_balances::collect_token_balances,
solana_program_runtime::{invoke_context::mock_process_instruction, timings::ExecuteTimings},
Expand Down Expand Up @@ -3848,6 +3848,9 @@ fn test_program_fees() {
let fee_structure =
FeeStructure::new(0.000005, 0.0, vec![(200, 0.0000005), (1400000, 0.000005)]);
bank.set_fee_structure(&fee_structure);
let use_default_loaded_accounts_data_size = bank
.feature_set
.is_active(&feature_set::default_loaded_accounts_data_size_limit::id());
let (bank, bank_forks) = bank.wrap_with_bank_forks_for_tests();
let mut bank_client = BankClient::new_shared(bank);
let authority_keypair = Keypair::new();
Expand All @@ -3874,9 +3877,12 @@ fn test_program_fees() {
let expected_normal_fee = fee_structure.calculate_fee(
&sanitized_message,
congestion_multiplier,
&process_compute_budget_instructions(sanitized_message.program_instructions_iter())
.unwrap_or_default()
.into(),
&process_compute_budget_instructions(
sanitized_message.program_instructions_iter(),
use_default_loaded_accounts_data_size,
)
.unwrap_or_else(|_| ComputeBudgetLimits::new_with(use_default_loaded_accounts_data_size))
.into(),
false,
true,
);
Expand All @@ -3902,9 +3908,12 @@ fn test_program_fees() {
let expected_prioritized_fee = fee_structure.calculate_fee(
&sanitized_message,
congestion_multiplier,
&process_compute_budget_instructions(sanitized_message.program_instructions_iter())
.unwrap_or_default()
.into(),
&process_compute_budget_instructions(
sanitized_message.program_instructions_iter(),
use_default_loaded_accounts_data_size,
)
.unwrap_or_else(|_| ComputeBudgetLimits::new_with(use_default_loaded_accounts_data_size))
.into(),
false,
true,
);
Expand Down
16 changes: 12 additions & 4 deletions runtime-transaction/src/runtime_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ mod tests {
},
solana_sdk::{
compute_budget::ComputeBudgetInstruction,
feature_set::FeatureSet,
instruction::Instruction,
message::Message,
reserved_account_keys::ReservedAccountKeys,
Expand Down Expand Up @@ -221,10 +222,15 @@ mod tests {
svt: SanitizedVersionedTransaction,
is_simple_vote: Option<bool>,
) -> bool {
RuntimeTransaction::<SanitizedVersionedMessage>::try_from(svt, None, is_simple_vote)
.unwrap()
.meta
.is_simple_vote_tx
RuntimeTransaction::<SanitizedVersionedMessage>::try_from(
svt,
None,
is_simple_vote,
&FeatureSet::default(),
)
.unwrap()
.meta
.is_simple_vote_tx
}

assert!(!get_is_simple_vote(
Expand Down Expand Up @@ -257,6 +263,7 @@ mod tests {
non_vote_sanitized_versioned_transaction(),
Some(hash),
None,
&FeatureSet::default(),
)
.unwrap();

Expand Down Expand Up @@ -291,6 +298,7 @@ mod tests {
.to_sanitized_versioned_transaction(),
Some(hash),
None,
&FeatureSet::default(),
)
.unwrap();

Expand Down
1 change: 0 additions & 1 deletion runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ use {
solana_measure::{measure, measure::Measure, measure_us},
solana_perf::perf_libs,
solana_program_runtime::{
compute_budget_processor::{process_compute_budget_instructions, ComputeBudgetLimits},
invoke_context::BuiltinFunctionWithContext,
loaded_programs::ProgramCacheEntry,
timings::{ExecuteTimingType, ExecuteTimings},
Expand Down
17 changes: 13 additions & 4 deletions runtime/src/bank/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10061,9 +10061,15 @@ fn calculate_test_fee(
lamports_per_signature: u64,
fee_structure: &FeeStructure,
) -> u64 {
let budget_limits = process_compute_budget_instructions(message.program_instructions_iter())
.unwrap_or_default()
.into();
// feature gate `default_loaded_accounts_data_size_limit` status does not impact transaction
// fee. Safe to set it as activated for tests relate to fee
let use_default_loaded_accounts_data_size = true;
let budget_limits = process_compute_budget_instructions(
message.program_instructions_iter(),
use_default_loaded_accounts_data_size,
)
.unwrap_or_else(|_| ComputeBudgetLimits::new_with(use_default_loaded_accounts_data_size))
.into();

fee_structure.calculate_fee(message, lamports_per_signature, &budget_limits, false, true)
}
Expand Down Expand Up @@ -11138,7 +11144,10 @@ fn create_mock_realloc_tx(
account_metas,
);
Transaction::new_signed_with_payer(
&[instruction],
&[
instruction,
ComputeBudgetInstruction::set_loaded_accounts_data_size_limit(64 * 1024 * 1024),
],
Some(&payer.pubkey()),
&[payer],
recent_blockhash,
Expand Down
51 changes: 34 additions & 17 deletions svm/src/account_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,10 +904,11 @@ mod tests {

#[test]
fn test_get_requested_loaded_accounts_data_size_limit() {
// an prrivate helper function
// an private helper function
fn test(
instructions: &[solana_sdk::instruction::Instruction],
expected_result: &Result<Option<NonZeroUsize>>,
use_default_loaded_accounts_data_size: bool,
) {
let payer_keypair = Keypair::new();
let tx = SanitizedTransaction::from_transaction_for_tests(Transaction::new(
Expand All @@ -917,7 +918,10 @@ mod tests {
));
assert_eq!(
*expected_result,
get_requested_loaded_accounts_data_size_limit(tx.message())
get_requested_loaded_accounts_data_size_limit(
tx.message(),
use_default_loaded_accounts_data_size
)
);
}

Expand All @@ -937,24 +941,37 @@ mod tests {
solana_sdk::instruction::Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
];

let result_default_limit = Ok(Some(
NonZeroUsize::new(
usize::try_from(compute_budget_processor::DEFAULT_LOADED_ACCOUNTS_DATA_SIZE_BYTES)
.unwrap(),
)
.unwrap(),
));
let result_requested_limit: Result<Option<NonZeroUsize>> =
Ok(Some(NonZeroUsize::new(99).unwrap()));
let result_invalid_limit = Err(TransactionError::InvalidLoadedAccountsDataSizeLimit);

// the results should be:
// if tx doesn't set limit, then default limit (64MiB)
// if tx doesn't set limit, then default limit
// if tx sets limit, then requested limit
// if tx sets limit to zero, then TransactionError::InvalidLoadedAccountsDataSizeLimit
test(tx_not_set_limit, &result_default_limit);
test(tx_set_limit_99, &result_requested_limit);
test(tx_set_limit_0, &result_invalid_limit);
for use_default_loaded_accounts_data_size in [true, false] {
test(
tx_not_set_limit,
&Ok(Some(
NonZeroUsize::new(
usize::try_from(
ComputeBudgetLimits::get_default_loaded_accounts_data_size_bytes(
use_default_loaded_accounts_data_size,
),
)
.unwrap(),
)
.unwrap(),
)),
use_default_loaded_accounts_data_size,
);
test(
tx_set_limit_99,
&Ok(Some(NonZeroUsize::new(99).unwrap())),
use_default_loaded_accounts_data_size,
);
test(
tx_set_limit_0,
&Err(TransactionError::InvalidLoadedAccountsDataSizeLimit),
use_default_loaded_accounts_data_size,
);
}
}

struct ValidateFeePayerTestParameter {
Expand Down

0 comments on commit 7ba56f4

Please sign in to comment.