From a9422c2fd093b19bbf1d53397130af38bc06c848 Mon Sep 17 00:00:00 2001 From: hopinheimer Date: Sat, 31 May 2025 16:07:38 +0530 Subject: [PATCH 1/4] fix: adding reserve prefix to decode --- ssz/src/decode.rs | 42 +++++++------- ssz/src/decode/impls.rs | 120 ++++++++++++++++++++-------------------- ssz_derive/src/lib.rs | 28 +++++----- 3 files changed, 95 insertions(+), 95 deletions(-) diff --git a/ssz/src/decode.rs b/ssz/src/decode.rs index 814aa70..97015da 100644 --- a/ssz/src/decode.rs +++ b/ssz/src/decode.rs @@ -116,7 +116,7 @@ pub trait Decode: Sized { /// /// The supplied bytes must be the exact length required to decode `Self`, excess bytes will /// result in an error. - fn from_ssz_bytes(bytes: &[u8]) -> Result; + fn from_ssz_bytes(__bytes: &[u8]) -> Result; } #[derive(Copy, Clone, Debug)] @@ -132,7 +132,7 @@ pub struct Offset { /// /// See [`SszDecoder`](struct.SszDecoder.html) for usage examples. pub struct SszDecoderBuilder<'a> { - bytes: &'a [u8], + __bytes: &'a [u8], items: SmallVec8<&'a [u8]>, offsets: SmallVec8, items_index: usize, @@ -141,9 +141,9 @@ pub struct SszDecoderBuilder<'a> { impl<'a> SszDecoderBuilder<'a> { /// Instantiate a new builder that should build a `SszDecoder` over the given `bytes` which /// are assumed to be the SSZ encoding of some object. - pub fn new(bytes: &'a [u8]) -> Self { + pub fn new(__bytes: &'a [u8]) -> Self { Self { - bytes, + __bytes, items: smallvec![], offsets: smallvec![], items_index: 0, @@ -191,10 +191,10 @@ impl<'a> SszDecoderBuilder<'a> { self.items_index += ssz_fixed_len; let slice = - self.bytes + self.__bytes .get(start..self.items_index) .ok_or(DecodeError::InvalidByteLength { - len: self.bytes.len(), + len: self.__bytes.len(), expected: self.items_index, })?; @@ -203,9 +203,9 @@ impl<'a> SszDecoderBuilder<'a> { self.offsets.push(Offset { position: self.items.len(), offset: sanitize_offset( - read_offset(&self.bytes[self.items_index..])?, + read_offset(&self.__bytes[self.items_index..])?, self.offsets.last().map(|o| o.offset), - self.bytes.len(), + self.__bytes.len(), None, )?, }); @@ -236,19 +236,19 @@ impl<'a> SszDecoderBuilder<'a> { let a = pair[0]; let b = pair[1]; - self.items[a.position] = &self.bytes[a.offset..b.offset]; + self.items[a.position] = &self.__bytes[a.offset..b.offset]; } // Handle the last offset, pushing a slice from it's start through to the end of // `self.bytes`. if let Some(last) = self.offsets.last() { - self.items[last.position] = &self.bytes[last.offset..] + self.items[last.position] = &self.__bytes[last.offset..] } } else { // If the container is fixed-length, ensure there are no excess bytes. - if self.items_index != self.bytes.len() { + if self.items_index != self.__bytes.len() { return Err(DecodeError::InvalidByteLength { - len: self.bytes.len(), + len: self.__bytes.len(), expected: self.items_index, }); } @@ -336,13 +336,13 @@ impl<'a> SszDecoder<'a> { /// /// - `bytes` is empty. /// - the union selector is not a valid value (i.e., larger than the maximum number of variants. -pub fn split_union_bytes(bytes: &[u8]) -> Result<(UnionSelector, &[u8]), DecodeError> { - let selector = bytes +pub fn split_union_bytes(__bytes: &[u8]) -> Result<(UnionSelector, &[u8]), DecodeError> { + let selector = __bytes .first() .copied() .ok_or(DecodeError::OutOfBoundsByte { i: 0 }) .and_then(UnionSelector::new)?; - let body = bytes + let body = __bytes .get(1..) .ok_or(DecodeError::OutOfBoundsByte { i: 1 })?; Ok((selector, body)) @@ -350,10 +350,10 @@ pub fn split_union_bytes(bytes: &[u8]) -> Result<(UnionSelector, &[u8]), DecodeE /// Reads a `BYTES_PER_LENGTH_OFFSET`-byte length from `bytes`, where `bytes.len() >= /// BYTES_PER_LENGTH_OFFSET`. -pub fn read_offset(bytes: &[u8]) -> Result { - decode_offset(bytes.get(0..BYTES_PER_LENGTH_OFFSET).ok_or( +pub fn read_offset(__bytes: &[u8]) -> Result { + decode_offset(__bytes.get(0..BYTES_PER_LENGTH_OFFSET).ok_or( DecodeError::InvalidLengthPrefix { - len: bytes.len(), + len: __bytes.len(), expected: BYTES_PER_LENGTH_OFFSET, }, )?) @@ -361,15 +361,15 @@ pub fn read_offset(bytes: &[u8]) -> Result { /// Decode bytes as a little-endian usize, returning an `Err` if `bytes.len() != /// BYTES_PER_LENGTH_OFFSET`. -fn decode_offset(bytes: &[u8]) -> Result { - let len = bytes.len(); +fn decode_offset(__bytes: &[u8]) -> Result { + let len = __bytes.len(); let expected = BYTES_PER_LENGTH_OFFSET; if len != expected { Err(DecodeError::InvalidLengthPrefix { len, expected }) } else { let mut array: [u8; BYTES_PER_LENGTH_OFFSET] = std::default::Default::default(); - array.clone_from_slice(bytes); + array.clone_from_slice(__bytes); Ok(u32::from_le_bytes(array) as usize) } diff --git a/ssz/src/decode/impls.rs b/ssz/src/decode/impls.rs index 6c9b7b5..c05ce54 100644 --- a/ssz/src/decode/impls.rs +++ b/ssz/src/decode/impls.rs @@ -19,15 +19,15 @@ macro_rules! impl_decodable_for_uint { $bit_size / 8 } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let len = bytes.len(); + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let len = __bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { let mut array: [u8; $bit_size / 8] = std::default::Default::default(); - array.clone_from_slice(bytes); + array.clone_from_slice(__bytes); Ok(Self::from_le_bytes(array)) } @@ -74,8 +74,8 @@ macro_rules! impl_decode_for_tuples { } } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let mut builder = SszDecoderBuilder::new(bytes); + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let mut builder = SszDecoderBuilder::new(__bytes); $( builder.register_type::<$T>()?; @@ -204,19 +204,19 @@ impl Decode for bool { 1 } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let len = bytes.len(); + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let len = __bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - match bytes[0] { + match __bytes[0] { 0b0000_0000 => Ok(false), 0b0000_0001 => Ok(true), _ => Err(DecodeError::BytesInvalid(format!( "Out-of-range for boolean: {}", - bytes[0] + __bytes[0] ))), } } @@ -232,8 +232,8 @@ impl Decode for NonZeroUsize { ::ssz_fixed_len() } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let x = usize::from_ssz_bytes(bytes)?; + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let x = usize::from_ssz_bytes(__bytes)?; if x == 0 { Err(DecodeError::BytesInvalid( @@ -251,8 +251,8 @@ impl Decode for Option { fn is_ssz_fixed_len() -> bool { false } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let (selector, body) = split_union_bytes(bytes)?; + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let (selector, body) = split_union_bytes(__bytes)?; match selector.into() { 0u8 => Ok(None), 1u8 => ::from_ssz_bytes(body).map(Option::Some), @@ -270,8 +270,8 @@ impl Decode for Arc { T::ssz_fixed_len() } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - T::from_ssz_bytes(bytes).map(Arc::new) + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + T::from_ssz_bytes(__bytes).map(Arc::new) } } @@ -284,14 +284,14 @@ impl Decode for Address { 20 } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let len = bytes.len(); + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let len = __bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - Ok(Self::from_slice(bytes)) + Ok(Self::from_slice(__bytes)) } } } @@ -305,16 +305,16 @@ impl Decode for FixedBytes { N } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - if bytes.len() != N { + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + if __bytes.len() != N { return Err(DecodeError::InvalidByteLength { - len: bytes.len(), + len: __bytes.len(), expected: N, }); } let mut fixed_array = [0u8; N]; - fixed_array.copy_from_slice(bytes); + fixed_array.copy_from_slice(__bytes); Ok(Self(fixed_array)) } @@ -329,14 +329,14 @@ impl Decode for Bloom { 256 } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let len = bytes.len(); + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let len = __bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - Ok(Self::from_slice(bytes)) + Ok(Self::from_slice(__bytes)) } } } @@ -350,14 +350,14 @@ impl Decode for U256 { 32 } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let len = bytes.len(); + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let len = __bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - Ok(U256::from_le_slice(bytes)) + Ok(U256::from_le_slice(__bytes)) } } } @@ -369,8 +369,8 @@ impl Decode for Bytes { } #[inline] - fn from_ssz_bytes(bytes: &[u8]) -> Result { - Ok(bytes.to_vec().into()) + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + Ok(__bytes.to_vec().into()) } } @@ -383,14 +383,14 @@ impl Decode for U128 { 16 } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let len = bytes.len(); + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let len = __bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - Ok(U128::from_le_slice(bytes)) + Ok(U128::from_le_slice(__bytes)) } } } @@ -404,15 +404,15 @@ impl Decode for [u8; N] { N } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - let len = bytes.len(); + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + let len = __bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { let mut array: [u8; N] = [0; N]; - array.copy_from_slice(bytes); + array.copy_from_slice(__bytes); Ok(array) } @@ -424,16 +424,16 @@ impl Decode for Vec { false } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - if bytes.is_empty() { + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + if __bytes.is_empty() { Ok(vec![]) } else if T::is_ssz_fixed_len() { - bytes + __bytes .chunks(T::ssz_fixed_len()) .map(T::from_ssz_bytes) .collect() } else { - decode_list_of_variable_length_items(bytes, None) + decode_list_of_variable_length_items(__bytes, None) } } } @@ -443,16 +443,16 @@ impl Decode for SmallVec<[T; N]> { false } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - if bytes.is_empty() { + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + if __bytes.is_empty() { Ok(SmallVec::new()) } else if T::is_ssz_fixed_len() { - bytes + __bytes .chunks(T::ssz_fixed_len()) .map(T::from_ssz_bytes) .collect() } else { - decode_list_of_variable_length_items(bytes, None) + decode_list_of_variable_length_items(__bytes, None) } } } @@ -466,16 +466,16 @@ where false } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - if bytes.is_empty() { + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + if __bytes.is_empty() { Ok(Self::from_iter(iter::empty())) } else if <(K, V)>::is_ssz_fixed_len() { - bytes + __bytes .chunks(<(K, V)>::ssz_fixed_len()) .map(<(K, V)>::from_ssz_bytes) .collect() } else { - decode_list_of_variable_length_items(bytes, None) + decode_list_of_variable_length_items(__bytes, None) } } } @@ -488,16 +488,16 @@ where false } - fn from_ssz_bytes(bytes: &[u8]) -> Result { - if bytes.is_empty() { + fn from_ssz_bytes(__bytes: &[u8]) -> Result { + if __bytes.is_empty() { Ok(Self::from_iter(iter::empty())) } else if T::is_ssz_fixed_len() { - bytes + __bytes .chunks(T::ssz_fixed_len()) .map(T::from_ssz_bytes) .collect() } else { - decode_list_of_variable_length_items(bytes, None) + decode_list_of_variable_length_items(__bytes, None) } } } @@ -508,17 +508,17 @@ where /// significantly faster as it is optimized to read same-typed items whilst `ssz::SszDecoder` /// supports reading items of differing types. pub fn decode_list_of_variable_length_items>( - bytes: &[u8], + __bytes: &[u8], max_len: Option, ) -> Result { - if bytes.is_empty() { + if __bytes.is_empty() { return Container::try_from_iter(iter::empty()).map_err(|e| { DecodeError::BytesInvalid(format!("Error trying to collect empty list: {:?}", e)) }); } - let first_offset = read_offset(bytes)?; - sanitize_offset(first_offset, None, bytes.len(), Some(first_offset))?; + let first_offset = read_offset(__bytes)?; + sanitize_offset(first_offset, None, __bytes.len(), Some(first_offset))?; if first_offset % BYTES_PER_LENGTH_OFFSET != 0 || first_offset < BYTES_PER_LENGTH_OFFSET { return Err(DecodeError::InvalidListFixedBytesLen(first_offset)); @@ -537,15 +537,15 @@ pub fn decode_list_of_variable_length_items process_results( (1..=num_items).map(|i| { let slice_option = if i == num_items { - bytes.get(offset..) + __bytes.get(offset..) } else { let start = offset; - let next_offset = read_offset(&bytes[(i * BYTES_PER_LENGTH_OFFSET)..])?; + let next_offset = read_offset(&__bytes[(i * BYTES_PER_LENGTH_OFFSET)..])?; offset = - sanitize_offset(next_offset, Some(offset), bytes.len(), Some(first_offset))?; + sanitize_offset(next_offset, Some(offset), __bytes.len(), Some(first_offset))?; - bytes.get(start..offset) + __bytes.get(start..offset) }; let slice = slice_option.ok_or(DecodeError::OutOfBoundsByte { i: offset })?; diff --git a/ssz_derive/src/lib.rs b/ssz_derive/src/lib.rs index c6ca75d..18da998 100644 --- a/ssz_derive/src/lib.rs +++ b/ssz_derive/src/lib.rs @@ -818,7 +818,7 @@ fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Tok } fixed_decodes.push(quote! { - let (slice, bytes) = bytes.split_at(#ssz_fixed_len); + let (slice, __bytes) = __bytes.split_at(#ssz_fixed_len); let #ident = #from_ssz_bytes?; }); is_fixed_lens.push(is_ssz_fixed_len); @@ -848,11 +848,11 @@ fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Tok } } - fn from_ssz_bytes(bytes: &[u8]) -> std::result::Result { + fn from_ssz_bytes(__bytes: &[u8]) -> std::result::Result { if ::is_ssz_fixed_len() { - if bytes.len() != ::ssz_fixed_len() { + if __bytes.len() != ::ssz_fixed_len() { return Err(ssz::DecodeError::InvalidByteLength { - len: bytes.len(), + len: __bytes.len(), expected: ::ssz_fixed_len(), }); } @@ -867,7 +867,7 @@ fn ssz_decode_derive_struct(item: &DeriveInput, struct_data: &DataStruct) -> Tok )* }) } else { - let mut builder = ssz::SszDecoderBuilder::new(bytes); + let mut builder = ssz::SszDecoderBuilder::new(__bytes); #( #register_types @@ -932,7 +932,7 @@ fn ssz_decode_derive_struct_transparent( }); } else { fields.push(quote! { - #name: <_>::from_ssz_bytes(bytes)?, + #name: <_>::from_ssz_bytes(__bytes)?, }); wrapped_type = Some(ty); } @@ -944,7 +944,7 @@ fn ssz_decode_derive_struct_transparent( }); } else { fields.push(quote! { - #index:<_>::from_ssz_bytes(bytes)?, + #index:<_>::from_ssz_bytes(__bytes)?, }); wrapped_type = Some(ty); } @@ -963,7 +963,7 @@ fn ssz_decode_derive_struct_transparent( <#ty as ssz::Decode>::ssz_fixed_len() } - fn from_ssz_bytes(bytes: &[u8]) -> std::result::Result { + fn from_ssz_bytes(__bytes: &[u8]) -> std::result::Result { Ok(Self { #( #fields @@ -1009,8 +1009,8 @@ fn ssz_decode_derive_enum_tag(derive_input: &DeriveInput, enum_data: &DataEnum) 1 } - fn from_ssz_bytes(bytes: &[u8]) -> std::result::Result { - let byte = bytes + fn from_ssz_bytes(__bytes: &[u8]) -> std::result::Result { + let byte = __bytes .first() .copied() .ok_or(ssz::DecodeError::OutOfBoundsByte { i: 0 })?; @@ -1061,12 +1061,12 @@ fn ssz_decode_derive_enum_union(derive_input: &DeriveInput, enum_data: &DataEnum false } - fn from_ssz_bytes(bytes: &[u8]) -> Result { + fn from_ssz_bytes(__bytes: &[u8]) -> Result { // Sanity check to ensure the definition here does not drift from the one defined in // `ssz`. debug_assert_eq!(#MAX_UNION_SELECTOR, ssz::MAX_UNION_SELECTOR); - let (selector, body) = ssz::split_union_bytes(bytes)?; + let (selector, body) = ssz::split_union_bytes(__bytes)?; match selector.into() { #( @@ -1128,9 +1128,9 @@ fn ssz_decode_derive_enum_transparent( false } - fn from_ssz_bytes(bytes: &[u8]) -> Result { + fn from_ssz_bytes(__bytes: &[u8]) -> Result { #( - if let Ok(var) = <#var_types as ssz::Decode>::from_ssz_bytes(bytes) { + if let Ok(var) = <#var_types as ssz::Decode>::from_ssz_bytes(__bytes) { return Ok(#constructors(var)); } )* From 03f8402a695b047e69089ab484523cb1182fa6ba Mon Sep 17 00:00:00 2001 From: hopinheimer Date: Sat, 31 May 2025 16:21:17 +0530 Subject: [PATCH 2/4] formatting --- ssz/src/decode.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ssz/src/decode.rs b/ssz/src/decode.rs index 97015da..ec81ff0 100644 --- a/ssz/src/decode.rs +++ b/ssz/src/decode.rs @@ -190,13 +190,12 @@ impl<'a> SszDecoderBuilder<'a> { let start = self.items_index; self.items_index += ssz_fixed_len; - let slice = - self.__bytes - .get(start..self.items_index) - .ok_or(DecodeError::InvalidByteLength { - len: self.__bytes.len(), - expected: self.items_index, - })?; + let slice = self.__bytes.get(start..self.items_index).ok_or( + DecodeError::InvalidByteLength { + len: self.__bytes.len(), + expected: self.items_index, + }, + )?; self.items.push(slice); } else { From 4354694edce282bb72d6f995bc88539d922a433c Mon Sep 17 00:00:00 2001 From: hopinheimer Date: Fri, 6 Jun 2025 15:19:20 +0530 Subject: [PATCH 3/4] fix: removing some unwanted renaming of variables and added test --- ssz/src/decode.rs | 42 ++++++------- ssz/src/decode/impls.rs | 120 +++++++++++++++++++------------------- ssz_derive/tests/tests.rs | 16 +++++ 3 files changed, 97 insertions(+), 81 deletions(-) diff --git a/ssz/src/decode.rs b/ssz/src/decode.rs index ec81ff0..a923f63 100644 --- a/ssz/src/decode.rs +++ b/ssz/src/decode.rs @@ -116,7 +116,7 @@ pub trait Decode: Sized { /// /// The supplied bytes must be the exact length required to decode `Self`, excess bytes will /// result in an error. - fn from_ssz_bytes(__bytes: &[u8]) -> Result; + fn from_ssz_bytes(bytes: &[u8]) -> Result; } #[derive(Copy, Clone, Debug)] @@ -132,7 +132,7 @@ pub struct Offset { /// /// See [`SszDecoder`](struct.SszDecoder.html) for usage examples. pub struct SszDecoderBuilder<'a> { - __bytes: &'a [u8], + bytes: &'a [u8], items: SmallVec8<&'a [u8]>, offsets: SmallVec8, items_index: usize, @@ -141,9 +141,9 @@ pub struct SszDecoderBuilder<'a> { impl<'a> SszDecoderBuilder<'a> { /// Instantiate a new builder that should build a `SszDecoder` over the given `bytes` which /// are assumed to be the SSZ encoding of some object. - pub fn new(__bytes: &'a [u8]) -> Self { + pub fn new(bytes: &'a [u8]) -> Self { Self { - __bytes, + bytes, items: smallvec![], offsets: smallvec![], items_index: 0, @@ -190,9 +190,9 @@ impl<'a> SszDecoderBuilder<'a> { let start = self.items_index; self.items_index += ssz_fixed_len; - let slice = self.__bytes.get(start..self.items_index).ok_or( + let slice = self.bytes.get(start..self.items_index).ok_or( DecodeError::InvalidByteLength { - len: self.__bytes.len(), + len: self.bytes.len(), expected: self.items_index, }, )?; @@ -202,9 +202,9 @@ impl<'a> SszDecoderBuilder<'a> { self.offsets.push(Offset { position: self.items.len(), offset: sanitize_offset( - read_offset(&self.__bytes[self.items_index..])?, + read_offset(&self.bytes[self.items_index..])?, self.offsets.last().map(|o| o.offset), - self.__bytes.len(), + self.bytes.len(), None, )?, }); @@ -235,19 +235,19 @@ impl<'a> SszDecoderBuilder<'a> { let a = pair[0]; let b = pair[1]; - self.items[a.position] = &self.__bytes[a.offset..b.offset]; + self.items[a.position] = &self.bytes[a.offset..b.offset]; } // Handle the last offset, pushing a slice from it's start through to the end of // `self.bytes`. if let Some(last) = self.offsets.last() { - self.items[last.position] = &self.__bytes[last.offset..] + self.items[last.position] = &self.bytes[last.offset..] } } else { // If the container is fixed-length, ensure there are no excess bytes. - if self.items_index != self.__bytes.len() { + if self.items_index != self.bytes.len() { return Err(DecodeError::InvalidByteLength { - len: self.__bytes.len(), + len: self.bytes.len(), expected: self.items_index, }); } @@ -335,13 +335,13 @@ impl<'a> SszDecoder<'a> { /// /// - `bytes` is empty. /// - the union selector is not a valid value (i.e., larger than the maximum number of variants. -pub fn split_union_bytes(__bytes: &[u8]) -> Result<(UnionSelector, &[u8]), DecodeError> { - let selector = __bytes +pub fn split_union_bytes(bytes: &[u8]) -> Result<(UnionSelector, &[u8]), DecodeError> { + let selector = bytes .first() .copied() .ok_or(DecodeError::OutOfBoundsByte { i: 0 }) .and_then(UnionSelector::new)?; - let body = __bytes + let body = bytes .get(1..) .ok_or(DecodeError::OutOfBoundsByte { i: 1 })?; Ok((selector, body)) @@ -349,10 +349,10 @@ pub fn split_union_bytes(__bytes: &[u8]) -> Result<(UnionSelector, &[u8]), Decod /// Reads a `BYTES_PER_LENGTH_OFFSET`-byte length from `bytes`, where `bytes.len() >= /// BYTES_PER_LENGTH_OFFSET`. -pub fn read_offset(__bytes: &[u8]) -> Result { - decode_offset(__bytes.get(0..BYTES_PER_LENGTH_OFFSET).ok_or( +pub fn read_offset(bytes: &[u8]) -> Result { + decode_offset(bytes.get(0..BYTES_PER_LENGTH_OFFSET).ok_or( DecodeError::InvalidLengthPrefix { - len: __bytes.len(), + len: bytes.len(), expected: BYTES_PER_LENGTH_OFFSET, }, )?) @@ -360,15 +360,15 @@ pub fn read_offset(__bytes: &[u8]) -> Result { /// Decode bytes as a little-endian usize, returning an `Err` if `bytes.len() != /// BYTES_PER_LENGTH_OFFSET`. -fn decode_offset(__bytes: &[u8]) -> Result { - let len = __bytes.len(); +fn decode_offset(bytes: &[u8]) -> Result { + let len = bytes.len(); let expected = BYTES_PER_LENGTH_OFFSET; if len != expected { Err(DecodeError::InvalidLengthPrefix { len, expected }) } else { let mut array: [u8; BYTES_PER_LENGTH_OFFSET] = std::default::Default::default(); - array.clone_from_slice(__bytes); + array.clone_from_slice(bytes); Ok(u32::from_le_bytes(array) as usize) } diff --git a/ssz/src/decode/impls.rs b/ssz/src/decode/impls.rs index c05ce54..6c9b7b5 100644 --- a/ssz/src/decode/impls.rs +++ b/ssz/src/decode/impls.rs @@ -19,15 +19,15 @@ macro_rules! impl_decodable_for_uint { $bit_size / 8 } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let len = __bytes.len(); + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let len = bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { let mut array: [u8; $bit_size / 8] = std::default::Default::default(); - array.clone_from_slice(__bytes); + array.clone_from_slice(bytes); Ok(Self::from_le_bytes(array)) } @@ -74,8 +74,8 @@ macro_rules! impl_decode_for_tuples { } } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let mut builder = SszDecoderBuilder::new(__bytes); + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let mut builder = SszDecoderBuilder::new(bytes); $( builder.register_type::<$T>()?; @@ -204,19 +204,19 @@ impl Decode for bool { 1 } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let len = __bytes.len(); + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let len = bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - match __bytes[0] { + match bytes[0] { 0b0000_0000 => Ok(false), 0b0000_0001 => Ok(true), _ => Err(DecodeError::BytesInvalid(format!( "Out-of-range for boolean: {}", - __bytes[0] + bytes[0] ))), } } @@ -232,8 +232,8 @@ impl Decode for NonZeroUsize { ::ssz_fixed_len() } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let x = usize::from_ssz_bytes(__bytes)?; + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let x = usize::from_ssz_bytes(bytes)?; if x == 0 { Err(DecodeError::BytesInvalid( @@ -251,8 +251,8 @@ impl Decode for Option { fn is_ssz_fixed_len() -> bool { false } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let (selector, body) = split_union_bytes(__bytes)?; + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let (selector, body) = split_union_bytes(bytes)?; match selector.into() { 0u8 => Ok(None), 1u8 => ::from_ssz_bytes(body).map(Option::Some), @@ -270,8 +270,8 @@ impl Decode for Arc { T::ssz_fixed_len() } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - T::from_ssz_bytes(__bytes).map(Arc::new) + fn from_ssz_bytes(bytes: &[u8]) -> Result { + T::from_ssz_bytes(bytes).map(Arc::new) } } @@ -284,14 +284,14 @@ impl Decode for Address { 20 } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let len = __bytes.len(); + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let len = bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - Ok(Self::from_slice(__bytes)) + Ok(Self::from_slice(bytes)) } } } @@ -305,16 +305,16 @@ impl Decode for FixedBytes { N } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - if __bytes.len() != N { + fn from_ssz_bytes(bytes: &[u8]) -> Result { + if bytes.len() != N { return Err(DecodeError::InvalidByteLength { - len: __bytes.len(), + len: bytes.len(), expected: N, }); } let mut fixed_array = [0u8; N]; - fixed_array.copy_from_slice(__bytes); + fixed_array.copy_from_slice(bytes); Ok(Self(fixed_array)) } @@ -329,14 +329,14 @@ impl Decode for Bloom { 256 } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let len = __bytes.len(); + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let len = bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - Ok(Self::from_slice(__bytes)) + Ok(Self::from_slice(bytes)) } } } @@ -350,14 +350,14 @@ impl Decode for U256 { 32 } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let len = __bytes.len(); + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let len = bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - Ok(U256::from_le_slice(__bytes)) + Ok(U256::from_le_slice(bytes)) } } } @@ -369,8 +369,8 @@ impl Decode for Bytes { } #[inline] - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - Ok(__bytes.to_vec().into()) + fn from_ssz_bytes(bytes: &[u8]) -> Result { + Ok(bytes.to_vec().into()) } } @@ -383,14 +383,14 @@ impl Decode for U128 { 16 } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let len = __bytes.len(); + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let len = bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { - Ok(U128::from_le_slice(__bytes)) + Ok(U128::from_le_slice(bytes)) } } } @@ -404,15 +404,15 @@ impl Decode for [u8; N] { N } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - let len = __bytes.len(); + fn from_ssz_bytes(bytes: &[u8]) -> Result { + let len = bytes.len(); let expected = ::ssz_fixed_len(); if len != expected { Err(DecodeError::InvalidByteLength { len, expected }) } else { let mut array: [u8; N] = [0; N]; - array.copy_from_slice(__bytes); + array.copy_from_slice(bytes); Ok(array) } @@ -424,16 +424,16 @@ impl Decode for Vec { false } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - if __bytes.is_empty() { + fn from_ssz_bytes(bytes: &[u8]) -> Result { + if bytes.is_empty() { Ok(vec![]) } else if T::is_ssz_fixed_len() { - __bytes + bytes .chunks(T::ssz_fixed_len()) .map(T::from_ssz_bytes) .collect() } else { - decode_list_of_variable_length_items(__bytes, None) + decode_list_of_variable_length_items(bytes, None) } } } @@ -443,16 +443,16 @@ impl Decode for SmallVec<[T; N]> { false } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - if __bytes.is_empty() { + fn from_ssz_bytes(bytes: &[u8]) -> Result { + if bytes.is_empty() { Ok(SmallVec::new()) } else if T::is_ssz_fixed_len() { - __bytes + bytes .chunks(T::ssz_fixed_len()) .map(T::from_ssz_bytes) .collect() } else { - decode_list_of_variable_length_items(__bytes, None) + decode_list_of_variable_length_items(bytes, None) } } } @@ -466,16 +466,16 @@ where false } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - if __bytes.is_empty() { + fn from_ssz_bytes(bytes: &[u8]) -> Result { + if bytes.is_empty() { Ok(Self::from_iter(iter::empty())) } else if <(K, V)>::is_ssz_fixed_len() { - __bytes + bytes .chunks(<(K, V)>::ssz_fixed_len()) .map(<(K, V)>::from_ssz_bytes) .collect() } else { - decode_list_of_variable_length_items(__bytes, None) + decode_list_of_variable_length_items(bytes, None) } } } @@ -488,16 +488,16 @@ where false } - fn from_ssz_bytes(__bytes: &[u8]) -> Result { - if __bytes.is_empty() { + fn from_ssz_bytes(bytes: &[u8]) -> Result { + if bytes.is_empty() { Ok(Self::from_iter(iter::empty())) } else if T::is_ssz_fixed_len() { - __bytes + bytes .chunks(T::ssz_fixed_len()) .map(T::from_ssz_bytes) .collect() } else { - decode_list_of_variable_length_items(__bytes, None) + decode_list_of_variable_length_items(bytes, None) } } } @@ -508,17 +508,17 @@ where /// significantly faster as it is optimized to read same-typed items whilst `ssz::SszDecoder` /// supports reading items of differing types. pub fn decode_list_of_variable_length_items>( - __bytes: &[u8], + bytes: &[u8], max_len: Option, ) -> Result { - if __bytes.is_empty() { + if bytes.is_empty() { return Container::try_from_iter(iter::empty()).map_err(|e| { DecodeError::BytesInvalid(format!("Error trying to collect empty list: {:?}", e)) }); } - let first_offset = read_offset(__bytes)?; - sanitize_offset(first_offset, None, __bytes.len(), Some(first_offset))?; + let first_offset = read_offset(bytes)?; + sanitize_offset(first_offset, None, bytes.len(), Some(first_offset))?; if first_offset % BYTES_PER_LENGTH_OFFSET != 0 || first_offset < BYTES_PER_LENGTH_OFFSET { return Err(DecodeError::InvalidListFixedBytesLen(first_offset)); @@ -537,15 +537,15 @@ pub fn decode_list_of_variable_length_items process_results( (1..=num_items).map(|i| { let slice_option = if i == num_items { - __bytes.get(offset..) + bytes.get(offset..) } else { let start = offset; - let next_offset = read_offset(&__bytes[(i * BYTES_PER_LENGTH_OFFSET)..])?; + let next_offset = read_offset(&bytes[(i * BYTES_PER_LENGTH_OFFSET)..])?; offset = - sanitize_offset(next_offset, Some(offset), __bytes.len(), Some(first_offset))?; + sanitize_offset(next_offset, Some(offset), bytes.len(), Some(first_offset))?; - __bytes.get(start..offset) + bytes.get(start..offset) }; let slice = slice_option.ok_or(DecodeError::OutOfBoundsByte { i: offset })?; diff --git a/ssz_derive/tests/tests.rs b/ssz_derive/tests/tests.rs index 8db0381..0abc1b0 100644 --- a/ssz_derive/tests/tests.rs +++ b/ssz_derive/tests/tests.rs @@ -257,3 +257,19 @@ fn transparent_struct_newtype_skipped_field_reverse() { &vec![42_u8].as_ssz_bytes(), ); } + +#[derive(PartialEq, Debug, Encode, Decode)] +struct StructWithMoreThanOneFieldAndFirstFieldAsBytes{ + bytes: Vec, + length: u8, +} + +#[test] +fn struct_with_more_than_one_field_and_first_field_as_bytes() { + let test_struct = StructWithMoreThanOneFieldAndFirstFieldAsBytes { + bytes: vec![42_u8], + length: 7, + }; + + assert_encode_decode(&test_struct, &[5, 0, 0, 0, 7, 42]); +} From 9cfe6dd58471d696e0fe75f2780bf1bcd0e2f428 Mon Sep 17 00:00:00 2001 From: hopinheimer Date: Fri, 6 Jun 2025 15:19:55 +0530 Subject: [PATCH 4/4] clippy changes --- ssz/src/decode.rs | 13 +++++++------ ssz_derive/tests/tests.rs | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/ssz/src/decode.rs b/ssz/src/decode.rs index a923f63..814aa70 100644 --- a/ssz/src/decode.rs +++ b/ssz/src/decode.rs @@ -190,12 +190,13 @@ impl<'a> SszDecoderBuilder<'a> { let start = self.items_index; self.items_index += ssz_fixed_len; - let slice = self.bytes.get(start..self.items_index).ok_or( - DecodeError::InvalidByteLength { - len: self.bytes.len(), - expected: self.items_index, - }, - )?; + let slice = + self.bytes + .get(start..self.items_index) + .ok_or(DecodeError::InvalidByteLength { + len: self.bytes.len(), + expected: self.items_index, + })?; self.items.push(slice); } else { diff --git a/ssz_derive/tests/tests.rs b/ssz_derive/tests/tests.rs index 0abc1b0..2d5561e 100644 --- a/ssz_derive/tests/tests.rs +++ b/ssz_derive/tests/tests.rs @@ -259,7 +259,7 @@ fn transparent_struct_newtype_skipped_field_reverse() { } #[derive(PartialEq, Debug, Encode, Decode)] -struct StructWithMoreThanOneFieldAndFirstFieldAsBytes{ +struct StructWithMoreThanOneFieldAndFirstFieldAsBytes { bytes: Vec, length: u8, }