Skip to content

Commit

Permalink
update alloy-primitives package (#26)
Browse files Browse the repository at this point in the history
add generic  alloy types encode/decode
  • Loading branch information
eserilev authored Aug 2, 2024
1 parent 0c88b6c commit 5331ee4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
36 changes: 26 additions & 10 deletions 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, B256, U128, U256};
use alloy_primitives::{Address, Bytes, FixedBytes, U128, U256};
use core::num::NonZeroUsize;
use itertools::process_results;
use smallvec::SmallVec;
Expand Down Expand Up @@ -296,24 +296,27 @@ impl Decode for Address {
}
}

impl Decode for B256 {
impl<const N: usize> Decode for FixedBytes<N> {
fn is_ssz_fixed_len() -> bool {
true
}

fn ssz_fixed_len() -> usize {
32
N
}

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(B256::from_slice(bytes))
if bytes.len() != N {
return Err(DecodeError::InvalidByteLength {
len: bytes.len(),
expected: N,
});
}

let mut fixed_array = [0u8; N];
fixed_array.copy_from_slice(bytes);

Ok(Self(fixed_array))
}
}

Expand All @@ -338,6 +341,18 @@ impl Decode for U256 {
}
}

impl Decode for Bytes {
#[inline]
fn is_ssz_fixed_len() -> bool {
false
}

#[inline]
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
Ok(bytes.to_vec().into())
}
}

impl Decode for U128 {
fn is_ssz_fixed_len() -> bool {
true
Expand Down Expand Up @@ -527,6 +542,7 @@ pub fn decode_list_of_variable_length_items<T: Decode, Container: TryFromIter<T>
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::B256;

// Note: decoding of valid bytes is generally tested "indirectly" in the `/tests` dir, by
// encoding then decoding the element.
Expand Down
42 changes: 37 additions & 5 deletions 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, B256, U128, U256};
use alloy_primitives::{Address, Bytes, FixedBytes, U128, U256};
use core::num::NonZeroUsize;
use smallvec::SmallVec;
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -427,21 +427,52 @@ impl Encode for Address {
}
}

impl Encode for B256 {
impl<const N: usize> Encode for FixedBytes<N> {
#[inline]
fn is_ssz_fixed_len() -> bool {
true
}

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

#[inline]
fn ssz_fixed_len() -> usize {
32
N
}

#[inline]
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(&self.0);
}

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

impl Encode for Bytes {
#[inline]
fn is_ssz_fixed_len() -> bool {
false
}

#[inline]
fn ssz_bytes_len(&self) -> usize {
32
self.0.len()
}

#[inline]
fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(self.as_slice());
buf.extend_from_slice(&self.0);
}

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

Expand Down Expand Up @@ -510,6 +541,7 @@ impl_encodable_for_u8_array!(48);
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::B256;

#[test]
fn vec_of_u8() {
Expand Down

0 comments on commit 5331ee4

Please sign in to comment.