Skip to content

Commit

Permalink
Removes TransactionContext::get_instruction_trace().
Browse files Browse the repository at this point in the history
Adds TransactionContext::get_instruction_trace_length() and TransactionContext::get_instruction_context_at_index().
  • Loading branch information
Lichtso committed Aug 19, 2022
1 parent 0d1beb4 commit 7c9436a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
29 changes: 19 additions & 10 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/message_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
25 changes: 16 additions & 9 deletions sdk/src/transaction_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7c9436a

Please sign in to comment.