-
Notifications
You must be signed in to change notification settings - Fork 172
sdk: Improve IntrospectedInstruction #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
febo
merged 3 commits into
anza-xyz:main
from
blueshift-gg:improve-introspected-instruction
Dec 6, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,10 +42,10 @@ where | |
|
|
||
| /// Load the number of instructions in the currently executing `Transaction`. | ||
| #[inline(always)] | ||
| pub fn num_instructions(&self) -> u16 { | ||
| pub fn num_instructions(&self) -> usize { | ||
| // SAFETY: The first 2 bytes of the Instructions sysvar data represents the | ||
| // number of instructions. | ||
| unsafe { u16::from_le_bytes(*(self.data.as_ptr() as *const [u8; 2])) } | ||
| u16::from_le_bytes(unsafe { *(self.data.as_ptr() as *const [u8; 2]) }) as usize | ||
| } | ||
|
|
||
| /// Load the current `Instruction`'s index in the currently executing | ||
|
|
@@ -75,10 +75,7 @@ where | |
| .as_ptr() | ||
| .add(size_of::<u16>() + index * size_of::<u16>()) as *const u16); | ||
|
|
||
| IntrospectedInstruction { | ||
| raw: self.data.as_ptr().add(offset as usize), | ||
| marker: PhantomData, | ||
| } | ||
| IntrospectedInstruction::new_unchecked(self.data.as_ptr().add(offset as usize)) | ||
| } | ||
|
|
||
| /// Creates and returns an `IntrospectedInstruction` for the instruction at the specified index. | ||
|
|
@@ -87,7 +84,7 @@ where | |
| &self, | ||
| index: usize, | ||
| ) -> Result<IntrospectedInstruction, ProgramError> { | ||
| if index >= self.num_instructions() as usize { | ||
| if index >= self.num_instructions() { | ||
| return Err(ProgramError::InvalidInstructionData); | ||
| } | ||
|
|
||
|
|
@@ -133,10 +130,34 @@ impl<'a> TryFrom<&'a AccountView> for Instructions<Ref<'a, [u8]>> { | |
| #[derive(Clone, Debug, Eq, PartialEq)] | ||
| pub struct IntrospectedInstruction<'a> { | ||
| pub raw: *const u8, | ||
| pub marker: PhantomData<&'a [u8]>, | ||
| marker: PhantomData<&'a [u8]>, | ||
| } | ||
|
|
||
| impl IntrospectedInstruction<'_> { | ||
| /// Create a new IntrospectedInstruction. | ||
deanmlittle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// This function is unsafe because it does not verify anything about the pointer. | ||
| /// | ||
| /// It is private and used internally within the `get_instruction_account_at` function, which | ||
| /// performs the necessary index verification. However, to optimize performance for users | ||
| /// who are sure that the index is in bounds, we have exposed it as an unsafe function. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess, this comment was copied from |
||
| #[inline(always)] | ||
| unsafe fn new_unchecked(raw: *const u8) -> Self { | ||
| Self { | ||
| raw, | ||
| marker: PhantomData, | ||
| } | ||
| } | ||
|
|
||
| /// Load the number of instructions in the currently executing `Transaction`. | ||
deanmlittle marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #[inline(always)] | ||
| pub fn num_account_metas(&self) -> usize { | ||
| // SAFETY: The first 2 bytes represent the number of accounts in the instruction. | ||
| u16::from_le_bytes(unsafe { *(self.raw as *const [u8; 2]) }) as usize | ||
| } | ||
|
|
||
| /// Get the instruction account at the specified index. | ||
| /// | ||
| /// # Safety | ||
|
|
@@ -166,9 +187,9 @@ impl IntrospectedInstruction<'_> { | |
| index: usize, | ||
| ) -> Result<&IntrospectedInstructionAccount, ProgramError> { | ||
| // SAFETY: The first 2 bytes represent the number of accounts in the instruction. | ||
| let num_accounts = u16::from_le_bytes(unsafe { *(self.raw as *const [u8; 2]) }); | ||
| let num_accounts = self.num_account_metas(); | ||
|
|
||
| if index >= num_accounts as usize { | ||
| if index >= num_accounts { | ||
| return Err(ProgramError::InvalidArgument); | ||
| } | ||
|
|
||
|
|
@@ -180,24 +201,23 @@ impl IntrospectedInstruction<'_> { | |
| #[inline(always)] | ||
| pub fn get_program_id(&self) -> &Address { | ||
| // SAFETY: The first 2 bytes represent the number of accounts in the instruction. | ||
| let num_accounts = u16::from_le_bytes(unsafe { *(self.raw as *const [u8; 2]) }); | ||
| let num_accounts = self.num_account_metas(); | ||
|
|
||
| // SAFETY: The program ID is located after the instruction accounts. | ||
| unsafe { | ||
| &*(self.raw.add( | ||
| size_of::<u16>() | ||
| + num_accounts as usize * size_of::<IntrospectedInstructionAccount>(), | ||
| ) as *const Address) | ||
| &*(self | ||
| .raw | ||
| .add(size_of::<u16>() + num_accounts * size_of::<IntrospectedInstructionAccount>()) | ||
| as *const Address) | ||
| } | ||
| } | ||
|
|
||
| /// Get the instruction data of the `Instruction`. | ||
| #[inline(always)] | ||
| pub fn get_instruction_data(&self) -> &[u8] { | ||
| // SAFETY: The first 2 bytes represent the number of accounts in the instruction. | ||
| let offset = u16::from_le_bytes(unsafe { *(self.raw as *const [u8; 2]) }) as usize | ||
| * size_of::<IntrospectedInstructionAccount>() | ||
| + ADDRESS_BYTES; | ||
| let offset = | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bit in particular looks much nicer! |
||
| self.num_account_metas() * size_of::<IntrospectedInstructionAccount>() + ADDRESS_BYTES; | ||
|
|
||
| // SAFETY: The instruction data length is located after the program ID. | ||
| let data_len = u16::from_le_bytes(unsafe { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!