feat(merkle_root): calculate merkle root from leaves
Signed-off-by: Johns Beharry <johns@peakshift.com>
This commit is contained in:
parent
af87fbf203
commit
80ac60cc0e
@ -14,22 +14,13 @@ pub fn merkle_root(values: &[Vec<u8>]) -> Vec<u8> {
|
||||
|
||||
o.append(&mut values.to_vec());
|
||||
|
||||
println!("o {:?}", o);
|
||||
|
||||
for i in (0..values_len).rev() {
|
||||
let mut current_value: Vec<u8> = o[i * 2].clone();
|
||||
current_value.append(&mut o[i * 2 + 1].clone());
|
||||
|
||||
o[i] = hash(¤t_value[..]);
|
||||
println!("i {:?}", i);
|
||||
print!(" ");
|
||||
println!("o[i * 2] {} -- {:?}", i * 2, o[i * 2]);
|
||||
print!(" ");
|
||||
println!("o[i * 2 + 1] {} -- {:?}", i * 2 + 1, o[i * 2 + 1]);
|
||||
}
|
||||
|
||||
// println!("{:?}", o);
|
||||
|
||||
o[1].clone()
|
||||
}
|
||||
|
||||
@ -48,15 +39,12 @@ mod tests {
|
||||
0x28, 0x7b, 0x56, 0xd9, 0x51, 0x7b, 0x9c, 0x94, 0x81, 0x27, 0x31, 0x9a, 0x09, 0xa7,
|
||||
0xa3, 0x6d, 0xea, 0xc8,
|
||||
];
|
||||
|
||||
println!("{:?}", expected);
|
||||
println!("{:?}", output);
|
||||
assert_eq!(expected, output.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_merkle_root() {
|
||||
let mut input = vec![
|
||||
let input = vec![
|
||||
"a".as_bytes().to_vec(),
|
||||
"b".as_bytes().to_vec(),
|
||||
"c".as_bytes().to_vec(),
|
||||
@ -65,39 +53,11 @@ mod tests {
|
||||
|
||||
let output = merkle_root(&input[..]);
|
||||
|
||||
let mut leaf_1_2: Vec<u8> = input[0].clone(); // a
|
||||
leaf_1_2.append(&mut input[1].clone()); // b
|
||||
|
||||
let mut leaf_3_4: Vec<u8> = input[2].clone(); // c
|
||||
leaf_3_4.append(&mut input[3].clone()); // d
|
||||
|
||||
let node_1 = hash(&leaf_1_2[..]);
|
||||
let node_2 = hash(&leaf_3_4[..]);
|
||||
|
||||
let mut root: Vec<u8> = node_1.clone(); // ab
|
||||
root.append(&mut node_2.clone()); // cd
|
||||
|
||||
let mr = hash(&root[..]);
|
||||
|
||||
let expected = &[183, 91, 96, 122, 144, 174, 84, 92, 97, 156, 140, 192, 66, 221, 55, 229, 234, 48, 118, 7, 61, 207, 39, 125, 150, 32, 94, 90, 19, 88, 122, 163];
|
||||
|
||||
print!(" ");
|
||||
print!(" ");
|
||||
print!(" ");
|
||||
print!(" ");
|
||||
print!(" ");
|
||||
print!("origional input");
|
||||
println!("{:?}", input);
|
||||
print!("NODE #1 HASH ");
|
||||
println!("{:?}", node_1);
|
||||
print!("NODE #2 HASH ");
|
||||
println!("{:?}", node_2);
|
||||
print!("ROOT HASH ");
|
||||
println!("{:?}", root);
|
||||
print!(" ");
|
||||
println!("{:?}", expected);
|
||||
|
||||
// merkle root of [[a],[b],[c],[d]]
|
||||
let expected = &[
|
||||
183, 91, 96, 122, 144, 174, 84, 92, 97, 156, 140, 192, 66, 221, 55, 229,
|
||||
234, 48, 118, 7, 61, 207, 39, 125, 150, 32, 94, 90, 19, 88, 122, 163,
|
||||
];
|
||||
assert_eq!(expected, output.as_slice());
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user