Summary
The Instructions sysvar (Sysvar1nstructions1111111111111111111111111) lets a
program read all top-level instructions in the current transaction by index or
relative offset. CPI inner instructions are not accessible.
Instructions sysvar
Programs can inspect all top-level instructions in the current transaction via
the Instructions sysvar (Sysvar1nstructions1111111111111111111111111).
This lets a program inspect the other instructions in the same transaction, for
example to verify that a specific instruction is in the same transaction as the
instruction it's executing.
The Instructions sysvar only contains top-level instructions from the transaction message. Inner instructions invoked via CPI are not accessible through this sysvar.
The instructions sysvar is not accessed through the normal Sysvar trait.
Instead, it is accessed through free functions in the
solana_instructions_sysvar
crate.
The sysvar data is serialized with a custom binary layout:
| Offset | Size | Description |
|---|---|---|
| 0 | 2 | num_instructions (u16, little-endian) |
| 2 | 2 * N | Byte offsets for each instruction (u16 each) |
| varies | varies | Serialized instruction data |
| last 2 bytes | 2 | Current instruction index (u16, little-endian) |
Each serialized instruction contains: the number of accounts (u16), the accounts as 33-byte entries (1 flag byte + 32-byte pubkey), the program ID (32 bytes), the data length (u16), and the raw data bytes.
Key functions:
load_current_index_checked(account_info): Returns the index of the currently executing instruction. The runtime updates this value each time a new top-level instruction begins.load_instruction_at_checked(index, account_info): Deserializes the instruction at the given absolute index.get_instruction_relative(offset, account_info): Loads an instruction relative to the current one (e.g.,-1for the previous instruction,1for the next).
Is this page helpful?