Skip to content

Commit

Permalink
Support tree hash by value
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Sep 13, 2024
1 parent 7758a1e commit 4ad1965
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ itertools = "0.13.0"
[dev-dependencies]
serde_json = "1.0.0"
tree_hash_derive = "0.8.0"

[patch.crates-io]
tree_hash = { git = "https://github.com/sigp/tree_hash", branch = "self-by-value" }
tree_hash_derive = { git = "https://github.com/sigp/tree_hash", branch = "self-by-value" }
12 changes: 6 additions & 6 deletions src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,41 +643,41 @@ impl<'de, N: Unsigned + Clone> Deserialize<'de> for Bitfield<Fixed<N>> {
}
}

impl<N: Unsigned + Clone> tree_hash::TreeHash for Bitfield<Variable<N>> {
impl<N: Unsigned + Clone> tree_hash::TreeHash for &Bitfield<Variable<N>> {
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::List
}

fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
fn tree_hash_packed_encoding(self) -> tree_hash::PackedEncoding {
unreachable!("List should never be packed.")
}

fn tree_hash_packing_factor() -> usize {
unreachable!("List should never be packed.")
}

fn tree_hash_root(&self) -> Hash256 {
fn tree_hash_root(self) -> Hash256 {

Check warning on line 659 in src/bitfield.rs

View check run for this annotation

Codecov / codecov/patch

src/bitfield.rs#L659

Added line #L659 was not covered by tests
// Note: we use `as_slice` because it does _not_ have the length-delimiting bit set (or
// present).
let root = bitfield_bytes_tree_hash_root::<N>(self.as_slice());
tree_hash::mix_in_length(&root, self.len())
}
}

impl<N: Unsigned + Clone> tree_hash::TreeHash for Bitfield<Fixed<N>> {
impl<N: Unsigned + Clone> tree_hash::TreeHash for &Bitfield<Fixed<N>> {
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
fn tree_hash_packed_encoding(self) -> tree_hash::PackedEncoding {
unreachable!("Vector should never be packed.")
}

fn tree_hash_packing_factor() -> usize {
unreachable!("Vector should never be packed.")
}

fn tree_hash_root(&self) -> Hash256 {
fn tree_hash_root(self) -> Hash256 {

Check warning on line 680 in src/bitfield.rs

View check run for this annotation

Codecov / codecov/patch

src/bitfield.rs#L680

Added line #L680 was not covered by tests
bitfield_bytes_tree_hash_root::<N>(self.as_slice())
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/fixed_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,23 @@ impl<T, N: Unsigned> IntoIterator for FixedVector<T, N> {
}
}

impl<T, N: Unsigned> tree_hash::TreeHash for FixedVector<T, N>
impl<'a, T, N: Unsigned> tree_hash::TreeHash for &'a FixedVector<T, N>
where
T: tree_hash::TreeHash,
&'a T: tree_hash::TreeHash,
{
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::Vector
}

fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
fn tree_hash_packed_encoding(self) -> tree_hash::PackedEncoding {
unreachable!("Vector should never be packed.")
}

fn tree_hash_packing_factor() -> usize {
unreachable!("Vector should never be packed.")
}

fn tree_hash_root(&self) -> Hash256 {
fn tree_hash_root(self) -> Hash256 {
vec_tree_hash_root::<T, N>(&self.vec)
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/tree_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ use typenum::Unsigned;

/// A helper function providing common functionality between the `TreeHash` implementations for
/// `FixedVector` and `VariableList`.
pub fn vec_tree_hash_root<T, N>(vec: &[T]) -> Hash256
pub fn vec_tree_hash_root<'a, T: 'a, N>(vec: &'a [T]) -> Hash256
where
T: TreeHash,
&'a T: TreeHash,
N: Unsigned,
{
match T::tree_hash_type() {
match <&T>::tree_hash_type() {
TreeHashType::Basic => {
let mut hasher = MerkleHasher::with_leaves(
(N::to_usize() + T::tree_hash_packing_factor() - 1) / T::tree_hash_packing_factor(),
(N::to_usize() + <&T>::tree_hash_packing_factor() - 1)
/ <&T>::tree_hash_packing_factor(),
);

for item in vec {
Expand Down
8 changes: 4 additions & 4 deletions src/variable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +193,23 @@ impl<T, N: Unsigned> IntoIterator for VariableList<T, N> {
}
}

impl<T, N: Unsigned> tree_hash::TreeHash for VariableList<T, N>
impl<'a, T, N: Unsigned> tree_hash::TreeHash for &'a VariableList<T, N>
where
T: tree_hash::TreeHash,
&'a T: tree_hash::TreeHash,
{
fn tree_hash_type() -> tree_hash::TreeHashType {
tree_hash::TreeHashType::List
}

fn tree_hash_packed_encoding(&self) -> tree_hash::PackedEncoding {
fn tree_hash_packed_encoding(self) -> tree_hash::PackedEncoding {
unreachable!("List should never be packed.")
}

fn tree_hash_packing_factor() -> usize {
unreachable!("List should never be packed.")
}

fn tree_hash_root(&self) -> Hash256 {
fn tree_hash_root(self) -> Hash256 {
let root = vec_tree_hash_root::<T, N>(&self.vec);

tree_hash::mix_in_length(&root, self.len())
Expand Down

0 comments on commit 4ad1965

Please sign in to comment.