Skip to content

Commit

Permalink
Merge branch 'main' into akash/process_operations
Browse files Browse the repository at this point in the history
  • Loading branch information
h3lio5 authored Nov 29, 2023
2 parents 9f800cf + f91c0ec commit ed40cde
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 7 additions & 7 deletions native/ssz_nif/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ where
Elx: Decoder<'a>,
Ssz: TreeHash + FromElx<Elx>,
{
let list_size = list.len();
let root = hash_vector_tree_root::<'a, Elx, Ssz>((list, max_size))?;
let bytes = tree_hash::mix_in_length(&Hash256::from(root), max_size).0;
let bytes = tree_hash::mix_in_length(&Hash256::from(root), list_size).0;
Ok(bytes)
}

Expand All @@ -95,16 +96,15 @@ where
Ok(vec_tree_hash_root(&x, max_size))
}

/// A helper function providing common functionality between the `TreeHash` implementations for
/// `FixedVector` and `VariableList`.
pub fn vec_tree_hash_root<T>(vec: &[T], size: usize) -> [u8; 32]
/// Taken from `ssz_types` and modified to take `max_size` as dynamic parameter.
pub fn vec_tree_hash_root<T>(vec: &[T], max_size: usize) -> [u8; 32]
where
T: TreeHash,
{
let root = match T::tree_hash_type() {
TreeHashType::Basic => {
let mut hasher: MerkleHasher = MerkleHasher::with_leaves(
(size + T::tree_hash_packing_factor() - 1) / T::tree_hash_packing_factor(),
let mut hasher = MerkleHasher::with_leaves(
(max_size + T::tree_hash_packing_factor() - 1) / T::tree_hash_packing_factor(),
);

for item in vec {
Expand All @@ -118,7 +118,7 @@ where
.expect("ssz_types variable vec should not have a remaining buffer")
}
TreeHashType::Container | TreeHashType::List | TreeHashType::Vector => {
let mut hasher = MerkleHasher::with_leaves(size);
let mut hasher = MerkleHasher::with_leaves(max_size);

for item in vec {
hasher
Expand Down
9 changes: 8 additions & 1 deletion test/unit/ssz_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,17 @@ defmodule Unit.SSZTests do
Enum.join(deserialized)
])

assert serialized ==
Base.decode16!(
"0C000000140000001F000000617366617366617331383431383238303139327A6439673861733066373061307366"
)

assert {:ok, ^serialized} = Ssz.to_ssz_typed(deserialized, SszTypes.Transaction)
assert {:ok, ^deserialized} = Ssz.list_from_ssz(serialized, SszTypes.Transaction)

assert {:ok, _hash} =
hash = Base.decode16!("D5ACD42F851C9AE241B55AB79B23D7EC613E01BB6404B4A49D8CF214DBA26CF2")

assert {:ok, ^hash} =
Ssz.hash_list_tree_root_typed(deserialized, 1_048_576, SszTypes.Transaction)
end

Expand Down

0 comments on commit ed40cde

Please sign in to comment.