penumbra_sdk_tct/internal/complete/
item.rs

1use crate::prelude::*;
2
3/// A witnessed hash of a commitment at the true leaf of a complete tree.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Derivative, Serialize, Deserialize)]
5pub struct Item {
6    hash: Hash,
7    commitment: StateCommitment,
8}
9
10impl Item {
11    /// Create a new `Item` from a [`Hash`](struct@Hash).
12    pub fn new(hash: Hash, commitment: StateCommitment) -> Self {
13        Self { hash, commitment }
14    }
15}
16
17impl GetHash for Item {
18    #[inline]
19    fn hash(&self) -> Hash {
20        self.hash
21    }
22
23    #[inline]
24    fn cached_hash(&self) -> Option<Hash> {
25        Some(self.hash)
26    }
27}
28
29impl Height for Item {
30    type Height = Zero;
31}
32
33impl Complete for Item {
34    type Focus = frontier::Item;
35}
36
37impl Witness for Item {
38    #[inline]
39    fn witness(&self, index: impl Into<u64>) -> Option<(AuthPath<Self>, Hash)> {
40        debug_assert_eq!(index.into(), 0, "non-zero index when witnessing leaf");
41        Some((path::Leaf, self.hash))
42    }
43}
44
45impl ForgetOwned for Item {
46    fn forget_owned(
47        self,
48        _forgotten: Option<Forgotten>,
49        index: impl Into<u64>,
50    ) -> (Insert<Self>, bool) {
51        debug_assert_eq!(index.into(), 0, "non-zero index when forgetting leaf");
52        (Insert::Hash(self.hash), true)
53    }
54}
55
56impl GetPosition for Item {
57    fn position(&self) -> Option<u64> {
58        None
59    }
60}
61
62impl<'tree> structure::Any<'tree> for Item {
63    fn kind(&self) -> Kind {
64        Kind::Leaf {
65            commitment: Some(self.commitment),
66        }
67    }
68
69    fn forgotten(&self) -> Forgotten {
70        Forgotten::default()
71    }
72
73    fn children(&self) -> Vec<HashOrNode<'tree>> {
74        vec![]
75    }
76}
77
78impl OutOfOrderOwned for Item {
79    fn uninitialized_out_of_order_insert_commitment_owned(
80        this: Insert<Self>,
81        index: u64,
82        commitment: StateCommitment,
83    ) -> Self {
84        if index != 0 {
85            panic!("non-zero index when inserting commitment");
86        }
87        let hash = match this {
88            Insert::Keep(Item { hash, .. }) => hash,
89            Insert::Hash(hash) => hash,
90        };
91        Item { hash, commitment }
92    }
93}
94
95impl UncheckedSetHash for Item {
96    fn unchecked_set_hash(&mut self, index: u64, height: u8, hash: Hash) {
97        if index != 0 {
98            panic!("non-zero index when setting hash");
99        }
100        if height != 0 {
101            panic!("non-zero height when setting hash");
102        }
103        self.hash = hash;
104    }
105
106    fn finish_initialize(&mut self) {
107        if self.hash.is_uninitialized() {
108            self.hash = Hash::of(self.commitment);
109        }
110    }
111}