mithril_common/signable_builder/
signed_entity.rs

1use chrono::{DateTime, Utc};
2
3use super::Artifact;
4use crate::entities::{
5    CardanoStakeDistribution, CardanoTransactionsSnapshot, MithrilStakeDistribution,
6    SignedEntityType, Snapshot,
7};
8#[cfg(any(test, feature = "test_tools"))]
9use crate::test_utils::fake_data;
10
11#[cfg(any(test, feature = "test_tools"))]
12use crate::entities::{CardanoDbBeacon, Epoch};
13
14/// Aggregate for signed entity
15#[derive(Debug, Clone)]
16pub struct SignedEntity<T>
17where
18    T: Artifact,
19{
20    /// Signed entity id.
21    pub signed_entity_id: String,
22
23    /// Signed entity type.
24    pub signed_entity_type: SignedEntityType,
25
26    /// Certificate id for this signed entity.
27    pub certificate_id: String,
28
29    /// Artifact
30    pub artifact: T,
31
32    /// Date and time when the signed_entity was created
33    pub created_at: DateTime<Utc>,
34}
35
36impl SignedEntity<Snapshot> {
37    cfg_test_tools! {
38        /// Create a dummy [SignedEntity] for [Snapshot] entity
39        pub fn dummy() -> Self {
40            SignedEntity {
41                signed_entity_id: "snapshot-id-123".to_string(),
42                signed_entity_type: SignedEntityType::CardanoImmutableFilesFull(CardanoDbBeacon::default()),
43                certificate_id: "certificate-hash-123".to_string(),
44                artifact: fake_data::snapshots(1)[0].to_owned(),
45                created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
46                    .unwrap()
47                    .with_timezone(&Utc),
48            }
49        }
50    }
51}
52
53impl SignedEntity<MithrilStakeDistribution> {
54    cfg_test_tools! {
55        /// Create a dummy [SignedEntity] for [MithrilStakeDistribution] entity
56        pub fn dummy() -> Self {
57            SignedEntity {
58                signed_entity_id: "mithril-stake-distribution-id-123".to_string(),
59                signed_entity_type: SignedEntityType::MithrilStakeDistribution(Epoch(1)),
60                certificate_id: "certificate-hash-123".to_string(),
61                artifact: fake_data::mithril_stake_distributions(1)[0].to_owned(),
62                created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
63                    .unwrap()
64                    .with_timezone(&Utc),
65            }
66        }
67    }
68}
69
70impl SignedEntity<CardanoTransactionsSnapshot> {
71    cfg_test_tools! {
72        /// Create a dummy [SignedEntity] for [CardanoTransactionsSnapshot] entity
73        pub fn dummy() -> Self {
74            let block_number = crate::entities::BlockNumber(50);
75            SignedEntity {
76                signed_entity_id: "snapshot-id-123".to_string(),
77                signed_entity_type: SignedEntityType::CardanoTransactions(Epoch(5), block_number),
78                certificate_id: "certificate-hash-123".to_string(),
79                artifact: CardanoTransactionsSnapshot::new("mkroot123".to_string(), block_number),
80                created_at: DateTime::parse_from_rfc3339("2023-01-19T13:43:05.618857482Z")
81                    .unwrap()
82                    .with_timezone(&Utc),
83            }
84        }
85    }
86}
87
88impl SignedEntity<CardanoStakeDistribution> {
89    cfg_test_tools! {
90        /// Create a dummy [SignedEntity] for [CardanoStakeDistribution] entity
91        pub fn dummy() -> Self {
92            SignedEntity {
93                signed_entity_id: "cardano-stake-distribution-id-123".to_string(),
94                signed_entity_type: SignedEntityType::CardanoStakeDistribution(Epoch(1)),
95                certificate_id: "certificate-hash-123".to_string(),
96                artifact: fake_data::cardano_stake_distributions(1)[0].to_owned(),
97                created_at: DateTime::parse_from_rfc3339("2024-07-29T16:15:05.618857482Z")
98                    .unwrap()
99                    .with_timezone(&Utc),
100            }
101        }
102    }
103}