solana_program/
instruction.rs

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