Skip to content

Commit

Permalink
feat: impl Encode Decode for alloy_primitves::Bloom (#30)
Browse files Browse the repository at this point in the history
* feat: impl Encode Decode alloy_primitves::Bloom

* Some basic tests

* Fix long bytes lengths

---------

Co-authored-by: Michael Sproul <michael@sigmaprime.io>
  • Loading branch information
yash-atreya and michaelsproul authored Aug 23, 2024
1 parent c0476cb commit 0d60076
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
23 changes: 22 additions & 1 deletion ssz/src/decode/impls.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -320,6 +320,27 @@ impl<const N: usize> Decode for FixedBytes<N> {
}
}

impl Decode for Bloom {
fn is_ssz_fixed_len() -> bool {
true
}

fn ssz_fixed_len() -> usize {
256
}

fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
let len = bytes.len();
let expected = <Self as Decode>::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
Expand Down
29 changes: 28 additions & 1 deletion ssz/src/encode/impls.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -454,6 +454,33 @@ impl<const N: usize> Encode for FixedBytes<N> {
}
}

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<u8>) {
buf.extend_from_slice(&self.0 .0);
}

#[inline]
fn as_ssz_bytes(&self) -> Vec<u8> {
self.0.to_vec()
}
}

impl Encode for Bytes {
#[inline]
fn is_ssz_fixed_len() -> bool {
Expand Down
30 changes: 28 additions & 2 deletions ssz/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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());
}
}

0 comments on commit 0d60076

Please sign in to comment.