Skip to main content

lexe_common/api/
version.rs

1use std::{collections::BTreeSet, fmt};
2
3use lexe_enclave::enclave::{MachineId, Measurement};
4use lexe_std::fmt::DisplayIter;
5#[cfg(test)]
6use proptest_derive::Arbitrary;
7use serde::{Deserialize, Serialize};
8
9#[cfg(test)]
10use crate::test_utils::arbitrary;
11
12/// Upgradeable API struct for a measurement.
13#[derive(Debug, PartialEq, Serialize, Deserialize)]
14#[cfg_attr(test, derive(Arbitrary))]
15pub struct MeasurementStruct {
16    pub measurement: Measurement,
17}
18
19/// API-upgradeable struct for a [`BTreeSet<NodeEnclave>`].
20#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
21#[cfg_attr(test, derive(Arbitrary))]
22pub struct CurrentEnclaves {
23    /// All current node enclaves.
24    /// TODO(maurice): remove rename after v0.8.7 is gone.
25    #[serde(rename = "releases", alias = "enclaves")]
26    pub enclaves: BTreeSet<NodeEnclave>,
27}
28
29impl CurrentEnclaves {
30    /// Returns the latest (most recent) node enclave, if any.
31    pub fn latest(&self) -> Option<&NodeEnclave> {
32        self.enclaves.last()
33    }
34}
35
36/// The subset of node enclaves that a specific user needs to provision to.
37#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
38#[cfg_attr(test, derive(Arbitrary))]
39pub struct EnclavesToProvision {
40    pub enclaves: BTreeSet<NodeEnclave>,
41}
42
43impl fmt::Display for EnclavesToProvision {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        DisplayIter(self.enclaves.iter()).fmt(f)
46    }
47}
48
49/// The machine_id, semver version and measurement of a node enclave.
50///
51/// [`Ord`]ered by [`semver::Version`] precedence.
52#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
53#[cfg_attr(test, derive(Arbitrary))]
54pub struct NodeEnclave {
55    /// e.g. "0.1.0", "0.0.0-dev.1"
56    #[cfg_attr(test, proptest(strategy = "arbitrary::any_semver_version()"))]
57    pub version: semver::Version,
58    pub measurement: Measurement,
59    pub machine_id: MachineId,
60}
61
62impl Ord for NodeEnclave {
63    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
64        self.version
65            .cmp_precedence(&other.version)
66            .then_with(|| self.machine_id.cmp(&other.machine_id))
67    }
68}
69
70impl PartialOrd for NodeEnclave {
71    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
72        Some(self.cmp(other))
73    }
74}
75
76impl fmt::Display for NodeEnclave {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        let version = &self.version;
79        let measurement = &self.measurement;
80        let machine_id = &self.machine_id;
81        write!(
82            f,
83            "(version=node-v{version}, measurement={measurement}, machine_id={machine_id})"
84        )
85    }
86}
87
88#[cfg(test)]
89mod test {
90    use super::*;
91    use crate::test_utils::roundtrip;
92
93    #[test]
94    fn measurement_struct_roundtrip() {
95        roundtrip::query_string_roundtrip_proptest::<MeasurementStruct>();
96    }
97
98    #[test]
99    fn node_enclave_roundtrip() {
100        roundtrip::json_value_roundtrip_proptest::<NodeEnclave>();
101    }
102}