penumbra_sdk_tct/internal/frontier/
item.rs

1use crate::prelude::*;
2
3/// The hash of the most-recently-inserted item, stored at the tip of the frontier.
4#[derive(Debug, Clone, Copy, Derivative, Serialize, Deserialize)]
5pub struct Item {
6    item: Insert<(StateCommitment, Hash)>,
7}
8
9impl From<StateCommitment> for Item {
10    fn from(commitment: StateCommitment) -> Self {
11        Self {
12            item: Insert::Keep((commitment, Hash::of(commitment))),
13        }
14    }
15}
16
17impl From<Hash> for Item {
18    fn from(hash: Hash) -> Self {
19        Self {
20            item: Insert::Hash(hash),
21        }
22    }
23}
24
25impl GetHash for Item {
26    #[inline]
27    fn hash(&self) -> Hash {
28        match self.item {
29            Insert::Hash(hash) => hash,
30            Insert::Keep((_, hash)) => hash,
31        }
32    }
33
34    #[inline]
35    fn cached_hash(&self) -> Option<Hash> {
36        Some(self.hash())
37    }
38}
39
40impl Height for Item {
41    type Height = Zero;
42}
43
44impl Focus for Item {
45    type Complete = complete::Item;
46
47    #[inline]
48    fn finalize_owned(self) -> Insert<Self::Complete> {
49        self.item
50            .map(|(commitment, hash)| complete::Item::new(hash, commitment))
51    }
52}
53
54impl Witness for Item {
55    #[inline]
56    fn witness(&self, index: impl Into<u64>) -> Option<(AuthPath<Self>, Hash)> {
57        debug_assert_eq!(index.into(), 0, "non-zero index when witnessing leaf");
58        Some((path::Leaf, self.hash()))
59    }
60}
61
62impl GetPosition for Item {
63    #[inline]
64    fn position(&self) -> Option<u64> {
65        None
66    }
67}
68
69impl Forget for Item {
70    #[inline]
71    fn forget(&mut self, _forgotten: Option<Forgotten>, index: impl Into<u64>) -> bool {
72        if index.into() == 0 {
73            if let Insert::Keep((_, hash)) = self.item {
74                self.item = Insert::Hash(hash);
75                true
76            } else {
77                false
78            }
79        } else {
80            panic!("non-zero index when forgetting item");
81        }
82    }
83}
84
85impl<'tree> structure::Any<'tree> for Item {
86    fn kind(&self) -> Kind {
87        Kind::Leaf {
88            commitment: self.item.keep().map(|(commitment, _)| commitment),
89        }
90    }
91
92    fn forgotten(&self) -> Forgotten {
93        Forgotten::default()
94    }
95
96    fn children(&self) -> Vec<HashOrNode<'tree>> {
97        vec![]
98    }
99}
100
101impl OutOfOrder for Item {
102    fn uninitialized(_position: Option<u64>, _forgotten: Forgotten) -> Self {
103        Self {
104            item: Insert::Hash(Hash::uninitialized()),
105        }
106    }
107
108    fn uninitialized_out_of_order_insert_commitment(
109        &mut self,
110        index: u64,
111        commitment: StateCommitment,
112    ) {
113        if index == 0 {
114            let hash = match self.item {
115                Insert::Keep((_drop_old_commitment, hash)) => hash,
116                Insert::Hash(hash) => hash,
117            };
118            self.item = Insert::Keep((commitment, hash));
119        } else {
120            panic!("non-zero index when inserting commitment");
121        }
122    }
123}
124
125impl UncheckedSetHash for Item {
126    fn unchecked_set_hash(&mut self, index: u64, height: u8, hash: Hash) {
127        if index != 0 {
128            panic!("non-zero index when setting hash");
129        }
130        if height != 0 {
131            panic!("non-zero height when setting hash");
132        }
133        self.item = match self.item {
134            Insert::Keep((commitment, _drop_old_hash)) => Insert::Keep((commitment, hash)),
135            Insert::Hash(_drop_old_hash) => Insert::Hash(hash),
136        }
137    }
138
139    fn finish_initialize(&mut self) {
140        match self.item {
141            Insert::Keep((commitment, ref mut hash)) => {
142                if hash.is_uninitialized() {
143                    *hash = Hash::of(commitment);
144                }
145            }
146            Insert::Hash(ref mut hash) => {
147                if hash.is_uninitialized() {
148                    // An uninitialized frontier hash should be set to the zero hash, which is the
149                    // empty hash for the frontier
150                    *hash = Hash::zero();
151                }
152            }
153        }
154    }
155}