Skip to content

Commit 2f42cb8

Browse files
committed
Adapt build script to new fake aggregator data structure
1 parent f79cf02 commit 2f42cb8

File tree

5 files changed

+84
-47
lines changed

5 files changed

+84
-47
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mithril-build-script/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ include = ["**/*.rs", "Cargo.toml", "README.md", ".gitignore"]
1111

1212
[dependencies]
1313
semver = "1.0.21"
14+
serde_json = "1.0.115"
1415
serde_yaml = "0.9.31"

internal/mithril-build-script/src/fake_aggregator.rs

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ use std::collections::BTreeMap;
22
use std::collections::BTreeSet;
33
use std::fmt::Write as _;
44
use std::fs;
5+
use std::fs::File;
56
use std::path::Path;
67

8+
use serde_json;
9+
710
pub type ArtifactId = String;
811
pub type FileContent = String;
912

@@ -29,11 +32,6 @@ pub struct FakeAggregatorData {
2932

3033
impl FakeAggregatorData {
3134
pub fn load_from_folder(folder: &Path) -> Self {
32-
fn extract_artifact_id(filename: &str, prefix: &str) -> String {
33-
let id_with_extension = filename.strip_prefix(prefix).unwrap();
34-
id_with_extension.strip_suffix(".json").unwrap().to_string()
35-
}
36-
3735
let mut data = FakeAggregatorData::default();
3836

3937
for entry in list_json_files_in_folder(folder) {
@@ -49,41 +47,32 @@ impl FakeAggregatorData {
4947
"epoch-settings.json" => {
5048
data.epoch_settings = file_content;
5149
}
52-
"mithril-stake-distributions.json" => {
50+
"mithril-stake-distributions-list.json" => {
5351
data.msds_list = file_content;
5452
}
55-
"snapshots.json" => {
53+
"snapshots-list.json" => {
5654
data.snapshots_list = file_content;
5755
}
58-
"certificates.json" => {
56+
"certificates-list.json" => {
5957
data.certificates_list = file_content;
6058
}
61-
"ctx-snapshots.json" => {
59+
"ctx-snapshots-list.json" => {
6260
data.ctx_snapshots_list = file_content;
6361
}
64-
_ if filename.starts_with("mithril-stake-distribution-") => {
65-
data.individual_msds.insert(
66-
extract_artifact_id(&filename, "mithril-stake-distribution-"),
67-
file_content,
68-
);
62+
"mithril-stake-distributions.json" => {
63+
data.individual_msds = Self::read_artifacts_json_file(&entry.path());
6964
}
70-
_ if filename.starts_with("snapshot-") => {
71-
data.individual_snapshots
72-
.insert(extract_artifact_id(&filename, "snapshot-"), file_content);
65+
"snapshots.json" => {
66+
data.individual_snapshots = Self::read_artifacts_json_file(&entry.path());
7367
}
74-
_ if filename.starts_with("certificate-") => {
75-
data.individual_certificates
76-
.insert(extract_artifact_id(&filename, "certificate-"), file_content);
68+
"certificates.json" => {
69+
data.individual_certificates = Self::read_artifacts_json_file(&entry.path());
7770
}
78-
_ if filename.starts_with("ctx-snapshot-") => {
79-
data.individual_ctx_snapshots.insert(
80-
extract_artifact_id(&filename, "ctx-snapshot-"),
81-
file_content,
82-
);
71+
"ctx-snapshots.json" => {
72+
data.individual_ctx_snapshots = Self::read_artifacts_json_file(&entry.path());
8373
}
84-
_ if filename.starts_with("ctx-proof-") => {
85-
data.ctx_proofs
86-
.insert(extract_artifact_id(&filename, "ctx-proof-"), file_content);
74+
"ctx-proofs.json" => {
75+
data.ctx_proofs = Self::read_artifacts_json_file(&entry.path());
8776
}
8877
// unknown file
8978
_ => {}
@@ -177,9 +166,31 @@ impl FakeAggregatorData {
177166
)
178167
)
179168
}
169+
170+
fn read_artifacts_json_file(json_file: &Path) -> BTreeMap<ArtifactId, FileContent> {
171+
let file = File::open(json_file).unwrap();
172+
let parsed_json: serde_json::Value = serde_json::from_reader(&file).unwrap();
173+
174+
let json_object = parsed_json.as_object().unwrap();
175+
let res: Result<Vec<_>, _> = json_object
176+
.iter()
177+
.map(|(key, value)| extract_artifact_id_and_content(key, value))
178+
.collect();
179+
180+
let tree = BTreeMap::from_iter(res.unwrap());
181+
tree
182+
}
183+
}
184+
185+
fn extract_artifact_id_and_content(
186+
key: &String,
187+
value: &serde_json::Value,
188+
) -> Result<(ArtifactId, FileContent), String> {
189+
let json_content = serde_json::to_string(value).map_err(|e| e.to_string())?;
190+
Ok((key.to_owned(), json_content))
180191
}
181192

182-
pub fn list_json_files_in_folder(folder: &Path) -> impl Iterator<Item = std::fs::DirEntry> + '_ {
193+
pub fn list_json_files_in_folder(folder: &Path) -> impl Iterator<Item = fs::DirEntry> + '_ {
183194
crate::list_files_in_folder(folder)
184195
.filter(|e| e.file_name().to_string_lossy().ends_with(".json"))
185196
}
@@ -253,6 +264,8 @@ pub fn generate_ids_array(array_name: &str, ids: BTreeSet<ArtifactId>) -> String
253264

254265
#[cfg(test)]
255266
mod tests {
267+
use crate::get_temp_dir;
268+
256269
use super::*;
257270

258271
#[test]
@@ -312,4 +325,24 @@ fn b() {}
312325
)
313326
)
314327
}
328+
329+
#[test]
330+
fn read_artifacts_json_file() {
331+
let dir = get_temp_dir("read_artifacts_json_file");
332+
let file = dir.join("test.json");
333+
let json_content = r#"{
334+
"hash1": { "name": "artifact1" },
335+
"hash2": { "name": "artifact2" }
336+
}"#;
337+
let expected = BTreeMap::from([
338+
("hash1".to_string(), r#"{"name":"artifact1"}"#.to_string()),
339+
("hash2".to_string(), r#"{"name":"artifact2"}"#.to_string()),
340+
]);
341+
342+
fs::write(&file, json_content).unwrap();
343+
344+
let id_per_json = FakeAggregatorData::read_artifacts_json_file(&file);
345+
346+
assert_eq!(expected, id_per_json);
347+
}
315348
}

internal/mithril-build-script/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ pub(crate) fn list_files_in_folder(folder: &Path) -> impl Iterator<Item = fs::Di
6262
})
6363
}
6464

65+
#[cfg(test)]
66+
// Note: adding `mithril-common` as a dev-dependency would lead to a circular dependency, so
67+
// we can't use its `TempDir` api.
68+
pub(crate) fn get_temp_dir(dir_name: &str) -> PathBuf {
69+
let dir = std::env::temp_dir()
70+
.join("mithril_test")
71+
.join("build_script")
72+
.join(dir_name);
73+
74+
if dir.exists() {
75+
fs::remove_dir_all(&dir).unwrap();
76+
}
77+
fs::create_dir_all(&dir).unwrap();
78+
79+
dir
80+
}
81+
6582
#[cfg(test)]
6683
mod tests {
6784
use super::*;

internal/mithril-build-script/src/open_api.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,9 @@ pub fn get_open_api_versions_mapping() -> HashMap<OpenAPIFileName, semver::Versi
7272
#[cfg(test)]
7373
mod tests {
7474
use super::*;
75+
use crate::get_temp_dir;
7576
use std::path::Path;
7677

77-
// Note: adding `mithril-common` as a dev-dependency would lead to a circular dependency, so
78-
// we can't use its `TempDir` api.
79-
fn get_temp_dir(dir_name: &str) -> PathBuf {
80-
let dir = std::env::temp_dir()
81-
.join("mithril_test")
82-
.join("build_script")
83-
.join(dir_name);
84-
85-
if dir.exists() {
86-
fs::remove_dir_all(&dir).unwrap();
87-
}
88-
fs::create_dir_all(&dir).unwrap();
89-
90-
dir
91-
}
92-
9378
fn write_minimal_open_api_file(version: &str, path: &Path) {
9479
fs::write(
9580
path,

0 commit comments

Comments
 (0)