Add way to look at tx instructions (#11943)
This commit is contained in:
51
sdk/src/sysvar/instructions.rs
Normal file
51
sdk/src/sysvar/instructions.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
//! This account contains the serialized transaction instructions
|
||||
//!
|
||||
|
||||
use crate::instruction::Instruction;
|
||||
use crate::sanitize::SanitizeError;
|
||||
use crate::sysvar::Sysvar;
|
||||
|
||||
pub type Instructions = Vec<Instruction>;
|
||||
|
||||
crate::declare_sysvar_id!("instructions1111111111111111111111111111111", Instructions);
|
||||
|
||||
impl Sysvar for Instructions {}
|
||||
|
||||
#[cfg(not(feature = "program"))]
|
||||
use crate::clock::Epoch;
|
||||
#[cfg(not(feature = "program"))]
|
||||
use crate::genesis_config::ClusterType;
|
||||
|
||||
#[cfg(not(feature = "program"))]
|
||||
pub fn is_enabled(_epoch: Epoch, cluster_type: ClusterType) -> bool {
|
||||
cluster_type == ClusterType::Development
|
||||
}
|
||||
|
||||
pub fn get_current_instruction(data: &[u8]) -> u16 {
|
||||
let mut instr_fixed_data = [0u8; 2];
|
||||
let len = data.len();
|
||||
instr_fixed_data.copy_from_slice(&data[len - 2..len]);
|
||||
u16::from_le_bytes(instr_fixed_data)
|
||||
}
|
||||
|
||||
pub fn store_current_instruction(data: &mut [u8], instruction_index: u16) {
|
||||
let last_index = data.len() - 2;
|
||||
data[last_index..last_index + 2].copy_from_slice(&instruction_index.to_le_bytes());
|
||||
}
|
||||
|
||||
pub fn get_instruction(index: usize, data: &[u8]) -> Result<Instruction, SanitizeError> {
|
||||
solana_sdk::message::Message::deserialize_instruction(index, data)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_store_instruction() {
|
||||
let mut data = [4u8; 10];
|
||||
store_current_instruction(&mut data, 3);
|
||||
assert_eq!(get_current_instruction(&data), 3);
|
||||
assert_eq!([4u8; 8], data[0..8]);
|
||||
}
|
||||
}
|
@@ -11,6 +11,7 @@ use crate::{
|
||||
pub mod clock;
|
||||
pub mod epoch_schedule;
|
||||
pub mod fees;
|
||||
pub mod instructions;
|
||||
pub mod recent_blockhashes;
|
||||
pub mod rent;
|
||||
pub mod rewards;
|
||||
@@ -28,6 +29,7 @@ pub fn is_sysvar_id(id: &Pubkey) -> bool {
|
||||
|| slot_hashes::check_id(id)
|
||||
|| slot_history::check_id(id)
|
||||
|| stake_history::check_id(id)
|
||||
|| instructions::check_id(id)
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
|
Reference in New Issue
Block a user