remove unnecessary wrapper function (#34428)

* remove unnecessary wrapper function

* add test to FeeStructure
This commit is contained in:
Tao Zhu 2023-12-15 14:56:19 -06:00 committed by GitHub
parent f214a8220f
commit 7360f48eb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 58 deletions

View File

@ -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();

View File

@ -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)
);
}
}