2019-02-14 01:09:18 +00:00
|
|
|
use super::ethereum_types::{Address, H256};
|
|
|
|
use super::{merkle_hash, ssz_encode, TreeHash};
|
|
|
|
use hashing::hash;
|
|
|
|
|
|
|
|
impl TreeHash for u8 {
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
2019-02-14 01:09:18 +00:00
|
|
|
ssz_encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeHash for u16 {
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
2019-02-14 01:09:18 +00:00
|
|
|
ssz_encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeHash for u32 {
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
2019-02-14 01:09:18 +00:00
|
|
|
ssz_encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeHash for u64 {
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
2019-02-14 01:09:18 +00:00
|
|
|
ssz_encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeHash for usize {
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
2019-02-14 01:09:18 +00:00
|
|
|
ssz_encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:07:04 +00:00
|
|
|
impl TreeHash for bool {
|
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
|
|
|
ssz_encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 01:09:18 +00:00
|
|
|
impl TreeHash for Address {
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
2019-02-14 01:09:18 +00:00
|
|
|
ssz_encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeHash for H256 {
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
2019-02-14 01:09:18 +00:00
|
|
|
ssz_encode(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TreeHash for [u8] {
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
2019-02-14 01:09:18 +00:00
|
|
|
if self.len() > 32 {
|
|
|
|
return hash(&self);
|
|
|
|
}
|
|
|
|
self.to_vec()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> TreeHash for Vec<T>
|
|
|
|
where
|
|
|
|
T: TreeHash,
|
|
|
|
{
|
2019-02-17 17:30:18 +00:00
|
|
|
/// Returns the merkle_hash of a list of hash_tree_root_internal values created
|
2019-02-14 01:09:18 +00:00
|
|
|
/// from the given list.
|
|
|
|
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
|
|
|
|
/// to be handled properly (i.e. hashed) as byte array.
|
2019-02-17 17:30:18 +00:00
|
|
|
fn hash_tree_root_internal(&self) -> Vec<u8> {
|
|
|
|
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root_internal()).collect();
|
2019-02-14 01:09:18 +00:00
|
|
|
merkle_hash(&mut tree_hashes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_impl_tree_hash_vec() {
|
2019-02-17 17:30:18 +00:00
|
|
|
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root_internal();
|
2019-02-14 01:09:18 +00:00
|
|
|
assert_eq!(result.len(), 32);
|
|
|
|
}
|
|
|
|
}
|