Skip to content

Commit

Permalink
chore(deps): change ethereum-types to alloy-primitives
Browse files Browse the repository at this point in the history
Update Cargo.toml
  • Loading branch information
KolbyML authored and michaelsproul committed Jul 1, 2024
1 parent 4ebca2f commit 6baa121
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 39 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.0", features = ["getrandom"] }
ethereum_ssz_derive = { version = "0.5.4", path = "../ssz_derive" }

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

[features]
arbitrary = ["ethereum-types/arbitrary"]
arbitrary = ["alloy-primitives/arbitrary"]
18 changes: 9 additions & 9 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, B256, 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,7 +296,7 @@ impl Decode for H160 {
}
}

impl Decode for H256 {
impl Decode for B256 {
fn is_ssz_fixed_len() -> bool {
true
}
Expand All @@ -312,7 +312,7 @@ impl Decode for H256 {
if len != expected {
Err(DecodeError::InvalidByteLength { len, expected })
} else {
Ok(H256::from_slice(bytes))
Ok(B256::from_slice(bytes))
}
}
}
Expand All @@ -333,7 +333,7 @@ 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))
}
}
}
Expand All @@ -354,7 +354,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 @@ -576,17 +576,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
30 changes: 11 additions & 19 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, B256, 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,11 +423,11 @@ 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 Encode for B256 {
fn is_ssz_fixed_len() -> bool {
true
}
Expand All @@ -441,7 +441,7 @@ impl Encode for H256 {
}

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

Expand All @@ -459,11 +459,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 +477,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 @@ -619,16 +611,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
19 changes: 10 additions & 9 deletions ssz/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use ethereum_types::H256;
use ssz::{Decode, DecodeError, Encode};
use ssz_derive::{Decode, Encode};

mod round_trip {
use alloy_primitives::B256;

use super::*;
use std::collections::BTreeMap;
use std::iter::FromIterator;
Expand Down Expand Up @@ -37,28 +38,28 @@ mod round_trip {
}

#[test]
fn h256() {
let items: Vec<H256> = vec![H256::zero(), H256::from([1; 32]), H256::random()];
fn b256() {
let items: Vec<B256> = vec![B256::ZERO, B256::from([1; 32]), B256::random()];

round_trip(items);
}

#[test]
fn vec_of_h256() {
let items: Vec<Vec<H256>> = vec![
fn vec_of_b256() {
let items: Vec<Vec<B256>> = vec![
vec![],
vec![H256::zero(), H256::from([1; 32]), H256::random()],
vec![B256::ZERO, B256::from([1; 32]), B256::random()],
];

round_trip(items);
}

#[test]
fn option_vec_h256() {
let items: Vec<Option<Vec<H256>>> = vec![
fn option_vec_b256() {
let items: Vec<Option<Vec<B256>>> = vec![
None,
Some(vec![]),
Some(vec![H256::zero(), H256::from([1; 32]), H256::random()]),
Some(vec![B256::ZERO, B256::from([1; 32]), B256::random()]),
];

round_trip(items);
Expand Down

0 comments on commit 6baa121

Please sign in to comment.