Skip to content

Commit

Permalink
Switch to alloy (#21)
Browse files Browse the repository at this point in the history
* chore(deps): change ethereum-types to alloy-primitives

Update Cargo.toml

* update alloy-primitives package (#24)

* update alloy-primitives package (#26)

add generic  alloy types encode/decode

* Add U128 tests

* U256 tests

* Bytes tests

---------

Co-authored-by: Kolby Moroz Liebl <[email protected]>
Co-authored-by: Eitan Seri-Levi <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2024
1 parent 0b538a4 commit 37a99a0
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 55 deletions.
5 changes: 3 additions & 2 deletions ssz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ categories = ["cryptography::cryptocurrencies"]
name = "ssz"

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

[dependencies]
ethereum-types = "0.14.1"
alloy-primitives = "0.7.7"
smallvec = { version = "1.6.1", features = ["const_generics"] }
itertools = "0.13.0"

[features]
arbitrary = ["ethereum-types/arbitrary"]
arbitrary = ["alloy-primitives/arbitrary"]
48 changes: 32 additions & 16 deletions ssz/src/decode/impls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::decode::try_from_iter::{TryCollect, TryFromIter};
use alloy_primitives::{Address, Bytes, FixedBytes, U128, U256};
use core::num::NonZeroUsize;
use ethereum_types::{H160, H256, U128, U256};
use itertools::process_results;
use smallvec::SmallVec;
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -275,7 +275,7 @@ impl<T: Decode> Decode for Arc<T> {
}
}

impl Decode for H160 {
impl Decode for Address {
fn is_ssz_fixed_len() -> bool {
true
}
Expand All @@ -296,24 +296,27 @@ impl Decode for H160 {
}
}

impl Decode for H256 {
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(H256::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 @@ -333,11 +336,23 @@ impl Decode for U256 {
if len != expected {
Err(DecodeError::InvalidByteLength { len, expected })
} else {
Ok(U256::from_little_endian(bytes))
Ok(U256::from_le_slice(bytes))
}
}
}

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 All @@ -354,7 +369,7 @@ impl Decode for U128 {
if len != expected {
Err(DecodeError::InvalidByteLength { len, expected })
} else {
Ok(U128::from_little_endian(bytes))
Ok(U128::from_le_slice(bytes))
}
}
}
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 Expand Up @@ -576,17 +592,17 @@ mod tests {
}

#[test]
fn invalid_h256() {
fn invalid_b256() {
assert_eq!(
H256::from_ssz_bytes(&[0; 33]),
B256::from_ssz_bytes(&[0; 33]),
Err(DecodeError::InvalidByteLength {
len: 33,
expected: 32
})
);

assert_eq!(
H256::from_ssz_bytes(&[0; 31]),
B256::from_ssz_bytes(&[0; 31]),
Err(DecodeError::InvalidByteLength {
len: 31,
expected: 32
Expand Down
66 changes: 45 additions & 21 deletions ssz/src/encode/impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use alloy_primitives::{Address, Bytes, FixedBytes, U128, U256};
use core::num::NonZeroUsize;
use ethereum_types::{H160, H256, U128, U256};
use smallvec::SmallVec;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
Expand Down Expand Up @@ -409,7 +409,7 @@ impl Encode for NonZeroUsize {
}
}

impl Encode for H160 {
impl Encode for Address {
fn is_ssz_fixed_len() -> bool {
true
}
Expand All @@ -423,25 +423,56 @@ impl Encode for H160 {
}

fn ssz_append(&self, buf: &mut Vec<u8>) {
buf.extend_from_slice(self.as_bytes());
buf.extend_from_slice(self.as_slice());
}
}

impl Encode for H256 {
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_bytes());
buf.extend_from_slice(&self.0);
}

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

Expand All @@ -459,11 +490,7 @@ impl Encode for U256 {
}

fn ssz_append(&self, buf: &mut Vec<u8>) {
let n = <Self as Encode>::ssz_fixed_len();
let s = buf.len();

buf.resize(s + n, 0);
self.to_little_endian(&mut buf[s..]);
buf.extend_from_slice(self.as_le_slice());
}
}

Expand All @@ -481,11 +508,7 @@ impl Encode for U128 {
}

fn ssz_append(&self, buf: &mut Vec<u8>) {
let n = <Self as Encode>::ssz_fixed_len();
let s = buf.len();

buf.resize(s + n, 0);
self.to_little_endian(&mut buf[s..]);
buf.extend_from_slice(self.as_le_slice());
}
}

Expand Down Expand Up @@ -518,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 Expand Up @@ -619,16 +643,16 @@ mod tests {
}

#[test]
fn ssz_encode_h256() {
assert_eq!(H256::from(&[0; 32]).as_ssz_bytes(), vec![0; 32]);
assert_eq!(H256::from(&[1; 32]).as_ssz_bytes(), vec![1; 32]);
fn ssz_encode_b256() {
assert_eq!(B256::from(&[0; 32]).as_ssz_bytes(), vec![0; 32]);
assert_eq!(B256::from(&[1; 32]).as_ssz_bytes(), vec![1; 32]);

let bytes = vec![
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
];

assert_eq!(H256::from_slice(&bytes).as_ssz_bytes(), bytes);
assert_eq!(B256::from_slice(&bytes).as_ssz_bytes(), bytes);
}

#[test]
Expand Down
Loading

0 comments on commit 37a99a0

Please sign in to comment.