mithril_common/messages/
cardano_blocks_transactions_snapshot.rs

1use chrono::DateTime;
2use chrono::Utc;
3use serde::{Deserialize, Serialize};
4
5use crate::entities::{BlockNumber, Epoch};
6
7/// Message structure of a Cardano Blocks and Transactions snapshot
8#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
9pub struct CardanoBlocksTransactionsSnapshotMessage {
10    /// Merkle root of the Cardano Blocks and Transactions snapshot
11    pub merkle_root: String,
12
13    /// Epoch of the Cardano Blocks and Transactions snapshot
14    pub epoch: Epoch,
15
16    /// The maximum block number signed in the Cardano Blocks and Transactions snapshot
17    pub block_number_signed: BlockNumber,
18
19    /// The block number of the tip of the chain at snapshot time of the Cardano Blocks and Transactions
20    pub block_number_tip: BlockNumber,
21
22    /// Hash of the Cardano Blocks and Transactions snapshot
23    pub hash: String,
24
25    /// Hash of the associated certificate
26    pub certificate_hash: String,
27
28    /// Time of creation
29    pub created_at: DateTime<Utc>,
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    fn golden_message_current() -> CardanoBlocksTransactionsSnapshotMessage {
37        CardanoBlocksTransactionsSnapshotMessage {
38            merkle_root: "mkroot-123".to_string(),
39            epoch: Epoch(7),
40            block_number_signed: BlockNumber(5),
41            block_number_tip: BlockNumber(15),
42            hash: "hash-123".to_string(),
43            certificate_hash: "certificate-hash-123".to_string(),
44            created_at: DateTime::parse_from_rfc3339("2023-01-19T13:41:01.618857482Z")
45                .unwrap()
46                .with_timezone(&Utc),
47        }
48    }
49
50    const CURRENT_JSON: &str = r#"{
51        "merkle_root": "mkroot-123",
52        "epoch": 7,
53        "block_number_signed": 5,
54        "block_number_tip":15,
55        "hash": "hash-123",
56        "certificate_hash": "certificate-hash-123",
57        "created_at": "2023-01-19T13:41:01.618857482Z"
58    }"#;
59
60    #[test]
61    fn test_current_json_deserialized_into_current_message() {
62        let json = CURRENT_JSON;
63        let message: CardanoBlocksTransactionsSnapshotMessage = serde_json::from_str(json).expect(
64            "This JSON is expected to be successfully parsed into a CardanoBlocksTransactionsSnapshotMessage instance.",
65        );
66
67        assert_eq!(golden_message_current(), message);
68    }
69}