diff --git a/ssz/src/decode/impls.rs b/ssz/src/decode/impls.rs index 9b57745..00c547f 100644 --- a/ssz/src/decode/impls.rs +++ b/ssz/src/decode/impls.rs @@ -1,6 +1,6 @@ use super::*; use crate::decode::try_from_iter::{TryCollect, TryFromIter}; -use alloy_primitives::{Address, Bytes, FixedBytes, U128, U256}; +use alloy_primitives::{Address, Bloom, Bytes, FixedBytes, U128, U256}; use core::num::NonZeroUsize; use itertools::process_results; use smallvec::SmallVec; @@ -320,6 +320,27 @@ impl Decode for FixedBytes { } } +impl Decode for Bloom { + fn is_ssz_fixed_len() -> bool { + true + } + + fn ssz_fixed_len() -> usize { + 256 + } + + 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)) + } + } +} + impl Decode for U256 { fn is_ssz_fixed_len() -> bool { true diff --git a/ssz/src/encode/impls.rs b/ssz/src/encode/impls.rs index c691164..d779e64 100644 --- a/ssz/src/encode/impls.rs +++ b/ssz/src/encode/impls.rs @@ -1,5 +1,5 @@ use super::*; -use alloy_primitives::{Address, Bytes, FixedBytes, U128, U256}; +use alloy_primitives::{Address, Bloom, Bytes, FixedBytes, U128, U256}; use core::num::NonZeroUsize; use smallvec::SmallVec; use std::collections::{BTreeMap, BTreeSet}; @@ -454,6 +454,33 @@ impl Encode for FixedBytes { } } +impl Encode for Bloom { + #[inline] + fn is_ssz_fixed_len() -> bool { + true + } + + #[inline] + fn ssz_bytes_len(&self) -> usize { + 256 + } + + #[inline] + fn ssz_fixed_len() -> usize { + 256 + } + + #[inline] + fn ssz_append(&self, buf: &mut Vec) { + buf.extend_from_slice(&self.0 .0); + } + + #[inline] + fn as_ssz_bytes(&self) -> Vec { + self.0.to_vec() + } +} + impl Encode for Bytes { #[inline] fn is_ssz_fixed_len() -> bool { diff --git a/ssz/tests/tests.rs b/ssz/tests/tests.rs index ec5b979..139ad39 100644 --- a/ssz/tests/tests.rs +++ b/ssz/tests/tests.rs @@ -1,4 +1,4 @@ -use alloy_primitives::{Address, Bytes, B256, U128, U256}; +use alloy_primitives::{Address, Bloom, Bytes, B256, U128, U256}; use ssz::{Decode, DecodeError, Encode}; use ssz_derive::{Decode, Encode}; use std::num::NonZeroUsize; @@ -540,6 +540,26 @@ mod round_trip { let data = vec![(48u8, Some(0u64)), (0u8, None), (u8::MAX, Some(u64::MAX))]; round_trip(data); } + + #[test] + fn bloom() { + let data = vec![ + Bloom::ZERO, + Bloom::with_last_byte(5), + Bloom::repeat_byte(73), + ]; + round_trip(data); + } + + #[test] + fn vec_bloom() { + let data = vec![ + vec![Bloom::ZERO, Bloom::ZERO, Bloom::with_last_byte(5)], + vec![], + vec![Bloom::repeat_byte(73), Bloom::repeat_byte(72)], + ]; + round_trip(data); + } } /// Decode tests that are expected to fail. @@ -560,7 +580,13 @@ mod decode_fail { #[test] fn hash256() { - let long_bytes = vec![0xff; 257]; + let long_bytes = vec![0xff; 33]; assert!(B256::from_ssz_bytes(&long_bytes).is_err()); } + + #[test] + fn bloom() { + let long_bytes = vec![0xff; 257]; + assert!(Bloom::from_ssz_bytes(&long_bytes).is_err()); + } }