From b035991c352af51a51f77f051679db5e1d8a17d4 Mon Sep 17 00:00:00 2001 From: Jack May Date: Tue, 12 Apr 2022 09:49:42 -0700 Subject: [PATCH] migrate memberes from deprecated structs (#24263) --- program-runtime/src/invoke_context.rs | 56 ++++++++++++++----------- sdk/src/transaction_context.rs | 60 +++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 27 deletions(-) diff --git a/program-runtime/src/invoke_context.rs b/program-runtime/src/invoke_context.rs index 5419981d2..6c03daf3e 100644 --- a/program-runtime/src/invoke_context.rs +++ b/program-runtime/src/invoke_context.rs @@ -177,8 +177,6 @@ pub struct StackFrame<'a> { pub number_of_program_accounts: usize, pub keyed_accounts: Vec>, pub keyed_accounts_range: std::ops::Range, - pub orig_account_lengths: Option>, - pub allocator: Option>>, } impl<'a> StackFrame<'a> { @@ -191,8 +189,6 @@ impl<'a> StackFrame<'a> { number_of_program_accounts, keyed_accounts, keyed_accounts_range, - orig_account_lengths: None, - allocator: None, } } @@ -220,8 +216,10 @@ pub struct InvokeContext<'a> { pub timings: ExecuteDetailsTimings, pub blockhash: Hash, pub lamports_per_signature: u64, - pub check_aligned: bool, - pub check_size: bool, + check_aligned: bool, + check_size: bool, + orig_account_lengths: Vec>>, + allocators: Vec>>>, } impl<'a> InvokeContext<'a> { @@ -258,6 +256,8 @@ impl<'a> InvokeContext<'a> { lamports_per_signature, check_aligned: true, check_size: true, + orig_account_lengths: Vec::new(), + allocators: Vec::new(), } } @@ -443,6 +443,8 @@ impl<'a> InvokeContext<'a> { std::mem::transmute(keyed_accounts.as_slice()) }), )); + self.orig_account_lengths.push(None); + self.allocators.push(None); self.transaction_context.push( program_indices, instruction_accounts, @@ -454,6 +456,8 @@ impl<'a> InvokeContext<'a> { /// Pop a stack frame from the invocation stack pub fn pop(&mut self) -> Result<(), InstructionError> { + self.orig_account_lengths.pop(); + self.allocators.pop(); self.invoke_stack.pop(); self.transaction_context.pop() } @@ -1085,57 +1089,61 @@ impl<'a> InvokeContext<'a> { &mut self, orig_account_lengths: Vec, ) -> Result<(), InstructionError> { - let stack_frame = &mut self - .invoke_stack + *self + .orig_account_lengths .last_mut() - .ok_or(InstructionError::CallDepth)?; - stack_frame.orig_account_lengths = Some(orig_account_lengths); + .ok_or(InstructionError::CallDepth)? = Some(orig_account_lengths); Ok(()) } /// Get the original account lengths pub fn get_orig_account_lengths(&self) -> Result<&[usize], InstructionError> { - self.invoke_stack + self.orig_account_lengths .last() - .and_then(|frame| frame.orig_account_lengths.as_ref()) - .ok_or(InstructionError::CallDepth) + .and_then(|orig_account_lengths| orig_account_lengths.as_ref()) .map(|orig_account_lengths| orig_account_lengths.as_slice()) + .ok_or(InstructionError::CallDepth) } + // Set should alignment be enforced during user pointer translation pub fn set_check_aligned(&mut self, check_aligned: bool) { self.check_aligned = check_aligned; } + // Sshould alignment be enforced during user pointer translation pub fn get_check_aligned(&self) -> bool { self.check_aligned } + // Set should type size be checked during user pointer translation pub fn set_check_size(&mut self, check_size: bool) { self.check_size = check_size; } + // Set should type size be checked during user pointer translation pub fn get_check_size(&self) -> bool { self.check_size } + // Get this instruction's memory allocator + pub fn get_allocator(&self) -> Result>, InstructionError> { + self.allocators + .last() + .and_then(|allocator| allocator.clone()) + .ok_or(InstructionError::CallDepth) + } + + // Set this instruction's memory allocator pub fn set_allocator( &mut self, allocator: Rc>, ) -> Result<(), InstructionError> { - let stack_frame = &mut self - .invoke_stack + *self + .allocators .last_mut() - .ok_or(InstructionError::CallDepth)?; - stack_frame.allocator = Some(allocator); + .ok_or(InstructionError::CallDepth)? = Some(allocator); Ok(()) } - - pub fn get_allocator(&self) -> Result>, InstructionError> { - self.invoke_stack - .last() - .and_then(|frame| frame.allocator.clone()) - .ok_or(InstructionError::CallDepth) - } } pub struct MockInvokeContextPreparation { diff --git a/sdk/src/transaction_context.rs b/sdk/src/transaction_context.rs index 90db0ed3c..49a19f207 100644 --- a/sdk/src/transaction_context.rs +++ b/sdk/src/transaction_context.rs @@ -112,11 +112,10 @@ impl TransactionContext { self.account_keys.iter().rposition(|key| key == pubkey) } - /// Gets an InstructionContext by its nesting level in the stack - pub fn get_instruction_context_at( + pub fn get_instruction_context_indicies( &self, level: usize, - ) -> Result<&InstructionContext, InstructionError> { + ) -> Result<(usize, usize), InstructionError> { let top_level_index = *self .instruction_stack .get(0) @@ -129,6 +128,15 @@ impl TransactionContext { .get(level) .ok_or(InstructionError::CallDepth)? }; + Ok((top_level_index, cpi_index)) + } + + /// Gets an InstructionContext by its nesting level in the stack + pub fn get_instruction_context_at( + &self, + level: usize, + ) -> Result<&InstructionContext, InstructionError> { + let (top_level_index, cpi_index) = self.get_instruction_context_indicies(level)?; let instruction_context = self .instruction_trace .get(top_level_index) @@ -138,6 +146,21 @@ impl TransactionContext { Ok(instruction_context) } + /// Gets an InstructionContext by its nesting level in the stack + pub fn get_instruction_context_mut_at( + &mut self, + level: usize, + ) -> Result<&mut InstructionContext, InstructionError> { + let (top_level_index, cpi_index) = self.get_instruction_context_indicies(level)?; + let instruction_context = self + .instruction_trace + .get_mut(top_level_index) + .and_then(|instruction_trace| instruction_trace.get_mut(cpi_index)) + .ok_or(InstructionError::CallDepth)?; + debug_assert_eq!(instruction_context.nesting_level, level); + Ok(instruction_context) + } + /// Gets the max height of the InstructionContext stack pub fn get_instruction_context_capacity(&self) -> usize { self.instruction_context_capacity @@ -158,6 +181,17 @@ impl TransactionContext { self.get_instruction_context_at(level) } + /// Returns the current InstructionContext + pub fn get_current_instruction_context_mut( + &mut self, + ) -> Result<&mut InstructionContext, InstructionError> { + let level = self + .get_instruction_context_stack_height() + .checked_sub(1) + .ok_or(InstructionError::CallDepth)?; + self.get_instruction_context_mut_at(level) + } + /// Pushes a new InstructionContext pub fn push( &mut self, @@ -237,6 +271,26 @@ impl TransactionContext { pub fn get_instruction_trace(&self) -> &InstructionTrace { &self.instruction_trace } + + // /// Set the original account lengths + // pub fn set_orig_account_lengths( + // &mut self, + // orig_account_lengths: Vec, + // ) -> Result<(), InstructionError> { + // let level = self + // .get_instruction_context_stack_height() + // .checked_sub(1) + // .ok_or(InstructionError::CallDepth)?; + // let (top_level_index, cpi_index) = self.get_instruction_context_indicies(level)?; + // let instruction_context = self + // .instruction_trace + // .get_mut(top_level_index) + // .and_then(|instruction_trace| instruction_trace.get_mut(cpi_index)) + // .ok_or(InstructionError::CallDepth)?; + // debug_assert_eq!(instruction_context.nesting_level, level); + // instruction_context.orig_account_lengths = Some(orig_account_lengths); + // Ok(()) + // } } /// Return data at the end of a transaction