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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use std::sync::Arc;

use async_trait::async_trait;

use mithril_common::entities::{CardanoTransactionsSigningConfig, Epoch, ProtocolParameters};
use mithril_common::StdResult;
use mithril_persistence::sqlite::{ConnectionExtensions, SqliteConnection};
use mithril_persistence::store::adapter::AdapterError;
use sqlite::Value;

use crate::database::query::{
    DeleteEpochSettingsQuery, GetEpochSettingsQuery, UpdateEpochSettingsQuery,
};
use crate::entities::AggregatorEpochSettings;
use crate::services::EpochPruningTask;
use crate::EpochSettingsStorer;

/// Service to deal with epoch settings (read & write).
pub struct EpochSettingsStore {
    connection: Arc<SqliteConnection>,

    /// Number of epochs before previous records will be pruned at the next call to
    /// [save_protocol_parameters][EpochSettingStore::save_protocol_parameters].
    retention_limit: Option<u64>,
}

impl EpochSettingsStore {
    /// Create a new EpochSettings store
    pub fn new(connection: Arc<SqliteConnection>, retention_limit: Option<u64>) -> Self {
        Self {
            connection,
            retention_limit,
        }
    }

    #[deprecated(since = "0.5.72", note = "temporary fix, should be removed")]
    /// Replace empty JSON values '{}' injected with Migration #28
    pub fn replace_cardano_signing_config_empty_values(
        &self,
        cardano_signing_config: CardanoTransactionsSigningConfig,
    ) -> StdResult<()> {
        let query = r#"
            update epoch_setting 
            set cardano_transactions_signing_config = ?
            where cardano_transactions_signing_config == '{}'"#;

        let mut statement = self.connection.prepare(query)?;
        statement.bind::<&[(_, Value)]>(&[(
            1,
            serde_json::to_string(&cardano_signing_config)?.into(),
        )])?;

        statement.next()?;

        Ok(())
    }
}

#[async_trait]
impl EpochSettingsStorer for EpochSettingsStore {
    async fn save_epoch_settings(
        &self,
        epoch: Epoch,
        epoch_settings: AggregatorEpochSettings,
    ) -> StdResult<Option<AggregatorEpochSettings>> {
        let epoch_settings_record = self
            .connection
            .fetch_first(UpdateEpochSettingsQuery::one(epoch, epoch_settings))
            .map_err(|e| AdapterError::GeneralError(e.context("persist epoch settings failure")))?
            .unwrap_or_else(|| panic!("No entity returned by the persister, epoch = {epoch:?}"));

        Ok(Some(epoch_settings_record.into()))
    }

    async fn get_protocol_parameters(&self, epoch: Epoch) -> StdResult<Option<ProtocolParameters>> {
        Ok(self
            .get_epoch_settings(epoch)
            .await?
            .map(|epoch_settings| epoch_settings.protocol_parameters))
    }

    async fn get_epoch_settings(&self, epoch: Epoch) -> StdResult<Option<AggregatorEpochSettings>> {
        let mut cursor = self
            .connection
            .fetch(GetEpochSettingsQuery::by_epoch(epoch)?)
            .map_err(|e| AdapterError::GeneralError(e.context("Could not get epoch settings")))?;

        if let Some(epoch_settings_record) = cursor.next() {
            return Ok(Some(epoch_settings_record.into()));
        }
        Ok(None)
    }
}

