Skip to content

Commit

Permalink
feat: ensure CardanoDatabaseSnapshot identifiers are unique by comp…
Browse files Browse the repository at this point in the history
…uting a unique hash
  • Loading branch information
dlachaume committed Dec 24, 2024
1 parent ca4ca6f commit 60ac5ed
Showing 1 changed file with 124 additions and 3 deletions.
127 changes: 124 additions & 3 deletions mithril-common/src/entities/cardano_database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use semver::Version;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use strum::EnumDiscriminants;

use crate::{
Expand All @@ -10,6 +11,9 @@ use crate::{
/// 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,

Expand Down Expand Up @@ -41,14 +45,28 @@ impl CardanoDatabaseSnapshot {
) -> Self {
let cardano_node_version = format!("{cardano_node_version}");

Self {
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())
}
}

Expand Down Expand Up @@ -106,6 +124,109 @@ pub struct ArtifactsLocations {
#[typetag::serde]
impl Artifact for CardanoDatabaseSnapshot {
fn get_id(&self) -> String {
self.merkle_root.clone()
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),
)
}

#[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_different_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()
);

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()
);
}
}

0 comments on commit 60ac5ed

Please sign in to comment.