penumbra_sct/
event.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use anyhow::{anyhow, Context as _};
use pbjson_types::Timestamp;
use penumbra_tct as tct;
use tct::builder::{block, epoch};

use penumbra_proto::{
    core::component::sct::v1::{self as pb},
    DomainType, Name as _,
};

use crate::CommitmentSource;

pub fn anchor(height: u64, anchor: tct::Root, timestamp: i64) -> pb::EventAnchor {
    pb::EventAnchor {
        height,
        anchor: Some(anchor.into()),
        timestamp: Some(Timestamp {
            seconds: timestamp,
            nanos: 0,
        }),
    }
}

#[derive(Debug, Clone)]
pub struct EventBlockRoot {
    pub height: u64,
    pub root: block::Root,
    pub timestamp_seconds: i64,
}

impl TryFrom<pb::EventBlockRoot> for EventBlockRoot {
    type Error = anyhow::Error;

    fn try_from(value: pb::EventBlockRoot) -> Result<Self, Self::Error> {
        fn inner(value: pb::EventBlockRoot) -> anyhow::Result<EventBlockRoot> {
            Ok(EventBlockRoot {
                height: value.height,
                root: value.root.ok_or(anyhow!("missing `root`"))?.try_into()?,
                timestamp_seconds: value
                    .timestamp
                    .ok_or(anyhow!("missing `timestamp`"))?
                    .seconds,
            })
        }
        inner(value).context(format!("parsing {}", pb::EventBlockRoot::NAME))
    }
}

impl From<EventBlockRoot> for pb::EventBlockRoot {
    fn from(value: EventBlockRoot) -> Self {
        Self {
            height: value.height,
            root: Some(value.root.into()),
            timestamp: Some(Timestamp {
                seconds: value.timestamp_seconds,
                nanos: 0,
            }),
        }
    }
}

impl DomainType for EventBlockRoot {
    type Proto = pb::EventBlockRoot;
}

pub fn epoch_root(index: u64, root: epoch::Root, timestamp: i64) -> pb::EventEpochRoot {
    pb::EventEpochRoot {
        index,
        root: Some(root.into()),
        timestamp: Some(Timestamp {
            seconds: timestamp,
            nanos: 0,
        }),
    }
}

pub fn commitment(
    commitment: tct::StateCommitment,
    position: tct::Position,
    source: CommitmentSource,
) -> pb::EventCommitment {
    pb::EventCommitment {
        commitment: Some(commitment.into()),
        position: position.into(),
        source: Some(source.into()),
    }
}