mithril_aggregator/database/record/
epoch_settings.rs

1use mithril_common::entities::{CardanoTransactionsSigningConfig, Epoch, ProtocolParameters};
2use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity};
3
4use crate::entities::AggregatorEpochSettings;
5
6/// Settings for an epoch, including the protocol parameters.
7#[derive(Debug, PartialEq)]
8pub struct EpochSettingsRecord {
9    /// Epoch settings id, i.e. the epoch number.
10    pub epoch_settings_id: Epoch,
11
12    /// Protocol parameters.
13    pub protocol_parameters: ProtocolParameters,
14
15    /// Cardano transactions signing configuration.
16    pub cardano_transactions_signing_config: CardanoTransactionsSigningConfig,
17}
18
19impl From<EpochSettingsRecord> for AggregatorEpochSettings {
20    fn from(other: EpochSettingsRecord) -> Self {
21        Self {
22            protocol_parameters: other.protocol_parameters,
23            cardano_transactions_signing_config: other.cardano_transactions_signing_config,
24        }
25    }
26}
27
28impl SqLiteEntity for EpochSettingsRecord {
29    fn hydrate(row: sqlite::Row) -> Result<Self, HydrationError>
30    where
31        Self: Sized,
32    {
33        let epoch_settings_id_int = row.read::<i64, _>(0);
34        let protocol_parameters_string = &row.read::<&str, _>(1);
35        let cardano_transactions_signing_config_string = &row.read::<&str, _>(2);
36
37        let epoch_settings_record = Self {
38            epoch_settings_id: Epoch(epoch_settings_id_int.try_into().map_err(|e| {
39                HydrationError::InvalidData(format!(
40                    "Could not cast i64 ({epoch_settings_id_int}) to u64. Error: '{e}'"
41                ))
42            })?),
43            protocol_parameters: serde_json::from_str(protocol_parameters_string).map_err(
44                |e| {
45                    HydrationError::InvalidData(format!(
46                        "Could not turn string '{protocol_parameters_string}' to ProtocolParameters. Error: {e}"
47                    ))
48                },
49            )?,
50            cardano_transactions_signing_config: serde_json::from_str(cardano_transactions_signing_config_string).map_err(
51                |e| {
52                    HydrationError::InvalidData(format!(
53                        "Could not turn string '{cardano_transactions_signing_config_string}' to CardanoTransactionsSigningConfig. Error: {e}"
54                    ))
55                },
56            )?,
57
58        };
59
60        Ok(epoch_settings_record)
61    }
62
63    fn get_projection() -> Projection {
64        let mut projection = Projection::default();
65        projection.add_field(
66            "epoch_setting_id",
67            "{:epoch_setting:}.epoch_setting_id",
68            "integer",
69        );
70        projection.add_field(
71            "protocol_parameters",
72            "{:epoch_setting:}.protocol_parameters",
73            "text",
74        );
75        projection.add_field(
76            "cardano_transactions_signing_config",
77            "{:epoch_setting:}.cardano_transactions_signing_config",
78            "text",
79        );
80
81        projection
82    }
83}