daml_grpc/data/
time_model.rs

1use crate::data::{DamlError, DamlResult};
2use crate::grpc_protobuf::com::daml::ledger::api::v1::admin::TimeModel;
3use crate::util::{from_grpc_duration, to_grpc_duration, Required};
4use std::convert::TryFrom;
5use std::time::Duration;
6
7/// The ledger time model.
8#[derive(Debug, PartialEq, Eq, Default, Clone, Hash)]
9pub struct DamlTimeModel {
10    avg_transaction_latency: Duration,
11    min_skew: Duration,
12    max_skew: Duration,
13}
14impl DamlTimeModel {
15    pub fn new(
16        avg_transaction_latency: impl Into<Duration>,
17        min_skew: impl Into<Duration>,
18        max_skew: impl Into<Duration>,
19    ) -> Self {
20        Self {
21            avg_transaction_latency: avg_transaction_latency.into(),
22            min_skew: min_skew.into(),
23            max_skew: max_skew.into(),
24        }
25    }
26
27    /// The expected average latency of a transaction, i.e., the average time
28    /// from submitting the transaction and the transaction being assigned a record time.
29    pub const fn avg_transaction_latency(&self) -> &Duration {
30        &self.avg_transaction_latency
31    }
32
33    /// The minimum skew between ledger time and record time: `lt_TX` >= `rt_TX` - minSkew
34    pub const fn min_skew(&self) -> &Duration {
35        &self.min_skew
36    }
37
38    /// The maximum skew between ledger time and record time: `lt_TX` <= `rt_TX` + maxSkew
39    pub const fn max_skew(&self) -> &Duration {
40        &self.max_skew
41    }
42}
43
44impl TryFrom<DamlTimeModel> for TimeModel {
45    type Error = DamlError;
46
47    fn try_from(time_model: DamlTimeModel) -> DamlResult<Self> {
48        Ok(Self {
49            avg_transaction_latency: Some(to_grpc_duration(time_model.avg_transaction_latency())?),
50            min_skew: Some(to_grpc_duration(time_model.min_skew())?),
51            max_skew: Some(to_grpc_duration(time_model.max_skew())?),
52        })
53    }
54}
55
56impl TryFrom<TimeModel> for DamlTimeModel {
57    type Error = DamlError;
58
59    fn try_from(time_model: TimeModel) -> DamlResult<Self> {
60        let avg_transaction_latency = from_grpc_duration(&time_model.avg_transaction_latency.req()?);
61        let min_skew = from_grpc_duration(&time_model.min_skew.req()?);
62        let max_skew = from_grpc_duration(&time_model.max_skew.req()?);
63        Ok(Self::new(avg_transaction_latency, min_skew, max_skew))
64    }
65}