oasis_core_runtime/common/
version.rs

1//! Protocol and runtime versioning.
2// NOTE: This should be kept in sync with go/common/version/version.go.
3
4/// A protocol or runtime version.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
6pub struct Version {
7    #[cbor(optional)]
8    pub major: u16,
9
10    #[cbor(optional)]
11    pub minor: u16,
12
13    #[cbor(optional)]
14    pub patch: u16,
15}
16
17#[macro_export]
18macro_rules! version_from_cargo {
19    () => {
20        Version::new(
21            env!("CARGO_PKG_VERSION_MAJOR").parse::<u16>().unwrap(),
22            env!("CARGO_PKG_VERSION_MINOR").parse::<u16>().unwrap(),
23            env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap(),
24        )
25    };
26}
27
28impl Version {
29    /// Creates a new version with given major, minor, and patch segments.
30    pub const fn new(major: u16, minor: u16, patch: u16) -> Version {
31        Version {
32            major,
33            minor,
34            patch,
35        }
36    }
37
38    /// Checks if two versions are compatible.
39    pub fn is_compatible_with(&self, other: &Version) -> bool {
40        self.major == other.major
41    }
42}
43
44// Returns the version as a platform-dependent u64.
45impl From<Version> for u64 {
46    fn from(val: Version) -> Self {
47        ((val.major as u64) << 32) | ((val.minor as u64) << 16) | (val.patch as u64)
48    }
49}
50
51// Creates the version from a platform-dependent u64.
52impl From<u64> for Version {
53    fn from(v: u64) -> Version {
54        Version {
55            major: ((v >> 32) & 0xffff) as u16,
56            minor: ((v >> 16) & 0xffff) as u16,
57            patch: (v & 0xffff) as u16,
58        }
59    }
60}
61
62// Version of the protocol used for communication between the Oasis node(s)
63// and the runtime. This version MUST be compatible with the one supported by
64// the worker host.
65pub const PROTOCOL_VERSION: Version = Version {
66    major: 5,
67    minor: 1,
68    patch: 0,
69};
70
71/// Protocol versions.
72#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, cbor::Encode, cbor::Decode)]
73pub struct ProtocolVersions {
74    pub consensus_protocol: Version,
75    pub runtime_host_protocol: Version,
76    pub runtime_committee_protocol: Version,
77}
78
79#[cfg(test)]
80mod test {
81    use super::Version;
82
83    #[test]
84    fn test_version() {
85        assert!(Version {
86            major: 32,
87            minor: 25,
88            patch: 10,
89        }
90        .is_compatible_with(&Version {
91            major: 32,
92            minor: 10,
93            patch: 100,
94        }),);
95
96        let v = Version {
97            major: 17,
98            minor: 11,
99            patch: 1,
100        };
101        let vi: u64 = v.into();
102        assert_eq!(v, Version::from(vi));
103    }
104
105    #[test]
106    fn test_version_u64() {
107        for v in vec![
108            Version::default(),
109            Version {
110                major: 0,
111                minor: 0,
112                patch: 0,
113            },
114            Version {
115                major: 1,
116                minor: 1,
117                patch: 1,
118            },
119            Version {
120                major: 10,
121                minor: 20,
122                patch: 30,
123            },
124            Version {
125                major: 300,
126                minor: 400,
127                patch: 500,
128            },
129            Version {
130                major: 30000,
131                minor: 40000,
132                patch: 50000,
133            },
134        ] {
135            let vi: u64 = v.into();
136            assert_eq!(Version::from(vi), v)
137        }
138    }
139}