penumbra_sdk_tct/internal/complete/
tier.rs

1use crate::prelude::*;
2
3type N<Child> = super::super::complete::Node<Child>;
4type L<Item> = super::super::complete::Leaf<Item>;
5
6/// An eight-deep complete tree with the given item at each leaf.
7pub type Nested<Item> = N<N<N<N<N<N<N<N<L<Item>>>>>>>>>;
8// Count the levels:    1 2 3 4 5 6 7 8
9
10/// A complete tier of the tiered commitment tree, being an 8-deep sparse quad-tree.
11#[derive(Clone, Debug, Serialize, Deserialize)]
12pub struct Tier<Item: GetHash + Height + Clone> {
13    pub(in super::super) inner: Nested<Item>,
14}
15
16impl<Item: GetHash + Height + Clone> Height for Tier<Item> {
17    type Height = <Nested<Item> as Height>::Height;
18}
19
20impl<Item: GetHash + Height + Clone> GetHash for Tier<Item> {
21    #[inline]
22    fn hash(&self) -> Hash {
23        self.inner.hash()
24    }
25
26    #[inline]
27    fn cached_hash(&self) -> Option<Hash> {
28        self.inner.cached_hash()
29    }
30}
31
32impl<Item: Complete + Clone> Complete for Tier<Item>
33where
34    Item::Focus: Clone,
35{
36    type Focus = frontier::Tier<Item::Focus>;
37}
38
39impl<Item: GetHash + Witness + Clone> Witness for Tier<Item> {
40    #[inline]
41    fn witness(&self, index: impl Into<u64>) -> Option<(AuthPath<Self>, Hash)> {
42        self.inner.witness(index)
43    }
44}
45
46impl<Item: GetHash + ForgetOwned + Clone> ForgetOwned for Tier<Item> {
47    fn forget_owned(
48        self,
49        forgotten: Option<Forgotten>,
50        index: impl Into<u64>,
51    ) -> (Insert<Self>, bool) {
52        let (inner, forgotten) = self.inner.forget_owned(forgotten, index);
53        (inner.map(|inner| Tier { inner }), forgotten)
54    }
55}
56
57impl<Item: Complete + Clone> From<frontier::Tier<Item::Focus>> for Insert<Tier<Item>>
58where
59    Item::Focus: Clone,
60{
61    fn from(frontier: frontier::Tier<Item::Focus>) -> Self {
62        frontier.finalize_owned()
63    }
64}
65
66impl<Item: GetHash + Height + Clone> GetPosition for Tier<Item> {
67    fn position(&self) -> Option<u64> {
68        None
69    }
70}
71
72impl<'tree, Item: Height + structure::Any<'tree> + Clone> structure::Any<'tree> for Tier<Item> {
73    fn kind(&self) -> Kind {
74        self.inner.kind()
75    }
76
77    fn forgotten(&self) -> Forgotten {
78        structure::Any::forgotten(&self.inner)
79    }
80
81    fn children(&'tree self) -> Vec<HashOrNode<'tree>> {
82        (&self.inner as &dyn structure::Any).children()
83    }
84}
85
86impl<Item: GetHash + Height + OutOfOrderOwned + Clone> OutOfOrderOwned for Tier<Item> {
87    fn uninitialized_out_of_order_insert_commitment_owned(
88        this: Insert<Self>,
89        index: u64,
90        commitment: StateCommitment,
91    ) -> Self {
92        Tier {
93            inner: Nested::uninitialized_out_of_order_insert_commitment_owned(
94                this.map(|tier| tier.inner),
95                index,
96                commitment,
97            ),
98        }
99    }
100}
101
102impl<Item: GetHash + UncheckedSetHash + Clone> UncheckedSetHash for Tier<Item> {
103    fn unchecked_set_hash(&mut self, index: u64, height: u8, hash: Hash) {
104        self.inner.unchecked_set_hash(index, height, hash)
105    }
106
107    fn finish_initialize(&mut self) {
108        self.inner.finish_initialize()
109    }
110}