Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ pub enum DlpError {
UndelegateBufferAlreadyInitialized = 36,
#[error("Undelegate buffer PDA immutable")]
UndelegateBufferImmutable = 37,
#[error("Invalid data length for deserialization")]
InvalidDataLength = 38,
#[error("Invalid discriminator for delegation record")]
InvalidDiscriminator = 39,
#[error("Invalid delegation record deserialization")]
InvalidDelegationRecordData = 40,
#[error("An infallible error is encountered possibly due to logic error")]
InfallibleError = 100,
}
Expand Down
5 changes: 3 additions & 2 deletions src/state/utils/to_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ macro_rules! impl_to_bytes_with_discriminator_zero_copy {
&self,
data: &mut [u8],
) -> Result<(), ::solana_program::program_error::ProgramError> {
if data.len() < 8 {
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
let expected_len = 8 + ::std::mem::size_of::<Self>();
if data.len() != expected_len {
return Err($crate::error::DlpError::InvalidDataLength.into());
}
data[..8].copy_from_slice(&Self::discriminator().to_bytes());
data[8..].copy_from_slice(bytemuck::bytes_of(self));
Expand Down
18 changes: 9 additions & 9 deletions src/state/utils/try_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ macro_rules! impl_try_from_bytes_with_discriminator_zero_copy {
data: &[u8],
) -> Result<&Self, ::solana_program::program_error::ProgramError> {
if data.len() < 8 {
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
return Err($crate::error::DlpError::InvalidDataLength.into());
}
if Self::discriminator().to_bytes().ne(&data[..8]) {
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
return Err($crate::error::DlpError::InvalidDiscriminator.into());
}
bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
::solana_program::program_error::ProgramError::InvalidAccountData,
$crate::error::DlpError::InvalidDelegationRecordData.into(),
))
}
pub fn try_from_bytes_with_discriminator_mut(
data: &mut [u8],
) -> Result<&mut Self, ::solana_program::program_error::ProgramError> {
if data.len() < 8 {
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
return Err($crate::error::DlpError::InvalidDataLength.into());
}
if Self::discriminator().to_bytes().ne(&data[..8]) {
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
return Err($crate::error::DlpError::InvalidDiscriminator.into());
}
bytemuck::try_from_bytes_mut::<Self>(&mut data[8..]).or(Err(
::solana_program::program_error::ProgramError::InvalidAccountData,
$crate::error::DlpError::InvalidDelegationRecordData.into(),
))
}
}
Expand All @@ -40,13 +40,13 @@ macro_rules! impl_try_from_bytes_with_discriminator_borsh {
data: &[u8],
) -> Result<Self, ::solana_program::program_error::ProgramError> {
if data.len() < 8 {
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
return Err($crate::error::DlpError::InvalidDataLength.into());
}
if Self::discriminator().to_bytes().ne(&data[..8]) {
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
return Err($crate::error::DlpError::InvalidDiscriminator.into());
}
Self::try_from_slice(&data[8..]).or(Err(
::solana_program::program_error::ProgramError::InvalidAccountData,
$crate::error::DlpError::InvalidDelegationRecordData.into(),
))
}
}
Expand Down