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:
committed by
GitHub
parent
c16510152e
commit
f30f3bddbb
@ -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,
|
||||
|
@ -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,
|
||||
¶meter_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,
|
||||
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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)),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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);
|
||||
|
@ -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)),
|
||||
)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user