#[async_trait]
impl EpochPruningTask for EpochSettingsStore {
    fn pruned_data(&self) -> &'static str {
        "Epoch settings"
    }

    /// Prune useless old epoch settings.
    async fn prune(&self, epoch: Epoch) -> StdResult<()> {
        if let Some(threshold) = self.retention_limit {
            self.connection
                .apply(DeleteEpochSettingsQuery::below_epoch_threshold(
                    epoch - threshold,
                ))
                .map_err(AdapterError::QueryError)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use mithril_common::entities::BlockNumber;

    use crate::database::test_helper::{insert_epoch_settings, main_db_connection};

    use super::*;

    #[tokio::test]
    async fn replace_cardano_signing_config_empty_values_updates_only_empty_values() {
        let connection = main_db_connection().unwrap();
        connection.execute(
            r#"insert into epoch_setting (epoch_setting_id, protocol_parameters, cardano_transactions_signing_config) 
            values (
                1, 
                '{"k": 5, "m": 100, "phi_f": 0.65}', 
                '{"security_parameter": 70, "step": 20}'
            )"#,
        ).unwrap();
        connection.execute(
            r#"insert into epoch_setting (epoch_setting_id, protocol_parameters, cardano_transactions_signing_config) 
            values (
                2,
                '{"k": 73, "m": 100, "phi_f": 0.65}', 
                '{}'
            )"#,
        ).unwrap();

        let store = EpochSettingsStore::new(Arc::new(connection), None);

        let epoch_settings = store.get_epoch_settings(Epoch(1)).await.unwrap().unwrap();
        assert_eq!(
            CardanoTransactionsSigningConfig {
                security_parameter: BlockNumber(70),
                step: BlockNumber(20),
            },
            epoch_settings.cardano_transactions_signing_config
        );

        #[allow(deprecated)]
        store
            .replace_cardano_signing_config_empty_values(CardanoTransactionsSigningConfig {
                security_parameter: BlockNumber(50),
                step: BlockNumber(10),
            })
            .unwrap();

        {
            let epoch_settings = store.get_epoch_settings(Epoch(1)).await.unwrap().unwrap();
            assert_eq!(
                CardanoTransactionsSigningConfig {
                    security_parameter: BlockNumber(70),
                    step: BlockNumber(20),
                },
                epoch_settings.cardano_transactions_signing_config
            );
        }
        {
            let epoch_settings = store.get_epoch_settings(Epoch(2)).await.unwrap().unwrap();
            assert_eq!(
                CardanoTransactionsSigningConfig {
                    security_parameter: BlockNumber(50),
                    step: BlockNumber(10),
                },
                epoch_settings.cardano_transactions_signing_config
            );
        }
    }

    #[tokio::test]
    async fn prune_epoch_settings_older_than_threshold() {
        const EPOCH_SETTINGS_PRUNE_EPOCH_THRESHOLD: u64 = 5;

        let connection = main_db_connection().unwrap();
        insert_epoch_settings(&connection, &[1, 2]).unwrap();
        let store = EpochSettingsStore::new(
            Arc::new(connection),
            Some(EPOCH_SETTINGS_PRUNE_EPOCH_THRESHOLD),
        );

        store
            .prune(Epoch(2) + EPOCH_SETTINGS_PRUNE_EPOCH_THRESHOLD)
            .await
            .unwrap();

        let epoch1_params = store.get_epoch_settings(Epoch(1)).await.unwrap();
        let epoch2_params = store.get_epoch_settings(Epoch(2)).await.unwrap();

        assert!(
            epoch1_params.is_none(),
            "Epoch settings at epoch 1 should have been pruned",
        );
        assert!(
            epoch2_params.is_some(),
            "Epoch settings at epoch 2 should still exist",
        );
    }

    #[tokio::test]
    async fn without_threshold_nothing_is_pruned() {
        let connection = main_db_connection().unwrap();
        insert_epoch_settings(&connection, &[1, 2]).unwrap();
        let store = EpochSettingsStore::new(Arc::new(connection), None);

        store.prune(Epoch(100)).await.unwrap();

        let epoch1_params = store.get_epoch_settings(Epoch(1)).await.unwrap();
        let epoch2_params = store.get_epoch_settings(Epoch(2)).await.unwrap();

        assert!(
            epoch1_params.is_some(),
            "Epoch settings at epoch 1 should have been pruned",
        );
        assert!(
            epoch2_params.is_some(),
            "Epoch settings at epoch 2 should still exist",
        );
    }

    #[tokio::test]
    async fn save_epoch_settings_stores_in_database() {
        let connection = main_db_connection().unwrap();

        let store = EpochSettingsStore::new(Arc::new(connection), None);

        store
            .save_epoch_settings(Epoch(2), AggregatorEpochSettings::dummy())
            .await
            .expect("saving epoch settings should not fails");
        {
            let epoch_settings = store.get_epoch_settings(Epoch(1)).await.unwrap();
            assert_eq!(None, epoch_settings);
        }
        {
            let epoch_settings = store.get_epoch_settings(Epoch(2)).await.unwrap().unwrap();
            assert_eq!(AggregatorEpochSettings::dummy(), epoch_settings);
        }
        {
            let epoch_settings = store.get_epoch_settings(Epoch(3)).await.unwrap();
            assert_eq!(None, epoch_settings);
        }
    }
}