Skip to content

Commit 77cbcad

Browse files
committed
Kill configure_cpi_instruction
1 parent 7f0eff9 commit 77cbcad

File tree

3 files changed

+27
-40
lines changed

3 files changed

+27
-40
lines changed

program-runtime/src/invoke_context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,12 @@ impl<'a, 'ix_data> InvokeContext<'a, 'ix_data> {
426426
// This ? operator should not error out because `fn get_current_instruction_index` is also called
427427
// in `get_current_instruction_context`
428428
let parent_index = self.transaction_context.get_current_instruction_index()?;
429-
self.transaction_context.configure_cpi_instruction(
429+
self.transaction_context.configure_next_instruction(
430430
program_account_index,
431431
instruction_accounts,
432432
transaction_callee_map,
433433
Cow::Owned(instruction.data),
434-
parent_index as u16,
434+
Some(parent_index as u16),
435435
)?;
436436
Ok(())
437437
}
@@ -474,6 +474,7 @@ impl<'a, 'ix_data> InvokeContext<'a, 'ix_data> {
474474
instruction_accounts,
475475
transaction_callee_map,
476476
Cow::Borrowed(data),
477+
None,
477478
)?;
478479
Ok(())
479480
}

program-runtime/src/serialization.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ mod tests {
794794
instruction_accounts,
795795
dedup_map,
796796
Cow::Owned(instruction_data.clone()),
797+
None,
797798
)
798799
.unwrap();
799800
} else {

transaction-context/src/lib.rs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -323,42 +323,6 @@ impl<'ix_data> TransactionContext<'ix_data> {
323323
self.get_instruction_context_at_index_in_trace(index_in_trace)
324324
}
325325

326-
pub fn configure_cpi_instruction(
327-
&mut self,
328-
program_index: IndexOfAccount,
329-
instruction_accounts: Vec<InstructionAccount>,
330-
deduplication_map: Vec<u16>,
331-
instruction_data: Cow<'ix_data, [u8]>,
332-
parent_index: u16,
333-
) -> Result<(), InstructionError> {
334-
self.configure_next_instruction(
335-
program_index,
336-
instruction_accounts,
337-
deduplication_map,
338-
instruction_data,
339-
)?;
340-
self.transaction_frame.number_of_instructions = self
341-
.transaction_frame
342-
.number_of_instructions
343-
.saturating_add(1);
344-
self.transaction_frame.cpi_scratchpad = VmSlice::new(
345-
GUEST_INSTRUCTION_DATA_BASE_ADDRESS.saturating_add(
346-
GUEST_REGION_SIZE
347-
.saturating_mul(self.transaction_frame.number_of_instructions as u64),
348-
),
349-
0,
350-
);
351-
352-
// This ? operator is never going to fail because it is also called in
353-
// `configure_next_instruction`
354-
let instruction = self
355-
.instruction_trace
356-
.last_mut()
357-
.ok_or(InstructionError::CallDepth)?;
358-
instruction.index_of_parent_instruction = parent_index;
359-
Ok(())
360-
}
361-
362326
/// Configures the next instruction.
363327
///
364328
/// The last InstructionContext is always empty and pre-reserved for the next instruction.
@@ -368,6 +332,7 @@ impl<'ix_data> TransactionContext<'ix_data> {
368332
instruction_accounts: Vec<InstructionAccount>,
369333
deduplication_map: Vec<u16>,
370334
instruction_data: Cow<'ix_data, [u8]>,
335+
parent_index: Option<u16>,
371336
) -> Result<(), InstructionError> {
372337
debug_assert_eq!(deduplication_map.len(), MAX_ACCOUNTS_PER_TRANSACTION);
373338
let trace_len = self.instruction_trace.len();
@@ -378,6 +343,23 @@ impl<'ix_data> TransactionContext<'ix_data> {
378343
.last_mut()
379344
.ok_or(InstructionError::CallDepth)?;
380345

346+
// If we have a parent index, then we are dealing with a CPI.
347+
if let Some(parent_index) = parent_index {
348+
self.transaction_frame.number_of_instructions = self
349+
.transaction_frame
350+
.number_of_instructions
351+
.saturating_add(1);
352+
instruction.index_of_parent_instruction = parent_index;
353+
}
354+
355+
self.transaction_frame.cpi_scratchpad = VmSlice::new(
356+
GUEST_INSTRUCTION_DATA_BASE_ADDRESS.saturating_add(
357+
GUEST_REGION_SIZE
358+
.saturating_mul(self.transaction_frame.number_of_instructions as u64),
359+
),
360+
0,
361+
);
362+
381363
instruction.program_account_index_in_tx = program_index;
382364
instruction.configure_vm_slices(
383365
instruction_index as u64,
@@ -414,6 +396,7 @@ impl<'ix_data> TransactionContext<'ix_data> {
414396
instruction_accounts,
415397
dedup_map,
416398
Cow::Owned(instruction_data),
399+
None,
417400
)
418401
}
419402

@@ -872,6 +855,7 @@ mod tests {
872855
vec![InstructionAccount::new(1, false, false)],
873856
vec![0; MAX_ACCOUNTS_PER_TRANSACTION],
874857
Vec::new().into(),
858+
None,
875859
)
876860
.unwrap();
877861
transaction_context.push().unwrap();
@@ -899,6 +883,7 @@ mod tests {
899883
vec![InstructionAccount::new(1, false, false)],
900884
vec![0; MAX_ACCOUNTS_PER_TRANSACTION],
901885
Vec::new().into(),
886+
None,
902887
)
903888
.unwrap();
904889
transaction_context.push().unwrap();
@@ -918,12 +903,12 @@ mod tests {
918903
);
919904

920905
transaction_context
921-
.configure_cpi_instruction(
906+
.configure_next_instruction(
922907
0,
923908
vec![InstructionAccount::new(2, false, true)],
924909
vec![0; 256],
925910
Vec::new().into(),
926-
2,
911+
Some(2),
927912
)
928913
.unwrap();
929914
assert_eq!(

0 commit comments

Comments
 (0)