penumbra_sdk_sct/
event.rs

1use anyhow::{anyhow, Context as _};
2use pbjson_types::Timestamp;
3use penumbra_sdk_tct as tct;
4use tct::builder::{block, epoch};
5
6use penumbra_sdk_proto::{
7    core::component::sct::v1::{self as pb},
8    DomainType, Name as _,
9};
10
11use crate::CommitmentSource;
12
13pub fn anchor(height: u64, anchor: tct::Root, timestamp: i64) -> pb::EventAnchor {
14    pb::EventAnchor {
15        height,
16        anchor: Some(anchor.into()),
17        timestamp: Some(Timestamp {
18            seconds: timestamp,
19            nanos: 0,
20        }),
21    }
22}
23
24#[derive(Debug, Clone)]
25pub struct EventBlockRoot {
26    pub height: u64,
27    pub root: block::Root,
28    pub timestamp_seconds: i64,
29}
30
31impl TryFrom<pb::EventBlockRoot> for EventBlockRoot {
32    type Error = anyhow::Error;
33
34    fn try_from(value: pb::EventBlockRoot) -> Result<Self, Self::Error> {
35        fn inner(value: pb::EventBlockRoot) -> anyhow::Result<EventBlockRoot> {
36            Ok(EventBlockRoot {
37                height: value.height,
38                root: value.root.ok_or(anyhow!("missing `root`"))?.try_into()?,
39                timestamp_seconds: value
40                    .timestamp
41                    .ok_or(anyhow!("missing `timestamp`"))?
42                    .seconds,
43            })
44        }
45        inner(value).context(format!("parsing {}", pb::EventBlockRoot::NAME))
46    }
47}
48
49impl From<EventBlockRoot> for pb::EventBlockRoot {
50    fn from(value: EventBlockRoot) -> Self {
51        Self {
52            height: value.height,
53            root: Some(value.root.into()),
54            timestamp: Some(Timestamp {
55                seconds: value.timestamp_seconds,
56                nanos: 0,
57            }),
58        }
59    }
60}
61
62impl DomainType for EventBlockRoot {
63    type Proto = pb::EventBlockRoot;
64}
65
66pub fn epoch_root(index: u64, root: epoch::Root, timestamp: i64) -> pb::EventEpochRoot {
67    pb::EventEpochRoot {
68        index,
69        root: Some(root.into()),
70        timestamp: Some(Timestamp {
71            seconds: timestamp,
72            nanos: 0,
73        }),
74    }
75}
76
77pub fn commitment(
78    commitment: tct::StateCommitment,
79    position: tct::Position,
80    source: CommitmentSource,
81) -> pb::EventCommitment {
82    pb::EventCommitment {
83        commitment: Some(commitment.into()),
84        position: position.into(),
85        source: Some(source.into()),
86    }
87}