Add TreeHash trait to all types and structs

This commit is contained in:
Kirk Baird
2019-01-25 12:22:56 +11:00
parent 407bf5e06d
commit 9c9b07c182
31 changed files with 642 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::{AggregatePublicKey, Signature};
use bls_aggregates::AggregateSignature as RawAggregateSignature;
@@ -44,6 +44,12 @@ impl Decodable for AggregateSignature {
}
}
impl TreeHash for AggregateSignature {
fn hash_tree_root(&self) -> Vec<u8> {
hash(&self.0.as_bytes())
}
}
#[cfg(test)]
mod tests {
use super::super::ssz::ssz_encode;

View File

@@ -1,7 +1,9 @@
use super::SecretKey;
use bls_aggregates::PublicKey as RawPublicKey;
use hex::encode as hex_encode;
use ssz::{decode_ssz_list, ssz_encode, Decodable, DecodeError, Encodable, SszStream};
use ssz::{
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
};
use std::default;
use std::hash::{Hash, Hasher};
@@ -53,6 +55,12 @@ impl Decodable for PublicKey {
}
}
impl TreeHash for PublicKey {
fn hash_tree_root(&self) -> Vec<u8> {
hash(&self.0.as_bytes())
}
}
impl PartialEq for PublicKey {
fn eq(&self, other: &PublicKey) -> bool {
ssz_encode(self) == ssz_encode(other)

View File

@@ -1,5 +1,5 @@
use bls_aggregates::{DecodeError as BlsDecodeError, SecretKey as RawSecretKey};
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream, TreeHash};
/// A single BLS signature.
///
@@ -40,6 +40,12 @@ impl Decodable for SecretKey {
}
}
impl TreeHash for SecretKey {
fn hash_tree_root(&self) -> Vec<u8> {
self.0.as_bytes().clone()
}
}
#[cfg(test)]
mod tests {
use super::super::ssz::ssz_encode;

View File

@@ -1,4 +1,4 @@
use super::ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream};
use super::ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use super::{PublicKey, SecretKey};
use bls_aggregates::Signature as RawSignature;
@@ -57,6 +57,12 @@ impl Decodable for Signature {
}
}
impl TreeHash for Signature {
fn hash_tree_root(&self) -> Vec<u8> {
hash(&self.0.as_bytes())
}
}
#[cfg(test)]
mod tests {
use super::super::ssz::ssz_encode;

View File

@@ -149,6 +149,12 @@ impl ssz::Decodable for BooleanBitfield {
}
}
impl ssz::TreeHash for BooleanBitfield {
fn hash_tree_root(&self) -> Vec<u8> {
self.to_bytes().hash_tree_root()
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -1,5 +1,5 @@
use super::ethereum_types::{Address, H256};
use super::{merkle_hash, ssz_encode, TreeHash};
use super::{hash, merkle_hash, ssz_encode, TreeHash};
impl TreeHash for u8 {
fn hash_tree_root(&self) -> Vec<u8> {
@@ -25,6 +25,12 @@ impl TreeHash for u64 {
}
}
impl TreeHash for usize {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for Address {
fn hash_tree_root(&self) -> Vec<u8> {
ssz_encode(self)
@@ -37,6 +43,15 @@ impl TreeHash for H256 {
}
}
impl TreeHash for [u8] {
fn hash_tree_root(&self) -> Vec<u8> {
if self.len() > 32 {
return hash(&self);
}
self.to_vec()
}
}
impl<T> TreeHash for Vec<T>
where
T: TreeHash,

View File

@@ -23,7 +23,7 @@ pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
}
mhash.append(&mut datalen.to_vec());
hash(mhash.as_slice())
hash(&mhash)
}
/// Takes a flat vector of bytes. It then hashes 'chunk_size * 2' slices into
@@ -36,7 +36,7 @@ fn hash_level(data: &mut Vec<u8>, chunk_size: usize) -> Vec<u8> {
// SSZ_CHUNK_SIZE vector
let mut c = two_chunks.to_vec();
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
result.append(&mut hash(c.as_slice()));
result.append(&mut hash(&c));
} else {
// Hash two chuncks together
result.append(&mut hash(two_chunks));