Refactor: process_instruction() (#20448)

* Adds first_instruction_account parameter to process_instruction().

* Removes InvokeContext::remove_first_keyed_account() from all BPF loaders.

* Removes InvokeContext::remove_first_keyed_account() from all builtin programs.

* Removes InvokeContext::remove_first_keyed_account() from all mock ups.

* Deprecates InvokeContext::remove_first_keyed_account().

* Documents index base of keyed_account_at_index().

* Adds dynamic offset to call sites of "keyed_account_at_index()".
This commit is contained in:
Alexander Meißner
2021-10-08 11:41:07 +02:00
committed by GitHub
parent a6a4cfda89
commit 4e65487d2f
17 changed files with 1364 additions and 1366 deletions

View File

@ -283,6 +283,7 @@ impl std::fmt::Debug for InstructionProcessor {
// These are just type aliases for work around of Debug-ing above pointers
type ErasedProcessInstructionWithContext = fn(
&'static Pubkey,
usize,
&'static [u8],
&'static mut dyn InvokeContext,
) -> Result<(), InstructionError>;
@ -361,15 +362,20 @@ impl InstructionProcessor {
if solana_sdk::native_loader::check_id(&root_account.owner()?) {
for (id, process_instruction) in &self.programs {
if id == root_id {
invoke_context.remove_first_keyed_account()?;
// Call the builtin program
return process_instruction(program_id, instruction_data, invoke_context);
return process_instruction(
program_id,
1, // root_id to be skipped
instruction_data,
invoke_context,
);
}
}
if !invoke_context.is_feature_active(&remove_native_loader::id()) {
// Call the program via the native loader
return self.native_loader.process_instruction(
&solana_sdk::native_loader::id(),
0,
instruction_data,
invoke_context,
);
@ -379,7 +385,12 @@ impl InstructionProcessor {
for (id, process_instruction) in &self.programs {
if id == owner_id {
// Call the program via a builtin loader
return process_instruction(program_id, instruction_data, invoke_context);
return process_instruction(
program_id,
0, // no root_id was provided
instruction_data,
invoke_context,
);
}
}
}
@ -1074,6 +1085,7 @@ mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
@ -1082,6 +1094,7 @@ mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {

View File

@ -138,12 +138,13 @@ impl NativeLoader {
pub fn process_instruction(
&self,
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
let (program_id, name_vec) = {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, 0)?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
if native_loader::id() != *program_id {
error!("Program id mismatch");
return Err(InstructionError::IncorrectProgramId);
@ -173,6 +174,7 @@ impl NativeLoader {
return Err(NativeLoaderError::InvalidAccountData.into());
}
trace!("Call native {:?}", name);
#[allow(deprecated)]
invoke_context.remove_first_keyed_account()?;
if name.ends_with("loader_program") {
let entrypoint =