tree_index/proof.rs
1/// A merkle proof for an index.
2///
3/// Merkle trees are proven by checking the parent hashes.
4#[derive(Debug, PartialEq)]
5pub struct Proof<'a> {
6 index: u64,
7 verified_by: u64,
8 nodes: &'a [u64],
9}
10
11impl<'a> Proof<'a> {
12 /// Create a new instance.
13 ///
14 /// ## Examples
15 /// ```rust
16 /// # extern crate tree_index;
17 /// # use tree_index::Proof;
18 /// let nodes = vec![];
19 /// let _proof = Proof::new(0, 0, &nodes);
20 /// ```
21 #[inline]
22 pub fn new(index: u64, verified_by: u64, nodes: &'a [u64]) -> Self {
23 Self {
24 index,
25 nodes,
26 verified_by,
27 }
28 }
29
30 /// Get the index which was used to verify this node.
31 ///
32 /// ## Examples
33 /// ```rust
34 /// # extern crate tree_index;
35 /// # use tree_index::Proof;
36 /// let nodes = vec![];
37 /// let proof = Proof::new(0, 0, &nodes);
38 /// assert_eq!(proof.index(), 0);
39 /// ```
40 #[inline]
41 pub fn index(&self) -> u64 {
42 self.index
43 }
44
45 /// Get the index for the node which verifies the input index.
46 ///
47 /// ## Examples
48 /// ```rust
49 /// # extern crate tree_index;
50 /// # use tree_index::Proof;
51 /// let nodes = vec![];
52 /// let proof = Proof::new(0, 0, &nodes);
53 /// assert_eq!(proof.verified_by(), 0);
54 /// ```
55 #[inline]
56 pub fn verified_by(&self) -> u64 {
57 self.verified_by
58 }
59
60 /// Merkle proof for the index you pass, written in `flat-tree` notation.
61 ///
62 /// ## Examples
63 /// ```rust
64 /// # extern crate tree_index;
65 /// # use tree_index::Proof;
66 /// let nodes = vec![];
67 /// let proof = Proof::new(0, 0, &nodes);
68 /// assert_eq!(proof.nodes().len(), 0);
69 /// ```
70 #[inline]
71 pub fn nodes(&self) -> &[u64] {
72 &self.nodes
73 }
74}