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
@ -34,7 +34,6 @@ const NOOP_PROGRAM_ID: [u8; 32] = [
|
||||
#[allow(clippy::unnecessary_wraps)]
|
||||
fn process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
Ok(())
|
||||
|
@ -7820,12 +7820,12 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> result::Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
if let Ok(instruction) = bincode::deserialize(data) {
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
if let Ok(instruction) = bincode::deserialize(instruction_data) {
|
||||
match instruction {
|
||||
MockInstruction::Deduction => {
|
||||
instruction_context
|
||||
@ -11260,7 +11260,6 @@ pub(crate) mod tests {
|
||||
}
|
||||
fn mock_vote_processor(
|
||||
_first_instruction_account: usize,
|
||||
_instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
@ -11320,7 +11319,6 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_vote_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
Err(InstructionError::Custom(42))
|
||||
@ -11370,7 +11368,6 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_ix_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
Err(InstructionError::Custom(42))
|
||||
@ -12565,12 +12562,12 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> result::Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let lamports = data[0] as u64;
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
let lamports = instruction_data[0] as u64;
|
||||
instruction_context
|
||||
.try_borrow_instruction_account(transaction_context, 2)?
|
||||
.checked_sub_lamports(lamports)?;
|
||||
@ -12624,7 +12621,6 @@ pub(crate) mod tests {
|
||||
#[allow(clippy::unnecessary_wraps)]
|
||||
fn mock_process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> result::Result<(), InstructionError> {
|
||||
Ok(())
|
||||
@ -12847,7 +12843,6 @@ pub(crate) mod tests {
|
||||
#[allow(clippy::unnecessary_wraps)]
|
||||
fn mock_ok_vote_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
Ok(())
|
||||
@ -13097,7 +13092,6 @@ pub(crate) mod tests {
|
||||
fn test_same_program_id_uses_unqiue_executable_accounts() {
|
||||
fn nested_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> result::Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
@ -13372,7 +13366,6 @@ pub(crate) mod tests {
|
||||
#[allow(clippy::unnecessary_wraps)]
|
||||
fn mock_ix_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
Ok(())
|
||||
@ -13411,7 +13404,6 @@ pub(crate) mod tests {
|
||||
#[allow(clippy::unnecessary_wraps)]
|
||||
fn mock_ix_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
Ok(())
|
||||
@ -13789,11 +13781,10 @@ pub(crate) mod tests {
|
||||
#[derive(Debug)]
|
||||
struct TestExecutor {}
|
||||
impl Executor for TestExecutor {
|
||||
fn execute<'a, 'b>(
|
||||
fn execute(
|
||||
&self,
|
||||
_first_instruction_account: usize,
|
||||
_instruction_data: &[u8],
|
||||
_invoke_context: &'a mut InvokeContext<'b>,
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
Ok(())
|
||||
}
|
||||
@ -15508,14 +15499,15 @@ pub(crate) mod tests {
|
||||
let mock_program_id = Pubkey::new(&[2u8; 32]);
|
||||
fn mock_process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> result::Result<(), InstructionError> {
|
||||
let mock_program_id = Pubkey::new(&[2u8; 32]);
|
||||
let transaction_context = &mut invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
let mut return_data = [0u8; MAX_RETURN_DATA];
|
||||
if !data.is_empty() {
|
||||
let index = usize::from_le_bytes(data.try_into().unwrap());
|
||||
if !instruction_data.is_empty() {
|
||||
let index = usize::from_le_bytes(instruction_data.try_into().unwrap());
|
||||
return_data[index] = 1;
|
||||
transaction_context
|
||||
.set_return_data(mock_program_id, return_data.to_vec())
|
||||
@ -15704,7 +15696,6 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_ix_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
@ -15915,7 +15906,6 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_ix_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
let compute_budget = invoke_context.get_compute_budget();
|
||||
@ -15960,7 +15950,6 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_ix_processor(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> std::result::Result<(), InstructionError> {
|
||||
let compute_budget = invoke_context.get_compute_budget();
|
||||
@ -16383,12 +16372,12 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_transfer_process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> result::Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
if let Ok(instruction) = bincode::deserialize(data) {
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
if let Ok(instruction) = bincode::deserialize(instruction_data) {
|
||||
match instruction {
|
||||
MockTransferInstruction::Transfer(amount) => {
|
||||
instruction_context
|
||||
@ -17151,12 +17140,12 @@ pub(crate) mod tests {
|
||||
|
||||
fn mock_realloc_process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> result::Result<(), InstructionError> {
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
if let Ok(instruction) = bincode::deserialize(data) {
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
if let Ok(instruction) = bincode::deserialize(instruction_data) {
|
||||
match instruction {
|
||||
MockReallocInstruction::Realloc(new_size, new_balance, _) => {
|
||||
// Set data length
|
||||
|
@ -18,7 +18,6 @@ use {
|
||||
fn process_instruction_with_program_logging(
|
||||
process_instruction: ProcessInstructionWithContext,
|
||||
first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
let logger = invoke_context.get_log_collector();
|
||||
@ -27,7 +26,7 @@ fn process_instruction_with_program_logging(
|
||||
let program_id = instruction_context.get_program_key(transaction_context)?;
|
||||
stable_log::program_invoke(&logger, program_id, invoke_context.get_stack_height());
|
||||
|
||||
let result = process_instruction(first_instruction_account, instruction_data, invoke_context);
|
||||
let result = process_instruction(first_instruction_account, invoke_context);
|
||||
|
||||
let transaction_context = &invoke_context.transaction_context;
|
||||
let instruction_context = transaction_context.get_current_instruction_context()?;
|
||||
@ -41,13 +40,10 @@ fn process_instruction_with_program_logging(
|
||||
|
||||
macro_rules! with_program_logging {
|
||||
($process_instruction:expr) => {
|
||||
|first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext| {
|
||||
|first_instruction_account: usize, invoke_context: &mut InvokeContext| {
|
||||
process_instruction_with_program_logging(
|
||||
$process_instruction,
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
)
|
||||
}
|
||||
@ -87,7 +83,7 @@ impl AbiExample for Builtin {
|
||||
Self {
|
||||
name: String::default(),
|
||||
id: Pubkey::default(),
|
||||
process_instruction_with_context: |_, _, _| Ok(()),
|
||||
process_instruction_with_context: |_, _| Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -214,7 +210,6 @@ fn genesis_builtins() -> Vec<Builtin> {
|
||||
/// place holder for precompile programs, remove when the precompile program is deactivated via feature activation
|
||||
fn dummy_process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
Ok(())
|
||||
|
@ -190,12 +190,12 @@ mod tests {
|
||||
|
||||
fn mock_system_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()?;
|
||||
if let Ok(instruction) = bincode::deserialize(data) {
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
if let Ok(instruction) = bincode::deserialize(instruction_data) {
|
||||
match instruction {
|
||||
MockSystemInstruction::Correct => Ok(()),
|
||||
MockSystemInstruction::TransferLamports { lamports } => {
|
||||
@ -389,14 +389,14 @@ mod tests {
|
||||
|
||||
fn mock_system_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 instruction_data = instruction_context.get_instruction_data();
|
||||
let mut to_account =
|
||||
instruction_context.try_borrow_instruction_account(transaction_context, 1)?;
|
||||
if let Ok(instruction) = bincode::deserialize(data) {
|
||||
if let Ok(instruction) = bincode::deserialize(instruction_data) {
|
||||
match instruction {
|
||||
MockSystemInstruction::BorrowFail => {
|
||||
let from_account = instruction_context
|
||||
@ -599,7 +599,6 @@ mod tests {
|
||||
let mock_program_id = Pubkey::new_unique();
|
||||
fn mock_process_instruction(
|
||||
_first_instruction_account: usize,
|
||||
_data: &[u8],
|
||||
_invoke_context: &mut InvokeContext,
|
||||
) -> Result<(), InstructionError> {
|
||||
Err(InstructionError::Custom(0xbabb1e))
|
||||
|
@ -265,13 +265,13 @@ fn transfer_with_seed(
|
||||
|
||||
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 keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
let instruction_data = instruction_context.get_instruction_data();
|
||||
let instruction = limited_deserialize(instruction_data)?;
|
||||
let keyed_accounts = invoke_context.get_keyed_accounts()?;
|
||||
|
||||
trace!("process_instruction: {:?}", instruction);
|
||||
trace!("keyed_accounts: {:?}", keyed_accounts);
|
||||
@ -1737,15 +1737,9 @@ mod tests {
|
||||
},
|
||||
],
|
||||
Ok(()),
|
||||
|first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext| {
|
||||
|first_instruction_account: usize, invoke_context: &mut InvokeContext| {
|
||||
invoke_context.blockhash = hash(&serialize(&0).unwrap());
|
||||
super::process_instruction(
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
)
|
||||
super::process_instruction(first_instruction_account, invoke_context)
|
||||
},
|
||||
);
|
||||
}
|
||||
@ -2105,15 +2099,9 @@ mod tests {
|
||||
},
|
||||
],
|
||||
Err(NonceError::NoRecentBlockhashes.into()),
|
||||
|first_instruction_account: usize,
|
||||
instruction_data: &[u8],
|
||||
invoke_context: &mut InvokeContext| {
|
||||
|first_instruction_account: usize, invoke_context: &mut InvokeContext| {
|
||||
invoke_context.blockhash = hash(&serialize(&0).unwrap());
|
||||
super::process_instruction(
|
||||
first_instruction_account,
|
||||
instruction_data,
|
||||
invoke_context,
|
||||
)
|
||||
super::process_instruction(first_instruction_account, invoke_context)
|
||||
},
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user