1#[derive(Debug)]
36pub struct Node<T> {
37 key: T,
38 depth: u16,
39 index_len: u16,
40 index: Option<T>,
41 left_child: Option<T>,
42 right_child: Option<T>,
43}
44
45impl<T> Node<T> {
46 pub fn new(
57 key: T,
58 depth: u16,
59 index_len: u16,
60 index: Option<T>,
61 left_child: Option<T>,
62 right_child: Option<T>,
63 ) -> Node<T> {
64 Node::<T> {
65 key: key,
66 depth: depth,
67 index: index,
68 index_len: index_len,
69 left_child: left_child,
70 right_child: right_child,
71 }
72 }
73
74 pub fn is_leaf(&self) -> bool {
77 self.left_child.is_none() && self.right_child.is_none()
78 }
79
80 pub fn is_root(&self) -> bool {
85 !self.is_leaf() && self.depth == 0 && self.index_len == 0
86 }
87
88 pub fn is_intermediate(&self) -> bool {
92 !self.is_leaf() && !self.is_root()
93 }
94
95 pub fn get_key(&self) -> &T {
98 &self.key
99 }
100
101 pub fn get_depth(&self) -> &u16 {
104 &self.depth
105 }
106
107 pub fn set_depth(&mut self, depth: u16) {
110 self.depth = depth;
111 }
112
113 pub fn get_index_length(&self) -> &u16 {
116 &self.index_len
117 }
118
119 pub fn set_index_length(&mut self, index_len: u16) {
122 self.index_len = index_len;
123 }
124
125 pub fn get_index(&self) -> &Option<T> {
128 &self.index
129 }
130
131 pub fn get_left_child(&self) -> &Option<T> {
134 &self.left_child
135 }
136
137 pub fn get_right_child(&self) -> &Option<T> {
140 &self.right_child
141 }
142}
143
144impl<T> From<Node<T>> for (T, Vec<u8>)
145where
146 T: AsRef<[u8]> + Copy,
147{
148 fn from(item: Node<T>) -> Self {
149 let mut bytes: Vec<u8> = vec![];
150 if item.is_root() {
151 bytes.push(0u8);
152 bytes.extend(item.get_left_child().unwrap().as_ref());
153 bytes.extend(item.get_right_child().unwrap().as_ref());
154 } else if item.is_leaf() {
155 bytes.push(1u8);
156 bytes.extend(&u16::to_be_bytes(*item.get_depth()));
157 } else {
158 bytes.push(2u8);
159 bytes.extend(&u16::to_be_bytes(*item.get_depth()));
160 bytes.extend(&u16::to_be_bytes(*item.get_index_length()));
161 bytes.extend(
162 &item.get_index().unwrap().as_ref()
163 [0..((*item.get_depth() + *item.get_index_length() - 1) / 8 + 1) as usize],
164 );
165 bytes.extend(item.get_left_child().unwrap().as_ref());
166 bytes.extend(item.get_right_child().unwrap().as_ref());
167 }
168 (*item.get_key(), bytes)
169 }
170}
171
172pub use crate::error::SmtError;
173use std::convert::TryFrom;
174use std::convert::TryInto;
175
176impl<T> TryFrom<(T, Vec<u8>)> for Node<T>
177where
178 T: AsRef<[u8]>
179 + Copy
180 + std::convert::TryFrom<Vec<u8>>
181 + std::convert::Into<Vec<u8>>
182 + std::fmt::Debug,
183 T::Error: std::fmt::Debug,
184{
185 type Error = SmtError;
186 fn try_from(mut item: (T, Vec<u8>)) -> Result<Self, Self::Error> {
187 let t_len = (item.0.into() as Vec<u8>).len();
188 let mut second_element = item.1.split_off(1);
189 match item.1[0] {
190 0u8 => {
191 if second_element.len() < t_len {
192 return Err(SmtError::CorruptedData {
193 key: format!("{:#?}", item.0),
194 condition: String::from("Vector not long enough to obtain left child."),
195 });
196 }
197 let right = second_element.split_off(t_len);
198 let left_key = second_element.try_into(); let right_key = right.try_into();
200 if right_key.is_err() {
201 return Err(SmtError::CorruptedData {
202 key: format!("{:#?}", item.0),
203 condition: String::from("Vector not long enough to obtain right child."),
204 });
205 }
206 Ok(Self::new(
207 item.0,
208 0,
209 0,
210 None,
211 Some(left_key.unwrap()),
212 Some(right_key.unwrap()),
213 ))
214 }
215 1u8 => {
216 if second_element.len() < 2 {
217 return Err(SmtError::CorruptedData {
218 key: format!("{:#?}", item.0),
219 condition: String::from(
220 "Not enough bytes to obtain the depth (at least 3).",
221 ),
222 });
223 }
224 let depth = u16::from_be_bytes(second_element[0..2].try_into().unwrap());
225 Ok(Self::new(
226 item.0,
227 depth,
228 t_len as u16 * 8 - depth,
229 Some(item.0),
230 None,
231 None,
232 ))
233 }
234 2u8 => {
235 if second_element.len() < 2 {
236 return Err(SmtError::CorruptedData {
237 key: format!("{:#?}", item.0),
238 condition: String::from(
239 "Not enough bytes to obtain the depth (at least 3).",
240 ),
241 });
242 }
243 let depth = u16::from_be_bytes(second_element[0..2].try_into().unwrap());
244 if second_element.len() < 4 {
245 return Err(SmtError::CorruptedData {
246 key: format!("{:#?}", item.0),
247 condition: String::from(
248 "Not enough bytes to obtain the index length (at least 5).",
249 ),
250 });
251 }
252 let index_len = u16::from_be_bytes(second_element[2..4].try_into().unwrap());
253 let first_index_byte: usize = 0;
254 let last_index_byte: usize = ((depth + index_len - 1) / 8).into();
255 let bytes_len: usize = last_index_byte - first_index_byte + 1;
256 second_element.drain(0..4);
257 if bytes_len > second_element.len() {
258 return Err(SmtError::CorruptedData {
259 key: format!("{:#?}", item.0),
260 condition: String::from("Not enough bytes to obtain the index."),
261 });
262 }
263 let mut left = second_element.split_off(bytes_len);
264 if t_len > left.len() {
265 return Err(SmtError::CorruptedData {
266 key: format!("{:#?}", item.0),
267 condition: String::from("Not enough bytes to obtain the left key."),
268 });
269 }
270 let right = left.split_off(t_len);
271 if t_len > right.len() {
272 return Err(SmtError::CorruptedData {
273 key: format!("{:#?}", item.0),
274 condition: String::from("Not enough bytes to obtain the right key."),
275 });
276 }
277 second_element.append(&mut vec![0u8; t_len - bytes_len]);
278 Ok(Self::new(
279 item.0,
280 depth as u16,
281 index_len as u16,
282 Some(second_element.try_into().unwrap()),
283 Some(left.try_into().unwrap()),
284 Some(right.try_into().unwrap()),
285 ))
286 }
287 _ => Err(SmtError::CorruptedData {
288 key: format!("{:#?}", item.0),
289 condition: String::from(
290 "Malformed node (node value does not start with 0, 1 or 2).",
291 ),
292 }),
293 }
294 }
295}
296
297#[cfg(test)]
298mod tests {
299 use super::*;
300
301 #[test]
302 fn test_new() {
303 let node: Node<[u8; 32]> = Node::new(
304 [1u8; 32],
305 3,
306 10,
307 Some([2u8; 32]),
308 Some([2u8; 32]),
309 Some([3u8; 32]),
310 );
311 assert_eq!(
312 node.get_key(),
313 &[1; 32],
314 "The root new node is not the one expected."
315 );
316 assert_eq!(
317 node.get_depth(),
318 &3,
319 "The index_lenght wasn't correctly assigned."
320 );
321 assert_eq!(
322 node.get_index_length(),
323 &10,
324 "The index_lenght wasn't correctly assigned."
325 );
326 assert_eq!(
327 node.get_index(),
328 &Some([2u8; 32]),
329 "The index wasn't correctly assigned."
330 );
331 assert_eq!(
332 node.get_left_child().unwrap(),
333 [2u8; 32],
334 "The left child wasn't correctly assigned."
335 );
336 assert_eq!(
337 node.get_right_child().unwrap(),
338 [3u8; 32],
339 "The right child wasn't correctly assigned."
340 );
341 }
342
343 #[test]
344 fn test_new_empty() {
345 let node: Node<[u8; 32]> = Node::new([1; 32], 3, 10, None, None, None);
346 assert_eq!(
347 node.get_key(),
348 &[1u8; 32],
349 "The root new node is not the one expected."
350 );
351 assert_eq!(
352 node.get_depth(),
353 &3,
354 "The index_lenght wasn't correctly assigned."
355 );
356 assert_eq!(
357 node.get_index_length(),
358 &10,
359 "The index_lenght wasn't correctly assigned."
360 );
361 assert_eq!(
362 node.get_index(),
363 &None,
364 "The index wasn't correctly assigned."
365 );
366 assert_eq!(
367 node.get_left_child(),
368 &None,
369 "The left child wasn't correctly assigned."
370 );
371 assert_eq!(
372 node.get_right_child(),
373 &None,
374 "The right child wasn't correctly assigned."
375 );
376 }
377
378 #[test]
379 fn test_get_key() {
380 let node: Node<[u8; 32]> = Node::new(
381 [1; 32],
382 3,
383 10,
384 Some([2u8; 32]),
385 Some([2u8; 32]),
386 Some([3u8; 32]),
387 );
388 assert_eq!(
389 node.get_key(),
390 &[1; 32],
391 "The root new node is not the one expected."
392 );
393 }
394
395 #[test]
396 fn test_get_key_empty() {
397 let node: Node<[u8; 32]> = Node::new([1; 32], 3, 10, None, None, None);
398 assert_eq!(
399 node.get_key(),
400 &[1; 32],
401 "The root new node is not the one expected."
402 );
403 }
404
405 #[test]
406 fn test_get_index_length() {
407 let node: Node<[u8; 32]> = Node::new(
408 [1; 32],
409 3,
410 10,
411 Some([2u8; 32]),
412 Some([2u8; 32]),
413 Some([3u8; 32]),
414 );
415 assert_eq!(
416 node.get_index_length(),
417 &10,
418 "The correct index_length value wasn't returned."
419 );
420 }
421
422 #[test]
423 fn test_get_index_length_empty() {
424 let node: Node<[u8; 32]> = Node::new([1; 32], 3, 252, None, None, None);
425 assert_eq!(
426 node.get_index_length(),
427 &252,
428 "The correct key value wasn't returned."
429 );
430 }
431
432 #[test]
433 fn test_get_index() {
434 let node: Node<[u8; 32]> = Node::new(
435 [1; 32],
436 3,
437 10,
438 Some([2u8; 32]),
439 Some([2u8; 32]),
440 Some([3u8; 32]),
441 );
442 assert_eq!(
443 node.get_index(),
444 &Some([2u8; 32]),
445 "The correct index_lenght value wasn't returned."
446 );
447 }
448
449 #[test]
450 fn test_get_index_empty() {
451 let node: Node<[u8; 32]> = Node::new([1; 32], 3, 252, None, None, None);
452 assert_eq!(
453 node.get_index(),
454 &None,
455 "The correct key value wasn't returned."
456 );
457 }
458
459 #[test]
460 fn test_get_left_child_some() {
461 let node: Node<[u8; 32]> = Node::new(
462 [1; 32],
463 3,
464 10,
465 Some([2u8; 32]),
466 Some([2u8; 32]),
467 Some([3u8; 32]),
468 );
469 assert_eq!(
470 node.get_left_child().unwrap(),
471 [2u8; 32],
472 "The correct left child value wasn't returned."
473 )
474 }
475
476 #[test]
477 fn test_get_left_child_none() {
478 let node: Node<[u8; 32]> =
479 Node::new([1; 32], 3, 10, Some([2u8; 32]), None, Some([3u8; 32]));
480 assert_eq!(
481 node.get_left_child(),
482 &None,
483 "The correct left child value wasn't returned."
484 )
485 }
486
487 #[test]
488 fn test_get_right_child_some() {
489 let node: Node<[u8; 32]> = Node::new(
490 [1; 32],
491 3,
492 10,
493 Some([2u8; 32]),
494 Some([2u8; 32]),
495 Some([3u8; 32]),
496 );
497 assert_eq!(
498 node.get_right_child().unwrap(),
499 [3u8; 32],
500 "The correct right child value wasn't returned."
501 )
502 }
503
504 #[test]
505 fn test_get_right_child_none() {
506 let node: Node<[u8; 32]> =
507 Node::new([1; 32], 3, 10, Some([2u8; 32]), Some([2u8; 32]), None);
508 assert_eq!(
509 node.get_right_child(),
510 &None,
511 "The correct right child value wasn't returned."
512 )
513 }
514
515 #[test]
516 fn test_is_leaf() {
517 let node: Node<[u8; 32]> = Node::new([1; 32], 3, 10, None, None, None);
518 assert_eq!(
519 node.is_leaf(),
520 true,
521 "The return was false although it is a leaf."
522 )
523 }
524
525 #[test]
526 fn test_is_intermediate() {
527 let node: Node<[u8; 32]> = Node::new(
528 [1; 32],
529 3,
530 10,
531 Some([2u8; 32]),
532 Some([2u8; 32]),
533 Some([3u8; 32]),
534 );
535 assert!(
536 !node.is_leaf(),
537 "The return was true although it is a full node."
538 );
539 assert!(
540 node.is_intermediate(),
541 "The return was true although it is a full node."
542 )
543 }
544
545 #[test]
546 fn test_is_root() {
547 let node: Node<[u8; 32]> = Node::new([1; 32], 0, 0, None, Some([2u8; 32]), Some([3u8; 32]));
548 assert_eq!(
549 node.is_root(),
550 true,
551 "The return was true although it is a full node."
552 )
553 }
554
555 #[test]
556 fn test_is_not_root_node1() {
557 let node: Node<[u8; 32]> = Node::new(
558 [1; 32],
559 3,
560 10,
561 Some([2u8; 32]),
562 Some([2u8; 32]),
563 Some([3u8; 32]),
564 );
565 assert_eq!(
566 node.is_root(),
567 false,
568 "The return was true although it is a full node."
569 )
570 }
571
572 #[test]
573 fn test_is_not_root_node2() {
574 let node: Node<[u8; 32]> = Node::new([1; 32], 3, 10, None, None, None);
575 assert_eq!(
576 node.is_root(),
577 false,
578 "The return was true although it is a full node."
579 )
580 }
581
582 #[test]
583 fn test_is_not_root_leaf() {
584 let node: Node<[u8; 32]> = Node::new(
585 [1; 32],
586 3,
587 10,
588 Some([2u8; 32]),
589 Some([2u8; 32]),
590 Some([3u8; 32]),
591 );
592 assert_eq!(
593 node.is_root(),
594 false,
595 "The return was true although it is a full node."
596 )
597 }
598
599 #[test]
600 fn from_root_to_bytes() {
601 let node: Node<[u8; 32]> = Node::new([1; 32], 0, 0, None, Some([1u8; 32]), Some([2u8; 32]));
602 let (key, result): ([u8; 32], Vec<u8>) = node.into();
603 let value: Vec<u8> = vec![
604 0u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
605 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
606 2, 2, 2, 2, 2, 2, 2, 2,
607 ];
608 assert_eq!(
609 [1; 32], key,
610 "The value of the root should be vec![0u8, [1;32], [2;32]]"
611 );
612 assert_eq!(
613 value, result,
614 "The value of the root should be vec![0u8, [1;32], [2;32]]"
615 );
616 }
617
618 #[test]
619 fn from_leaf_to_bytes() {
620 let node: Node<[u8; 32]> = Node::new([1; 32], 2, 10, None, None, None);
621 let (key, result): ([u8; 32], Vec<u8>) = node.into();
622 let value: Vec<u8> = vec![1u8, 0, 2];
623 assert_eq!(
624 [1; 32], key,
625 "The value of the root should be vec![0u8, [1;32], [2;32]]"
626 );
627 assert_eq!(
628 value, result,
629 "The value of the root should be vec![0u8, [1;32], [2;32]]"
630 );
631 }
632
633 #[test]
634 fn from_intermediate_to_bytes() {
635 let node: Node<[u8; 32]> = Node::new(
636 [1; 32],
637 8,
638 8,
639 Some([2u8; 32]),
640 Some([1u8; 32]),
641 Some([2u8; 32]),
642 );
643 let (key, result): ([u8; 32], Vec<u8>) = node.into();
644 let value: Vec<u8> = vec![
645 2u8, 0, 8, 0, 8, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
646 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
647 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
648 ];
649 assert_eq!(
650 key, [1; 32],
651 "The value of the root should be vec![0u8, [1;32], [2;32]]"
652 );
653 assert_eq!(
654 value, result,
655 "The value of the root should be vec![0u8, [1;32], [2;32]]"
656 );
657 }
658
659 #[test]
660 fn from_bytes_to_root() {
661 let node: Node<[u8; 32]> = Node::new([1; 32], 0, 0, None, Some([1u8; 32]), Some([2u8; 32]));
662 let key = [1u8; 32];
663 let value: Vec<u8> = vec![
664 0u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
665 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
666 2, 2, 2, 2, 2, 2, 2, 2,
667 ];
668 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
669
670 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
671 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
672 assert_eq!(
673 root.get_depth(),
674 node.get_depth(),
675 "The depth is not correct"
676 );
677 assert_eq!(
678 root.get_index_length(),
679 node.get_index_length(),
680 "The index length is not correct"
681 );
682 assert_eq!(
683 root.get_index(),
684 node.get_index(),
685 "The index length is not correct"
686 );
687 assert_eq!(
688 root.get_left_child(),
689 node.get_left_child(),
690 "The left child is not correct"
691 );
692 assert_eq!(
693 root.get_right_child(),
694 node.get_right_child(),
695 "The right child is not correct"
696 );
697 }
698
699 #[test]
700 fn from_bytes_to_root_not_enough_for_left() {
701 let key = [1u8; 32];
702 let value: Vec<u8> = vec![
703 0u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
704 1, 1, 1, 1,
705 ];
706 assert_eq!(
707 Node::<[u8; 32]>::try_from((key, value)).err(),
708 Some(SmtError::CorruptedData {
709 key: format!("{:#?}", key),
710 condition: String::from("Vector not long enough to obtain left child.")
711 }),
712 "Error messages do not match"
713 );
714 }
715
716 #[test]
717 fn from_bytes_to_root_not_enough_for_right() {
718 let key = [1u8; 32];
719 let value: Vec<u8> = vec![
720 0u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
721 1, 1, 1, 1, 1, 2,
722 ];
723 assert_eq!(
724 Node::<[u8; 32]>::try_from((key, value)).err(),
725 Some(SmtError::CorruptedData {
726 key: format!("{:#?}", key),
727 condition: String::from("Vector not long enough to obtain right child.")
728 }),
729 "Error messages do not match"
730 );
731 }
732
733 #[test]
734 fn from_bytes_to_leaf() {
735 let node: Node<[u8; 32]> = Node::new([1; 32], 2, 254, Some([1; 32]), None, None);
736 let key = [1; 32];
737 let value: Vec<u8> = vec![1u8, 0, 2];
738 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
739
740 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
741 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
742 assert_eq!(
743 root.get_depth(),
744 node.get_depth(),
745 "The depth is not correct"
746 );
747 assert_eq!(
748 root.get_index_length(),
749 node.get_index_length(),
750 "The index length is not correct"
751 );
752 assert_eq!(
753 root.get_index(),
754 node.get_index(),
755 "The index length is not correct"
756 );
757 assert_eq!(
758 root.get_left_child(),
759 node.get_left_child(),
760 "The left child is not correct"
761 );
762 assert_eq!(
763 root.get_right_child(),
764 node.get_right_child(),
765 "The right child is not correct"
766 );
767 }
768
769 #[test]
770 fn from_bytes_to_leaf_not_enough_for_depth() {
771 let key = [1; 32];
772 let value: Vec<u8> = vec![1u8, 0];
773 assert_eq!(
774 Node::<[u8; 32]>::try_from((key, value)).err(),
775 Some(SmtError::CorruptedData {
776 key: format!("{:#?}", key),
777 condition: String::from("Not enough bytes to obtain the depth (at least 3).")
778 }),
779 "Error messages do not match"
780 );
781 }
782
783 #[test]
784 fn from_bytes_to_intermediate_1byte_mod0() {
785 let node: Node<[u8; 32]> = Node::new(
786 [1; 32],
787 8,
788 8,
789 Some([2u8; 32]),
790 Some([1u8; 32]),
791 Some([2u8; 32]),
792 );
793 let key = [1; 32];
794 let value: Vec<u8> = vec![
795 2u8, 0, 8, 0, 8, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
796 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
797 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
798 ];
799 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
800 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
801 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
802 assert_eq!(
803 root.get_depth(),
804 node.get_depth(),
805 "The depth is not correct"
806 );
807 assert_eq!(
808 root.get_index_length(),
809 node.get_index_length(),
810 "The index length is not correct"
811 );
812 assert_eq!(
813 root.get_index().unwrap()[0..2],
814 node.get_index().unwrap()[0..2],
815 "The index length is not correct"
816 );
817 assert_eq!(
818 root.get_left_child(),
819 node.get_left_child(),
820 "The left child is not correct"
821 );
822 assert_eq!(
823 root.get_right_child(),
824 node.get_right_child(),
825 "The right child is not correct"
826 );
827 }
828
829 #[test]
830 fn from_bytes_to_intermediate_1bytes_mod1() {
831 let node: Node<[u8; 32]> =
832 Node::new([3; 32], 7, 8, Some([64; 32]), Some([1; 32]), Some([2; 32]));
833 let key = [3; 32];
834 let mut value: Vec<u8> = vec![2u8, 0, 7, 0, 8, 64, 64];
835 value.extend([1u8; 32].iter().clone());
836 value.extend([2u8; 32].iter().clone());
837 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
838
839 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
840 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
841 assert_eq!(
842 root.get_depth(),
843 node.get_depth(),
844 "The depth is not correct"
845 );
846 assert_eq!(
847 root.get_index_length(),
848 node.get_index_length(),
849 "The index length is not correct"
850 );
851 assert_eq!(
852 root.get_index().unwrap()[0..2],
853 node.get_index().unwrap()[0..2],
854 "The index is not correct"
855 );
856 assert_eq!(
857 root.get_left_child(),
858 node.get_left_child(),
859 "The left child is not correct"
860 );
861 assert_eq!(
862 root.get_right_child(),
863 node.get_right_child(),
864 "The right child is not correct"
865 );
866 }
867
868 #[test]
869 fn from_bytes_to_intermediate_1bytes_mod4() {
870 let node: Node<[u8; 32]> =
871 Node::new([3; 32], 12, 8, Some([2; 32]), Some([1; 32]), Some([2; 32]));
872 let key = [3; 32];
873 let mut value: Vec<u8> = vec![2u8, 0, 12, 0, 8, 2, 2, 2];
874 value.extend([1u8; 32].iter().clone());
875 value.extend([2u8; 32].iter().clone());
876 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
877
878 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
879 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
880 assert_eq!(
881 root.get_depth(),
882 node.get_depth(),
883 "The depth is not correct"
884 );
885 assert_eq!(
886 root.get_index_length(),
887 node.get_index_length(),
888 "The index length is not correct"
889 );
890 assert_eq!(
891 root.get_index().unwrap()[0..3],
892 node.get_index().unwrap()[0..3],
893 "The index is not correct"
894 );
895 assert_eq!(
896 root.get_left_child(),
897 node.get_left_child(),
898 "The left child is not correct"
899 );
900 assert_eq!(
901 root.get_right_child(),
902 node.get_right_child(),
903 "The right child is not correct"
904 );
905 }
906
907 #[test]
908 fn from_value_to_intermediate_multiple_bytes_mod0() {
909 let node: Node<[u8; 32]> =
910 Node::new([3; 32], 8, 20, Some([2; 32]), Some([1; 32]), Some([2; 32]));
911 let key = [3; 32];
912 let mut value: Vec<u8> = vec![2u8, 0, 8, 0, 20, 2, 2, 2, 2];
913 value.extend([1u8; 32].iter().clone());
914 value.extend([2u8; 32].iter().clone());
915 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
916
917 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
918 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
919 assert_eq!(
920 root.get_depth(),
921 node.get_depth(),
922 "The depth is not correct"
923 );
924 assert_eq!(
925 root.get_index_length(),
926 node.get_index_length(),
927 "The index length is not correct"
928 );
929 assert_eq!(
930 root.get_index().unwrap()[0..4],
931 node.get_index().unwrap()[0..4],
932 "The index length is not correct"
933 );
934 assert_eq!(
935 root.get_left_child(),
936 node.get_left_child(),
937 "The left child is not correct"
938 );
939 assert_eq!(
940 root.get_right_child(),
941 node.get_right_child(),
942 "The right child is not correct"
943 );
944 }
945
946 #[test]
947 fn from_value_to_intermediate_multiple_bytes_mod1() {
948 let node: Node<[u8; 32]> =
949 Node::new([3; 32], 9, 20, Some([2; 32]), Some([1; 32]), Some([2; 32]));
950 let key = [3; 32];
951 let mut value: Vec<u8> = vec![2u8, 0, 9, 0, 20, 2, 2, 2, 2];
952 value.extend([1u8; 32].iter().clone());
953 value.extend([2u8; 32].iter().clone());
954 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
955
956 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
957 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
958 assert_eq!(
959 root.get_depth(),
960 node.get_depth(),
961 "The depth is not correct"
962 );
963 assert_eq!(
964 root.get_index_length(),
965 node.get_index_length(),
966 "The index length is not correct"
967 );
968 assert_eq!(
969 root.get_index().unwrap()[0..4],
970 node.get_index().unwrap()[0..4],
971 "The index length is not correct"
972 );
973 assert_eq!(
974 root.get_left_child(),
975 node.get_left_child(),
976 "The left child is not correct"
977 );
978 assert_eq!(
979 root.get_right_child(),
980 node.get_right_child(),
981 "The right child is not correct"
982 );
983 }
984
985 #[test]
986 fn from_value_to_intermediate_multiple_bytes_mod4() {
987 let node: Node<[u8; 32]> =
988 Node::new([3; 32], 4, 20, Some([2; 32]), Some([1; 32]), Some([2; 32]));
989 let key = [3; 32];
990 let mut value: Vec<u8> = vec![2u8, 0, 4, 0, 20, 2, 2, 2];
991 value.extend([1u8; 32].iter().clone());
992 value.extend([2u8; 32].iter().clone());
993 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
994
995 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
996 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
997 assert_eq!(
998 root.get_depth(),
999 node.get_depth(),
1000 "The depth is not correct"
1001 );
1002 assert_eq!(
1003 root.get_index_length(),
1004 node.get_index_length(),
1005 "The index length is not correct"
1006 );
1007 assert_eq!(
1008 root.get_index().unwrap()[0..3],
1009 node.get_index().unwrap()[0..3],
1010 "The index length is not correct"
1011 );
1012 assert_eq!(
1013 root.get_left_child(),
1014 node.get_left_child(),
1015 "The left child is not correct"
1016 );
1017 assert_eq!(
1018 root.get_right_child(),
1019 node.get_right_child(),
1020 "The right child is not correct"
1021 );
1022 }
1023
1024 #[test]
1025 fn from_value_to_intermediate_multiple_bytes_mod7() {
1026 let node: Node<[u8; 32]> =
1027 Node::new([3; 32], 15, 20, Some([2; 32]), Some([1; 32]), Some([2; 32]));
1028 let key = [3; 32];
1029 let mut value: Vec<u8> = vec![2u8, 0, 15, 0, 20, 2, 2, 2, 2, 2];
1030 value.extend([1u8; 32].iter().clone());
1031 value.extend([2u8; 32].iter().clone());
1032 let root: Node<[u8; 32]> = (key, value).try_into().unwrap();
1033
1034 assert_eq!(root.is_root(), node.is_root(), "The node should be root");
1035 assert_eq!(root.get_key(), node.get_key(), "The key is not correct");
1036 assert_eq!(
1037 root.get_depth(),
1038 node.get_depth(),
1039 "The depth is not correct"
1040 );
1041 assert_eq!(
1042 root.get_index_length(),
1043 node.get_index_length(),
1044 "The index length is not correct"
1045 );
1046 assert_eq!(
1047 root.get_index().unwrap()[0..5],
1048 node.get_index().unwrap()[0..5],
1049 "The index is not correct"
1050 );
1051 assert_eq!(
1052 root.get_left_child(),
1053 node.get_left_child(),
1054 "The left child is not correct"
1055 );
1056 assert_eq!(
1057 root.get_right_child(),
1058 node.get_right_child(),
1059 "The right child is not correct"
1060 );
1061 }
1062
1063 #[test]
1064 fn from_value_to_intermediate_not_enough_for_depth() {
1065 let key = [3; 32];
1066 let value: Vec<u8> = vec![2u8, 0];
1067 assert_eq!(
1068 Node::<[u8; 32]>::try_from((key, value)).err(),
1069 Some(SmtError::CorruptedData {
1070 key: format!("{:#?}", key),
1071 condition: String::from("Not enough bytes to obtain the depth (at least 3).")
1072 }),
1073 "Error messages do not match"
1074 );
1075 }
1076
1077 #[test]
1078 fn from_value_to_intermediate_not_enough_for_index_len() {
1079 let key = [3; 32];
1080 let value: Vec<u8> = vec![2u8, 0, 1, 0];
1081 assert_eq!(
1082 Node::<[u8; 32]>::try_from((key, value)).err(),
1083 Some(SmtError::CorruptedData {
1084 key: format!("{:#?}", key),
1085 condition: String::from(
1086 "Not enough bytes to obtain the index length (at least 5)."
1087 )
1088 }),
1089 "Error messages do not match"
1090 );
1091 }
1092
1093 #[test]
1094 fn from_value_to_intermediate_not_enough_for_index() {
1095 let key = [3; 32];
1096 let value: Vec<u8> = vec![2u8, 0, 1, 0, 1];
1097 assert_eq!(
1098 Node::<[u8; 32]>::try_from((key, value)).err(),
1099 Some(SmtError::CorruptedData {
1100 key: format!("{:#?}", key),
1101 condition: String::from("Not enough bytes to obtain the index.")
1102 }),
1103 "Error messages do not match"
1104 );
1105 }
1106
1107 #[test]
1108 fn from_value_to_intermediate_not_enough_for_left_key() {
1109 let key = [3; 32];
1110 let value: Vec<u8> = vec![2u8, 0, 1, 0, 1, 1, 1, 1, 1];
1111 assert_eq!(
1112 Node::<[u8; 32]>::try_from((key, value)).err(),
1113 Some(SmtError::CorruptedData {
1114 key: format!("{:#?}", key),
1115 condition: String::from("Not enough bytes to obtain the left key.")
1116 }),
1117 "Error messages do not match"
1118 );
1119 }
1120
1121 #[test]
1122 fn from_value_to_intermediate_not_enough_for_right_key() {
1123 let key = [3; 32];
1124 let value: Vec<u8> = vec![
1125 2u8, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1126 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
1127 ];
1128 assert_eq!(
1129 Node::<[u8; 32]>::try_from((key, value)).err(),
1130 Some(SmtError::CorruptedData {
1131 key: format!("{:#?}", key),
1132 condition: String::from("Not enough bytes to obtain the right key.")
1133 }),
1134 "Error messages do not match"
1135 );
1136 }
1137
1138 #[test]
1139 fn test_malformed_data() {
1140 let key = [3; 32];
1141 let value: Vec<u8> = vec![
1142 4u8, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1143 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
1144 ];
1145 assert_eq!(
1146 Node::<[u8; 32]>::try_from((key, value)).err(),
1147 Some(SmtError::CorruptedData {
1148 key: format!("{:#?}", key),
1149 condition: String::from(
1150 "Malformed node (node value does not start with 0, 1 or 2)."
1151 )
1152 }),
1153 "Error messages do not match"
1154 );
1155 }
1156}