Refactor: Remove program_id from process_instruction() (#20540)

* Replaces usage of program_id parameter by invoke_context.get_caller()?.

* Removes "pubkey: &Pubkey" parameter from "process_instruction()".
This commit is contained in:
Alexander Meißner 2021-10-10 22:29:18 +02:00 committed by GitHub
parent c16510152e
commit f30f3bddbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 110 additions and 175 deletions

View File

@ -1993,7 +1993,7 @@ fn read_and_verify_elf(program_location: &str) -> Result<Vec<u8>, Box<dyn std::e
let mut program_data = Vec::new();
file.read_to_end(&mut program_data)
.map_err(|err| format!("Unable to read program file: {}", err))?;
let mut invoke_context = MockInvokeContext::new(vec![]);
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// Verify the program
<dyn Executable<BpfError, ThisInstructionMeter>>::from_elf(

View File

@ -282,7 +282,6 @@ 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,
@ -353,18 +352,17 @@ impl InstructionProcessor {
/// This method calls the instruction's program entrypoint method
pub fn process_instruction(
&self,
program_id: &Pubkey,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
if let Some(root_account) = invoke_context.get_keyed_accounts()?.iter().next() {
let root_id = root_account.unsigned_key();
if solana_sdk::native_loader::check_id(&root_account.owner()?) {
let owner_id = &root_account.owner()?;
if solana_sdk::native_loader::check_id(owner_id) {
for (id, process_instruction) in &self.programs {
if id == root_id {
// Call the builtin program
return process_instruction(
program_id,
1, // root_id to be skipped
instruction_data,
invoke_context,
@ -374,19 +372,16 @@ impl InstructionProcessor {
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,
);
}
} else {
let owner_id = &root_account.owner()?;
for (id, process_instruction) in &self.programs {
if id == owner_id {
// Call the program via a builtin loader
return process_instruction(
program_id,
0, // no root_id was provided
instruction_data,
invoke_context,
@ -620,11 +615,8 @@ impl InstructionProcessor {
instruction_processor.add_program(program_id, *process_instruction);
}
let mut result = instruction_processor.process_instruction(
program_id,
&instruction.data,
invoke_context,
);
let mut result =
instruction_processor.process_instruction(&instruction.data, invoke_context);
if result.is_ok() {
// Verify the called program has not misbehaved
let demote_program_write_locks =
@ -1084,7 +1076,6 @@ mod tests {
let mut instruction_processor = InstructionProcessor::default();
#[allow(clippy::unnecessary_wraps)]
fn mock_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
@ -1093,7 +1084,6 @@ mod tests {
}
#[allow(clippy::unnecessary_wraps)]
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_context: &mut dyn InvokeContext,

View File

@ -14,7 +14,6 @@ use solana_sdk::{
keyed_account::keyed_account_at_index,
native_loader,
process_instruction::{InvokeContext, LoaderEntrypoint},
pubkey::Pubkey,
};
use std::{
collections::HashMap,
@ -137,12 +136,12 @@ 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 program_id = invoke_context.get_caller()?;
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
if native_loader::id() != *program_id {

View File

@ -100,7 +100,6 @@ fn get_invoke_context<'a>() -> &'a mut dyn InvokeContext {
pub fn builtin_process_instruction(
process_instruction: solana_sdk::entrypoint::ProcessInstruction,
program_id: &Pubkey,
_first_instruction_account: usize,
input: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -108,6 +107,7 @@ pub fn builtin_process_instruction(
set_invoke_context(invoke_context);
let logger = invoke_context.get_logger();
let program_id = invoke_context.get_caller()?;
stable_log::program_invoke(&logger, program_id, invoke_context.invoke_depth());
// Skip the processor account
@ -184,13 +184,11 @@ pub fn builtin_process_instruction(
macro_rules! processor {
($process_instruction:expr) => {
Some(
|program_id: &Pubkey,
first_instruction_account: usize,
|first_instruction_account: usize,
input: &[u8],
invoke_context: &mut dyn solana_sdk::process_instruction::InvokeContext| {
$crate::builtin_process_instruction(
$process_instruction,
program_id,
first_instruction_account,
input,
invoke_context,

View File

@ -95,7 +95,7 @@ fn bench_program_alu(bencher: &mut Bencher) {
.unwrap();
inner_iter.write_u64::<LittleEndian>(0).unwrap();
let loader_id = bpf_loader::id();
let mut invoke_context = MockInvokeContext::new(vec![]);
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let elf = load_elf("bench_alu").unwrap();
let mut executable = <dyn Executable<BpfError, ThisInstructionMeter>>::from_elf(
@ -216,13 +216,13 @@ fn bench_instruction_count_tuner(_bencher: &mut Bencher) {
.collect();
let instruction_data = vec![0u8];
let mut invoke_context = MockInvokeContext::new(keyed_accounts);
let mut invoke_context = MockInvokeContext::new(&loader_id, keyed_accounts);
invoke_context.compute_meter.remaining = BUDGET;
// Serialize account data
let keyed_accounts = invoke_context.get_keyed_accounts().unwrap();
let (mut serialized, account_lengths) = serialize_parameters(
&bpf_loader::id(),
&loader_id,
&solana_sdk::pubkey::new_rand(),
keyed_accounts,
&instruction_data,

View File

@ -203,13 +203,13 @@ fn run_program(
file.read_to_end(&mut data).unwrap();
let loader_id = bpf_loader::id();
let (parameter_bytes, account_lengths) = serialize_parameters(
&bpf_loader::id(),
&loader_id,
program_id,
&parameter_accounts,
&instruction_data,
)
.unwrap();
let mut invoke_context = MockInvokeContext::new(parameter_accounts);
let mut invoke_context = MockInvokeContext::new(&loader_id, parameter_accounts);
let compute_meter = invoke_context.get_compute_meter();
let mut instruction_meter = ThisInstructionMeter { compute_meter };
@ -284,7 +284,7 @@ fn run_program(
}
let parameter_accounts = invoke_context.get_keyed_accounts().unwrap();
deserialize_parameters(
&bpf_loader::id(),
&loader_id,
parameter_accounts,
parameter_bytes.as_slice(),
&account_lengths,

View File

@ -167,13 +167,11 @@ pub fn create_vm<'a>(
}
pub fn process_instruction(
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
process_instruction_common(
program_id,
first_instruction_account,
instruction_data,
invoke_context,
@ -182,13 +180,11 @@ pub fn process_instruction(
}
pub fn process_instruction_jit(
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
process_instruction_common(
program_id,
first_instruction_account,
instruction_data,
invoke_context,
@ -197,7 +193,6 @@ pub fn process_instruction_jit(
}
fn process_instruction_common(
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -213,6 +208,7 @@ fn process_instruction_common(
1 - (invoke_context.invoke_depth() > 1) as usize,
);
let program_id = invoke_context.get_caller()?;
if first_account.unsigned_key() != program_id {
ic_logger_msg!(logger, "Program id mismatch");
return Err(InstructionError::IncorrectProgramId);
@ -273,12 +269,12 @@ fn process_instruction_common(
invoke_context,
use_jit,
)?;
let program_id = invoke_context.get_caller()?;
invoke_context.add_executor(program_id, executor.clone());
executor
}
};
executor.execute(
program_id,
first_instruction_account,
instruction_data,
invoke_context,
@ -286,6 +282,8 @@ fn process_instruction_common(
)?
} else {
debug_assert_eq!(first_instruction_account, 1);
let program_id = invoke_context.get_caller()?;
if !check_loader_id(program_id) {
ic_logger_msg!(logger, "Invalid BPF loader id");
return Err(InstructionError::IncorrectProgramId);
@ -293,7 +291,6 @@ fn process_instruction_common(
if bpf_loader_upgradeable::check_id(program_id) {
process_loader_upgradeable_instruction(
program_id,
first_instruction_account,
instruction_data,
invoke_context,
@ -301,7 +298,6 @@ fn process_instruction_common(
)?;
} else {
process_loader_instruction(
program_id,
first_instruction_account,
instruction_data,
invoke_context,
@ -313,13 +309,13 @@ fn process_instruction_common(
}
fn process_loader_upgradeable_instruction(
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
use_jit: bool,
) -> Result<(), InstructionError> {
let logger = invoke_context.get_logger();
let program_id = invoke_context.get_caller()?;
let keyed_accounts = invoke_context.get_keyed_accounts()?;
match limited_deserialize(instruction_data)? {
@ -867,12 +863,12 @@ fn common_close_account(
}
fn process_loader_instruction(
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
use_jit: bool,
) -> Result<(), InstructionError> {
let program_id = invoke_context.get_caller()?;
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
if program.owner()? != *program_id {
@ -947,7 +943,6 @@ impl Debug for BpfExecutor {
impl Executor for BpfExecutor {
fn execute(
&self,
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -962,6 +957,7 @@ impl Executor for BpfExecutor {
let keyed_accounts = invoke_context.get_keyed_accounts()?;
let program = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
let loader_id = &program.owner()?;
let program_id = invoke_context.get_caller()?;
let (mut parameter_bytes, account_lengths) = serialize_parameters(
loader_id,
program_id,
@ -972,6 +968,7 @@ impl Executor for BpfExecutor {
let mut create_vm_time = Measure::start("create_vm");
let mut execute_time;
{
let program_id = &invoke_context.get_caller()?.clone();
let compute_meter = invoke_context.get_compute_meter();
let mut vm = match create_vm(
loader_id,
@ -1064,6 +1061,7 @@ impl Executor for BpfExecutor {
execute_time.as_us(),
deserialize_time.as_us(),
);
let program_id = invoke_context.get_caller()?;
stable_log::program_success(&logger, program_id);
Ok(())
}
@ -1121,10 +1119,9 @@ mod tests {
let mut keyed_accounts = keyed_accounts.to_vec();
keyed_accounts.insert(0, (false, false, owner, &processor_account));
super::process_instruction(
owner,
1,
instruction_data,
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts)),
&mut MockInvokeContext::new(owner, create_keyed_accounts_unified(&keyed_accounts)),
)
}
@ -1315,11 +1312,11 @@ mod tests {
let mut keyed_accounts = keyed_accounts.clone();
keyed_accounts.insert(0, (false, false, &program_key, &processor_account));
let mut invoke_context =
MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
MockInvokeContext::new(&program_key, create_keyed_accounts_unified(&keyed_accounts));
invoke_context.compute_meter = MockComputeMeter::default();
assert_eq!(
Err(InstructionError::ProgramFailedToComplete),
super::process_instruction(&program_key, 1, &[], &mut invoke_context)
super::process_instruction(1, &[], &mut invoke_context)
);
}

View File

@ -3331,7 +3331,7 @@ mod tests {
leader_schedule_epoch: 4,
unix_timestamp: 5,
};
let mut invoke_context = MockInvokeContext::new(vec![]);
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let mut data = vec![];
bincode::serialize_into(&mut data, &src_clock).unwrap();
invoke_context
@ -3376,7 +3376,7 @@ mod tests {
first_normal_epoch: 3,
first_normal_slot: 4,
};
let mut invoke_context = MockInvokeContext::new(vec![]);
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let mut data = vec![];
bincode::serialize_into(&mut data, &src_epochschedule).unwrap();
invoke_context
@ -3428,7 +3428,7 @@ mod tests {
lamports_per_signature: 1,
},
};
let mut invoke_context = MockInvokeContext::new(vec![]);
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let mut data = vec![];
bincode::serialize_into(&mut data, &src_fees).unwrap();
invoke_context
@ -3471,7 +3471,7 @@ mod tests {
exemption_threshold: 2.0,
burn_percent: 3,
};
let mut invoke_context = MockInvokeContext::new(vec![]);
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let mut data = vec![];
bincode::serialize_into(&mut data, &src_rent).unwrap();
invoke_context

View File

@ -1,9 +1,6 @@
use solana_sdk::{
instruction::InstructionError, process_instruction::InvokeContext, pubkey::Pubkey,
};
use solana_sdk::{instruction::InstructionError, process_instruction::InvokeContext};
pub fn process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,

View File

@ -14,7 +14,6 @@ use solana_sdk::{
use std::collections::BTreeSet;
pub fn process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -144,6 +143,7 @@ mod tests {
account::{Account, AccountSharedData},
keyed_account::create_keyed_accounts_unified,
process_instruction::MockInvokeContext,
pubkey::Pubkey,
signature::{Keypair, Signer},
system_instruction::SystemInstruction,
};
@ -158,10 +158,9 @@ mod tests {
let mut keyed_accounts = keyed_accounts.to_vec();
keyed_accounts.insert(0, (false, false, owner, &processor_account));
super::process_instruction(
owner,
1,
instruction_data,
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts)),
&mut MockInvokeContext::new(owner, create_keyed_accounts_unified(&keyed_accounts)),
)
}

View File

@ -7,7 +7,6 @@ use {
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index},
process_instruction::{get_sysvar, InvokeContext},
program_utils::limited_deserialize,
pubkey::Pubkey,
stake::{
instruction::StakeInstruction,
program::id,
@ -24,7 +23,6 @@ use {
pub use solana_sdk::stake::instruction::*;
pub fn process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -333,6 +331,7 @@ mod tests {
instruction::{AccountMeta, Instruction},
keyed_account::create_keyed_accounts_unified,
process_instruction::{mock_set_sysvar, MockInvokeContext},
pubkey::Pubkey,
rent::Rent,
stake::{
config as stake_config,
@ -379,10 +378,9 @@ mod tests {
let mut keyed_accounts = keyed_accounts.to_vec();
keyed_accounts.insert(0, (false, false, owner, &processor_account));
super::process_instruction(
owner,
1,
instruction_data,
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts)),
&mut MockInvokeContext::new(owner, create_keyed_accounts_unified(&keyed_accounts)),
)
}
@ -440,20 +438,17 @@ mod tests {
.collect();
let processor_id = id();
keyed_accounts.insert(0, (false, false, &processor_id, &processor_account));
let mut invoke_context =
MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
let mut invoke_context = MockInvokeContext::new(
&processor_id,
create_keyed_accounts_unified(&keyed_accounts),
);
mock_set_sysvar(
&mut invoke_context,
sysvar::clock::id(),
sysvar::clock::Clock::default(),
)
.unwrap();
super::process_instruction(
&Pubkey::default(),
1,
&instruction.data,
&mut invoke_context,
)
super::process_instruction(1, &instruction.data, &mut invoke_context)
}
}
@ -1104,7 +1099,7 @@ mod tests {
(true, false, &custodian, &custodian_account),
];
let mut invoke_context =
MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
MockInvokeContext::new(&id(), create_keyed_accounts_unified(&keyed_accounts));
let clock = Clock::default();
let mut data = vec![];
bincode::serialize_into(&mut data, &clock).unwrap();
@ -1114,7 +1109,6 @@ mod tests {
assert_eq!(
super::process_instruction(
&Pubkey::default(),
1,
&serialize(&StakeInstruction::SetLockupChecked(LockupCheckedArgs {
unix_timestamp: None,

View File

@ -5075,7 +5075,7 @@ mod tests {
let stake_lamports = 42;
let signers = vec![authorized_pubkey].into_iter().collect();
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
for state in &[
StakeState::Initialized(Meta::auto(&authorized_pubkey)),
@ -5179,7 +5179,7 @@ mod tests {
#[test]
fn test_merge_self_fails() {
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let stake_address = Pubkey::new_unique();
let authority_pubkey = Pubkey::new_unique();
let signers = HashSet::from_iter(vec![authority_pubkey]);
@ -5232,7 +5232,7 @@ mod tests {
let signers = vec![authorized_pubkey].into_iter().collect();
let wrong_signers = vec![wrong_authorized_pubkey].into_iter().collect();
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
for state in &[
StakeState::Initialized(Meta::auto(&authorized_pubkey)),
@ -5298,7 +5298,7 @@ mod tests {
let authorized_pubkey = solana_sdk::pubkey::new_rand();
let stake_lamports = 42;
let signers = vec![authorized_pubkey].into_iter().collect();
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
for state in &[
StakeState::Uninitialized,
@ -5368,7 +5368,7 @@ mod tests {
.expect("source_stake_account");
let source_stake_keyed_account =
KeyedAccount::new(&source_stake_pubkey, true, &source_stake_account);
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
assert_eq!(
stake_keyed_account.merge(
@ -5438,7 +5438,7 @@ mod tests {
let mut clock = Clock::default();
let mut stake_history = StakeHistory::default();
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
clock.epoch = 0;
let mut effective = base_lamports;
@ -6016,7 +6016,7 @@ mod tests {
..Delegation::default()
},
};
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let identical = good_stake;
assert!(
@ -6105,7 +6105,7 @@ mod tests {
#[test]
fn test_metas_can_merge_pre_v4() {
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// Identical Metas can merge
assert!(MergeKind::metas_can_merge(
&invoke_context,
@ -6191,7 +6191,7 @@ mod tests {
#[test]
fn test_metas_can_merge_v4() {
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// Identical Metas can merge
assert!(MergeKind::metas_can_merge(
&invoke_context,
@ -6357,7 +6357,7 @@ mod tests {
let stake_keyed_account = KeyedAccount::new(&authority_pubkey, true, &stake_account);
let mut clock = Clock::default();
let mut stake_history = StakeHistory::default();
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// Uninitialized state fails
assert_eq!(
@ -6584,7 +6584,7 @@ mod tests {
let inactive = MergeKind::Inactive(Meta::default(), lamports);
let activation_epoch = MergeKind::ActivationEpoch(meta, stake);
let fully_active = MergeKind::FullyActive(meta, stake);
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
assert_eq!(
inactive
@ -6670,7 +6670,7 @@ mod tests {
credits_observed: credits_a,
};
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
// activating stake merge, match credits observed
let activation_epoch_a = MergeKind::ActivationEpoch(meta, stake_a);

View File

@ -308,7 +308,6 @@ fn verify_rent_exemption(
}
pub fn process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -428,10 +427,9 @@ mod tests {
let mut keyed_accounts = keyed_accounts.to_vec();
keyed_accounts.insert(0, (false, false, owner, &processor_account));
super::process_instruction(
owner,
1,
instruction_data,
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts)),
&mut MockInvokeContext::new(owner, create_keyed_accounts_unified(&keyed_accounts)),
)
}

View File

@ -175,7 +175,7 @@ native machine code before execting it in the virtual machine.",
(Vec::from(bytes.as_slice_mut()), account_lengths)
}
};
let mut invoke_context = MockInvokeContext::new(accounts);
let mut invoke_context = MockInvokeContext::new(&bpf_loader::id(), accounts);
let logger = invoke_context.logger.clone();
let compute_meter = invoke_context.get_compute_meter();
let mut instruction_meter = ThisInstructionMeter { compute_meter };

View File

@ -32,7 +32,6 @@ const NOOP_PROGRAM_ID: [u8; 32] = [
#[allow(clippy::unnecessary_wraps)]
fn process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,

View File

@ -6685,7 +6685,6 @@ pub(crate) mod tests {
}
fn mock_process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -10333,11 +10332,11 @@ pub(crate) mod tests {
Pubkey::new(&[42u8; 32])
}
fn mock_vote_processor(
program_id: &Pubkey,
_first_instruction_account: usize,
_instruction_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
let program_id = invoke_context.get_caller()?;
if mock_vote_program_id() != *program_id {
return Err(InstructionError::IncorrectProgramId);
}
@ -10391,7 +10390,6 @@ pub(crate) mod tests {
let mut bank = Bank::new_for_tests(&genesis_config);
fn mock_vote_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
@ -10442,7 +10440,6 @@ pub(crate) mod tests {
let mut bank = Bank::new_for_tests(&genesis_config);
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
@ -11284,7 +11281,6 @@ pub(crate) mod tests {
let mut bank = Bank::new_for_tests(&genesis_config);
fn mock_process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -11345,7 +11341,6 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
@ -11532,7 +11527,6 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_ok_vote_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
@ -11783,7 +11777,6 @@ pub(crate) mod tests {
#[test]
fn test_same_program_id_uses_unqiue_executable_accounts() {
fn nested_processor(
_program_id: &Pubkey,
first_instruction_account: usize,
_data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -12155,7 +12148,6 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
@ -12205,7 +12197,6 @@ pub(crate) mod tests {
#[allow(clippy::unnecessary_wraps)]
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_context: &mut dyn InvokeContext,
@ -12593,7 +12584,6 @@ pub(crate) mod tests {
impl Executor for TestExecutor {
fn execute(
&self,
_program_id: &Pubkey,
_first_instruction_account: usize,
_instruction_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
@ -13097,7 +13087,6 @@ pub(crate) mod tests {
// intentionally create bogus native programs
#[allow(clippy::unnecessary_wraps)]
fn mock_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
@ -14595,7 +14584,6 @@ pub(crate) mod tests {
let mut bank = Bank::new_for_tests(&genesis_config);
fn mock_ix_processor(
_pubkey: &Pubkey,
first_instruction_account: usize,
_data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -14805,7 +14793,6 @@ pub(crate) mod tests {
let mut bank = Bank::new_for_tests(&genesis_config);
fn mock_ix_processor(
_pubkey: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
invoke_context: &mut dyn InvokeContext,

View File

@ -13,7 +13,6 @@ use solana_frozen_abi::abi_example::AbiExample;
fn process_instruction_with_program_logging(
process_instruction: ProcessInstructionWithContext,
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -21,15 +20,12 @@ fn process_instruction_with_program_logging(
debug_assert_eq!(first_instruction_account, 1);
let logger = invoke_context.get_logger();
let program_id = invoke_context.get_caller()?;
stable_log::program_invoke(&logger, program_id, invoke_context.invoke_depth());
let result = process_instruction(
program_id,
first_instruction_account,
instruction_data,
invoke_context,
);
let result = process_instruction(first_instruction_account, instruction_data, invoke_context);
let program_id = invoke_context.get_caller()?;
match &result {
Ok(()) => stable_log::program_success(&logger, program_id),
Err(err) => stable_log::program_failure(&logger, program_id, err),
@ -39,13 +35,11 @@ fn process_instruction_with_program_logging(
macro_rules! with_program_logging {
($process_instruction:expr) => {
|program_id: &Pubkey,
first_instruction_account: usize,
|first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext| {
process_instruction_with_program_logging(
$process_instruction,
program_id,
first_instruction_account,
instruction_data,
invoke_context,
@ -94,7 +88,7 @@ impl AbiExample for Builtin {
Self {
name: String::default(),
id: Pubkey::default(),
process_instruction_with_context: |_, _, _, _| Ok(()),
process_instruction_with_context: |_, _, _| Ok(()),
}
}
}
@ -141,7 +135,6 @@ fn genesis_builtins() -> Vec<Builtin> {
/// place holder for secp256k1, remove when the precompile program is deactivated via feature activation
fn dummy_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,

View File

@ -550,11 +550,8 @@ impl MessageProcessor {
let result = invoke_context
.push(program_id, message, instruction, program_indices, None)
.and_then(|_| {
instruction_processor.process_instruction(
program_id,
&instruction.data,
&mut invoke_context,
)?;
instruction_processor
.process_instruction(&instruction.data, &mut invoke_context)?;
invoke_context.verify(message, instruction, program_indices)?;
timings.accumulate(&invoke_context.timings);
Ok(())
@ -598,11 +595,11 @@ mod tests {
}
fn mock_process_instruction(
program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError> {
let program_id = invoke_context.get_caller()?;
let keyed_accounts = invoke_context.get_keyed_accounts()?;
assert_eq!(
*program_id,
@ -821,7 +818,6 @@ mod tests {
}
fn mock_system_process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -995,7 +991,6 @@ mod tests {
}
fn mock_system_process_instruction(
_program_id: &Pubkey,
first_instruction_account: usize,
data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -1523,7 +1518,6 @@ mod tests {
fn test_precompile() {
let mock_program_id = Pubkey::new_unique();
fn mock_process_instruction(
_program_id: &Pubkey,
_first_instruction_account: usize,
_data: &[u8],
_invoke_context: &mut dyn InvokeContext,

View File

@ -263,7 +263,6 @@ fn transfer_with_seed(
}
pub fn process_instruction(
_owner: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -522,13 +521,11 @@ mod tests {
..Account::default()
}));
let mut keyed_accounts = keyed_accounts.to_vec();
let processor_id = Pubkey::default();
keyed_accounts.insert(0, (false, false, &processor_id, &processor_account));
keyed_accounts.insert(0, (false, false, owner, &processor_account));
super::process_instruction(
owner,
1,
instruction_data,
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts)),
&mut MockInvokeContext::new(owner, create_keyed_accounts_unified(&keyed_accounts)),
)
}
@ -661,7 +658,7 @@ mod tests {
Address::create(
&to,
Some((&from, seed, &owner)),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Err(SystemError::AddressWithSeedMismatch.into())
);
@ -679,7 +676,7 @@ mod tests {
let to_address = Address::create(
&to,
Some((&from, seed, &new_owner)),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
)
.unwrap();
@ -692,7 +689,7 @@ mod tests {
2,
&new_owner,
&HashSet::new(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Err(InstructionError::MissingRequiredSignature)
);
@ -719,7 +716,7 @@ mod tests {
2,
&new_owner,
&[from, to].iter().cloned().collect::<HashSet<_>>(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Ok(())
);
@ -751,7 +748,7 @@ mod tests {
2,
&new_owner,
&[from, to].iter().cloned().collect::<HashSet<_>>(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(SystemError::ResultWithNegativeLamports.into()));
}
@ -775,7 +772,7 @@ mod tests {
MAX_PERMITTED_DATA_LENGTH + 1,
&system_program::id(),
signers,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert!(result.is_err());
assert_eq!(
@ -792,7 +789,7 @@ mod tests {
MAX_PERMITTED_DATA_LENGTH,
&system_program::id(),
signers,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert!(result.is_ok());
assert_eq!(to_account.borrow().lamports(), 50);
@ -825,7 +822,7 @@ mod tests {
2,
&new_owner,
signers,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
@ -844,7 +841,7 @@ mod tests {
2,
&new_owner,
signers,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
let from_lamports = from_account.borrow().lamports();
@ -862,7 +859,7 @@ mod tests {
2,
&new_owner,
signers,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
assert_eq!(from_lamports, 100);
@ -890,7 +887,7 @@ mod tests {
2,
&new_owner,
&[owned_key].iter().cloned().collect::<HashSet<_>>(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
@ -904,7 +901,7 @@ mod tests {
2,
&new_owner,
&[from].iter().cloned().collect::<HashSet<_>>(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
@ -918,7 +915,7 @@ mod tests {
2,
&new_owner,
&[owned_key].iter().cloned().collect::<HashSet<_>>(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(InstructionError::MissingRequiredSignature));
}
@ -944,7 +941,7 @@ mod tests {
2,
&sysvar::id(),
&signers,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Ok(()));
}
@ -973,7 +970,7 @@ mod tests {
disabled_features: vec![feature_set::rent_for_sysvars::id()]
.into_iter()
.collect(),
..MockInvokeContext::new(vec![])
..MockInvokeContext::new(&Pubkey::default(), vec![])
},
);
assert_eq!(result, Err(SystemError::InvalidProgramId.into()));
@ -1007,7 +1004,7 @@ mod tests {
2,
&new_owner,
&signers,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(SystemError::AccountAlreadyInUse.into()));
}
@ -1041,7 +1038,7 @@ mod tests {
0,
&solana_sdk::pubkey::new_rand(),
&signers,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Err(InstructionError::InvalidArgument),
);
@ -1059,7 +1056,7 @@ mod tests {
&pubkey.into(),
&new_owner,
&HashSet::new(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Err(InstructionError::MissingRequiredSignature)
);
@ -1071,7 +1068,7 @@ mod tests {
&pubkey.into(),
&system_program::id(),
&HashSet::new(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Ok(())
);
@ -1100,7 +1097,7 @@ mod tests {
&from.into(),
&new_owner,
&[from].iter().cloned().collect::<HashSet<_>>(),
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Ok(())
);
@ -1123,7 +1120,7 @@ mod tests {
disabled_features: vec![feature_set::rent_for_sysvars::id()]
.into_iter()
.collect(),
..MockInvokeContext::new(vec![])
..MockInvokeContext::new(&Pubkey::default(), vec![])
},
),
Err(SystemError::InvalidProgramId.into())
@ -1165,7 +1162,7 @@ mod tests {
&from_keyed_account,
&to_keyed_account,
50,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
)
.unwrap();
let from_lamports = from_keyed_account.account.borrow().lamports();
@ -1179,7 +1176,7 @@ mod tests {
&from_keyed_account,
&to_keyed_account,
100,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(SystemError::ResultWithNegativeLamports.into()));
assert_eq!(from_keyed_account.account.borrow().lamports(), 50);
@ -1190,7 +1187,7 @@ mod tests {
&from_keyed_account,
&to_keyed_account,
0,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
)
.is_ok());
assert_eq!(from_keyed_account.account.borrow().lamports(), 50);
@ -1204,7 +1201,7 @@ mod tests {
&from_keyed_account,
&to_keyed_account,
0,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Err(InstructionError::MissingRequiredSignature)
);
@ -1232,7 +1229,7 @@ mod tests {
&from_owner,
&to_keyed_account,
50,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
)
.unwrap();
let from_lamports = from_keyed_account.account.borrow().lamports();
@ -1249,7 +1246,7 @@ mod tests {
&from_owner,
&to_keyed_account,
100,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
);
assert_eq!(result, Err(SystemError::ResultWithNegativeLamports.into()));
assert_eq!(from_keyed_account.account.borrow().lamports(), 50);
@ -1264,7 +1261,7 @@ mod tests {
&from_owner,
&to_keyed_account,
0,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
)
.is_ok());
assert_eq!(from_keyed_account.account.borrow().lamports(), 50);
@ -1295,7 +1292,7 @@ mod tests {
&KeyedAccount::new(&from, true, &from_account),
&KeyedAccount::new(&to, false, &to_account),
50,
&MockInvokeContext::new(vec![]),
&MockInvokeContext::new(&Pubkey::default(), vec![]),
),
Err(InstructionError::InvalidArgument),
)
@ -1610,12 +1607,13 @@ mod tests {
(true, false, &owner, &nonce_acc),
(false, false, &blockhash_id, &new_recent_blockhashes_account),
];
let mut invoke_context =
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
let mut invoke_context = &mut MockInvokeContext::new(
&Pubkey::default(),
create_keyed_accounts_unified(&keyed_accounts),
);
invoke_context.blockhash = *blockhash;
assert_eq!(
super::process_instruction(
&Pubkey::default(),
1,
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
invoke_context,
@ -2034,12 +2032,13 @@ mod tests {
(true, false, &owner, &nonce_acc),
(false, false, &blockhash_id, &new_recent_blockhashes_account),
];
let mut invoke_context =
&mut MockInvokeContext::new(create_keyed_accounts_unified(&keyed_accounts));
let mut invoke_context = &mut MockInvokeContext::new(
&Pubkey::default(),
create_keyed_accounts_unified(&keyed_accounts),
);
invoke_context.blockhash = *blockhash;
assert_eq!(
super::process_instruction(
&Pubkey::default(),
1,
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
invoke_context,

View File

@ -79,7 +79,6 @@ macro_rules! declare_builtin_name {
/// use solana_sdk::declare_builtin;
///
/// fn my_process_instruction(
/// program_id: &Pubkey,
/// first_instruction_account: usize,
/// keyed_accounts: &[KeyedAccount],
/// instruction_data: &[u8],
@ -111,7 +110,6 @@ macro_rules! declare_builtin_name {
/// use solana_sdk::declare_builtin;
///
/// fn my_process_instruction(
/// program_id: &Pubkey,
/// first_instruction_account: usize,
/// keyed_accounts: &[KeyedAccount],
/// instruction_data: &[u8],

View File

@ -94,7 +94,6 @@ macro_rules! declare_name {
/// };
///
/// fn my_process_instruction(
/// program_id: &Pubkey,
/// first_instruction_account: usize,
/// instruction_data: &[u8],
/// invoke_context: &mut dyn InvokeContext,
@ -128,7 +127,6 @@ macro_rules! declare_name {
/// };
///
/// fn my_process_instruction(
/// program_id: &Pubkey,
/// first_instruction_account: usize,
/// instruction_data: &[u8],
/// invoke_context: &mut dyn InvokeContext,
@ -155,12 +153,11 @@ macro_rules! declare_program(
#[no_mangle]
pub extern "C" fn $name(
program_id: &$crate::pubkey::Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn $crate::process_instruction::InvokeContext,
) -> Result<(), $crate::instruction::InstructionError> {
$entrypoint(program_id, first_instruction_account, instruction_data, invoke_context)
$entrypoint(first_instruction_account, instruction_data, invoke_context)
}
)
);

View File

@ -289,7 +289,7 @@ mod test {
}
fn create_invoke_context_with_blockhash<'a>(seed: usize) -> MockInvokeContext<'a> {
let mut invoke_context = MockInvokeContext::new(vec![]);
let mut invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
let (blockhash, fee_calculator) = create_test_blockhash(seed);
invoke_context.blockhash = blockhash;
invoke_context.fee_calculator = fee_calculator;
@ -979,7 +979,7 @@ mod test {
let min_lamports = rent.minimum_balance(State::size());
with_test_keyed_account(min_lamports + 42, true, |nonce_account| {
let mut signers = HashSet::new();
let invoke_context = MockInvokeContext::new(vec![]);
let invoke_context = MockInvokeContext::new(&Pubkey::default(), vec![]);
signers.insert(*nonce_account.signer_key().unwrap());
let result = nonce_account.authorize_nonce_account(
&Pubkey::default(),

View File

@ -28,7 +28,7 @@ pub type LoaderEntrypoint = unsafe extern "C" fn(
) -> Result<(), InstructionError>;
pub type ProcessInstructionWithContext =
fn(&Pubkey, usize, &[u8], &mut dyn InvokeContext) -> Result<(), InstructionError>;
fn(usize, &[u8], &mut dyn InvokeContext) -> Result<(), InstructionError>;
pub struct InvokeContextStackFrame<'a> {
pub key: Pubkey,
@ -400,7 +400,6 @@ pub trait Executor: Debug + Send + Sync {
/// Execute the program
fn execute(
&self,
program_id: &Pubkey,
first_instruction_account: usize,
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
@ -456,7 +455,7 @@ pub struct MockInvokeContext<'a> {
}
impl<'a> MockInvokeContext<'a> {
pub fn new(keyed_accounts: Vec<KeyedAccount<'a>>) -> Self {
pub fn new(program_id: &Pubkey, keyed_accounts: Vec<KeyedAccount<'a>>) -> Self {
let compute_budget = ComputeBudget::default();
let mut invoke_context = MockInvokeContext {
invoke_stack: Vec::with_capacity(compute_budget.max_invoke_depth),
@ -476,10 +475,7 @@ impl<'a> MockInvokeContext<'a> {
};
invoke_context
.invoke_stack
.push(InvokeContextStackFrame::new(
Pubkey::default(),
keyed_accounts,
));
.push(InvokeContextStackFrame::new(*program_id, keyed_accounts));
invoke_context
}
}