Remove SSZ dep from tree_hash

This commit is contained in:
Paul Hauner 2019-04-17 11:18:00 +10:00
parent af39f096e7
commit 10eeced227
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
5 changed files with 24 additions and 33 deletions

View File

@ -8,4 +8,3 @@ edition = "2018"
ethereum-types = "0.5" ethereum-types = "0.5"
hashing = { path = "../hashing" } hashing = { path = "../hashing" }
int_to_bytes = { path = "../int_to_bytes" } int_to_bytes = { path = "../int_to_bytes" }
ssz = { path = "../ssz" }

View File

@ -1,13 +1,12 @@
use super::resize::{grow_merkle_cache, shrink_merkle_cache}; use super::resize::{grow_merkle_cache, shrink_merkle_cache};
use super::*; use super::*;
use ssz::ssz_encode;
mod vec; mod vec;
impl CachedTreeHashSubTree<u64> for u64 { impl CachedTreeHashSubTree<u64> for u64 {
fn new_tree_hash_cache(&self) -> Result<TreeHashCache, Error> { fn new_tree_hash_cache(&self) -> Result<TreeHashCache, Error> {
Ok(TreeHashCache::from_bytes( Ok(TreeHashCache::from_bytes(
merkleize(ssz_encode(self)), merkleize(self.to_le_bytes().to_vec()),
false, false,
)?) )?)
} }
@ -23,7 +22,7 @@ impl CachedTreeHashSubTree<u64> for u64 {
chunk: usize, chunk: usize,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
if self != other { if self != other {
let leaf = merkleize(ssz_encode(self)); let leaf = merkleize(self.to_le_bytes().to_vec());
cache.modify_chunk(chunk, &leaf)?; cache.modify_chunk(chunk, &leaf)?;
} }

View File

@ -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<u8> {
unreachable!("Vector should never be packed.")
}
fn tree_hash_packing_factor() -> usize {
unreachable!("Vector should never be packed.")
}
fn tree_hash_root(&self) -> Vec<u8> {
tree_hash::standard_tree_hash::vec_tree_hash_root(self)
}
}
};
}

View File

@ -1,7 +1,6 @@
use super::*; use super::*;
use hashing::hash; use hashing::hash;
use int_to_bytes::int_to_bytes32; use int_to_bytes::int_to_bytes32;
use ssz::ssz_encode;
pub use impls::vec_tree_hash_root; pub use impls::vec_tree_hash_root;

View File

@ -9,7 +9,7 @@ macro_rules! impl_for_bitsize {
} }
fn tree_hash_packed_encoding(&self) -> Vec<u8> { fn tree_hash_packed_encoding(&self) -> Vec<u8> {
ssz_encode(self) self.to_le_bytes().to_vec()
} }
fn tree_hash_packing_factor() -> usize { fn tree_hash_packing_factor() -> usize {
@ -28,7 +28,24 @@ impl_for_bitsize!(u16, 16);
impl_for_bitsize!(u32, 32); impl_for_bitsize!(u32, 32);
impl_for_bitsize!(u64, 64); impl_for_bitsize!(u64, 64);
impl_for_bitsize!(usize, 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<u8> {
(*self as u8).tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
u8::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> Vec<u8> {
int_to_bytes32(*self as u64)
}
}
impl TreeHash for [u8; 4] { impl TreeHash for [u8; 4] {
fn tree_hash_type() -> TreeHashType { fn tree_hash_type() -> TreeHashType {
@ -44,7 +61,7 @@ impl TreeHash for [u8; 4] {
} }
fn tree_hash_root(&self) -> Vec<u8> { fn tree_hash_root(&self) -> Vec<u8> {
merkle_root(&ssz::ssz_encode(self)) merkle_root(&self[..])
} }
} }
@ -54,7 +71,7 @@ impl TreeHash for H256 {
} }
fn tree_hash_packed_encoding(&self) -> Vec<u8> { fn tree_hash_packed_encoding(&self) -> Vec<u8> {
ssz_encode(self) self.as_bytes().to_vec()
} }
fn tree_hash_packing_factor() -> usize { fn tree_hash_packing_factor() -> usize {
@ -62,7 +79,7 @@ impl TreeHash for H256 {
} }
fn tree_hash_root(&self) -> Vec<u8> { fn tree_hash_root(&self) -> Vec<u8> {
merkle_root(&ssz::ssz_encode(self)) merkle_root(&self.as_bytes().to_vec())
} }
} }