From 7c9436a6f24cdb01f776e0d888bb40b6ab7ea92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Tue, 16 Aug 2022 11:14:09 +0200 Subject: [PATCH] Removes TransactionContext::get_instruction_trace(). Adds TransactionContext::get_instruction_trace_length() and TransactionContext::get_instruction_context_at_index(). --- programs/bpf_loader/src/syscalls/mod.rs | 29 ++++++++++++++++--------- runtime/src/message_processor.rs | 2 +- sdk/src/transaction_context.rs | 25 +++++++++++++-------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/programs/bpf_loader/src/syscalls/mod.rs b/programs/bpf_loader/src/syscalls/mod.rs index 0ef173d63dfa6b..2893849a9f0582 100644 --- a/programs/bpf_loader/src/syscalls/mod.rs +++ b/programs/bpf_loader/src/syscalls/mod.rs @@ -1712,25 +1712,34 @@ declare_syscall!( // Reverse iterate through the instruction trace, // ignoring anything except instructions on the same level let stack_height = invoke_context.get_stack_height(); - let instruction_trace = invoke_context.transaction_context.get_instruction_trace(); - let mut current_index = 0; - let mut instruction_context = None; - for current_instruction_context in instruction_trace.iter().rev() { - if current_instruction_context.get_stack_height() == TRANSACTION_LEVEL_STACK_HEIGHT + let instruction_trace_length = invoke_context + .transaction_context + .get_instruction_trace_length(); + let mut reverse_index_at_stack_height = 0; + let mut found_instruction_context = None; + for index_in_trace in (0..instruction_trace_length).rev() { + let instruction_context = question_mark!( + invoke_context + .transaction_context + .get_instruction_context_at_index_in_trace(index_in_trace) + .map_err(SyscallError::InstructionError), + result + ); + if instruction_context.get_stack_height() == TRANSACTION_LEVEL_STACK_HEIGHT && stack_height > TRANSACTION_LEVEL_STACK_HEIGHT { break; } - if current_instruction_context.get_stack_height() == stack_height { - if index.saturating_add(1) == current_index { - instruction_context = Some(current_instruction_context); + if instruction_context.get_stack_height() == stack_height { + if index.saturating_add(1) == reverse_index_at_stack_height { + found_instruction_context = Some(instruction_context); break; } - current_index = current_index.saturating_add(1); + reverse_index_at_stack_height = reverse_index_at_stack_height.saturating_add(1); } } - if let Some(instruction_context) = instruction_context { + if let Some(instruction_context) = found_instruction_context { let ProcessedSiblingInstruction { data_len, accounts_len, diff --git a/runtime/src/message_processor.rs b/runtime/src/message_processor.rs index 23eb1e800e9818..c95bfc0b72eae2 100644 --- a/runtime/src/message_processor.rs +++ b/runtime/src/message_processor.rs @@ -680,6 +680,6 @@ mod tests { InstructionError::Custom(0xbabb1e) )) ); - assert_eq!(transaction_context.get_instruction_trace().len(), 2); + assert_eq!(transaction_context.get_instruction_trace_length(), 2); } } diff --git a/sdk/src/transaction_context.rs b/sdk/src/transaction_context.rs index ca5cba14db312a..a90755ca16ac61 100644 --- a/sdk/src/transaction_context.rs +++ b/sdk/src/transaction_context.rs @@ -137,6 +137,21 @@ impl TransactionContext { self.account_keys.iter().rposition(|key| key == pubkey) } + /// Returns instruction trace length + pub fn get_instruction_trace_length(&self) -> usize { + self.instruction_trace.len() + } + + /// Gets an InstructionContext by its index in the trace + pub fn get_instruction_context_at_index_in_trace( + &self, + index_in_trace: usize, + ) -> Result<&InstructionContext, InstructionError> { + self.instruction_trace + .get(index_in_trace) + .ok_or(InstructionError::CallDepth) + } + /// Gets an InstructionContext by its nesting level in the stack pub fn get_instruction_context_at_nesting_level( &self, @@ -146,10 +161,7 @@ impl TransactionContext { .instruction_stack .get(nesting_level) .ok_or(InstructionError::CallDepth)?; - let instruction_context = self - .instruction_trace - .get(index_in_trace) - .ok_or(InstructionError::CallDepth)?; + let instruction_context = self.get_instruction_context_at_index_in_trace(index_in_trace)?; debug_assert_eq!(instruction_context.nesting_level, nesting_level); Ok(instruction_context) } @@ -265,11 +277,6 @@ impl TransactionContext { Ok(()) } - /// Returns instruction trace - pub fn get_instruction_trace(&self) -> &[InstructionContext] { - &self.instruction_trace - } - /// Calculates the sum of all lamports within an instruction fn instruction_accounts_lamport_sum( &self,