Skip to content

Commit

Permalink
Add test to ensure instructions sysvar behaves as expected from withi…
Browse files Browse the repository at this point in the history
…n a CPI
  • Loading branch information
mvines committed Oct 11, 2021
1 parent a4c9b0f commit aa5154d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions programs/bpf/rust/instruction_introspection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
extern crate solana_program;
use solana_program::{
account_info::next_account_info, account_info::AccountInfo, entrypoint,
entrypoint::ProgramResult, msg, program_error::ProgramError, pubkey::Pubkey,
entrypoint::ProgramResult, msg, program::invoke, program_error::ProgramError, pubkey::Pubkey,
sysvar::instructions,
instruction::{Instruction,AccountMeta},
};

entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
Expand Down Expand Up @@ -41,5 +42,19 @@ fn process_instruction(

msg!(&format!("data[0]: {}", instruction.data[0]));
msg!(&format!("index: {}", current_instruction));

if instruction_data.len() == 2 {
// CPI ourself with the same arguments to confirm the instructions sysvar reports the same
// results from within a CPI
invoke(
&Instruction::new_with_bytes(
*program_id,
&[instruction_data[0], instruction_data[1], 1],
vec![AccountMeta::new_readonly(instructions::id(), false)],
),
&[instruction_accounts.clone()],
)?;
}

Ok(())
}

0 comments on commit aa5154d

Please sign in to comment.