gemachain_program/sysvar/
instructions.rs

1#![allow(clippy::integer_arithmetic)]
2//! This account contains the serialized transaction instructions
3
4use crate::{instruction::Instruction, sanitize::SanitizeError};
5
6// Instructions Sysvar, dummy type, use the associated helpers instead of the Sysvar trait
7pub struct Instructions();
8
9crate::declare_sysvar_id!("Sysvar1nstructions1111111111111111111111111", Instructions);
10
11/// Load the current instruction's index from the Instructions Sysvar data
12pub fn load_current_index(data: &[u8]) -> u16 {
13    let mut instr_fixed_data = [0u8; 2];
14    let len = data.len();
15    instr_fixed_data.copy_from_slice(&data[len - 2..len]);
16    u16::from_le_bytes(instr_fixed_data)
17}
18
19/// Store the current instruction's index in the Instructions Sysvar data
20pub fn store_current_index(data: &mut [u8], instruction_index: u16) {
21    let last_index = data.len() - 2;
22    data[last_index..last_index + 2].copy_from_slice(&instruction_index.to_le_bytes());
23}
24
25/// Load an instruction at the specified index
26pub fn load_instruction_at(index: usize, data: &[u8]) -> Result<Instruction, SanitizeError> {
27    crate::message::Message::deserialize_instruction(index, data)
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_load_store_instruction() {
36        let mut data = [4u8; 10];
37        store_current_index(&mut data, 3);
38        assert_eq!(load_current_index(&data), 3);
39        assert_eq!([4u8; 8], data[0..8]);
40    }
41}