-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #703 from input-output-hk/jpraynaud/698-add-snapsh…
…ot-list-message Add snapshot list message and adapter
- Loading branch information
Showing
17 changed files
with
206 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
mithril-aggregator/src/message_adapters/to_snapshot_list_message.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use mithril_common::entities::Snapshot; | ||
use mithril_common::messages::{SnapshotListItemMessage, SnapshotListMessage}; | ||
|
||
/// Adapter to convert a list of [Snapshot] to [SnapshotListMessage] instances | ||
pub struct ToSnapshotListMessageAdapter; | ||
|
||
impl ToSnapshotListMessageAdapter { | ||
/// Method to trigger the conversion | ||
pub fn adapt(snapshots: Vec<Snapshot>) -> SnapshotListMessage { | ||
snapshots | ||
.into_iter() | ||
.map(|snapshot| SnapshotListItemMessage { | ||
digest: snapshot.digest, | ||
beacon: snapshot.beacon, | ||
certificate_hash: snapshot.certificate_hash, | ||
size: snapshot.size, | ||
created_at: snapshot.created_at, | ||
locations: snapshot.locations, | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use mithril_common::test_utils::fake_data; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn adapt_ok() { | ||
let mut snapshot = fake_data::snapshots(1)[0].to_owned(); | ||
snapshot.digest = "digest123".to_string(); | ||
let snapshot_list_message = ToSnapshotListMessageAdapter::adapt(vec![snapshot]); | ||
|
||
assert_eq!("digest123".to_string(), snapshot_list_message[0].digest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
mithril-client/src/message_adapters/from_snapshot_list_message.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use mithril_common::entities::Snapshot; | ||
use mithril_common::messages::SnapshotListMessage; | ||
|
||
/// Adapter to convert [SnapshotListMessage] to [SnapshotList] instances | ||
pub struct FromSnapshotListMessageAdapter; | ||
|
||
impl FromSnapshotListMessageAdapter { | ||
/// Method to trigger the conversion | ||
pub fn adapt(snapshot_list_message: SnapshotListMessage) -> Vec<Snapshot> { | ||
snapshot_list_message | ||
.into_iter() | ||
.map(|snapshot_list_item_message| Snapshot { | ||
digest: snapshot_list_item_message.digest, | ||
beacon: snapshot_list_item_message.beacon, | ||
certificate_hash: snapshot_list_item_message.certificate_hash, | ||
size: snapshot_list_item_message.size, | ||
created_at: snapshot_list_item_message.created_at, | ||
locations: snapshot_list_item_message.locations, | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use mithril_common::messages::SnapshotListItemMessage; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn adapt_ok() { | ||
let snapshot_list_message: SnapshotListMessage = vec![SnapshotListItemMessage::dummy()]; | ||
let snapshot_list = FromSnapshotListMessageAdapter::adapt(snapshot_list_message.clone()); | ||
|
||
assert_eq!(snapshot_list_message[0].digest, snapshot_list[0].digest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod from_certificate_message_adapter; | ||
mod from_snapshot_list_message; | ||
mod from_snapshot_message; | ||
|
||
pub use from_certificate_message_adapter::FromCertificateMessageAdapter; | ||
pub use from_snapshot_list_message::FromSnapshotListMessageAdapter; | ||
pub use from_snapshot_message::FromSnapshotMessageAdapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::entities::Beacon; | ||
|
||
#[cfg(any(test, feature = "test_only"))] | ||
use crate::entities::Epoch; | ||
|
||
/// Message structure of a snapshot list | ||
pub type SnapshotListMessage = Vec<SnapshotListItemMessage>; | ||
|
||
/// Message structure of a snapshot list item | ||
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)] | ||
pub struct SnapshotListItemMessage { | ||
/// Digest that is signed by the signer participants | ||
pub digest: String, | ||
|
||
/// Mithril beacon on the Cardano chain | ||
pub beacon: Beacon, | ||
|
||
/// Hash of the associated certificate | ||
pub certificate_hash: String, | ||
|
||
/// Size of the snapshot file in Bytes | ||
pub size: u64, | ||
|
||
/// Date and time at which the snapshot was created | ||
pub created_at: String, | ||
|
||
/// Locations where the binary content of the snapshot can be retrieved | ||
pub locations: Vec<String>, | ||
} | ||
|
||
impl SnapshotListItemMessage { | ||
#[cfg(any(test, feature = "test_only"))] | ||
/// Return a dummy test entity (test-only). | ||
pub fn dummy() -> Self { | ||
Self { | ||
digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(), | ||
beacon: Beacon { | ||
network: "preview".to_string(), | ||
epoch: Epoch(86), | ||
immutable_file_number: 1728, | ||
}, | ||
certificate_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb" | ||
.to_string(), | ||
size: 807803196, | ||
created_at: "2023-01-19T13:43:05.618857482Z".to_string(), | ||
locations: vec!["https://host/certificate.tar.gz".to_string()], | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
fn golden_message() -> SnapshotListMessage { | ||
vec![SnapshotListItemMessage { | ||
digest: "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6".to_string(), | ||
beacon: Beacon { | ||
network: "preview".to_string(), | ||
epoch: Epoch(86), | ||
immutable_file_number: 1728, | ||
}, | ||
certificate_hash: "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb" | ||
.to_string(), | ||
size: 807803196, | ||
created_at: "2023-01-19T13:43:05.618857482Z".to_string(), | ||
locations: vec!["https://host/certificate.tar.gz".to_string()], | ||
}] | ||
} | ||
|
||
// Test the retro compatibility with possible future upgrades. | ||
#[test] | ||
fn test_v1() { | ||
let json = r#"[{ | ||
"digest": "0b9f5ad7f33cc523775c82249294eb8a1541d54f08eb3107cafc5638403ec7c6", | ||
"beacon": { | ||
"network": "preview", | ||
"epoch": 86, | ||
"immutable_file_number": 1728 | ||
}, | ||
"certificate_hash": "d5daf6c03ace4a9c074e951844075b9b373bafc4e039160e3e2af01823e9abfb", | ||
"size": 807803196, | ||
"created_at": "2023-01-19T13:43:05.618857482Z", | ||
"locations": [ | ||
"https://host/certificate.tar.gz" | ||
] | ||
}]"#; | ||
let message: SnapshotListMessage = serde_json::from_str(json).expect( | ||
"This JSON is expected to be succesfully parsed into a SnapshotListMessage instance.", | ||
); | ||
|
||
assert_eq!(golden_message(), message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters