Skip to content

Commit

Permalink
remove unwraps in super_root
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Jan 20, 2025
1 parent 093243b commit dfed471
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
5 changes: 4 additions & 1 deletion crates/interop/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ pub type MessageGraphResult<T, P: InteropProvider> =
/// An error type for the [SuperRoot] struct's serialization and deserialization.
///
/// [SuperRoot]: crate::SuperRoot
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[derive(Debug, Clone, Error)]
pub enum SuperRootError {
/// Invalid super root version byte
#[error("Invalid super root version byte")]
InvalidVersionByte,
/// Unexpected encoded super root length
#[error("Unexpected encoded super root length")]
UnexpectedLength,
/// Slice conversion error
#[error("Slice conversion error: {0}")]
SliceConversionError(#[from] core::array::TryFromSliceError),
}

/// A [Result] alias for the [SuperRootError] type.
Expand Down
20 changes: 10 additions & 10 deletions crates/interop/src/super_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl SuperRoot {
if buf.len() < 8 {
return Err(SuperRootError::UnexpectedLength);
}
let timestamp = u64::from_be_bytes(buf[0..8].try_into().unwrap());
let timestamp = u64::from_be_bytes(buf[0..8].try_into()?);
buf.advance(8);

let mut output_roots = Vec::new();
Expand All @@ -52,7 +52,7 @@ impl SuperRoot {
return Err(SuperRootError::UnexpectedLength);
}

let chain_id = U256::from_be_bytes::<32>(buf[0..32].try_into().unwrap());
let chain_id = U256::from_be_bytes::<32>(buf[0..32].try_into()?);
buf.advance(32);
let output_root = B256::from_slice(&buf[0..32]);
buf.advance(32);
Expand Down Expand Up @@ -129,37 +129,37 @@ mod test {
#[test]
fn test_super_root_empty_buf() {
let buf: Vec<u8> = Vec::new();
assert_eq!(
assert!(matches!(
SuperRoot::decode(&mut buf.as_slice()).unwrap_err(),
SuperRootError::UnexpectedLength
);
));
}

#[test]
fn test_super_root_invalid_version() {
let buf = vec![0xFF];
assert_eq!(
assert!(matches!(
SuperRoot::decode(&mut buf.as_slice()).unwrap_err(),
SuperRootError::InvalidVersionByte
);
));
}

#[test]
fn test_super_root_invalid_length_at_timestamp() {
let buf = vec![SUPER_ROOT_VERSION, 0x00];
assert_eq!(
assert!(matches!(
SuperRoot::decode(&mut buf.as_slice()).unwrap_err(),
SuperRootError::UnexpectedLength
);
));
}

#[test]
fn test_super_root_invalid_length_malformed_output_roots() {
let buf = [&[SUPER_ROOT_VERSION], 64u64.to_be_bytes().as_ref(), &[0xbe, 0xef]].concat();
assert_eq!(
assert!(matches!(
SuperRoot::decode(&mut buf.as_slice()).unwrap_err(),
SuperRootError::UnexpectedLength
);
));
}

#[test]
Expand Down

0 comments on commit dfed471

Please sign in to comment.