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
use anyhow::{anyhow, Context};
use mithril_common::StdResult;
use tokio::sync::RwLock;

use mithril_common::entities::CertificatePending;
use mithril_persistence::store::adapter::StoreAdapter;

type Adapter = Box<dyn StoreAdapter<Key = String, Record = CertificatePending>>;

const KEY: &str = "certificate_pending";

/// Store for [CertificatePending].
pub struct CertificatePendingStore {
    adapter: RwLock<Adapter>,
}

impl CertificatePendingStore {
    /// Create a new instance.
    pub fn new(adapter: Adapter) -> Self {
        Self {
            adapter: RwLock::new(adapter),
        }
    }

    /// Fetch the current [CertificatePending] if any.
    pub async fn get(&self) -> StdResult<Option<CertificatePending>> {
        self.adapter
            .read()
            .await
            .get_record(&KEY.to_string())
            .await
            .with_context(|| "Certificate pending store: could not GET store.".to_string())
    }

    /// Save the given [CertificatePending].
    pub async fn save(&self, certificate: CertificatePending) -> StdResult<()> {
        self
            .adapter
            .write()
            .await
            .store_record(&KEY.to_string(), &certificate)
            .await
            .with_context(|| format!("Certificate pending store: error while saving pending certificate for epoch '{}'.", certificate.epoch))
    }

    /// Remove and return the current [CertificatePending] if any.
    pub async fn remove(&self) -> StdResult<Option<CertificatePending>> {
        self.adapter
            .write()
            .await
            .remove(&KEY.to_string())
            .await
            .map_err(|e| anyhow!(e))
            .with_context(|| {
                format!(
                    "Could not delete certificate pending (key = '{}') from store.",
                    &KEY
                )
            })
    }
}

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

    use mithril_common::entities::{Epoch, SignedEntityType};
    use mithril_common::test_utils::fake_data;
    use mithril_persistence::store::adapter::DumbStoreAdapter;

    async fn get_certificate_pending_store(is_populated: bool) -> CertificatePendingStore {
        let mut adapter: DumbStoreAdapter<String, CertificatePending> = DumbStoreAdapter::new();

        if is_populated {
            let certificate_pending = CertificatePending::new(
                Epoch(0),
                SignedEntityType::dummy(),
                fake_data::protocol_parameters(),
                fake_data::protocol_parameters(),
                fake_data::signers(4),
                fake_data::signers(5),
            );
            adapter
                .store_record(&KEY.to_string(), &certificate_pending)
                .await
                .unwrap();
        }
        CertificatePendingStore::new(Box::new(adapter))
    }

    #[tokio::test]
    async fn get_certificate_pending_with_existing_certificate() {
        let store = get_certificate_pending_store(true).await;
        let result = store.get().await.unwrap();

        assert!(result.is_some());
    }

    #[tokio::test]
    async fn get_certificate_pending_with_no_existing_certificate() {
        let store = get_certificate_pending_store(false).await;
        let result = store.get().await.unwrap();

        assert!(result.is_none());
    }

    #[tokio::test]
    async fn save_certificate_pending_once() {
        let store = get_certificate_pending_store(false).await;
        let signed_entity_type = SignedEntityType::dummy();
        let certificate_pending = CertificatePending::new(
            Epoch(2),
            signed_entity_type,
            fake_data::protocol_parameters(),
            fake_data::protocol_parameters(),
            fake_data::signers(1),
            fake_data::signers(2),
        );

        assert!(store.save(certificate_pending).await.is_ok());
        assert!(store.get().await.unwrap().is_some());
    }

    #[tokio::test]
    async fn update_certificate_pending() {
        let store = get_certificate_pending_store(true).await;
        let certificate_pending = store.get().await.unwrap().unwrap();

        assert!(store.save(certificate_pending).await.is_ok());
    }

    #[tokio::test]
    async fn remove_certificate_pending() {
        let store = get_certificate_pending_store(true).await;
        let epoch = Epoch(0);
        let certificate_pending = store.remove().await.unwrap().unwrap();

        assert_eq!(epoch, certificate_pending.epoch);
        assert!(store.get().await.unwrap().is_none());
    }
}