From 3411f54c2e8448af0492bbca5e2ce225e5c2ab16 Mon Sep 17 00:00:00 2001 From: Johns Beharry Date: Fri, 11 Jan 2019 19:25:13 -0400 Subject: [PATCH] feat(hashing): write test for merkle_root #146 Signed-off-by: Johns Beharry --- beacon_chain/utils/hashing/src/merkle_root.rs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 beacon_chain/utils/hashing/src/merkle_root.rs diff --git a/beacon_chain/utils/hashing/src/merkle_root.rs b/beacon_chain/utils/hashing/src/merkle_root.rs new file mode 100644 index 000000000..3251154f0 --- /dev/null +++ b/beacon_chain/utils/hashing/src/merkle_root.rs @@ -0,0 +1,29 @@ +use types::{Hash256} +use hashing::canonical_hash; + +fn merkle_root(values: Vec<>) -> Hash256 { + let o = values.len(); + + for v in &values { + canonical_hash(v.as_bytes()); + } +} + +#[cfg(test)] +mod tests { + #[test] + fn calculate_merkle_root() { + let values = vec!['abc', 'lmn', 'xyz', 'o0o']; + + let test_leaf_1 = canonical_hash(values[0]); + let test_leaf_2 = canonical_hash(values[1]); + let test_leaf_3 = canonical_hash(values[2]); + let test_leaf_4 = canonical_hash(values[3]); + let test_node_1 = canonical_hash(vec![test_leaf_1, test_leaf_2]); + let test_node_2 = canonical_hash(vec![test_leaf_3, test_leaf_4]); + let test_root = canonical_hash(vec![test_node_1, test_node_2]); + + let result = merkle_root(values); + assert_eq!(result, test_root); + } +}