Skip to main content

solana_program_runtime/
execution_budget.rs

1use {
2    solana_fee_structure::FeeDetails, solana_program_entrypoint::HEAP_LENGTH,
3    solana_transaction_context::MAX_INSTRUCTION_TRACE_LENGTH, std::num::NonZeroU32,
4};
5
6/// Max instruction stack depth. This is the maximum nesting of instructions that can happen during
7/// a transaction.
8pub const MAX_INSTRUCTION_STACK_DEPTH: usize = 5;
9/// Max instruction stack depth with SIMD-0268 enabled. Allows 8 nested CPIs.
10pub const MAX_INSTRUCTION_STACK_DEPTH_SIMD_0268: usize = 9;
11
12fn get_max_instruction_stack_depth(simd_0268_active: bool) -> usize {
13    if simd_0268_active {
14        MAX_INSTRUCTION_STACK_DEPTH_SIMD_0268
15    } else {
16        MAX_INSTRUCTION_STACK_DEPTH
17    }
18}
19
20//Default CPI invocation cost
21pub const DEFAULT_INVOCATION_COST: u64 = 946;
22
23/// Max call depth. This is the maximum nesting of SBF to SBF call that can happen within a program.
24pub const MAX_CALL_DEPTH: usize = 64;
25
26pub const MAX_COMPUTE_UNIT_LIMIT: u32 = 1_400_000;
27
28/// Roughly 0.5us/page, where page is 32K; given roughly 15CU/us, the
29/// default heap page cost = 0.5 * 15 ~= 8CU/page
30pub const DEFAULT_HEAP_COST: u64 = 8;
31pub const DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT: u32 = 200_000;
32// SIMD-170 defines max CUs to be allocated for any builtin program instructions, that
33// have not been migrated to sBPF programs.
34pub const MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT: u32 = 3_000;
35pub const MAX_HEAP_FRAME_BYTES: u32 = 256 * 1024;
36pub const MIN_HEAP_FRAME_BYTES: u32 = HEAP_LENGTH as u32;
37
38/// The total accounts data a transaction can load is limited to 64MiB to not break
39/// anyone in Mainnet-beta today. It can be set by set_loaded_accounts_data_size_limit instruction
40pub const MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES: NonZeroU32 =
41    NonZeroU32::new(64 * 1024 * 1024).unwrap();
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub struct SVMTransactionExecutionBudget {
45    /// Number of compute units that a transaction or individual instruction is
46    /// allowed to consume. Compute units are consumed by program execution,
47    /// resources they use, etc...
48    pub compute_unit_limit: u64,
49    /// Maximum program instruction invocation stack depth. Invocation stack
50    /// depth starts at 1 for transaction instructions and the stack depth is
51    /// incremented each time a program invokes an instruction and decremented
52    /// when a program returns.
53    pub max_instruction_stack_depth: usize,
54    /// Maximum cross-program invocation and instructions per transaction
55    pub max_instruction_trace_length: usize,
56    /// Maximum number of slices hashed per syscall
57    pub sha256_max_slices: u64,
58    /// Maximum SBF to BPF call depth
59    pub max_call_depth: usize,
60    /// Size of a stack frame in bytes, must match the size specified in the LLVM SBF backend
61    pub stack_frame_size: usize,
62    /// program heap region size, default: solana_program_entrypoint::HEAP_LENGTH
63    pub heap_size: u32,
64}
65
66#[cfg(feature = "dev-context-only-utils")]
67impl Default for SVMTransactionExecutionBudget {
68    fn default() -> Self {
69        Self::new_with_defaults(/* simd_0268_active */ false)
70    }
71}
72
73impl SVMTransactionExecutionBudget {
74    pub fn new_with_defaults(simd_0268_active: bool) -> Self {
75        SVMTransactionExecutionBudget {
76            compute_unit_limit: u64::from(MAX_COMPUTE_UNIT_LIMIT),
77            max_instruction_stack_depth: get_max_instruction_stack_depth(simd_0268_active),
78            max_instruction_trace_length: MAX_INSTRUCTION_TRACE_LENGTH,
79            sha256_max_slices: 20_000,
80            max_call_depth: MAX_CALL_DEPTH,
81            stack_frame_size: solana_sbpf::vm::get_stack_frame_size(),
82            heap_size: u32::try_from(solana_program_entrypoint::HEAP_LENGTH).unwrap(),
83        }
84    }
85}
86
87#[derive(Clone, Copy, Debug, PartialEq, Eq)]
88pub struct SVMTransactionExecutionCost {
89    /// Number of compute units consumed by a log_u64 call
90    pub log_64_units: u64,
91    /// Number of compute units consumed by a create_program_address call
92    pub create_program_address_units: u64,
93    /// Number of compute units consumed by an invoke call (not including the cost incurred by
94    /// the called program)
95    pub invoke_units: u64,
96    /// Base number of compute units consumed to call SHA256
97    pub sha256_base_cost: u64,
98    /// Incremental number of units consumed by SHA256 (based on bytes)
99    pub sha256_byte_cost: u64,
100    /// Number of compute units consumed by logging a `Pubkey`
101    pub log_pubkey_units: u64,
102    /// Number of account data bytes per compute unit charged during a cross-program invocation
103    pub cpi_bytes_per_unit: u64,
104    /// Base number of compute units consumed to get a sysvar
105    pub sysvar_base_cost: u64,
106    /// Number of compute units consumed to call secp256k1_recover
107    pub secp256k1_recover_cost: u64,
108    /// Number of compute units consumed to do a syscall without any work
109    pub syscall_base_cost: u64,
110    /// Number of compute units consumed to validate a curve25519 edwards point
111    pub curve25519_edwards_validate_point_cost: u64,
112    /// Number of compute units consumed to add two curve25519 edwards points
113    pub curve25519_edwards_add_cost: u64,
114    /// Number of compute units consumed to subtract two curve25519 edwards points
115    pub curve25519_edwards_subtract_cost: u64,
116    /// Number of compute units consumed to multiply a curve25519 edwards point
117    pub curve25519_edwards_multiply_cost: u64,
118    /// Number of compute units consumed for a multiscalar multiplication (msm) of edwards points.
119    /// The total cost is calculated as `msm_base_cost + (length - 1) * msm_incremental_cost`.
120    pub curve25519_edwards_msm_base_cost: u64,
121    /// Number of compute units consumed for a multiscalar multiplication (msm) of edwards points.
122    /// The total cost is calculated as `msm_base_cost + (length - 1) * msm_incremental_cost`.
123    pub curve25519_edwards_msm_incremental_cost: u64,
124    /// Number of compute units consumed to validate a curve25519 ristretto point
125    pub curve25519_ristretto_validate_point_cost: u64,
126    /// Number of compute units consumed to add two curve25519 ristretto points
127    pub curve25519_ristretto_add_cost: u64,
128    /// Number of compute units consumed to subtract two curve25519 ristretto points
129    pub curve25519_ristretto_subtract_cost: u64,
130    /// Number of compute units consumed to multiply a curve25519 ristretto point
131    pub curve25519_ristretto_multiply_cost: u64,
132    /// Number of compute units consumed for a multiscalar multiplication (msm) of ristretto points.
133    /// The total cost is calculated as `msm_base_cost + (length - 1) * msm_incremental_cost`.
134    pub curve25519_ristretto_msm_base_cost: u64,
135    /// Number of compute units consumed for a multiscalar multiplication (msm) of ristretto points.
136    /// The total cost is calculated as `msm_base_cost + (length - 1) * msm_incremental_cost`.
137    pub curve25519_ristretto_msm_incremental_cost: u64,
138    /// Number of compute units per additional 32k heap above the default (~.5
139    /// us per 32k at 15 units/us rounded up)
140    pub heap_cost: u64,
141    /// Memory operation syscall base cost
142    pub mem_op_base_cost: u64,
143    /// Number of compute units consumed to call alt_bn128_g1_addition
144    pub alt_bn128_g1_addition_cost: u64,
145    /// Number of compute units consumed to call alt_bn128_g2_addition
146    pub alt_bn128_g2_addition_cost: u64,
147    /// Number of compute units consumed to call alt_bn128_g1_multiplication.
148    pub alt_bn128_g1_multiplication_cost: u64,
149    /// Number of compute units consumed to call alt_bn128_g2_multiplication.
150    pub alt_bn128_g2_multiplication_cost: u64,
151    /// Total cost will be alt_bn128_pairing_one_pair_cost_first
152    /// + alt_bn128_pairing_one_pair_cost_other * (num_elems - 1)
153    pub alt_bn128_pairing_one_pair_cost_first: u64,
154    pub alt_bn128_pairing_one_pair_cost_other: u64,
155    /// Coefficient `a` of the quadratic function which determines the number
156    /// of compute units consumed to call poseidon syscall for a given number
157    /// of inputs.
158    pub poseidon_cost_coefficient_a: u64,
159    /// Coefficient `c` of the quadratic function which determines the number
160    /// of compute units consumed to call poseidon syscall for a given number
161    /// of inputs.
162    pub poseidon_cost_coefficient_c: u64,
163    /// Number of compute units consumed for accessing the remaining compute units.
164    pub get_remaining_compute_units_cost: u64,
165    /// Number of compute units consumed to call alt_bn128_g1_compress.
166    pub alt_bn128_g1_compress: u64,
167    /// Number of compute units consumed to call alt_bn128_g1_decompress.
168    pub alt_bn128_g1_decompress: u64,
169    /// Number of compute units consumed to call alt_bn128_g2_compress.
170    pub alt_bn128_g2_compress: u64,
171    /// Number of compute units consumed to call alt_bn128_g2_decompress.
172    pub alt_bn128_g2_decompress: u64,
173    /// Number of compute units consumed to add two bls12_381 g1 points.
174    pub bls12_381_g1_add_cost: u64,
175    /// Number of compute units consumed to add two bls12_381 g2 points.
176    pub bls12_381_g2_add_cost: u64,
177    /// Number of compute units consumed to subtract two bls12_381 g1 points.
178    pub bls12_381_g1_subtract_cost: u64,
179    /// Number of compute units consumed to subtract two bls12_381 g2 points.
180    pub bls12_381_g2_subtract_cost: u64,
181    /// Number of compute units consumed to multiply a bls12_381 g1 point.
182    pub bls12_381_g1_multiply_cost: u64,
183    /// Number of compute units consumed to multiply a bls12_381 g2 point.
184    pub bls12_381_g2_multiply_cost: u64,
185    /// Number of compute units consumed to decompress a bls12_381 g1 point.
186    pub bls12_381_g1_decompress_cost: u64,
187    /// Number of compute units consumed to decompress a bls12_381 g2 point.
188    pub bls12_381_g2_decompress_cost: u64,
189    /// Number of compute units consumed to validate a bls12_381 g1 point.
190    pub bls12_381_g1_validate_cost: u64,
191    /// Number of compute units consumed to validate a bls12_381 g2 point.
192    pub bls12_381_g2_validate_cost: u64,
193    /// Base number of compute units consumed to perform a bls12_381 pairing.
194    pub bls12_381_one_pair_cost: u64,
195    /// Incremental number of compute units consumed per pair in a bls12_381 pairing.
196    pub bls12_381_additional_pair_cost: u64,
197}
198
199impl Default for SVMTransactionExecutionCost {
200    fn default() -> Self {
201        SVMTransactionExecutionCost {
202            log_64_units: 100,
203            create_program_address_units: 1500,
204            invoke_units: DEFAULT_INVOCATION_COST,
205            sha256_base_cost: 85,
206            sha256_byte_cost: 1,
207            log_pubkey_units: 100,
208            cpi_bytes_per_unit: 250, // ~50MB at 200,000 units
209            sysvar_base_cost: 100,
210            secp256k1_recover_cost: 25_000,
211            syscall_base_cost: 100,
212            curve25519_edwards_validate_point_cost: 159,
213            curve25519_edwards_add_cost: 473,
214            curve25519_edwards_subtract_cost: 475,
215            curve25519_edwards_multiply_cost: 2_177,
216            curve25519_edwards_msm_base_cost: 2_273,
217            curve25519_edwards_msm_incremental_cost: 758,
218            curve25519_ristretto_validate_point_cost: 169,
219            curve25519_ristretto_add_cost: 521,
220            curve25519_ristretto_subtract_cost: 519,
221            curve25519_ristretto_multiply_cost: 2_208,
222            curve25519_ristretto_msm_base_cost: 2303,
223            curve25519_ristretto_msm_incremental_cost: 788,
224            heap_cost: DEFAULT_HEAP_COST,
225            mem_op_base_cost: 10,
226            alt_bn128_g1_addition_cost: 334,
227            alt_bn128_g2_addition_cost: 535,
228            alt_bn128_g1_multiplication_cost: 3_840,
229            alt_bn128_g2_multiplication_cost: 15_670,
230            alt_bn128_pairing_one_pair_cost_first: 36_364,
231            alt_bn128_pairing_one_pair_cost_other: 12_121,
232            poseidon_cost_coefficient_a: 61,
233            poseidon_cost_coefficient_c: 542,
234            get_remaining_compute_units_cost: 100,
235            alt_bn128_g1_compress: 30,
236            alt_bn128_g1_decompress: 398,
237            alt_bn128_g2_compress: 86,
238            alt_bn128_g2_decompress: 13610,
239            bls12_381_g1_add_cost: 128,
240            bls12_381_g2_add_cost: 203,
241            bls12_381_g1_subtract_cost: 129,
242            bls12_381_g2_subtract_cost: 204,
243            bls12_381_g1_multiply_cost: 4_627,
244            bls12_381_g2_multiply_cost: 8_255,
245            bls12_381_g1_decompress_cost: 2_100,
246            bls12_381_g2_decompress_cost: 3_050,
247            bls12_381_g1_validate_cost: 1_565,
248            bls12_381_g2_validate_cost: 1_968,
249            bls12_381_one_pair_cost: 25_445,
250            bls12_381_additional_pair_cost: 13_023,
251        }
252    }
253}
254
255impl SVMTransactionExecutionCost {
256    /// Returns cost of the Poseidon hash function for the given number of
257    /// inputs is determined by the following quadratic function:
258    ///
259    /// 61*n^2 + 542
260    ///
261    /// Which approximates the results of benchmarks of light-posiedon
262    /// library[0]. These results assume 1 CU per 33 ns. Examples:
263    ///
264    /// * 1 input
265    ///   * light-poseidon benchmark: `18,303 / 33 ≈ 555`
266    ///   * function: `61*1^2 + 542 = 603`
267    /// * 2 inputs
268    ///   * light-poseidon benchmark: `25,866 / 33 ≈ 784`
269    ///   * function: `61*2^2 + 542 = 786`
270    /// * 3 inputs
271    ///   * light-poseidon benchmark: `37,549 / 33 ≈ 1,138`
272    ///   * function; `61*3^2 + 542 = 1091`
273    ///
274    /// [0] https://github.com/Lightprotocol/light-poseidon#performance
275    pub fn poseidon_cost(&self, nr_inputs: u64) -> Option<u64> {
276        let squared_inputs = nr_inputs.checked_pow(2)?;
277        let mul_result = self
278            .poseidon_cost_coefficient_a
279            .checked_mul(squared_inputs)?;
280        let final_result = mul_result.checked_add(self.poseidon_cost_coefficient_c)?;
281
282        Some(final_result)
283    }
284}
285
286#[derive(Clone, Copy, Debug, PartialEq, Eq)]
287pub struct SVMTransactionExecutionAndFeeBudgetLimits {
288    pub budget: SVMTransactionExecutionBudget,
289    pub loaded_accounts_data_size_limit: u32,
290    pub fee_details: FeeDetails,
291}
292
293#[cfg(feature = "dev-context-only-utils")]
294impl Default for SVMTransactionExecutionAndFeeBudgetLimits {
295    fn default() -> Self {
296        Self {
297            budget: SVMTransactionExecutionBudget::default(),
298            loaded_accounts_data_size_limit: MAX_LOADED_ACCOUNTS_DATA_SIZE_BYTES.get(),
299            fee_details: FeeDetails::default(),
300        }
301    }
302}
303
304#[cfg(feature = "dev-context-only-utils")]
305impl SVMTransactionExecutionAndFeeBudgetLimits {
306    pub fn with_fee(fee_details: FeeDetails) -> Self {
307        Self {
308            fee_details,
309            ..SVMTransactionExecutionAndFeeBudgetLimits::default()
310        }
311    }
312}