lighthouse-pulse/eth2/utils/cached_tree_hash
2019-05-15 11:48:09 +10:00
..
benches Add benches, examples for cached hashing. 2019-04-28 11:38:32 +10:00
examples Tidy and add docs for cached tree hash 2019-04-29 17:46:01 +10:00
src spec: ensure types up to date with v0.6.1 2019-05-15 11:48:09 +10:00
tests Tidy and add docs for cached tree hash 2019-04-29 17:46:01 +10:00
Cargo.toml Fix clippy lints, impl treehash for slices 2019-04-29 15:32:41 +10:00
README.md Create cached_tree_hash crate. 2019-04-26 09:55:03 +10:00

Tree hashing

Provides both cached and non-cached tree hashing methods.

Standard Tree Hash

use tree_hash_derive::TreeHash;

#[derive(TreeHash)]
struct Foo {
	a: u64,
	b: Vec<u64>,
}

fn main() {
	let foo = Foo {
		a: 42,
		b: vec![1, 2, 3]
	};

	println!("root: {}", foo.tree_hash_root());
}

Cached Tree Hash

use tree_hash_derive::{TreeHash, CachedTreeHash};

#[derive(TreeHash, CachedTreeHash)]
struct Foo {
	a: u64,
	b: Vec<u64>,
}

#[derive(TreeHash, CachedTreeHash)]
struct Bar {
	a: Vec<Foo>,
	b: u64,
}

fn main() {
	let bar = Bar {
		a: vec![
			Foo {
				a: 42,
				b: vec![1, 2, 3]
			}
		],
		b: 42
	};

	let modified_bar = Bar {
		a: vec![
			Foo {
				a: 100,
				b: vec![1, 2, 3, 4, 5, 6]
			},
			Foo {
				a: 42,
				b: vec![]
			}
		],
		b: 99
	};


    let mut hasher = CachedTreeHasher::new(&bar).unwrap();
	hasher.update(&modified_bar).unwrap();

	// Assert that the cached tree hash matches a standard tree hash.
	assert_eq!(hasher.tree_hash_root(), modified_bar.tree_hash_root());
}