penumbra_sdk_tct/witness.rs
1/// When inserting a [`Commitment`](crate::Commitment) into a [`Tree`](crate::Tree), should we
2/// [`Keep`](Witness::Keep) it to allow it to be witnessed later, or [`Forget`](Witness::Forget)
3/// about it after updating the root hash of the tree?
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5#[cfg_attr(any(test, feature = "arbitrary"), derive(proptest_derive::Arbitrary))]
6pub enum Witness {
7 /// When inserting a [`Commitment`](crate::Commitment) into a [`Tree`](crate::Tree), this flag
8 /// indicates that we should immediately forget about it to save space, because we will not want
9 /// to witness its presence later.
10 ///
11 /// This is equivalent to inserting the commitment using [`Witness::Keep`] and then immediately
12 /// forgetting that same commitment using [`Tree::forget`](crate::Tree::forget), though it is
13 /// more efficient to directly forget commitments upon insertion rather than to remember them on
14 /// insertion and then immediately forget them.
15 Forget,
16 /// When inserting a [`Commitment`](crate::Commitment) into a [`Tree`](crate::Tree), this flag
17 /// indicates that we should keep this commitment to allow it to be witnessed later.
18 Keep,
19}
20
21impl serde::Serialize for Witness {
22 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
23 match self {
24 Witness::Forget => serializer.serialize_str("forget"),
25 Witness::Keep => serializer.serialize_str("keep"),
26 }
27 }
28}
29
30impl<'de> serde::Deserialize<'de> for Witness {
31 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
32 struct WitnessVisitor;
33
34 impl<'de> serde::de::Visitor<'de> for WitnessVisitor {
35 type Value = Witness;
36
37 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
38 formatter.write_str("one of \"keep\" or \"forget\"")
39 }
40
41 fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<Self::Value, E> {
42 match value.to_lowercase().as_str() {
43 "forget" => Ok(Witness::Forget),
44 "keep" => Ok(Witness::Keep),
45 _ => Err(E::custom(format!(
46 "invalid witness flag: expected \"forget\" or \"keep\", found \"{value}\""
47 ))),
48 }
49 }
50 }
51
52 deserializer.deserialize_str(WitnessVisitor)
53 }
54}