Skip to content

Commit aabd9fc

Browse files
authored
Merge pull request #1600 from input-output-hk/ensemble/1594/aggregate-fake-aggregator-json-data
Aggregate fake aggregator reference json files
2 parents b9e7813 + e55fc4e commit aabd9fc

File tree

143 files changed

+2021
-4266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+2021
-4266
lines changed

Cargo.lock

Lines changed: 6 additions & 5 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-build-script"
3-
version = "0.1.4"
3+
version = "0.2.0"
44
description = "A toolbox for Mithril crates build scripts"
55
authors = { workspace = true }
66
edition = { workspace = true }
@@ -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: 75 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,30 @@ 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+
BTreeMap::from_iter(res.unwrap())
181+
}
180182
}
181183

182-
pub fn list_json_files_in_folder(folder: &Path) -> impl Iterator<Item = std::fs::DirEntry> + '_ {
184+
fn extract_artifact_id_and_content(
185+
key: &String,
186+
value: &serde_json::Value,
187+
) -> Result<(ArtifactId, FileContent), String> {
188+
let json_content = serde_json::to_string_pretty(value).map_err(|e| e.to_string())?;
189+
Ok((key.to_owned(), json_content))
190+
}
191+
192+
pub fn list_json_files_in_folder(folder: &Path) -> impl Iterator<Item = fs::DirEntry> + '_ {
183193
crate::list_files_in_folder(folder)
184194
.filter(|e| e.file_name().to_string_lossy().ends_with(".json"))
185195
}
@@ -253,6 +263,8 @@ pub fn generate_ids_array(array_name: &str, ids: BTreeSet<ArtifactId>) -> String
253263

254264
#[cfg(test)]
255265
mod tests {
266+
use crate::get_temp_dir;
267+
256268
use super::*;
257269

258270
#[test]
@@ -312,4 +324,38 @@ fn b() {}
312324
)
313325
)
314326
}
327+
328+
#[test]
329+
fn parse_artifacts_json_into_btree_of_key_and_pretty_sub_json() {
330+
let dir = get_temp_dir("read_artifacts_json_file");
331+
let file = dir.join("test.json");
332+
fs::write(
333+
&file,
334+
r#"{
335+
"hash1": { "name": "artifact1" },
336+
"hash2": { "name": "artifact2" }
337+
}"#,
338+
)
339+
.unwrap();
340+
341+
let id_per_json = FakeAggregatorData::read_artifacts_json_file(&file);
342+
343+
let expected = BTreeMap::from([
344+
(
345+
"hash1".to_string(),
346+
r#"{
347+
"name": "artifact1"
348+
}"#
349+
.to_string(),
350+
),
351+
(
352+
"hash2".to_string(),
353+
r#"{
354+
"name": "artifact2"
355+
}"#
356+
.to_string(),
357+
),
358+
]);
359+
assert_eq!(expected, id_per_json);
360+
}
315361
}

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,

mithril-common/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-common"
3-
version = "0.3.23"
3+
version = "0.3.24"
44
description = "Common types, interfaces, and utilities for Mithril nodes."
55
authors = { workspace = true }
66
edition = { workspace = true }
@@ -98,7 +98,7 @@ slog-term = "2.9.0"
9898
tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread", "time"] }
9999

100100
[build-dependencies]
101-
mithril-build-script = { path = "../internal/mithril-build-script", version = "=0.1.4" }
101+
mithril-build-script = { path = "../internal/mithril-build-script", version = "=0.2.0" }
102102

103103
[features]
104104
default = []

mithril-test-lab/mithril-aggregator-fake/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator-fake"
3-
version = "0.2.5"
3+
version = "0.3.0"
44
description = "Mithril Fake Aggregator for client testing"
55
authors = { workspace = true }
66
documentation = { workspace = true }

mithril-test-lab/mithril-aggregator-fake/default_data/certificate-006416c78aa3180971c2bcd74ecc6127cd68a035a3981508285b14573b22bee5.json

Lines changed: 0 additions & 38 deletions
This file was deleted.

mithril-test-lab/mithril-aggregator-fake/default_data/certificate-02c191920d42218d5b942677745c79560e2a6a95cc68ac63906591662ac31b24.json

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)