From 10eeced227fe1be279e2ef92dea1ee8a02d86f5c Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 17 Apr 2019 11:18:00 +1000 Subject: [PATCH] Remove SSZ dep from `tree_hash` --- eth2/utils/tree_hash/Cargo.toml | 1 - .../tree_hash/src/cached_tree_hash/impls.rs | 5 ++-- eth2/utils/tree_hash/src/lib.rs | 23 ---------------- .../utils/tree_hash/src/standard_tree_hash.rs | 1 - .../tree_hash/src/standard_tree_hash/impls.rs | 27 +++++++++++++++---- 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/eth2/utils/tree_hash/Cargo.toml b/eth2/utils/tree_hash/Cargo.toml index 243a49446..328d91577 100644 --- a/eth2/utils/tree_hash/Cargo.toml +++ b/eth2/utils/tree_hash/Cargo.toml @@ -8,4 +8,3 @@ edition = "2018" ethereum-types = "0.5" hashing = { path = "../hashing" } int_to_bytes = { path = "../int_to_bytes" } -ssz = { path = "../ssz" } diff --git a/eth2/utils/tree_hash/src/cached_tree_hash/impls.rs b/eth2/utils/tree_hash/src/cached_tree_hash/impls.rs index 62d013881..6500e4eff 100644 --- a/eth2/utils/tree_hash/src/cached_tree_hash/impls.rs +++ b/eth2/utils/tree_hash/src/cached_tree_hash/impls.rs @@ -1,13 +1,12 @@ use super::resize::{grow_merkle_cache, shrink_merkle_cache}; use super::*; -use ssz::ssz_encode; mod vec; impl CachedTreeHashSubTree for u64 { fn new_tree_hash_cache(&self) -> Result { Ok(TreeHashCache::from_bytes( - merkleize(ssz_encode(self)), + merkleize(self.to_le_bytes().to_vec()), false, )?) } @@ -23,7 +22,7 @@ impl CachedTreeHashSubTree for u64 { chunk: usize, ) -> Result { if self != other { - let leaf = merkleize(ssz_encode(self)); + let leaf = merkleize(self.to_le_bytes().to_vec()); cache.modify_chunk(chunk, &leaf)?; } diff --git a/eth2/utils/tree_hash/src/lib.rs b/eth2/utils/tree_hash/src/lib.rs index fe2001002..fd1708a2d 100644 --- a/eth2/utils/tree_hash/src/lib.rs +++ b/eth2/utils/tree_hash/src/lib.rs @@ -49,26 +49,3 @@ macro_rules! impl_tree_hash_for_ssz_bytes { } }; } - -#[macro_export] -macro_rules! impl_vec_as_fixed_len { - ($type: ty) => { - impl tree_hash::TreeHash for $type { - fn tree_hash_type() -> tree_hash::TreeHashType { - tree_hash::TreeHashType::Vector - } - - fn tree_hash_packed_encoding(&self) -> Vec { - unreachable!("Vector should never be packed.") - } - - fn tree_hash_packing_factor() -> usize { - unreachable!("Vector should never be packed.") - } - - fn tree_hash_root(&self) -> Vec { - tree_hash::standard_tree_hash::vec_tree_hash_root(self) - } - } - }; -} diff --git a/eth2/utils/tree_hash/src/standard_tree_hash.rs b/eth2/utils/tree_hash/src/standard_tree_hash.rs index 130c360ed..812a2c352 100644 --- a/eth2/utils/tree_hash/src/standard_tree_hash.rs +++ b/eth2/utils/tree_hash/src/standard_tree_hash.rs @@ -1,7 +1,6 @@ use super::*; use hashing::hash; use int_to_bytes::int_to_bytes32; -use ssz::ssz_encode; pub use impls::vec_tree_hash_root; diff --git a/eth2/utils/tree_hash/src/standard_tree_hash/impls.rs b/eth2/utils/tree_hash/src/standard_tree_hash/impls.rs index c3be8d55b..be6b4ba07 100644 --- a/eth2/utils/tree_hash/src/standard_tree_hash/impls.rs +++ b/eth2/utils/tree_hash/src/standard_tree_hash/impls.rs @@ -9,7 +9,7 @@ macro_rules! impl_for_bitsize { } fn tree_hash_packed_encoding(&self) -> Vec { - ssz_encode(self) + self.to_le_bytes().to_vec() } fn tree_hash_packing_factor() -> usize { @@ -28,7 +28,24 @@ impl_for_bitsize!(u16, 16); impl_for_bitsize!(u32, 32); impl_for_bitsize!(u64, 64); impl_for_bitsize!(usize, 64); -impl_for_bitsize!(bool, 8); + +impl TreeHash for bool { + fn tree_hash_type() -> TreeHashType { + TreeHashType::Basic + } + + fn tree_hash_packed_encoding(&self) -> Vec { + (*self as u8).tree_hash_packed_encoding() + } + + fn tree_hash_packing_factor() -> usize { + u8::tree_hash_packing_factor() + } + + fn tree_hash_root(&self) -> Vec { + int_to_bytes32(*self as u64) + } +} impl TreeHash for [u8; 4] { fn tree_hash_type() -> TreeHashType { @@ -44,7 +61,7 @@ impl TreeHash for [u8; 4] { } fn tree_hash_root(&self) -> Vec { - merkle_root(&ssz::ssz_encode(self)) + merkle_root(&self[..]) } } @@ -54,7 +71,7 @@ impl TreeHash for H256 { } fn tree_hash_packed_encoding(&self) -> Vec { - ssz_encode(self) + self.as_bytes().to_vec() } fn tree_hash_packing_factor() -> usize { @@ -62,7 +79,7 @@ impl TreeHash for H256 { } fn tree_hash_root(&self) -> Vec { - merkle_root(&ssz::ssz_encode(self)) + merkle_root(&self.as_bytes().to_vec()) } }