From dfed471a8c4beb531cdc7301299abf0dc35c8a05 Mon Sep 17 00:00:00 2001 From: clabby Date: Sun, 19 Jan 2025 21:29:32 -0500 Subject: [PATCH] remove unwraps in super_root --- crates/interop/src/errors.rs | 5 ++++- crates/interop/src/super_root.rs | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/interop/src/errors.rs b/crates/interop/src/errors.rs index 05e1e9c7..3e50693d 100644 --- a/crates/interop/src/errors.rs +++ b/crates/interop/src/errors.rs @@ -44,7 +44,7 @@ pub type MessageGraphResult = /// 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")] @@ -52,6 +52,9 @@ pub enum SuperRootError { /// 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. diff --git a/crates/interop/src/super_root.rs b/crates/interop/src/super_root.rs index a8d3c802..f6b3cf62 100644 --- a/crates/interop/src/super_root.rs +++ b/crates/interop/src/super_root.rs @@ -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(); @@ -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); @@ -129,37 +129,37 @@ mod test { #[test] fn test_super_root_empty_buf() { let buf: Vec = 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]