solana_compute_budget/
compute_budget_limits.rs

1use {
2    crate::prioritization_fee::{PrioritizationFeeDetails, PrioritizationFeeType},
3    solana_sdk::{entrypoint::HEAP_LENGTH, fee::FeeBudgetLimits},
4    std::num::NonZeroU32,
5};
6
7/// Roughly 0.5us/page, where page is 32K; given roughly 15CU/us, the
8/// default heap page cost = 0.5 * 15 ~= 8CU/page
9pub const DEFAULT_HEAP_COST: u64 = 8;
10pub const DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT: u32 = 200_000;
11// SIMD-170 defines max CUs to be allocated for any builtin program instructions, that
12// have not been migrated to sBPF programs.
13pub const MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT: u32 = 3_000;
14pub const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000;
15pub const MAX_HEAP_FRAME_BYTES: u32 = 256 * 1024;
16pub const MIN_HEAP_FRAME_BYTES: u32 = HEAP_LENGTH as u32;
17
18/// The total accounts data a transaction can load is limited to 64MiB to not break
19/// anyone in Mainnet-beta today. It can be set by set_loaded_accounts_data_size_limit instruction
20pub const MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES: NonZeroU32 =
21    unsafe { NonZeroU32::new_unchecked(64 * 1024 * 1024) };
22
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub struct ComputeBudgetLimits {
25    pub updated_heap_bytes: u32,
26    pub compute_unit_limit: u32,
27    pub compute_unit_price: u64,
28    pub loaded_accounts_bytes: NonZeroU32,
29}
30
31impl Default for ComputeBudgetLimits {
32    fn default() -> Self {
33        ComputeBudgetLimits {
34            updated_heap_bytes: MIN_HEAP_FRAME_BYTES,
35            compute_unit_limit: MAX_COMPUTE_UNIT_LIMIT,
36            compute_unit_price: 0,
37            loaded_accounts_bytes: MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES,
38        }
39    }
40}
41
42impl From<ComputeBudgetLimits> for FeeBudgetLimits {
43    fn from(val: ComputeBudgetLimits) -> Self {
44        let prioritization_fee_details = PrioritizationFeeDetails::new(
45            PrioritizationFeeType::ComputeUnitPrice(val.compute_unit_price),
46            u64::from(val.compute_unit_limit),
47        );
48        let prioritization_fee = prioritization_fee_details.get_fee();
49
50        FeeBudgetLimits {
51            loaded_accounts_data_size_limit: val.loaded_accounts_bytes,
52            heap_cost: DEFAULT_HEAP_COST,
53            compute_unit_limit: u64::from(val.compute_unit_limit),
54            prioritization_fee,
55        }
56    }
57}