1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/// A merkle proof for an index.
///
/// Merkle trees are proven by checking the parent hashes.
#[derive(Debug, PartialEq)]
pub struct Proof<'a> {
  index: u64,
  verified_by: u64,
  nodes: &'a [u64],
}

impl<'a> Proof<'a> {
  /// Create a new instance.
  ///
  /// ## Examples
  /// ```rust
  /// # extern crate tree_index;
  /// # use tree_index::Proof;
  /// let nodes = vec![];
  /// let _proof = Proof::new(0, 0, &nodes);
  /// ```
  #[inline]
  pub fn new(index: u64, verified_by: u64, nodes: &'a [u64]) -> Self {
    Self {
      index,
      nodes,
      verified_by,
    }
  }

  /// Get the index which was used to verify this node.
  ///
  /// ## Examples
  /// ```rust
  /// # extern crate tree_index;
  /// # use tree_index::Proof;
  /// let nodes = vec![];
  /// let proof = Proof::new(0, 0, &nodes);
  /// assert_eq!(proof.index(), 0);
  /// ```
  #[inline]
  pub fn index(&self) -> u64 {
    self.index
  }

  /// Get the index for the node which verifies the input index.
  ///
  /// ## Examples
  /// ```rust
  /// # extern crate tree_index;
  /// # use tree_index::Proof;
  /// let nodes = vec![];
  /// let proof = Proof::new(0, 0, &nodes);
  /// assert_eq!(proof.verified_by(), 0);
  /// ```
  #[inline]
  pub fn verified_by(&self) -> u64 {
    self.verified_by
  }

  /// Merkle proof for the index you pass, written in `flat-tree` notation.
  ///
  /// ## Examples
  /// ```rust
  /// # extern crate tree_index;
  /// # use tree_index::Proof;
  /// let nodes = vec![];
  /// let proof = Proof::new(0, 0, &nodes);
  /// assert_eq!(proof.nodes().len(), 0);
  /// ```
  #[inline]
  pub fn nodes(&self) -> &[u64] {
    &self.nodes
  }
}