You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.2 KiB

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