Skip to content
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

token-2022: Add Pod-compatible versions of StateWithExtensions #6336

Merged
merged 6 commits into from
Mar 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions token/program-2022/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,21 +515,8 @@ impl<'data, S: BaseState + Pack> StateWithExtensions<'data, S> {
check_min_len_and_not_multisig(input, S::SIZE_OF)?;
let (base_data, rest) = input.split_at(S::SIZE_OF);
let base = S::unpack(base_data)?;
if let Some((account_type_index, tlv_start_index)) = type_and_tlv_indices::<S>(rest)? {
// type_and_tlv_indices() checks that returned indexes are within range
let account_type = AccountType::try_from(rest[account_type_index])
.map_err(|_| ProgramError::InvalidAccountData)?;
check_account_type::<S>(account_type)?;
Ok(Self {
base,
tlv_data: &rest[tlv_start_index..],
})
} else {
Ok(Self {
base,
tlv_data: &[],
})
}
let tlv_data = unpack_tlv_data::<S>(rest)?;
Ok(Self { base, tlv_data })
}
}
impl<'a, S: BaseState + Pack> BaseStateWithExtensions<S> for StateWithExtensions<'a, S> {
Expand Down Expand Up @@ -847,6 +834,17 @@ impl<'a, S: BaseState> BaseStateWithExtensionsMut<S> for StateWithExtensionsMut<
}
}

fn unpack_tlv_data<S: BaseState>(rest: &[u8]) -> Result<&[u8], ProgramError> {
if let Some((account_type_index, tlv_start_index)) = type_and_tlv_indices::<S>(rest)? {
let account_type = AccountType::try_from(rest[account_type_index])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you restore the comment here? I do think it was helpful so that we can feel confident indexing into rest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, will do!

.map_err(|_| ProgramError::InvalidAccountData)?;
check_account_type::<S>(account_type)?;
Ok(&rest[tlv_start_index..])
} else {
Ok(&[])
}
}

fn unpack_type_and_tlv_data_with_check_mut<
S: BaseState,
F: Fn(AccountType) -> Result<(), ProgramError>,
Expand Down