mithril_signer/database/repository/
protocol_initializer_repository.rs

1use std::sync::Arc;
2
3use anyhow::Ok;
4use async_trait::async_trait;
5
6use crate::database::query::{
7    DeleteProtocolInitializerQuery, InsertOrReplaceProtocolInitializerQuery,
8};
9use crate::database::record::ProtocolInitializerRecord;
10use crate::{
11    database::query::GetProtocolInitializerQuery, services::EpochPruningTask,
12    store::ProtocolInitializerStorer,
13};
14use mithril_common::{crypto_helper::ProtocolInitializer, entities::Epoch, StdResult};
15use mithril_persistence::sqlite::ConnectionExtensions;
16use mithril_persistence::{sqlite::SqliteConnection /*store::adapter::StoreAdapter*/};
17
18/// Implementation of the ProtocolInitializerStorer
19pub struct ProtocolInitializerRepository {
20    connection: Arc<SqliteConnection>,
21    retention_limit: Option<u64>,
22}
23
24impl ProtocolInitializerRepository {
25    /// Create a new ProtocolInitializerRepository.
26    pub fn new(connection: Arc<SqliteConnection>, retention_limit: Option<u64>) -> Self {
27        Self {
28            connection,
29            retention_limit,
30        }
31    }
32}
33
34#[async_trait]
35impl EpochPruningTask for ProtocolInitializerRepository {
36    fn pruned_data(&self) -> &'static str {
37        "Protocol initializer"
38    }
39
40    async fn prune(&self, epoch: Epoch) -> StdResult<()> {
41        if let Some(threshold) = self.retention_limit {
42            self.connection
43                .apply(DeleteProtocolInitializerQuery::below_epoch_threshold(
44                    epoch - threshold,
45                ))?;
46        }
47        Ok(())
48    }
49}
50
51#[async_trait]
52impl ProtocolInitializerStorer for ProtocolInitializerRepository {
53    async fn save_protocol_initializer(
54        &self,
55        epoch: Epoch,
56        protocol_initializer: ProtocolInitializer,
57    ) -> StdResult<Option<ProtocolInitializer>> {
58        let previous_protocol_initializer = self.get_protocol_initializer(epoch).await?;
59        let record = ProtocolInitializerRecord {
60            epoch,
61            protocol_initializer: protocol_initializer.clone(),
62            created_at: chrono::Utc::now(),
63        };
64        self.connection
65            .apply(InsertOrReplaceProtocolInitializerQuery::one(record).unwrap())?;
66
67        Ok(previous_protocol_initializer)
68    }
69
70    async fn get_protocol_initializer(
71        &self,
72        epoch: Epoch,
73    ) -> StdResult<Option<ProtocolInitializer>> {
74        let record = self
75            .connection
76            .fetch_first(GetProtocolInitializerQuery::for_epoch(epoch))?;
77
78        Ok(record.map(|record| record.protocol_initializer))
79    }
80
81    async fn get_last_protocol_initializer(
82        &self,
83        last: usize,
84    ) -> StdResult<Vec<(Epoch, ProtocolInitializer)>> {
85        let record: Vec<ProtocolInitializerRecord> = self
86            .connection
87            .fetch_collect(GetProtocolInitializerQuery::last_n(last))?;
88
89        Ok(record
90            .iter()
91            .map(|record| (record.epoch, record.protocol_initializer.to_owned()))
92            .collect())
93    }
94}