mithril_common/entities/
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use semver::Version;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use strum::EnumDiscriminants;

use crate::{
    entities::{CardanoDbBeacon, CompressionAlgorithm},
    signable_builder::Artifact,
};

use super::MultiFilesUri;

/// Cardano database snapshot.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CardanoDatabaseSnapshot {
    /// Unique hash of the Cardano database snapshot.
    pub hash: String,

    /// Merkle root of the Cardano database snapshot.
    pub merkle_root: String,

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

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

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

    /// 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,
}

impl CardanoDatabaseSnapshot {
    /// [CardanoDatabaseSnapshot] factory
    pub fn new(
        merkle_root: String,
        beacon: CardanoDbBeacon,
        total_db_size_uncompressed: u64,
        locations: ArtifactsLocations,
        compression_algorithm: CompressionAlgorithm,
        cardano_node_version: &Version,
    ) -> Self {
        let cardano_node_version = format!("{cardano_node_version}");
        let mut cardano_database_snapshot = Self {
            hash: "".to_string(),
            merkle_root,
            beacon,
            locations,
            total_db_size_uncompressed,
            compression_algorithm,
            cardano_node_version,
        };
        cardano_database_snapshot.hash = cardano_database_snapshot.compute_hash();

        cardano_database_snapshot
    }

    /// Cardano database snapshot hash computation
    fn compute_hash(&self) -> String {
        let mut hasher = Sha256::new();
        hasher.update(self.beacon.epoch.to_be_bytes());
        hasher.update(self.merkle_root.as_bytes());

        hex::encode(hasher.finalize())
    }
}

/// Locations of the immutable file digests.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum DigestLocation {
    /// Aggregator digest route location.
    Aggregator {
        /// URI of the aggregator digests route location.
        uri: String,
    },
    /// Cloud storage location.
    CloudStorage {
        /// URI of the cloud storage location.
        uri: String,
    },
}

/// Locations of the immutable files.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum ImmutablesLocation {
    /// Cloud storage location.
    CloudStorage {
        /// URI of the cloud storage location.
        uri: MultiFilesUri,
    },
}

/// Locations of the ancillary files.
#[derive(
    Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, EnumDiscriminants,
)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum AncillaryLocation {
    /// Cloud storage location.
    CloudStorage {
        /// URI of the cloud storage location.
        uri: String,
    },
}

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

    /// Locations of the immutable files.
    pub immutables: Vec<ImmutablesLocation>,

    /// Locations of the ancillary files.
    pub ancillary: Vec<AncillaryLocation>,
}

#[typetag::serde]
impl Artifact for CardanoDatabaseSnapshot {
    fn get_id(&self) -> String {
        self.hash.clone()
    }
}

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

    fn dummy() -> CardanoDatabaseSnapshot {
        CardanoDatabaseSnapshot::new(
            "mk-root-1111111111".to_string(),
            CardanoDbBeacon::new(2222, 55555),
            0,
            ArtifactsLocations {
                digests: vec![],
                immutables: vec![],
                ancillary: vec![],
            },
            CompressionAlgorithm::Gzip,
            &Version::new(1, 0, 0),
        )
    }

    mod cardano_database_snapshot_compute_hash {
        use super::*;

        #[test]
        fn test_cardano_database_snapshot_compute_hash() {
            let cardano_database_snapshot = CardanoDatabaseSnapshot {
                merkle_root: "mk-root-123".to_string(),
                beacon: CardanoDbBeacon::new(123, 98),
                ..dummy()
            };

            assert_eq!(
                "b1cc5e0deaa7856e8e811e349d6e639fa667aa70288602955f438c5893ce29c8",
                cardano_database_snapshot.compute_hash()
            );
        }

        #[test]
        fn compute_hash_returns_same_hash_with_same_cardano_database_snapshot() {
            assert_eq!(
                CardanoDatabaseSnapshot {
                    merkle_root: "mk-root-123".to_string(),
                    beacon: CardanoDbBeacon::new(123, 98),
                    ..dummy()
                }
                .compute_hash(),
                CardanoDatabaseSnapshot {
                    merkle_root: "mk-root-123".to_string(),
                    beacon: CardanoDbBeacon::new(123, 98),
                    ..dummy()
                }
                .compute_hash()
            );
        }

        #[test]
        fn compute_hash_returns_different_hash_with_different_merkle_root() {
            assert_ne!(
                CardanoDatabaseSnapshot {
                    merkle_root: "mk-root-123".to_string(),
                    beacon: CardanoDbBeacon::new(123, 98),
                    ..dummy()
                }
                .compute_hash(),
                CardanoDatabaseSnapshot {
                    merkle_root: "mk-root-456".to_string(),
                    beacon: CardanoDbBeacon::new(123, 98),
                    ..dummy()
                }
                .compute_hash()
            );
        }

        #[test]
        fn compute_hash_returns_different_hash_with_same_epoch_in_beacon() {
            assert_eq!(
                CardanoDatabaseSnapshot {
                    merkle_root: "mk-root-123".to_string(),
                    beacon: CardanoDbBeacon::new(123, 98),
                    ..dummy()
                }
                .compute_hash(),
                CardanoDatabaseSnapshot {
                    merkle_root: "mk-root-123".to_string(),
                    beacon: CardanoDbBeacon::new(123, 12),
                    ..dummy()
                }
                .compute_hash()
            );
        }

        #[test]
        fn compute_hash_returns_different_hash_with_different_beacon() {
            assert_ne!(
                CardanoDatabaseSnapshot {
                    merkle_root: "mk-root-123".to_string(),
                    beacon: CardanoDbBeacon::new(123, 98),
                    ..dummy()
                }
                .compute_hash(),
                CardanoDatabaseSnapshot {
                    merkle_root: "mk-root-123".to_string(),
                    beacon: CardanoDbBeacon::new(456, 98),
                    ..dummy()
                }
                .compute_hash()
            );
        }
    }
}