solana_sdk/
compute_budget.rs

1//! The compute budget native program.
2
3#![cfg(feature = "full")]
4
5#[cfg(feature = "borsh")]
6use {
7    crate::instruction::Instruction,
8    borsh::{BorshDeserialize, BorshSerialize},
9};
10
11crate::declare_id!("ComputeBudget111111111111111111111111111111");
12
13/// Compute Budget Instructions
14#[cfg_attr(feature = "frozen-abi", derive(AbiExample, AbiEnumVisitor))]
15#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))]
16#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
17pub enum ComputeBudgetInstruction {
18    Unused, // deprecated variant, reserved value.
19    /// Request a specific transaction-wide program heap region size in bytes.
20    /// The value requested must be a multiple of 1024. This new heap region
21    /// size applies to each program executed in the transaction, including all
22    /// calls to CPIs.
23    RequestHeapFrame(u32),
24    /// Set a specific compute unit limit that the transaction is allowed to consume.
25    SetComputeUnitLimit(u32),
26    /// Set a compute unit price in "micro-lamports" to pay a higher transaction
27    /// fee for higher transaction prioritization.
28    SetComputeUnitPrice(u64),
29    /// Set a specific transaction-wide account data size limit, in bytes, is allowed to load.
30    SetLoadedAccountsDataSizeLimit(u32),
31}
32
33impl ComputeBudgetInstruction {
34    #[cfg(feature = "borsh")]
35    /// Create a `ComputeBudgetInstruction::RequestHeapFrame` `Instruction`
36    pub fn request_heap_frame(bytes: u32) -> Instruction {
37        Instruction::new_with_borsh(id(), &Self::RequestHeapFrame(bytes), vec![])
38    }
39
40    #[cfg(feature = "borsh")]
41    /// Create a `ComputeBudgetInstruction::SetComputeUnitLimit` `Instruction`
42    pub fn set_compute_unit_limit(units: u32) -> Instruction {
43        Instruction::new_with_borsh(id(), &Self::SetComputeUnitLimit(units), vec![])
44    }
45
46    #[cfg(feature = "borsh")]
47    /// Create a `ComputeBudgetInstruction::SetComputeUnitPrice` `Instruction`
48    pub fn set_compute_unit_price(micro_lamports: u64) -> Instruction {
49        Instruction::new_with_borsh(id(), &Self::SetComputeUnitPrice(micro_lamports), vec![])
50    }
51
52    /// Serialize Instruction using borsh, this is only used in runtime::cost_model::tests but compilation
53    /// can't be restricted as it's used across packages
54    #[cfg(all(feature = "dev-context-only-utils", feature = "borsh"))]
55    pub fn pack(self) -> Result<Vec<u8>, borsh::io::Error> {
56        borsh::to_vec(&self)
57    }
58
59    #[cfg(feature = "borsh")]
60    /// Create a `ComputeBudgetInstruction::SetLoadedAccountsDataSizeLimit` `Instruction`
61    pub fn set_loaded_accounts_data_size_limit(bytes: u32) -> Instruction {
62        Instruction::new_with_borsh(id(), &Self::SetLoadedAccountsDataSizeLimit(bytes), vec![])
63    }
64}