Skip to main content

solana_program/
instruction.rs

1pub use {
2    solana_instruction::{
3        AccountMeta, Instruction, ProcessedSiblingInstruction, TRANSACTION_LEVEL_STACK_HEIGHT,
4    },
5    solana_instruction_error::InstructionError,
6};
7
8/// Returns a sibling instruction from the processed sibling instruction list.
9///
10/// The processed sibling instruction list is a reverse-ordered list of
11/// successfully processed sibling instructions. For example, given the call flow:
12///
13/// A
14/// B -> C -> D
15/// B -> E
16/// B -> F
17///
18/// Then B's processed sibling instruction list is: `[A]`
19/// Then F's processed sibling instruction list is: `[E, C]`
20pub fn get_processed_sibling_instruction(index: usize) -> Option<Instruction> {
21    #[cfg(target_os = "solana")]
22    {
23        solana_instruction::syscalls::get_processed_sibling_instruction(index)
24    }
25
26    #[cfg(not(target_os = "solana"))]
27    {
28        crate::program_stubs::sol_get_processed_sibling_instruction(index)
29    }
30}
31
32/// Get the current stack height, transaction-level instructions are height
33/// TRANSACTION_LEVEL_STACK_HEIGHT, fist invoked inner instruction is height
34/// TRANSACTION_LEVEL_STACK_HEIGHT + 1, etc...
35pub fn get_stack_height() -> usize {
36    #[cfg(target_os = "solana")]
37    {
38        solana_instruction::syscalls::get_stack_height()
39    }
40
41    #[cfg(not(target_os = "solana"))]
42    {
43        crate::program_stubs::sol_get_stack_height() as usize
44    }
45}
46
47// TODO: remove this.
48/// Addition that returns [`InstructionError::InsufficientFunds`] on overflow.
49///
50/// This is an internal utility function.
51#[doc(hidden)]
52pub fn checked_add(a: u64, b: u64) -> Result<u64, InstructionError> {
53    a.checked_add(b).ok_or(InstructionError::InsufficientFunds)
54}