Skip to content

Commit

Permalink
integrate alloy-primitives into milhouse (#42)
Browse files Browse the repository at this point in the history
* integrate alloy-primitives

* resolve merge conflicts

* update

* Use released crates & update tests

* Replace from_slice -> from

---------

Co-authored-by: Michael Sproul <michael@sigmaprime.io>
  • Loading branch information
eserilev and michaelsproul authored Aug 20, 2024
1 parent e838fd8 commit 99c05e8
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 33 deletions.
15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@ categories = ["data-structures", "cryptography::cryptocurrencies", ]
[dependencies]
derivative = "2.2.0"
ethereum_hashing = "0.7.0"
ethereum_ssz = "0.5.0"
ethereum_ssz_derive = "0.5.0"
itertools = "0.10.3"
ethereum_ssz = "0.6.0"
ethereum_ssz_derive = "0.6.0"
itertools = "0.13.0"
parking_lot = "0.12.1"
rayon = "1.5.1"
serde = { version = "1.0.0", features = ["derive"] }
tree_hash = "0.6.0"
tree_hash = "0.7.0"
triomphe = "0.1.5"
typenum = "1.14.0"
vec_map = "0.8.2"
smallvec = "1.8.0"
arbitrary = { version = "1.2.3", features = ["derive"] }
ethereum-types = { version = "0.14.1", features = ["arbitrary"] }
alloy-primitives = { version = "0.7.7", features = ["arbitrary"] }


[dev-dependencies]
ssz_types = "0.6.0"
ssz_types = "0.7.0"
proptest = "1.0.0"
tree_hash_derive = "0.6.0"
tree_hash_derive = "0.7.0"
criterion = "0.5"

[features]
Expand Down
2 changes: 1 addition & 1 deletion src/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where

impl<T> Leaf<T> {
pub fn new(value: T) -> Self {
Self::with_hash(value, Hash256::zero())
Self::with_hash(value, Hash256::ZERO)
}

pub fn with_hash(value: T, hash: Hash256) -> Self {
Expand Down
12 changes: 6 additions & 6 deletions src/packed_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<T: TreeHash + Clone> PackedLeaf<T> {
return hash;
}

let hash_bytes = hash.as_bytes_mut();
let hash_bytes = hash.as_mut_slice();

let value_len = BYTES_PER_CHUNK / T::tree_hash_packing_factor();
for (i, value) in self.values.iter().enumerate() {
Expand All @@ -50,7 +50,7 @@ impl<T: TreeHash + Clone> PackedLeaf<T> {

pub fn empty() -> Self {
PackedLeaf {
hash: RwLock::new(Hash256::zero()),
hash: RwLock::new(Hash256::ZERO),
values: Vec::with_capacity(T::tree_hash_packing_factor()),
}
}
Expand All @@ -60,22 +60,22 @@ impl<T: TreeHash + Clone> PackedLeaf<T> {
values.push(value);

PackedLeaf {
hash: RwLock::new(Hash256::zero()),
hash: RwLock::new(Hash256::ZERO),
values,
}
}

pub fn repeat(value: T, n: usize) -> Self {
assert!(n <= T::tree_hash_packing_factor());
PackedLeaf {
hash: RwLock::new(Hash256::zero()),
hash: RwLock::new(Hash256::ZERO),
values: vec![value; n],
}
}

pub fn insert_at_index(&self, index: usize, value: T) -> Result<Self, Error> {
let mut updated = PackedLeaf {
hash: RwLock::new(Hash256::zero()),
hash: RwLock::new(Hash256::ZERO),
values: self.values.clone(),
};
let sub_index = index % T::tree_hash_packing_factor();
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<T: TreeHash + Clone> PackedLeaf<T> {

pub fn insert_mut(&mut self, sub_index: usize, value: T) -> Result<(), Error> {
// Ensure hash is 0.
*self.hash.get_mut() = Hash256::zero();
*self.hash.get_mut() = Hash256::ZERO;

if sub_index == self.values.len() {
self.values.push(value);
Expand Down
18 changes: 9 additions & 9 deletions src/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,54 +45,54 @@ where
let new_layer = match &layer[..] {
[(repeat_leaf, 1)] => {
smallvec![(
Tree::node(repeat_leaf.clone(), Tree::zero(depth), Hash256::zero()),
Tree::node(repeat_leaf.clone(), Tree::zero(depth), Hash256::ZERO),
1,
)]
}
[(repeat_leaf, repeat_count)] if repeat_count % 2 == 0 => {
smallvec![(
Tree::node(repeat_leaf.clone(), repeat_leaf.clone(), Hash256::zero()),
Tree::node(repeat_leaf.clone(), repeat_leaf.clone(), Hash256::ZERO),
repeat_count / 2,
)]
}
[(repeat_leaf, repeat_count)] => {
smallvec![
(
Tree::node(repeat_leaf.clone(), repeat_leaf.clone(), Hash256::zero()),
Tree::node(repeat_leaf.clone(), repeat_leaf.clone(), Hash256::ZERO),
repeat_count / 2,
),
(
Tree::node(repeat_leaf.clone(), Tree::zero(depth), Hash256::zero()),
Tree::node(repeat_leaf.clone(), Tree::zero(depth), Hash256::ZERO),
1,
),
]
}
[(repeat_leaf, 1), (lonely_leaf, 1)] => {
smallvec![(
Tree::node(repeat_leaf.clone(), lonely_leaf.clone(), Hash256::zero()),
Tree::node(repeat_leaf.clone(), lonely_leaf.clone(), Hash256::ZERO),
1,
)]
}
[(repeat_leaf, repeat_count), (lonely_leaf, 1)] => {
if repeat_count % 2 == 0 {
smallvec![
(
Tree::node(repeat_leaf.clone(), repeat_leaf.clone(), Hash256::zero()),
Tree::node(repeat_leaf.clone(), repeat_leaf.clone(), Hash256::ZERO),
repeat_count / 2,
),
(
Tree::node(lonely_leaf.clone(), Tree::zero(depth), Hash256::zero()),
Tree::node(lonely_leaf.clone(), Tree::zero(depth), Hash256::ZERO),
1,
),
]
} else {
smallvec![
(
Tree::node(repeat_leaf.clone(), repeat_leaf.clone(), Hash256::zero()),
Tree::node(repeat_leaf.clone(), repeat_leaf.clone(), Hash256::ZERO),
repeat_count / 2,
),
(
Tree::node(repeat_leaf.clone(), lonely_leaf.clone(), Hash256::zero()),
Tree::node(repeat_leaf.clone(), lonely_leaf.clone(), Hash256::ZERO),
1,
),
]
Expand Down
2 changes: 1 addition & 1 deletion src/tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn build_partial_hash256_list() {
type N = U16;
let n = N::to_usize();
let vec = (0..n as u64)
.map(Hash256::from_low_u64_be)
.map(|n| Hash256::right_padding_from(&n.to_le_bytes()))
.collect::<Vec<_>>();

for k in 0..n {
Expand Down
12 changes: 8 additions & 4 deletions src/tests/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use typenum::{Unsigned, U64};
fn hash256_vec_iter() {
type N = U64;
let n = N::to_u64();
let vec = (0..n).map(Hash256::from_low_u64_be).collect::<Vec<_>>();
let vec = (0..n)
.map(|n| Hash256::right_padding_from(&n.to_le_bytes()))
.collect::<Vec<_>>();
let vector = Vector::<Hash256, N>::new(vec.clone()).unwrap();

assert_eq!(vector.iter().cloned().collect::<Vec<_>>(), vec);
Expand All @@ -16,7 +18,9 @@ fn hash256_vec_iter() {
fn hash256_list_iter() {
type N = U64;
let n = N::to_u64();
let vec = (0..n).map(Hash256::from_low_u64_be).collect::<Vec<_>>();
let vec = (0..n)
.map(|n| Hash256::right_padding_from(&n.to_le_bytes()))
.collect::<Vec<_>>();
let list = List::<Hash256, N>::new(vec.clone()).unwrap();

assert_eq!(list.iter().cloned().collect::<Vec<_>>(), vec);
Expand All @@ -27,7 +31,7 @@ fn hash256_list_iter_from() {
type N = U64;
let n = N::to_usize();
let vec = (0..n as u64)
.map(Hash256::from_low_u64_be)
.map(|n| Hash256::right_padding_from(&n.to_le_bytes()))
.collect::<Vec<_>>();
let list = List::<Hash256, N>::new(vec.clone()).unwrap();

Expand All @@ -52,7 +56,7 @@ fn hash256_vector_iter_from() {
type N = U64;
let n = N::to_usize();
let vec = (0..n as u64)
.map(Hash256::from_low_u64_be)
.map(|n| Hash256::right_padding_from(&n.to_le_bytes()))
.collect::<Vec<_>>();
let vect = Vector::<Hash256, N>::new(vec.clone()).unwrap();

Expand Down
10 changes: 5 additions & 5 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<T: Value> Tree<T> {

pub fn node_unboxed(left: Arc<Self>, right: Arc<Self>) -> Self {
Self::Node {
hash: RwLock::new(Hash256::zero()),
hash: RwLock::new(Hash256::ZERO),
left,
right,
}
Expand Down Expand Up @@ -124,14 +124,14 @@ impl<T: Value> Tree<T> {
Ok(Self::node(
left.with_updated_leaf(index, new_value, new_depth)?,
right.clone(),
Hash256::zero(),
Hash256::ZERO,
))
} else {
// Index lies on the right, recurse right
Ok(Self::node(
left.clone(),
right.with_updated_leaf(index, new_value, new_depth)?,
Hash256::zero(),
Hash256::ZERO,
))
}
}
Expand All @@ -146,7 +146,7 @@ impl<T: Value> Tree<T> {
// Split zero node into a node with left and right, and recurse into
// the appropriate subtree
let new_zero = Self::zero(depth - 1);
Self::node(new_zero.clone(), new_zero, Hash256::zero())
Self::node(new_zero.clone(), new_zero, Hash256::ZERO)
.with_updated_leaf(index, new_value, depth)
}
}
Expand Down Expand Up @@ -442,7 +442,7 @@ impl<T: Value + Send + Sync> Tree<T> {
let (left_hash, right_hash) =
rayon::join(|| left.tree_hash(), || right.tree_hash());
let tree_hash =
Hash256::from(hash32_concat(left_hash.as_bytes(), right_hash.as_bytes()));
Hash256::from(hash32_concat(left_hash.as_slice(), right_hash.as_slice()));
*hash.write() = tree_hash;
tree_hash
}
Expand Down

0 comments on commit 99c05e8

Please sign in to comment.