Skip to content

Commit a2c0490

Browse files
feat: Add specific error codes for deserialization failures (#121)
Signed-off-by: AvhiMaz <[email protected]> Co-authored-by: Gabriele Picco <[email protected]>
1 parent 3edb410 commit a2c0490

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ pub enum DlpError {
8181
UndelegateBufferAlreadyInitialized = 36,
8282
#[error("Undelegate buffer PDA immutable")]
8383
UndelegateBufferImmutable = 37,
84+
#[error("Invalid data length for deserialization")]
85+
InvalidDataLength = 38,
86+
#[error("Invalid discriminator for delegation record")]
87+
InvalidDiscriminator = 39,
88+
#[error("Invalid delegation record deserialization")]
89+
InvalidDelegationRecordData = 40,
8490
#[error("An infallible error is encountered possibly due to logic error")]
8591
InfallibleError = 100,
8692
}

src/state/utils/to_bytes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ macro_rules! impl_to_bytes_with_discriminator_zero_copy {
66
&self,
77
data: &mut [u8],
88
) -> Result<(), ::solana_program::program_error::ProgramError> {
9-
if data.len() < 8 {
10-
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
9+
let expected_len = 8 + ::std::mem::size_of::<Self>();
10+
if data.len() != expected_len {
11+
return Err($crate::error::DlpError::InvalidDataLength.into());
1112
}
1213
data[..8].copy_from_slice(&Self::discriminator().to_bytes());
1314
data[8..].copy_from_slice(bytemuck::bytes_of(self));

src/state/utils/try_from_bytes.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ macro_rules! impl_try_from_bytes_with_discriminator_zero_copy {
66
data: &[u8],
77
) -> Result<&Self, ::solana_program::program_error::ProgramError> {
88
if data.len() < 8 {
9-
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
9+
return Err($crate::error::DlpError::InvalidDataLength.into());
1010
}
1111
if Self::discriminator().to_bytes().ne(&data[..8]) {
12-
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
12+
return Err($crate::error::DlpError::InvalidDiscriminator.into());
1313
}
1414
bytemuck::try_from_bytes::<Self>(&data[8..]).or(Err(
15-
::solana_program::program_error::ProgramError::InvalidAccountData,
15+
$crate::error::DlpError::InvalidDelegationRecordData.into(),
1616
))
1717
}
1818
pub fn try_from_bytes_with_discriminator_mut(
1919
data: &mut [u8],
2020
) -> Result<&mut Self, ::solana_program::program_error::ProgramError> {
2121
if data.len() < 8 {
22-
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
22+
return Err($crate::error::DlpError::InvalidDataLength.into());
2323
}
2424
if Self::discriminator().to_bytes().ne(&data[..8]) {
25-
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
25+
return Err($crate::error::DlpError::InvalidDiscriminator.into());
2626
}
2727
bytemuck::try_from_bytes_mut::<Self>(&mut data[8..]).or(Err(
28-
::solana_program::program_error::ProgramError::InvalidAccountData,
28+
$crate::error::DlpError::InvalidDelegationRecordData.into(),
2929
))
3030
}
3131
}
@@ -40,13 +40,13 @@ macro_rules! impl_try_from_bytes_with_discriminator_borsh {
4040
data: &[u8],
4141
) -> Result<Self, ::solana_program::program_error::ProgramError> {
4242
if data.len() < 8 {
43-
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
43+
return Err($crate::error::DlpError::InvalidDataLength.into());
4444
}
4545
if Self::discriminator().to_bytes().ne(&data[..8]) {
46-
return Err(::solana_program::program_error::ProgramError::InvalidAccountData);
46+
return Err($crate::error::DlpError::InvalidDiscriminator.into());
4747
}
4848
Self::try_from_slice(&data[8..]).or(Err(
49-
::solana_program::program_error::ProgramError::InvalidAccountData,
49+
$crate::error::DlpError::InvalidDelegationRecordData.into(),
5050
))
5151
}
5252
}

0 commit comments

Comments
 (0)