daml_grpc/service/
daml_time_service.rs

1use std::fmt::Debug;
2
3use chrono::DateTime;
4use chrono::Utc;
5use futures::stream::StreamExt;
6use futures::Stream;
7use tonic::transport::Channel;
8use tracing::{instrument, trace};
9
10use crate::data::DamlError;
11use crate::data::DamlResult;
12use crate::grpc_protobuf::com::daml::ledger::api::v1::testing::time_service_client::TimeServiceClient;
13use crate::grpc_protobuf::com::daml::ledger::api::v1::testing::{GetTimeRequest, SetTimeRequest};
14use crate::service::common::make_request;
15use crate::util;
16use crate::util::Required;
17
18/// Get and set the time of a Daml ledger (requires `testing` feature).
19#[derive(Debug)]
20pub struct DamlTimeService<'a> {
21    channel: Channel,
22    ledger_id: &'a str,
23    auth_token: Option<&'a str>,
24}
25
26impl<'a> DamlTimeService<'a> {
27    pub fn new(channel: Channel, ledger_id: &'a str, auth_token: Option<&'a str>) -> Self {
28        Self {
29            channel,
30            ledger_id,
31            auth_token,
32        }
33    }
34
35    /// Override the JWT token to use for this service.
36    pub fn with_token(self, auth_token: &'a str) -> Self {
37        Self {
38            auth_token: Some(auth_token),
39            ..self
40        }
41    }
42
43    /// Override the ledger id to use for this service.
44    pub fn with_ledger_id(self, ledger_id: &'a str) -> Self {
45        Self {
46            ledger_id,
47            ..self
48        }
49    }
50
51    /// DOCME fully document this
52    #[instrument(skip(self))]
53    pub async fn get_time(&self) -> DamlResult<impl Stream<Item = DamlResult<DateTime<Utc>>>> {
54        let payload = GetTimeRequest {
55            ledger_id: self.ledger_id.to_string(),
56        };
57        trace!(payload = ?payload, token = ?self.auth_token);
58        let time_stream = self.client().get_time(make_request(payload, self.auth_token)?).await?.into_inner();
59        Ok(time_stream.inspect(|response| trace!(?response)).map(|item| match item {
60            Ok(r) => Ok(util::from_grpc_timestamp(&r.current_time.req()?)),
61            Err(e) => Err(DamlError::from(e)),
62        }))
63    }
64
65    /// DOCME fully document this
66    #[instrument(skip(self))]
67    pub async fn set_time(
68        &self,
69        current_time: impl Into<DateTime<Utc>> + Debug,
70        new_time: impl Into<DateTime<Utc>> + Debug,
71    ) -> DamlResult<()> {
72        let payload = SetTimeRequest {
73            ledger_id: self.ledger_id.to_string(),
74            current_time: Some(util::to_grpc_timestamp(current_time.into())?),
75            new_time: Some(util::to_grpc_timestamp(new_time.into())?),
76        };
77        trace!(payload = ?payload, token = ?self.auth_token);
78        self.client().set_time(make_request(payload, self.auth_token)?).await?;
79        Ok(())
80    }
81
82    fn client(&self) -> TimeServiceClient<Channel> {
83        TimeServiceClient::new(self.channel.clone())
84    }
85}