Skip to content

Commit

Permalink
feat: Implement an Unknown variant in the artifact locations
Browse files Browse the repository at this point in the history
  • Loading branch information
sfauvel committed Feb 18, 2025
1 parent 9a2faa1 commit 21f5408
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 27 deletions.
100 changes: 73 additions & 27 deletions mithril-client-cli/src/commands/cardano_db_v2/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,18 @@ fn digest_location_rows(locations: &[DigestLocation]) -> Vec<Vec<CellStruct>> {

locations
.iter()
.filter(|location| *location != &DigestLocation::Unknown)
.enumerate()
.map(|(index, location)| match location {
DigestLocation::Aggregator { uri } => {
vec![
format!("{location_name} ({})", index + 1).cell(),
format!("Aggregator, uri: \"{}\"", uri).cell(),
]
}
DigestLocation::CloudStorage { uri } => {
vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, uri: \"{}\"", uri).cell(),
]
}
.filter_map(|(index, location)| match location {
DigestLocation::Aggregator { uri } => Some(vec![
format!("{location_name} ({})", index + 1).cell(),
format!("Aggregator, uri: \"{}\"", uri).cell(),
]),
DigestLocation::CloudStorage { uri } => Some(vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, uri: \"{}\"", uri).cell(),
]),
DigestLocation::Unknown => None,
})
.collect()
}
Expand All @@ -145,16 +143,16 @@ fn immutables_location_rows(locations: &[ImmutablesLocation]) -> Vec<Vec<CellStr

locations
.iter()
.filter(|location| *location != &ImmutablesLocation::Unknown)
.enumerate()
.map(|(index, location)| match location {
.filter_map(|(index, location)| match location {
ImmutablesLocation::CloudStorage { uri } => match uri {
MultiFilesUri::Template(template_uri) => {
vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, template_uri: \"{}\"", template_uri.0).cell(),
]
}
MultiFilesUri::Template(template_uri) => Some(vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, template_uri: \"{}\"", template_uri.0).cell(),
]),
},
ImmutablesLocation::Unknown => None,
})
.collect()
}
Expand All @@ -164,14 +162,14 @@ fn ancillary_location_rows(locations: &[AncillaryLocation]) -> Vec<Vec<CellStruc

locations
.iter()
.filter(|location| *location != &AncillaryLocation::Unknown)
.enumerate()
.map(|(index, location)| match location {
AncillaryLocation::CloudStorage { uri } => {
vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, uri: \"{}\"", uri).cell(),
]
}
.filter_map(|(index, location)| match location {
AncillaryLocation::CloudStorage { uri } => Some(vec![
format!("{location_name} ({})", index + 1).cell(),
format!("CloudStorage, uri: \"{}\"", uri).cell(),
]),
AncillaryLocation::Unknown => None,
})
.collect()
}
Expand Down Expand Up @@ -212,6 +210,22 @@ mod tests {
assert!(rows_rendered.contains("Aggregator, uri: \"http://aggregator.net/\""));
}

#[test]
fn digest_location_rows_display_and_count_only_known_location() {
let locations = vec![
DigestLocation::Unknown,
DigestLocation::CloudStorage {
uri: "http://cloudstorage.com/".to_string(),
},
];

let rows = digest_location_rows(&locations);
assert_eq!(1, rows.len());

let rows_rendered = rows.table().display().unwrap().to_string();
assert!(rows_rendered.contains("Digest location (1)"));
}

#[test]
fn immutables_location_rows_when_no_uri_found() {
let rows = immutables_location_rows(&[]);
Expand Down Expand Up @@ -243,6 +257,22 @@ mod tests {
assert!(rows_rendered.contains("CloudStorage, template_uri: \"http://cloudstorage2.com/\""));
}

#[test]
fn immutables_location_row_display_and_count_only_known_location() {
let locations = vec![
ImmutablesLocation::Unknown {},
ImmutablesLocation::CloudStorage {
uri: MultiFilesUri::Template(TemplateUri("http://cloudstorage2.com/".to_string())),
},
];

let rows = immutables_location_rows(&locations);
assert_eq!(1, rows.len());

let rows_rendered = rows.table().display().unwrap().to_string();
assert!(rows_rendered.contains("Immutables location (1)"));
}

#[test]
fn ancillary_location_rows_when_no_uri_found() {
let rows = ancillary_location_rows(&[]);
Expand Down Expand Up @@ -273,4 +303,20 @@ mod tests {
assert!(rows_rendered.contains("Ancillary location (2)"));
assert!(rows_rendered.contains("CloudStorage, uri: \"http://cloudstorage2.com/\""));
}

#[test]
fn ancillary_location_rows_display_and_count_only_known_location() {
let locations = vec![
AncillaryLocation::Unknown {},
AncillaryLocation::CloudStorage {
uri: "http://cloudstorage2.com/".to_string(),
},
];

let rows = ancillary_location_rows(&locations);
assert_eq!(1, rows.len());

let rows_rendered = rows.table().display().unwrap().to_string();
assert!(rows_rendered.contains("Ancillary location (1)"));
}
}
9 changes: 9 additions & 0 deletions mithril-common/src/entities/cardano_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ pub enum DigestLocation {
/// URI of the cloud storage location.
uri: String,
},
/// Other location not known.
#[serde(other)]
Unknown,
}

/// Locations of the immutable files.
Expand All @@ -91,6 +94,9 @@ pub enum ImmutablesLocation {
/// URI of the cloud storage location.
uri: MultiFilesUri,
},
/// Other location not known.
#[serde(other)]
Unknown,
}

/// Locations of the ancillary files.
Expand All @@ -102,6 +108,9 @@ pub enum AncillaryLocation {
/// URI of the cloud storage location.
uri: String,
},
/// Other location not known.
#[serde(other)]
Unknown,
}

/// Locations of the Cardano database related files.
Expand Down
50 changes: 50 additions & 0 deletions mithril-common/src/messages/cardano_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,54 @@ mod tests {

assert_eq!(golden_current_message(), message);
}

#[test]
fn test_a_future_json_deserialized_with_unknown_location_types() {
let json = r#"
{
"hash": "d4071d518a3ace0f6c04a9c0745b9e9560e3e2af1b373bafc4e0398423e9abfb",
"merkle_root": "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6",
"beacon": {
"epoch": 123,
"immutable_file_number": 2345
},
"certificate_hash": "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb",
"total_db_size_uncompressed": 800796318,
"locations": {
"digests": [
{
"type": "whatever",
"new_field": "digest-1"
}
],
"immutables": [
{
"type": "whatever",
"new_field": [123, 125]
}
],
"ancillary": [
{
"type": "whatever",
"new_field": "ancillary-3"
}
]
},
"compression_algorithm": "gzip",
"cardano_node_version": "0.0.1",
"created_at": "2023-01-19T13:43:05.618857482Z"
}"#;
let message: CardanoDatabaseSnapshotMessage = serde_json::from_str(json).expect(
"This JSON is expected to be successfully parsed into a CardanoDatabaseSnapshotMessage instance.",
);

assert_eq!(message.locations.digests.len(), 1);
assert_eq!(DigestLocation::Unknown, message.locations.digests[0]);

assert_eq!(message.locations.immutables.len(), 1);
assert_eq!(ImmutablesLocation::Unknown, message.locations.immutables[0]);

assert_eq!(message.locations.ancillary.len(), 1);
assert_eq!(AncillaryLocation::Unknown, message.locations.ancillary[0]);
}
}

0 comments on commit 21f5408

Please sign in to comment.