Refactor: Use InstructionContext::get_instruction_data()
(#24014)
* Adds transaction_context and instruction_context where invoke_context.get_keyed_accounts() is used. * Use instruction_context.get_instruction_data() instead of an explicit parameter. * Removes instruction_data parameter from Executor::execute(). * Removes instruction_data parameter from ProcessInstructionWithContext.
This commit is contained in:
committed by
GitHub
parent
cf59c000d9
commit
1b45c509c3
@ -22,9 +22,11 @@ use {
|
||||
|
||||
pub fn process_instruction(
|
||||
first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
match limited_deserialize(instruction_data)? {
|
||||
ProgramInstruction::CreateLookupTable {
|
||||
recent_slot,
|
||||
@ -62,6 +64,8 @@ impl Processor {
|
||||
untrusted_recent_slot: Slot,
|
||||
bump_seed: u8,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let _instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
let lookup_table_account =
|
||||
@ -158,6 +162,8 @@ impl Processor {
|
||||
invoke_context: &mut InvokeContext,
|
||||
first_instruction_account: usize,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let _instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
let lookup_table_account =
|
||||
@ -211,6 +217,8 @@ impl Processor {
|
||||
first_instruction_account: usize,
|
||||
new_addresses: Vec<Pubkey>,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let _instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
let lookup_table_account =
|
||||
@ -328,6 +336,8 @@ impl Processor {
|
||||
invoke_context: &mut InvokeContext,
|
||||
first_instruction_account: usize,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let _instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
let lookup_table_account =
|
||||
@ -378,6 +388,8 @@ impl Processor {
|
||||
invoke_context: &mut InvokeContext,
|
||||
first_instruction_account: usize,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let _instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
let lookup_table_account =
|
||||
|
@ -257,33 +257,20 @@ pub fn create_vm<'a, 'b>(
|
||||
|
||||
pub fn process_instruction(
|
||||
first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
process_instruction_common(
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
false,
|
||||
)
|
||||
process_instruction_common(first_instruction_account, invoke_context, false)
|
||||
}
|
||||
|
||||
pub fn process_instruction_jit(
|
||||
first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
process_instruction_common(
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
true,
|
||||
)
|
||||
process_instruction_common(first_instruction_account, invoke_context, true)
|
||||
}
|
||||
|
||||
fn process_instruction_common(
|
||||
first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
use_jit: bool,
|
||||
) -> Result<(), InstructionError> {
|
||||
@ -394,7 +381,7 @@ fn process_instruction_common(
|
||||
get_or_create_executor_time.as_us()
|
||||
);
|
||||
|
||||
executor.execute(program_account_index, instruction_data, invoke_context)
|
||||
executor.execute(program_account_index, invoke_context)
|
||||
} else {
|
||||
drop(program);
|
||||
debug_assert_eq!(first_instruction_account, 1);
|
||||
@ -404,19 +391,13 @@ fn process_instruction_common(
|
||||
if bpf_loader_upgradeable::check_id(program_id) {
|
||||
process_loader_upgradeable_instruction(
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
use_jit,
|
||||
)
|
||||
} else if bpf_loader::check_id(program_id)
|
||||
|| (!disable_deprecated_loader && bpf_loader_deprecated::check_id(program_id))
|
||||
{
|
||||
process_loader_instruction(
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
use_jit,
|
||||
)
|
||||
process_loader_instruction(first_instruction_account, invoke_context, use_jit)
|
||||
} else if disable_deprecated_loader && bpf_loader_deprecated::check_id(program_id) {
|
||||
ic_logger_msg!(log_collector, "Deprecated loader is no longer supported");
|
||||
Err(InstructionError::UnsupportedProgramId)
|
||||
@ -429,13 +410,13 @@ fn process_instruction_common(
|
||||
|
||||
fn process_loader_upgradeable_instruction(
|
||||
first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
use_jit: bool,
|
||||
) -> Result<(), InstructionError> {
|
||||
let log_collector = invoke_context.get_log_collector();
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
let program_id = instruction_context.get_program_key(transaction_context)?;
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
@ -1062,12 +1043,12 @@ fn common_close_account(
|
||||
|
||||
fn process_loader_instruction(
|
||||
first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
use_jit: bool,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
let program_id = instruction_context.get_program_key(transaction_context)?;
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
let program = keyed_account_at_index(keyed_accounts, first_instruction_account)?;
|
||||
@ -1148,11 +1129,10 @@ impl Debug for BpfExecutor {
|
||||
}
|
||||
|
||||
impl Executor for BpfExecutor {
|
||||
fn execute<'a, 'b>(
|
||||
fn execute(
|
||||
&self,
|
||||
_first_instruction_account: usize,
|
||||
_instruction_data: &[u8],
|
||||
invoke_context: &'a mut InvokeContext<'b>,
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
let log_collector = invoke_context.get_log_collector();
|
||||
let compute_meter = invoke_context.get_compute_meter();
|
||||
@ -1588,18 +1568,12 @@ mod tests {
|
||||
Vec::new(),
|
||||
None,
|
||||
Err(InstructionError::ProgramFailedToComplete),
|
||||
|first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext| {
|
||||
|first_instruction_account: usize, invoke_context: &mut InvokeContext| {
|
||||
invoke_context
|
||||
.get_compute_meter()
|
||||
.borrow_mut()
|
||||
.mock_set_remaining(0);
|
||||
super::process_instruction(
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
)
|
||||
super::process_instruction(first_instruction_account, invoke_context)
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -5,7 +5,6 @@ use {
|
||||
|
||||
pub fn process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
// Do nothing, compute budget instructions handled by the runtime
|
||||
|
@ -17,9 +17,11 @@ use {
|
||||
|
||||
pub fn process_instruction(
|
||||
first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let data = instruction_context.get_instruction_data();
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
let key_list: ConfigKeys = limited_deserialize(data)?;
|
||||
|
@ -25,11 +25,11 @@ use {
|
||||
|
||||
pub fn process_instruction(
|
||||
first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let data = instruction_context.get_instruction_data();
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
trace!("process_instruction: {:?}", data);
|
||||
|
@ -111,12 +111,10 @@ fn bench_process_vote_instruction(
|
||||
invoke_context
|
||||
.push(&instruction_accounts, &[0], &instruction_data)
|
||||
.unwrap();
|
||||
assert!(solana_vote_program::vote_processor::process_instruction(
|
||||
1,
|
||||
&instruction_data,
|
||||
&mut invoke_context
|
||||
)
|
||||
.is_ok());
|
||||
assert!(
|
||||
solana_vote_program::vote_processor::process_instruction(1, &mut invoke_context)
|
||||
.is_ok()
|
||||
);
|
||||
invoke_context.pop().unwrap();
|
||||
});
|
||||
}
|
||||
|
@ -18,11 +18,11 @@ use {
|
||||
|
||||
pub fn process_instruction(
|
||||
first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let data = instruction_context.get_instruction_data();
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
trace!("process_instruction: {:?}", data);
|
||||
@ -236,15 +236,9 @@ mod tests {
|
||||
instruction_accounts,
|
||||
None,
|
||||
expected_result,
|
||||
|first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext| {
|
||||
|first_instruction_account: usize, invoke_context: &mut InvokeContext| {
|
||||
invoke_context.feature_set = std::sync::Arc::new(FeatureSet::default());
|
||||
super::process_instruction(
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
)
|
||||
super::process_instruction(first_instruction_account, invoke_context)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -8,11 +8,13 @@ use {
|
||||
std::result::Result,
|
||||
};
|
||||
|
||||
fn verify<T: Pod + Verifiable>(
|
||||
input: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
let proof = ProofInstruction::decode_data::<T>(input).ok_or_else(|| {
|
||||
fn verify<T: Pod + Verifiable>(invoke_context: &mut InvokeContext) -> Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
let instruction = ProofInstruction::decode_data::<T>(instruction_data);
|
||||
|
||||
let proof = instruction.ok_or_else(|| {
|
||||
ic_msg!(invoke_context, "invalid proof data");
|
||||
InstructionError::InvalidInstructionData
|
||||
})?;
|
||||
@ -25,7 +27,6 @@ fn verify<T: Pod + Verifiable>(
|
||||
|
||||
pub fn process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
input: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
if invoke_context.get_stack_height() != TRANSACTION_LEVEL_STACK_HEIGHT {
|
||||
@ -40,26 +41,31 @@ pub fn process_instruction(
|
||||
compute_meter.borrow_mut().consume(100_000)?;
|
||||
}
|
||||
|
||||
match ProofInstruction::decode_type(input).ok_or(InstructionError::InvalidInstructionData)? {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
let instruction = ProofInstruction::decode_type(instruction_data);
|
||||
|
||||
match instruction.ok_or(InstructionError::InvalidInstructionData)? {
|
||||
ProofInstruction::VerifyCloseAccount => {
|
||||
ic_msg!(invoke_context, "VerifyCloseAccount");
|
||||
verify::<CloseAccountData>(input, invoke_context)
|
||||
verify::<CloseAccountData>(invoke_context)
|
||||
}
|
||||
ProofInstruction::VerifyWithdraw => {
|
||||
ic_msg!(invoke_context, "VerifyWithdraw");
|
||||
verify::<WithdrawData>(input, invoke_context)
|
||||
verify::<WithdrawData>(invoke_context)
|
||||
}
|
||||
ProofInstruction::VerifyWithdrawWithheldTokens => {
|
||||
ic_msg!(invoke_context, "VerifyWithdraw");
|
||||
verify::<WithdrawData>(input, invoke_context)
|
||||
verify::<WithdrawData>(invoke_context)
|
||||
}
|
||||
ProofInstruction::VerifyTransfer => {
|
||||
ic_msg!(invoke_context, "VerifyTransfer");
|
||||
verify::<TransferData>(input, invoke_context)
|
||||
verify::<TransferData>(invoke_context)
|
||||
}
|
||||
ProofInstruction::VerifyTransferWithFee => {
|
||||
ic_msg!(invoke_context, "VerifyTransferWithFee");
|
||||
verify::<TransferWithFeeData>(input, invoke_context)
|
||||
verify::<TransferWithFeeData>(invoke_context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user