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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::prelude::*;

/// A witnessed hash of a commitment at the true leaf of a complete tree.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Derivative, Serialize, Deserialize)]
pub struct Item {
    hash: Hash,
    commitment: StateCommitment,
}

impl Item {
    /// Create a new `Item` from a [`Hash`](struct@Hash).
    pub fn new(hash: Hash, commitment: StateCommitment) -> Self {
        Self { hash, commitment }
    }
}

impl GetHash for Item {
    #[inline]
    fn hash(&self) -> Hash {
        self.hash
    }

    #[inline]
    fn cached_hash(&self) -> Option<Hash> {
        Some(self.hash)
    }
}

impl Height for Item {
    type Height = Zero;
}

impl Complete for Item {
    type Focus = frontier::Item;
}

impl Witness for Item {
    #[inline]
    fn witness(&self, index: impl Into<u64>) -> Option<(AuthPath<Self>, Hash)> {
        debug_assert_eq!(index.into(), 0, "non-zero index when witnessing leaf");
        Some((path::Leaf, self.hash))
    }
}

impl ForgetOwned for Item {
    fn forget_owned(
        self,
        _forgotten: Option<Forgotten>,
        index: impl Into<u64>,
    ) -> (Insert<Self>, bool) {
        debug_assert_eq!(index.into(), 0, "non-zero index when forgetting leaf");
        (Insert::Hash(self.hash), true)
    }
}

impl GetPosition for Item {
    fn position(&self) -> Option<u64> {
        None
    }
}

impl<'tree> structure::Any<'tree> for Item {
    fn kind(&self) -> Kind {
        Kind::Leaf {
            commitment: Some(self.commitment),
        }
    }

    fn forgotten(&self) -> Forgotten {
        Forgotten::default()
    }

    fn children(&self) -> Vec<HashOrNode<'tree>> {
        vec![]
    }
}

impl OutOfOrderOwned for Item {
    fn uninitialized_out_of_order_insert_commitment_owned(
        this: Insert<Self>,
        index: u64,
        commitment: StateCommitment,
    ) -> Self {
        if index != 0 {
            panic!("non-zero index when inserting commitment");
        }
        let hash = match this {
            Insert::Keep(Item { hash, .. }) => hash,
            Insert::Hash(hash) => hash,
        };
        Item { hash, commitment }
    }
}

impl UncheckedSetHash for Item {
    fn unchecked_set_hash(&mut self, index: u64, height: u8, hash: Hash) {
        if index != 0 {
            panic!("non-zero index when setting hash");
        }
        if height != 0 {
            panic!("non-zero height when setting hash");
        }
        self.hash = hash;
    }

    fn finish_initialize(&mut self) {
        if self.hash.is_uninitialized() {
            self.hash = Hash::of(self.commitment);
        }
    }
}