diff --git a/beacon_chain/utils/ssz/src/impl_decode.rs b/beacon_chain/utils/ssz/src/impl_decode.rs index b86af1365..a6b8843bd 100644 --- a/beacon_chain/utils/ssz/src/impl_decode.rs +++ b/beacon_chain/utils/ssz/src/impl_decode.rs @@ -8,20 +8,20 @@ use super::{ macro_rules! impl_decodable_for_uint { ($type: ident, $bit_size: expr) => { impl Decodable for $type { - fn ssz_decode(bytes: &[u8], index: usize) + fn ssz_decode(bytes: &[u8], start_bytes: usize) -> Result<(Self, usize), DecodeError> { assert!((0 < $bit_size) & ($bit_size <= 64) & ($bit_size % 8 == 0)); let max_bytes = $bit_size / 8; - if bytes.len() >= (index + max_bytes) { - let end_bytes = index + max_bytes; + if bytes.len() >= (start_bytes + max_bytes) { + let end_bytes = start_bytes + max_bytes; let mut result: $type = 0; - for i in index..end_bytes { - let offset = ((index + max_bytes) - i - 1) * 8; - result |= ($type::from(bytes[i])) << offset; - }; + for (index, byte) in bytes.iter().enumerate().take(end_bytes).skip(start_bytes) { + let offset = (end_bytes - index - 1) * 8; + result |= ($type::from(*byte)) << offset; + } Ok((result, end_bytes)) } else { Err(DecodeError::TooShort) diff --git a/beacon_chain/utils/ssz/src/impl_encode.rs b/beacon_chain/utils/ssz/src/impl_encode.rs index 392f8667e..36776e071 100644 --- a/beacon_chain/utils/ssz/src/impl_encode.rs +++ b/beacon_chain/utils/ssz/src/impl_encode.rs @@ -35,7 +35,7 @@ macro_rules! impl_encodable_for_uint { } // Append bytes to the SszStream - s.append_encoded_raw(&mut buf.to_vec()); + s.append_encoded_raw(&buf.to_vec()); } } }