mithril_common/messages/
cardano_database_digest_list.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
use serde::{Deserialize, Serialize};

use crate::entities::{HexEncodedDigest, ImmutableFileName};

/// Message structure of a Cardano database digests list
pub type CardanoDatabaseDigestListMessage = Vec<CardanoDatabaseDigestListItemMessage>;

/// Message structure of a Cardano database digest list item
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CardanoDatabaseDigestListItemMessage {
    /// Immutable file name
    pub immutable_file_name: ImmutableFileName,

    /// Digest of an immutable file
    pub digest: HexEncodedDigest,
}

impl CardanoDatabaseDigestListItemMessage {
    /// Return a dummy test entity (test-only).
    pub fn dummy() -> Self {
        Self {
            immutable_file_name: "06685.chunk".to_string(),
            digest: "0af556ab2620dd9363bf76963a231abe8948a500ea6be31b131d87907ab09b1e".to_string(),
        }
    }
}

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

    const ACTUAL_JSON: &str = r#"
    [
        {
            "immutable_file_name": "06685.chunk",
            "digest": "0af556ab2620dd9363bf76963a231abe8948a500ea6be31b131d87907ab09b1e"
        }
    ]"#;

    fn golden_actual_message() -> CardanoDatabaseDigestListMessage {
        vec![CardanoDatabaseDigestListItemMessage {
            immutable_file_name: "06685.chunk".to_string(),
            digest: "0af556ab2620dd9363bf76963a231abe8948a500ea6be31b131d87907ab09b1e".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: CardanoDatabaseDigestListMessage = serde_json::from_str(json).expect(
            "This JSON is expected to be successfully parsed into a CardanoDatabaseDigestListMessage instance.",
        );

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