1use super::constants;
2use super::utils;
3
4pub struct TreeNode {
5 pub child_l: [u8; 32],
6 pub child_r: [u8; 32],
7}
8
9impl TreeNode {
10 pub fn bytes(&self) -> Vec<u8> {
11 concatenate_arrays(&self.child_l, &self.child_r)
12 }
13 pub fn ht(&self) -> [u8; 32] {
14 utils::hash_vec(self.bytes())
15 }
16}
17
18fn concatenate_arrays<T: Clone>(x: &[T], y: &[T]) -> Vec<T> {
19 let mut concat = x.to_vec();
20 concat.extend_from_slice(y);
21
22 concat
23}
24
25pub fn parse_node_bytes(b: Vec<u8>) -> TreeNode {
26 if b == constants::EMPTYNODEVALUE {
27 let n = TreeNode {
28 child_l: constants::EMPTYNODEVALUE,
29 child_r: constants::EMPTYNODEVALUE,
30 };
31 return n;
32 }
33 let child_l = &b[0..32];
34 let child_r = &b[32..];
35 TreeNode {
36 child_l: *array_ref!(child_l, 0, 32),
37 child_r: *array_ref!(child_r, 0, 32),
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44 use rustc_hex::ToHex;
45
46 #[test]
47 fn test_hash_vec() {
48 let n = TreeNode {
49 child_l: constants::EMPTYNODEVALUE,
50 child_r: constants::EMPTYNODEVALUE,
51 };
52 assert_eq!(
53 "ad3228b676f7d3cd4284a5443f17f1962b36e491b30a40b2405849e597ba5fb5",
54 n.ht().to_hex()
55 )
56 }
57}