test(hashing/merkle_root): write test for empty vector and odd leaf count

Signed-off-by: Johns Beharry <johns@peakshift.com>
This commit is contained in:
Johns Beharry 2019-03-21 02:51:49 +01:00
parent 8429f3bff1
commit 725401d2e1
No known key found for this signature in database
GPG Key ID: EC114974742E2178

View File

@ -8,11 +8,16 @@ pub fn hash(input: &[u8]) -> Vec<u8> {
result result
} }
// Get merkle root of some hashed values - the input leaf nodes is expected to already be hashed /// Get merkle root of some hashed values - the input leaf nodes is expected to already be hashed
// Outputs a `Vec<u8>` byte array of the merkle root given a set of leaf node values. /// Outputs a `Vec<u8>` byte array of the merkle root given a set of leaf node values.
pub fn merkle_root(values: &[Vec<u8>]) -> Vec<u8> { pub fn merkle_root(values: &[Vec<u8>]) -> Option<Vec<u8>> {
let values_len = values.len(); let values_len = values.len();
// check size of vector > 0 and ^ 2
if values.is_empty() || !values_len.is_power_of_two() {
return None
}
// vector to store hashes // vector to store hashes
// filled with 0 as placeholders // filled with 0 as placeholders
let mut o: Vec<Vec<u8>> = vec![vec![0]; values_len]; let mut o: Vec<Vec<u8>> = vec![vec![0]; values_len];
@ -30,7 +35,7 @@ pub fn merkle_root(values: &[Vec<u8>]) -> Vec<u8> {
} }
// the root hash will be at index 1 // the root hash will be at index 1
o[1].clone() return Some(o[1].clone())
} }
#[cfg(test)] #[cfg(test)]
@ -79,6 +84,22 @@ mod tests {
let expected = hash(&root[..]); let expected = hash(&root[..]);
assert_eq!(&expected[..], output.as_slice()); assert_eq!(&expected[..], output.unwrap().as_slice());
}
#[test]
fn test_empty_input_merkle_root() {
let input = vec![];
let output = merkle_root(&input[..]);
assert_eq!(None, output);
}
#[test]
fn test_odd_leaf_merkle_root() {
let input = vec![
hash("a".as_bytes()),
hash("b".as_bytes()),
hash("a".as_bytes()),
];
let output = merkle_root(&input[..]);
assert_eq!(None, output);
} }
} }