Skip to main content

lexe_common/api/
version.rs

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