iota_package_management/
system_package_versions.rs1use std::{collections::BTreeMap, sync::LazyLock};
6
7use anyhow::Context;
8use iota_protocol_config::ProtocolVersion;
9
10static VERSION_TABLE: LazyLock<BTreeMap<ProtocolVersion, SystemPackagesVersion>> =
14 LazyLock::new(|| {
15 BTreeMap::from(include!(concat!(
16 env!("OUT_DIR"),
17 "/system_packages_version_table.rs"
18 )))
19 });
20
21#[derive(Debug)]
22pub struct SystemPackagesVersion {
23 pub git_revision: String,
24 pub packages: Vec<SystemPackage>,
25}
26
27#[derive(Debug)]
28pub struct SystemPackage {
29 pub package_name: String,
31
32 pub repo_path: String,
35}
36
37impl PartialEq for SystemPackagesVersion {
38 fn eq(&self, other: &Self) -> bool {
39 self.git_revision == other.git_revision
40 }
41}
42
43pub fn latest_system_packages() -> &'static SystemPackagesVersion {
45 VERSION_TABLE
46 .last_key_value()
47 .expect("known system package version table should be nonempty")
48 .1
49}
50
51pub fn system_packages_for_protocol(
64 version: ProtocolVersion,
65) -> anyhow::Result<(&'static SystemPackagesVersion, ProtocolVersion)> {
66 let (protocol, system_packages) = VERSION_TABLE
67 .range(..=version)
68 .next_back()
69 .context(format!("Unrecognized protocol version {version:?}"))?;
70 Ok((system_packages, *protocol))
71}
72
73#[test]
74fn test_nonempty_version_table() {
76 assert!(!VERSION_TABLE.is_empty());
77}
78
79#[test]
80fn test_exact_version() {
82 let (system_packages, protocol) = system_packages_for_protocol(4.into()).unwrap();
83 assert_eq!(system_packages.git_revision, "49d5d7d99313");
84 assert_eq!(protocol, 4.into());
85 assert!(
86 system_packages
87 .packages
88 .iter()
89 .any(|p| p.package_name == "Iota")
90 );
91}
92
93#[test]
94#[ignore = "this test is not applicable, as we do not have version gaps yet."]
95fn test_gap_version() {
98 assert_eq!(
100 system_packages_for_protocol(4.into()).unwrap(),
101 system_packages_for_protocol(3.into()).unwrap(),
102 );
103 assert_ne!(
105 system_packages_for_protocol(5.into()).unwrap(),
106 system_packages_for_protocol(3.into()).unwrap(),
107 );
108}
109
110#[test]
111fn test_version_latest() {
113 assert_eq!(
114 system_packages_for_protocol(ProtocolVersion::MAX)
115 .unwrap()
116 .0,
117 latest_system_packages()
118 );
119
120 assert_eq!(
121 system_packages_for_protocol(ProtocolVersion::MAX + 1)
122 .unwrap()
123 .0,
124 latest_system_packages()
125 )
126}
127
128#[test]
129fn test_version_errors() {
131 assert!(system_packages_for_protocol(0.into()).is_err());
132}