mithril_common/messages/
cardano_database.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::entities::{
    AncillaryLocation, ArtifactsLocations, CardanoDbBeacon, CompressionAlgorithm, DigestLocation,
    Epoch, ImmutablesLocation,
};

/// Locations of the Cardano database related files.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactsLocationsMessagePart {
    /// Locations of the the immutable file digests.
    pub digests: Vec<DigestLocation>,
    /// Locations of the immutable files.
    pub immutables: Vec<ImmutablesLocation>,
    /// Locations of the ancillary files.
    pub ancillary: Vec<AncillaryLocation>,
}

impl From<ArtifactsLocations> for ArtifactsLocationsMessagePart {
    fn from(part: ArtifactsLocations) -> Self {
        Self {
            digests: part.digests,
            immutables: part.immutables,
            ancillary: part.ancillary,
        }
    }
}

/// Cardano database snapshot.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CardanoDatabaseSnapshotMessage {
    /// Merkle root of the Cardano database snapshot.
    pub merkle_root: String,

    /// Mithril beacon on the Cardano chain.
    pub beacon: CardanoDbBeacon,

    /// Hash of the associated certificate
    pub certificate_hash: String,

    /// Size of the uncompressed Cardano database files.
    pub total_db_size_uncompressed: u64,

    /// Locations of the Cardano database artifacts.
    pub locations: ArtifactsLocationsMessagePart,

    /// Compression algorithm of the Cardano database artifacts.
    pub compression_algorithm: CompressionAlgorithm,

    /// Version of the Cardano node used to create the snapshot.
    pub cardano_node_version: String,

    /// Date and time at which the snapshot was created
    pub created_at: DateTime<Utc>,
}

impl CardanoDatabaseSnapshotMessage {
    /// Return a dummy test entity (test-only).
    pub fn dummy() -> Self {
        Self {
            merkle_root: "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6"
                .to_string(),
            beacon: CardanoDbBeacon {
                epoch: Epoch(123),
                immutable_file_number: 2345,
            },
            certificate_hash: "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb"
                .to_string(),
            total_db_size_uncompressed: 800796318,
            created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
                .unwrap()
                .with_timezone(&Utc),
            locations: ArtifactsLocationsMessagePart {
                digests: vec![DigestLocation::Aggregator {
                    uri: "https://host-1/digest-1".to_string(),
                }],
                immutables: vec![
                    ImmutablesLocation::CloudStorage {
                        uri: "https://host-1/immutables-2".to_string(),
                    },
                    ImmutablesLocation::CloudStorage {
                        uri: "https://host-2/immutables-2".to_string(),
                    },
                ],
                ancillary: vec![AncillaryLocation::CloudStorage {
                    uri: "https://host-1/ancillary-3".to_string(),
                }],
            },
            compression_algorithm: CompressionAlgorithm::Gzip,
            cardano_node_version: "0.0.1".to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const ACTUAL_JSON: &str = r#"
    {
        "merkle_root": "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6",
        "beacon": {
            "epoch": 123,
            "immutable_file_number": 2345
        },
        "certificate_hash": "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb",
        "total_db_size_uncompressed": 800796318,
        "locations": {
            "digests": [
            {
                "type": "aggregator",
                "uri": "https://host-1/digest-1"
            }
            ],
            "immutables": [
            {
                "type": "cloud_storage",
                "uri": "https://host-1/immutables-2"
            },
            {
                "type": "cloud_storage",
                "uri": "https://host-2/immutables-2"
            }
            ],
            "ancillary": [
            {
                "type": "cloud_storage",
                "uri": "https://host-1/ancillary-3"
            }
            ]
        },
        "compression_algorithm": "gzip",
        "cardano_node_version": "0.0.1",
        "created_at": "2023-01-19T13:43:05.618857482Z"
    }"#;

    fn golden_actual_message() -> CardanoDatabaseSnapshotMessage {
        CardanoDatabaseSnapshotMessage {
            merkle_root: "c8224920b9f5ad7377594eb8a15f34f08eb3103cc5241d57cafc5638403ec7c6"
                .to_string(),
            beacon: CardanoDbBeacon {
                epoch: Epoch(123),
                immutable_file_number: 2345,
            },
            certificate_hash: "f6c01b373bafc4e039844071d5da3ace4a9c0745b9e9560e3e2af01823e9abfb"
                .to_string(),
            total_db_size_uncompressed: 800796318,
            created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
                .unwrap()
                .with_timezone(&Utc),
            locations: ArtifactsLocationsMessagePart {
                digests: vec![DigestLocation::Aggregator {
                    uri: "https://host-1/digest-1".to_string(),
                }],
                immutables: vec![
                    ImmutablesLocation::CloudStorage {
                        uri: "https://host-1/immutables-2".to_string(),
                    },
                    ImmutablesLocation::CloudStorage {
                        uri: "https://host-2/immutables-2".to_string(),
                    },
                ],
                ancillary: vec![AncillaryLocation::CloudStorage {
                    uri: "https://host-1/ancillary-3".to_string(),
                }],
            },
            compression_algorithm: CompressionAlgorithm::Gzip,
            cardano_node_version: "0.0.1".to_string(),
        }
    }

    // Test the backward compatibility with possible future upgrades.
    #[test]
    fn test_actual_json_deserialized_into_actual_message() {
        let json = ACTUAL_JSON;
        let message: CardanoDatabaseSnapshotMessage = serde_json::from_str(json).expect(
            "This JSON is expected to be successfully parsed into a CardanoDatabaseSnapshotMessage instance.",
        );

        assert_eq!(golden_actual_message(), message);
    }
}