* Partial revert "Updates documentation around what needs to be passed in CPI. (#21633)"

* Enforces the program_id being passed explicitly by removing it from get_instruction_keyed_accounts().

* instruction_accounts => instructions_account

(cherry picked from commit ba8e15848e)

Co-authored-by: Alexander Meißner <AlexanderMeissner@gmx.net>
This commit is contained in:
mergify[bot]
2021-12-21 17:54:18 +00:00
committed by GitHub
parent c296a6c9ed
commit 75fe0d3ecf
3 changed files with 23 additions and 18 deletions

View File

@ -724,9 +724,15 @@ impl<'a> InvokeContext<'a> {
/// Get the owner of the currently executing program /// Get the owner of the currently executing program
pub fn get_loader(&self) -> Result<Pubkey, InstructionError> { pub fn get_loader(&self) -> Result<Pubkey, InstructionError> {
self.get_instruction_keyed_accounts() let frame = self
.and_then(|keyed_accounts| keyed_accounts.first().ok_or(InstructionError::CallDepth)) .invoke_stack
.and_then(|keyed_account| keyed_account.owner()) .last()
.ok_or(InstructionError::CallDepth)?;
let first_instruction_account = frame
.number_of_program_accounts
.checked_sub(1)
.ok_or(InstructionError::CallDepth)?;
frame.keyed_accounts[first_instruction_account].owner()
} }
/// Removes the first keyed account /// Removes the first keyed account
@ -754,17 +760,15 @@ impl<'a> InvokeContext<'a> {
.ok_or(InstructionError::CallDepth) .ok_or(InstructionError::CallDepth)
} }
/// Get the list of keyed accounts skipping `first_instruction_account` many entries /// Get the list of keyed accounts without the chain of program accounts
///
/// Note: This only contains the `KeyedAccount`s passed by the caller.
pub fn get_instruction_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError> { pub fn get_instruction_keyed_accounts(&self) -> Result<&[KeyedAccount], InstructionError> {
let frame = self let frame = self
.invoke_stack .invoke_stack
.last() .last()
.ok_or(InstructionError::CallDepth)?; .ok_or(InstructionError::CallDepth)?;
let first_instruction_account = frame Ok(&frame.keyed_accounts[frame.number_of_program_accounts..])
.number_of_program_accounts
.checked_sub(1)
.ok_or(InstructionError::CallDepth)?;
Ok(&frame.keyed_accounts[first_instruction_account..])
} }
/// Get this invocation's LogCollector /// Get this invocation's LogCollector

View File

@ -2,7 +2,6 @@
extern crate solana_program; extern crate solana_program;
use solana_program::{ use solana_program::{
account_info::next_account_info,
account_info::AccountInfo, account_info::AccountInfo,
entrypoint, entrypoint,
entrypoint::ProgramResult, entrypoint::ProgramResult,
@ -25,20 +24,19 @@ fn process_instruction(
} }
let secp_instruction_index = instruction_data[0]; let secp_instruction_index = instruction_data[0];
let account_info_iter = &mut accounts.iter(); let instructions_account = accounts.last().ok_or(ProgramError::NotEnoughAccountKeys)?;
let instruction_accounts = next_account_info(account_info_iter)?; assert_eq!(*instructions_account.key, instructions::id());
assert_eq!(*instruction_accounts.key, instructions::id()); let data_len = instructions_account.try_borrow_data()?.len();
let data_len = instruction_accounts.try_borrow_data()?.len();
if data_len < 2 { if data_len < 2 {
return Err(ProgramError::InvalidAccountData); return Err(ProgramError::InvalidAccountData);
} }
let instruction = instructions::load_instruction_at_checked( let instruction = instructions::load_instruction_at_checked(
secp_instruction_index as usize, secp_instruction_index as usize,
instruction_accounts, instructions_account,
)?; )?;
let current_instruction = instructions::load_current_index_checked(instruction_accounts)?; let current_instruction = instructions::load_current_index_checked(instructions_account)?;
let my_index = instruction_data[1] as u16; let my_index = instruction_data[1] as u16;
assert_eq!(current_instruction, my_index); assert_eq!(current_instruction, my_index);
@ -56,7 +54,7 @@ fn process_instruction(
&[instruction_data[0], instruction_data[1], 1], &[instruction_data[0], instruction_data[1], 1],
vec![AccountMeta::new_readonly(instructions::id(), false)], vec![AccountMeta::new_readonly(instructions::id(), false)],
), ),
&[instruction_accounts.clone()], accounts,
)?; )?;
} }

View File

@ -1449,7 +1449,10 @@ fn test_program_bpf_instruction_introspection() {
); );
// Passing transaction // Passing transaction
let account_metas = vec![AccountMeta::new_readonly(sysvar::instructions::id(), false)]; let account_metas = vec![
AccountMeta::new(program_id, false),
AccountMeta::new(sysvar::instructions::id(), false),
];
let instruction0 = Instruction::new_with_bytes(program_id, &[0u8, 0u8], account_metas.clone()); let instruction0 = Instruction::new_with_bytes(program_id, &[0u8, 0u8], account_metas.clone());
let instruction1 = Instruction::new_with_bytes(program_id, &[0u8, 1u8], account_metas.clone()); let instruction1 = Instruction::new_with_bytes(program_id, &[0u8, 1u8], account_metas.clone());
let instruction2 = Instruction::new_with_bytes(program_id, &[0u8, 2u8], account_metas); let instruction2 = Instruction::new_with_bytes(program_id, &[0u8, 2u8], account_metas);