diff --git a/cost-model/src/cost_model.rs b/cost-model/src/cost_model.rs index 752d0f78f..28a1a01e7 100644 --- a/cost-model/src/cost_model.rs +++ b/cost-model/src/cost_model.rs @@ -11,8 +11,8 @@ use { solana_program_runtime::{ compute_budget::DEFAULT_HEAP_COST, compute_budget_processor::{ - process_compute_budget_instructions, ComputeBudgetLimits, - DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, MAX_COMPUTE_UNIT_LIMIT, + process_compute_budget_instructions, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT, + MAX_COMPUTE_UNIT_LIMIT, }, }, solana_sdk::{ @@ -53,24 +53,6 @@ impl CostModel { } } - // Calculate cost of loaded accounts size in the same way heap cost is charged at - // rate of 8cu per 32K. Citing `program_runtime\src\compute_budget.rs`: "(cost of - // heap is about) 0.5us per 32k at 15 units/us rounded up" - // - // Before feature `support_set_loaded_accounts_data_size_limit_ix` is enabled, or - // if user doesn't use compute budget ix `set_loaded_accounts_data_size_limit_ix` - // to set limit, `compute_budget.loaded_accounts_data_size_limit` is set to default - // limit of 64MB; which will convert to (64M/32K)*8CU = 16_000 CUs - // - pub fn calculate_loaded_accounts_data_size_cost( - compute_budget_limits: &ComputeBudgetLimits, - ) -> u64 { - FeeStructure::calculate_memory_usage_cost( - usize::try_from(compute_budget_limits.loaded_accounts_bytes).unwrap(), - DEFAULT_HEAP_COST, - ) - } - fn get_signature_cost(transaction: &SanitizedTransaction) -> u64 { transaction.signatures().len() as u64 * SIGNATURE_COST } @@ -150,8 +132,10 @@ impl CostModel { if feature_set .is_active(&include_loaded_accounts_data_size_in_fee_calculation::id()) { - loaded_accounts_data_size_cost = - Self::calculate_loaded_accounts_data_size_cost(&compute_budget_limits); + loaded_accounts_data_size_cost = FeeStructure::calculate_memory_usage_cost( + usize::try_from(compute_budget_limits.loaded_accounts_bytes).unwrap(), + DEFAULT_HEAP_COST, + ) } } Err(_) => { @@ -626,42 +610,6 @@ mod tests { ); } - #[allow(clippy::field_reassign_with_default)] - #[test] - fn test_calculate_loaded_accounts_data_size_cost() { - let mut compute_budget_limits = ComputeBudgetLimits::default(); - - // accounts data size are priced in block of 32K, ... - - // ... requesting less than 32K should still be charged as one block - compute_budget_limits.loaded_accounts_bytes = 31 * 1024; - assert_eq!( - DEFAULT_HEAP_COST, - CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget_limits) - ); - - // ... requesting exact 32K should be charged as one block - compute_budget_limits.loaded_accounts_bytes = 32 * 1024; - assert_eq!( - DEFAULT_HEAP_COST, - CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget_limits) - ); - - // ... requesting slightly above 32K should be charged as 2 block - compute_budget_limits.loaded_accounts_bytes = 33 * 1024; - assert_eq!( - DEFAULT_HEAP_COST * 2, - CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget_limits) - ); - - // ... requesting exact 64K should be charged as 2 block - compute_budget_limits.loaded_accounts_bytes = 64 * 1024; - assert_eq!( - DEFAULT_HEAP_COST * 2, - CostModel::calculate_loaded_accounts_data_size_cost(&compute_budget_limits) - ); - } - #[test] fn test_transaction_cost_with_mix_instruction_without_compute_budget() { let (mint_keypair, start_hash) = test_setup(); diff --git a/sdk/src/fee.rs b/sdk/src/fee.rs index b9fb7329c..0a883c531 100644 --- a/sdk/src/fee.rs +++ b/sdk/src/fee.rs @@ -149,3 +149,40 @@ impl ::solana_frozen_abi::abi_example::AbiExample for FeeStructure { FeeStructure::default() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_calculate_memory_usage_cost() { + let heap_cost = 99; + const K: usize = 1024; + + // accounts data size are priced in block of 32K, ... + + // ... requesting less than 32K should still be charged as one block + assert_eq!( + heap_cost, + FeeStructure::calculate_memory_usage_cost(31 * K, heap_cost) + ); + + // ... requesting exact 32K should be charged as one block + assert_eq!( + heap_cost, + FeeStructure::calculate_memory_usage_cost(32 * K, heap_cost) + ); + + // ... requesting slightly above 32K should be charged as 2 block + assert_eq!( + heap_cost * 2, + FeeStructure::calculate_memory_usage_cost(33 * K, heap_cost) + ); + + // ... requesting exact 64K should be charged as 2 block + assert_eq!( + heap_cost * 2, + FeeStructure::calculate_memory_usage_cost(64 * K, heap_cost) + ); + } +}