Refactoring: Move KeyedAccounts to InvokeContext (#15410)

Collects all parametric occurrences and the construction of keyed_accounts and puts them into InvokeContext.
This commit is contained in:
Alexander Meißner
2021-04-19 18:48:48 +02:00
committed by GitHub
parent 015bc034a5
commit 9dfcb921cf
27 changed files with 1508 additions and 1408 deletions

View File

@@ -10,7 +10,7 @@ use solana_sdk::{
decode_error::DecodeError,
entrypoint_native::ProgramEntrypoint,
instruction::InstructionError,
keyed_account::{next_keyed_account, KeyedAccount},
keyed_account::keyed_account_at_index,
native_loader,
process_instruction::{InvokeContext, LoaderEntrypoint},
pubkey::Pubkey,
@@ -135,24 +135,30 @@ impl NativeLoader {
pub fn process_instruction(
&self,
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
invoke_context: &dyn InvokeContext,
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
let mut keyed_accounts_iter = keyed_accounts.iter();
let program = next_keyed_account(&mut keyed_accounts_iter)?;
if native_loader::id() != *program_id {
error!("Program id mismatch");
return Err(InstructionError::IncorrectProgramId);
}
if program.owner()? != *program_id {
error!("Executable account now owned by loader");
return Err(InstructionError::IncorrectProgramId);
}
let (program_id, name_vec) = {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, 0)?;
if native_loader::id() != *program_id {
error!("Program id mismatch");
return Err(InstructionError::IncorrectProgramId);
}
if program.owner()? != *program_id {
error!("Executable account now owned by loader");
return Err(InstructionError::IncorrectProgramId);
}
// TODO: Remove these two copies (* deref is also a copy)
// Both could be avoided as we know that the first KeyedAccount
// still exists even after invoke_context.remove_first_keyed_account() is called
(
*program.unsigned_key(),
&program.try_account_ref()?.data().clone(),
)
};
let params = keyed_accounts_iter.as_slice();
let account = program.try_account_ref()?;
let name = match str::from_utf8(account.data()) {
let name = match str::from_utf8(name_vec) {
Ok(v) => v,
Err(e) => {
error!("Invalid UTF-8 sequence: {}", e);
@@ -164,21 +170,21 @@ impl NativeLoader {
return Err(NativeLoaderError::InvalidAccountData.into());
}
trace!("Call native {:?}", name);
invoke_context.remove_first_keyed_account()?;
if name.ends_with("loader_program") {
let entrypoint =
Self::get_entrypoint::<LoaderEntrypoint>(name, &self.loader_symbol_cache)?;
unsafe {
entrypoint(
program.unsigned_key(),
params,
instruction_data,
invoke_context,
)
}
unsafe { entrypoint(&program_id, instruction_data, invoke_context) }
} else {
let entrypoint =
Self::get_entrypoint::<ProgramEntrypoint>(name, &self.program_symbol_cache)?;
unsafe { entrypoint(program.unsigned_key(), params, instruction_data) }
unsafe {
entrypoint(
&program_id,
invoke_context.get_keyed_accounts()?,
instruction_data,
)
}
}
}
}