penumbra_sct/component/
rpc.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
use cnidarium::Storage;
use pbjson_types::Timestamp;
use penumbra_proto::core::component::sct::v1::query_service_server::QueryService;
use penumbra_proto::core::component::sct::v1::{
    AnchorByHeightRequest, AnchorByHeightResponse, EpochByHeightRequest, EpochByHeightResponse,
    TimestampByHeightRequest, TimestampByHeightResponse,
};
use tonic::Status;
use tracing::instrument;

use super::clock::EpochRead;
use super::tree::SctRead;

// TODO: Hide this and only expose a Router?
pub struct Server {
    storage: Storage,
}

impl Server {
    pub fn new(storage: Storage) -> Self {
        Self { storage }
    }
}

#[tonic::async_trait]
impl QueryService for Server {
    #[instrument(skip(self, request))]
    async fn epoch_by_height(
        &self,
        request: tonic::Request<EpochByHeightRequest>,
    ) -> Result<tonic::Response<EpochByHeightResponse>, Status> {
        let state = self.storage.latest_snapshot();

        let epoch = state
            .get_epoch_by_height(request.get_ref().height)
            .await
            .map_err(|e| tonic::Status::unknown(format!("could not get epoch for height: {e}")))?;

        Ok(tonic::Response::new(EpochByHeightResponse {
            epoch: Some(epoch.into()),
        }))
    }

    #[instrument(skip(self, request))]
    async fn anchor_by_height(
        &self,
        request: tonic::Request<AnchorByHeightRequest>,
    ) -> Result<tonic::Response<AnchorByHeightResponse>, Status> {
        let state = self.storage.latest_snapshot();

        let height = request.get_ref().height;
        let anchor = state.get_anchor_by_height(height).await.map_err(|e| {
            tonic::Status::unknown(format!("could not get anchor for height {height}: {e}"))
        })?;

        Ok(tonic::Response::new(AnchorByHeightResponse {
            anchor: anchor.map(Into::into),
        }))
    }

    #[instrument(skip(self, request))]
    async fn timestamp_by_height(
        &self,
        request: tonic::Request<TimestampByHeightRequest>,
    ) -> Result<tonic::Response<TimestampByHeightResponse>, Status> {
        let state = self.storage.latest_snapshot();

        let height = request.get_ref().height;
        let block_time = state.get_block_timestamp(height).await.map_err(|e| {
            tonic::Status::unknown(format!("could not get timestamp for height {height}: {e}"))
        })?;
        let timestamp = chrono::DateTime::parse_from_rfc3339(block_time.to_rfc3339().as_str())
            .expect("timestamp should roundtrip to string");

        Ok(tonic::Response::new(TimestampByHeightResponse {
            timestamp: Some(Timestamp {
                seconds: timestamp.timestamp(),
                nanos: timestamp.timestamp_subsec_nanos() as i32,
            }),
        }))
    }
}