mithril_aggregator/database/record/
epoch_settings.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
use mithril_common::entities::{CardanoTransactionsSigningConfig, Epoch, ProtocolParameters};
use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity};

use crate::entities::AggregatorEpochSettings;

/// Settings for an epoch, including the protocol parameters.
#[derive(Debug, PartialEq)]
pub struct EpochSettingsRecord {
    /// Epoch settings id, i.e. the epoch number.
    pub epoch_settings_id: Epoch,

    /// Protocol parameters.
    pub protocol_parameters: ProtocolParameters,

    /// Cardano transactions signing configuration.
    pub cardano_transactions_signing_config: CardanoTransactionsSigningConfig,
}

impl From<EpochSettingsRecord> for AggregatorEpochSettings {
    fn from(other: EpochSettingsRecord) -> Self {
        Self {
            protocol_parameters: other.protocol_parameters,
            cardano_transactions_signing_config: other.cardano_transactions_signing_config,
        }
    }
}

impl SqLiteEntity for EpochSettingsRecord {
    fn hydrate(row: sqlite::Row) -> Result<Self, HydrationError>
    where
        Self: Sized,
    {
        let epoch_settings_id_int = row.read::<i64, _>(0);
        let protocol_parameters_string = &row.read::<&str, _>(1);
        let cardano_transactions_signing_config_string = &row.read::<&str, _>(2);

        let epoch_settings_record = Self {
            epoch_settings_id: Epoch(epoch_settings_id_int.try_into().map_err(|e| {
                HydrationError::InvalidData(format!(
                    "Could not cast i64 ({epoch_settings_id_int}) to u64. Error: '{e}'"
                ))
            })?),
            protocol_parameters: serde_json::from_str(protocol_parameters_string).map_err(
                |e| {
                    HydrationError::InvalidData(format!(
                        "Could not turn string '{protocol_parameters_string}' to ProtocolParameters. Error: {e}"
                    ))
                },
            )?,
            cardano_transactions_signing_config: serde_json::from_str(cardano_transactions_signing_config_string).map_err(
                |e| {
                    HydrationError::InvalidData(format!(
                        "Could not turn string '{cardano_transactions_signing_config_string}' to CardanoTransactionsSigningConfig. Error: {e}"
                    ))
                },
            )?,

        };

        Ok(epoch_settings_record)
    }

    fn get_projection() -> Projection {
        let mut projection = Projection::default();
        projection.add_field(
            "epoch_setting_id",
            "{:epoch_setting:}.epoch_setting_id",
            "integer",
        );
        projection.add_field(
            "protocol_parameters",
            "{:epoch_setting:}.protocol_parameters",
            "text",
        );
        projection.add_field(
            "cardano_transactions_signing_config",
            "{:epoch_setting:}.cardano_transactions_signing_config",
            "text",
        );

        projection
    }
}