Skip to content

Commit

Permalink
charging CU for loaded accounts data size
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-stones committed May 14, 2024
1 parent 66f931a commit 413d50b
Showing 1 changed file with 5 additions and 43 deletions.
48 changes: 5 additions & 43 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use {
solana_sdk::{
borsh1::try_from_slice_unchecked,
compute_budget::{self, ComputeBudgetInstruction},
feature_set::{self, include_loaded_accounts_data_size_in_fee_calculation, FeatureSet},
feature_set::{self, FeatureSet},
fee::FeeStructure,
instruction::CompiledInstruction,
program_utils::limited_deserialize,
Expand Down Expand Up @@ -156,14 +156,10 @@ impl CostModel {
programs_execution_costs = u64::from(compute_budget_limits.compute_unit_limit);
}

if feature_set
.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id())
{
loaded_accounts_data_size_cost = FeeStructure::calculate_memory_usage_cost(
usize::try_from(compute_budget_limits.loaded_accounts_bytes).unwrap(),
DEFAULT_HEAP_COST,
)
}
loaded_accounts_data_size_cost = FeeStructure::calculate_memory_usage_cost(
usize::try_from(compute_budget_limits.loaded_accounts_bytes).unwrap(),
DEFAULT_HEAP_COST,
)
}
Err(_) => {
programs_execution_costs = 0;
Expand Down Expand Up @@ -567,8 +563,6 @@ mod tests {
let expected_execution_cost = BUILT_IN_INSTRUCTION_COSTS
.get(&system_program::id())
.unwrap();
// feature `include_loaded_accounts_data_size_in_fee_calculation` enabled, using
// default loaded_accounts_data_size_limit
const DEFAULT_PAGE_COST: u64 = 8;
let expected_loaded_accounts_data_size_cost =
solana_program_runtime::compute_budget_processor::MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES
Expand All @@ -586,35 +580,6 @@ mod tests {
);
}

#[test]
fn test_cost_model_calculate_cost_disabled_feature() {
let (mint_keypair, start_hash) = test_setup();
let tx = SanitizedTransaction::from_transaction_for_tests(system_transaction::transfer(
&mint_keypair,
&Keypair::new().pubkey(),
2,
start_hash,
));

let feature_set = FeatureSet::default();
assert!(!feature_set.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()));
let expected_account_cost = WRITE_LOCK_UNITS * 2;
let expected_execution_cost = BUILT_IN_INSTRUCTION_COSTS
.get(&system_program::id())
.unwrap();
// feature `include_loaded_accounts_data_size_in_fee_calculation` not enabled
let expected_loaded_accounts_data_size_cost = 0;

let tx_cost = CostModel::calculate_cost(&tx, &feature_set);
assert_eq!(expected_account_cost, tx_cost.write_lock_cost());
assert_eq!(*expected_execution_cost, tx_cost.programs_execution_cost());
assert_eq!(2, tx_cost.writable_accounts().len());
assert_eq!(
expected_loaded_accounts_data_size_cost,
tx_cost.loaded_accounts_data_size_cost()
);
}

#[test]
fn test_cost_model_calculate_cost_with_limit() {
let (mint_keypair, start_hash) = test_setup();
Expand All @@ -632,16 +597,13 @@ mod tests {
));

let feature_set = FeatureSet::all_enabled();
assert!(feature_set.is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()));
let expected_account_cost = WRITE_LOCK_UNITS * 2;
let expected_execution_cost = BUILT_IN_INSTRUCTION_COSTS
.get(&system_program::id())
.unwrap()
+ BUILT_IN_INSTRUCTION_COSTS
.get(&compute_budget::id())
.unwrap();
// feature `include_loaded_accounts_data_size_in_fee_calculation` is enabled, accounts data
// size limit is set.
let expected_loaded_accounts_data_size_cost = (data_limit as u64) / (32 * 1024) * 8;

let tx_cost = CostModel::calculate_cost(&tx, &feature_set);
Expand Down

0 comments on commit 413d50b

Please sign in to comment.