mithril_signer/database/record/
signed_beacon_record.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use chrono::{DateTime, Utc};
use sqlite::Row;

use mithril_common::entities::{Epoch, SignedEntityType};
use mithril_persistence::database::Hydrator;
use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity};

use crate::entities::BeaconToSign;

/// Database record of a beacon signed by the signer
#[derive(Debug, Clone, PartialEq)]
pub struct SignedBeaconRecord {
    /// The epoch when the beacon was issued
    pub epoch: Epoch,

    /// The signed entity type to sign
    pub signed_entity_type: SignedEntityType,

    /// Datetime when the beacon was initiated
    pub initiated_at: DateTime<Utc>,

    /// Datetime when the beacon was signed
    pub signed_at: DateTime<Utc>,
}

impl From<BeaconToSign> for SignedBeaconRecord {
    fn from(beacon: BeaconToSign) -> Self {
        Self {
            epoch: beacon.epoch,
            signed_entity_type: beacon.signed_entity_type,
            initiated_at: beacon.initiated_at,
            signed_at: Utc::now(),
        }
    }
}

#[cfg(test)]
impl SignedBeaconRecord {
    /// Create a fake `SignedBeaconRecord` for testing purposes
    ///
    /// The dates fields are set to constant values to make it easier to compare.
    pub(crate) fn fake(epoch: Epoch, signed_entity_type: SignedEntityType) -> Self {
        let initiated_at = DateTime::<Utc>::default();
        Self {
            epoch,
            signed_entity_type,
            initiated_at,
            signed_at: initiated_at + chrono::TimeDelta::minutes(3),
        }
    }

    pub(crate) fn fakes(records: &[(Epoch, Vec<SignedEntityType>)]) -> Vec<Self> {
        records
            .iter()
            .flat_map(|(epoch, signed_entity_types)| {
                signed_entity_types
                    .iter()
                    .map(|se| Self::fake(*epoch, se.clone()))
            })
            .collect()
    }
}

#[cfg(test)]
impl PartialEq<BeaconToSign> for SignedBeaconRecord {
    fn eq(&self, other: &BeaconToSign) -> bool {
        self.epoch.eq(&other.epoch)
            && self.signed_entity_type.eq(&other.signed_entity_type)
            && self.initiated_at.eq(&other.initiated_at)
    }
}

#[cfg(test)]
impl PartialEq<SignedBeaconRecord> for BeaconToSign {
    fn eq(&self, other: &SignedBeaconRecord) -> bool {
        other.eq(self)
    }
}

impl SqLiteEntity for SignedBeaconRecord {
    fn hydrate(row: Row) -> Result<Self, HydrationError>
    where
        Self: Sized,
    {
        let epoch = row.read::<i64, _>(0);
        let beacon_str = Hydrator::read_signed_entity_beacon_column(&row, 1);
        let signed_entity_type_id = usize::try_from(row.read::<i64, _>(2)).map_err(|e| {
            panic!(
                "Integer field signed_beacon.signed_entity_type_id cannot be turned into usize: {e}"
            )
        })?;
        let initiated_at = &row.read::<&str, _>(3);
        let signed_at = &row.read::<&str, _>(4);

        let signed_beacon = Self {
            epoch: Epoch(epoch.try_into().map_err(|e| {
                HydrationError::InvalidData(format!(
                    "Could not cast i64 ({epoch}) to u64. Error: '{e}'"
                ))
            })?),
            signed_entity_type: Hydrator::hydrate_signed_entity_type(signed_entity_type_id, &beacon_str)?,
            initiated_at: DateTime::parse_from_rfc3339(initiated_at).map_err(|e| {
                HydrationError::InvalidData(format!(
                    "Could not turn signed_beacon.initiated_at field value '{initiated_at}' to rfc3339 Datetime. Error: {e}"
                ))
            })?.with_timezone(&Utc),
            signed_at:DateTime::parse_from_rfc3339(signed_at).map_err(|e| {
                HydrationError::InvalidData(format!(
                    "Could not turn signed_beacon.initiated_at field value '{initiated_at}' to rfc3339 Datetime. Error: {e}"
                ))
            })?.with_timezone(&Utc),
        };

        Ok(signed_beacon)
    }

    fn get_projection() -> Projection {
        Projection::from(&[
            ("epoch", "{:signed_beacon:}.epoch", "int"),
            ("beacon", "{:signed_beacon:}.beacon", "text"),
            (
                "signed_entity_type_id",
                "{:signed_beacon:}.signed_entity_type_id",
                "int",
            ),
            ("created_at", "{:signed_beacon:}.initiated_at", "text"),
            ("expires_at", "{:signed_beacon:}.signed_at", "text"),
        ])
    }
}

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

    #[test]
    fn eq_beacon_to_sign() {
        let initiated_at = DateTime::<Utc>::default();
        let beacon_to_sign = BeaconToSign {
            epoch: Epoch(3),
            signed_entity_type: SignedEntityType::MithrilStakeDistribution(Epoch(4)),
            initiated_at,
        };
        let signed_beacon = SignedBeaconRecord {
            epoch: Epoch(3),
            signed_entity_type: SignedEntityType::MithrilStakeDistribution(Epoch(4)),
            initiated_at,
            signed_at: initiated_at + chrono::TimeDelta::minutes(2),
        };

        // Check `impl PartialEq<BeaconToSign> for SignedBeaconRecord`
        assert_eq!(signed_beacon, beacon_to_sign);
        assert_ne!(
            signed_beacon,
            BeaconToSign {
                epoch: beacon_to_sign.epoch + 13,
                ..beacon_to_sign.clone()
            }
        );

        // Check `impl PartialEq<SignedBeaconRecord> for BeaconToSign`
        assert_eq!(beacon_to_sign, signed_beacon);
        assert_ne!(
            beacon_to_sign,
            SignedBeaconRecord {
                epoch: signed_beacon.epoch + 11,
                ..signed_beacon.clone()
            }
        );
    }
}