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

Add Decode/Encode impls for generic alloy types #26

Merged
merged 1 commit into from
Aug 2, 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
4 changes: 2 additions & 2 deletions ssz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ categories = ["cryptography::cryptocurrencies"]
name = "ssz"

[dev-dependencies]
alloy-primitives = { version = "0.7.0", features = ["getrandom"] }
alloy-primitives = { version = "0.7.7", features = ["getrandom"] }
ethereum_ssz_derive = { version = "0.5.4", path = "../ssz_derive" }

[dependencies]
alloy-primitives = "0.7.0"
alloy-primitives = "0.7.7"
smallvec = { version = "1.6.1", features = ["const_generics"] }
itertools = "0.10.3"

Expand Down
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
Loading