Skip to content

Commit

Permalink
remove unnecessary wrapper function (solana-labs#34428)
Browse files Browse the repository at this point in the history
* remove unnecessary wrapper function

* add test to FeeStructure
  • Loading branch information
tao-stones authored Dec 15, 2023
1 parent f214a82 commit 7360f48
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 58 deletions.
64 changes: 6 additions & 58 deletions cost-model/src/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(_) => {
Expand Down Expand Up @@ -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();
Expand Down
37 changes: 37 additions & 0 deletions sdk/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

0 comments on commit 7360f48

Please sign in to comment.