Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl Encode Decode for alloy_primitves::Bloom #30

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Encode for Bloom {
#[inline]
fn is_ssz_fixed_len() -> bool {
true

Check warning on line 460 in ssz/src/encode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/encode/impls.rs#L459-L460

Added lines #L459 - L460 were not covered by tests
}

#[inline]
fn ssz_bytes_len(&self) -> usize {
256

Check warning on line 465 in ssz/src/encode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/encode/impls.rs#L465

Added line #L465 was not covered by tests
}

#[inline]
fn ssz_fixed_len() -> usize {
256

Check warning on line 470 in ssz/src/encode/impls.rs

View check run for this annotation

Codecov / codecov/patch

ssz/src/encode/impls.rs#L469-L470

Added lines #L469 - L470 were not covered by tests
}

#[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());
}
}
Loading