Modify tree_hash
This commit is contained in:
parent
eb3969af98
commit
5a2cf6219e
@ -1,6 +1,5 @@
|
|||||||
use super::ethereum_types::{Address, H256};
|
use super::ethereum_types::{Address, H256};
|
||||||
use super::{hash, merkle_hash, ssz_encode, TreeHash};
|
use super::{hash, merkle_hash, ssz_encode, TreeHash};
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
impl TreeHash for u8 {
|
impl TreeHash for u8 {
|
||||||
fn hash_tree_root(&self) -> Vec<u8> {
|
fn hash_tree_root(&self) -> Vec<u8> {
|
||||||
@ -67,57 +66,13 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K, V> TreeHash for HashMap<K, V>
|
|
||||||
where
|
|
||||||
K: Eq,
|
|
||||||
K: Hash,
|
|
||||||
K: Ord,
|
|
||||||
V: TreeHash,
|
|
||||||
{
|
|
||||||
/// Appends the tree_hash for each value of 'self, sorted by key,
|
|
||||||
/// into a byte array and returns the hash of said byte array
|
|
||||||
fn tree_hash(&self) -> Vec<u8> {
|
|
||||||
let mut items: Vec<_> = self.iter().collect();
|
|
||||||
items.sort_by(|a, b| a.0.cmp(b.0));
|
|
||||||
let mut result = Vec::new();
|
|
||||||
for item in items {
|
|
||||||
result.append(&mut item.1.tree_hash());
|
|
||||||
}
|
|
||||||
|
|
||||||
hash(&result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_impl_tree_hash_vec() {
|
fn test_impl_tree_hash_vec() {
|
||||||
let result = vec![1u32, 2, 3, 4, 5, 6, 7].tree_hash();
|
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root();
|
||||||
assert_eq!(result.len(), 32);
|
assert_eq!(result.len(), 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_impl_tree_hash_hashmap() {
|
|
||||||
let mut map = HashMap::new();
|
|
||||||
map.insert("c", 3);
|
|
||||||
map.insert("b", 2);
|
|
||||||
map.insert("g", 7);
|
|
||||||
map.insert("d", 6);
|
|
||||||
map.insert("e", 4);
|
|
||||||
map.insert("a", 1u32);
|
|
||||||
map.insert("f", 5);
|
|
||||||
let result = map.tree_hash();
|
|
||||||
|
|
||||||
// TODO: create tests that tie-out to an offical result
|
|
||||||
assert_eq!(
|
|
||||||
result,
|
|
||||||
[
|
|
||||||
130, 215, 165, 255, 224, 6, 144, 225, 14, 139, 67, 238, 205, 240, 20, 173, 53, 0,
|
|
||||||
105, 62, 49, 174, 244, 160, 114, 92, 232, 11, 102, 200, 112, 24
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,39 +11,31 @@ pub trait TreeHash {
|
|||||||
/// Note that this will consume 'list'.
|
/// Note that this will consume 'list'.
|
||||||
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
|
||||||
// flatten list
|
// flatten list
|
||||||
let (chunk_size, mut data) = list_to_blob(list);
|
let (chunk_size, mut chunkz) = list_to_blob(list);
|
||||||
|
|
||||||
// get data_len as bytes. It will hashed will the merkle root
|
// get data_len as bytes. It will hashed will the merkle root
|
||||||
let datalen = list.len().to_le_bytes();
|
let datalen = list.len().to_le_bytes();
|
||||||
|
|
||||||
// merklize
|
// Tree-hash
|
||||||
let mut mhash = hash_level(&mut data, chunk_size);
|
while chunkz.len() > HASHSIZE {
|
||||||
while mhash.len() > HASHSIZE {
|
let mut new_chunkz: Vec<u8> = Vec::new();
|
||||||
mhash = hash_level(&mut mhash, HASHSIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
mhash.append(&mut datalen.to_vec());
|
for two_chunks in chunkz.chunks(chunk_size * 2) {
|
||||||
hash(&mhash)
|
if two_chunks.len() == chunk_size {
|
||||||
}
|
// Odd number of chunks
|
||||||
|
let mut c = two_chunks.to_vec();
|
||||||
/// Takes a flat vector of bytes. It then hashes 'chunk_size * 2' slices into
|
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
||||||
/// a byte vector of hashes, divisible by HASHSIZE
|
new_chunkz.append(&mut hash(&c));
|
||||||
fn hash_level(data: &mut Vec<u8>, chunk_size: usize) -> Vec<u8> {
|
} else {
|
||||||
let mut result: Vec<u8> = Vec::new();
|
// Hash two chuncks together
|
||||||
for two_chunks in data.chunks(chunk_size * 2) {
|
new_chunkz.append(&mut hash(two_chunks));
|
||||||
if two_chunks.len() == chunk_size && data.len() > chunk_size {
|
}
|
||||||
// if there is only one chunk here, hash it with a zero-byte
|
|
||||||
// SSZ_CHUNK_SIZE vector
|
|
||||||
let mut c = two_chunks.to_vec();
|
|
||||||
c.append(&mut vec![0; SSZ_CHUNK_SIZE]);
|
|
||||||
result.append(&mut hash(&c));
|
|
||||||
} else {
|
|
||||||
// Hash two chuncks together
|
|
||||||
result.append(&mut hash(two_chunks));
|
|
||||||
}
|
}
|
||||||
|
chunkz = new_chunkz;
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
chunkz.append(&mut datalen.to_vec());
|
||||||
|
hash(&chunkz)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
||||||
@ -68,7 +60,6 @@ fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
|
|||||||
data.append(item);
|
data.append(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(chunk_size, data)
|
(chunk_size, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user