mithril_common/messages/
cardano_blocks_transactions_snapshot_list.rs

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