mithril_common/era/
era_reader.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use anyhow::anyhow;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::{str::FromStr, sync::Arc};
use thiserror::Error;

use crate::entities::Epoch;
use crate::{StdError, StdResult};

use super::SupportedEra;

/// Value object that represents a tag of Era change.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EraMarker {
    /// Era name
    pub name: String,

    /// Eventual information that advertises the Epoch of transition.
    pub epoch: Option<Epoch>,
}

impl EraMarker {
    /// instantiate a new [EraMarker].
    pub fn new(name: &str, epoch: Option<Epoch>) -> Self {
        let name = name.to_string();

        Self { name, epoch }
    }
}

/// Adapters are responsible of technically reading the information of
/// [EraMarker]s from a backend.
#[async_trait]
pub trait EraReaderAdapter: Sync + Send {
    /// Read era markers from the underlying adapter.
    async fn read(&self) -> StdResult<Vec<EraMarker>>;
}

/// This is a response from the [EraReader]. It contains [EraMarker]s read from
/// the adapter. It can try to cast the given markers to [SupportedEra]s.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EraEpochToken {
    current_epoch: Epoch,
    current_era: EraMarker,
    next_era: Option<EraMarker>,
}

impl EraEpochToken {
    /// Instantiate a new [EraMarker].
    pub fn new(current_epoch: Epoch, current_era: EraMarker, next_era: Option<EraMarker>) -> Self {
        Self {
            current_epoch,
            current_era,
            next_era,
        }
    }

    /// Try to cast the current [EraMarker] to a [SupportedEra]. If it fails,
    /// that means the current Era is not supported by this version of the
    /// software.
    pub fn get_current_supported_era(&self) -> StdResult<SupportedEra> {
        SupportedEra::from_str(&self.current_era.name)
            .map_err(|_| anyhow!(format!("Unsupported era '{}'.", &self.current_era.name)))
    }

    /// Return the [EraMarker] of the current Era.
    pub fn get_current_era_marker(&self) -> &EraMarker {
        &self.current_era
    }

    /// Return the epoch the Token has been created at
    pub fn get_current_epoch(&self) -> Epoch {
        self.current_epoch
    }

    /// Try to cast the next [EraMarker] to a [SupportedEra]. If it fails, that
    /// means the coming Era will not be supported by this version of the
    /// software. This mechanism is used to issue a warning to the user asking
    /// for upgrade.
    pub fn get_next_supported_era(&self) -> StdResult<Option<SupportedEra>> {
        match self.next_era.as_ref() {
            Some(marker) => Ok(Some(
                SupportedEra::from_str(&marker.name)
                    .map_err(|_| anyhow!(format!("Unsupported era '{}'.", &marker.name)))?,
            )),
            None => Ok(None),
        }
    }

    /// Return the [EraMarker] for the coming Era if any.
    pub fn get_next_era_marker(&self) -> Option<&EraMarker> {
        self.next_era.as_ref()
    }
}

/// The EraReader is responsible of giving the current Era and the Era to come.
/// It uses an [EraReaderAdapter] to read data from a backend.
pub struct EraReader {
    adapter: Arc<dyn EraReaderAdapter>,
}

/// Error type when [EraReader] fails to return a [EraEpochToken].
#[derive(Debug, Error)]
pub enum EraReaderError {
    /// Underlying adapter fails to return data.
    #[error("Adapter Error message: «{message}»")]
    AdapterFailure {
        /// context message
        message: String,

        /// nested underlying adapter error
        #[source]
        error: StdError,
    },

    /// Data returned from the adapter are inconsistent or incomplete.
    #[error(
        "Cannot determine the Era we are currently at epoch {epoch} using the adapter informations: {eras:?}"
    )]
    CurrentEraNotFound {
        /// Current Epoch
        epoch: Epoch,

        /// Eras given by the adapter
        eras: Vec<EraMarker>,
    },
}

impl EraReader {
    /// Instantiate the [EraReader] injecting the adapter.
    pub fn new(adapter: Arc<dyn EraReaderAdapter>) -> Self {
        Self { adapter }
    }

    /// This methods triggers the adapter to read the markers from the backend.
    /// It tries to determine the current Era and the next Era if any from the
    /// data returned from the adapter.
    pub async fn read_era_epoch_token(
        &self,
        current_epoch: Epoch,
    ) -> Result<EraEpochToken, EraReaderError> {
        let eras = self
            .adapter
            .read()
            .await
            .map_err(|e| EraReaderError::AdapterFailure {
                message: format!("Reading from EraReader adapter raised an error: '{}'.", &e),
                error: e,
            })?;

        let current_marker = eras.iter().filter(|&f| f.epoch.is_some()).fold(
            None,
            |acc: Option<&EraMarker>, marker| {
                if marker.epoch.unwrap() <= current_epoch
                    && (acc.is_none() || marker.epoch.unwrap() > acc.unwrap().epoch.unwrap())
                {
                    Some(marker)
                } else {
                    acc
                }
            },
        );
        let current_era_marker =
            current_marker.ok_or_else(|| EraReaderError::CurrentEraNotFound {
                epoch: current_epoch,
                eras: eras.clone(),
            })?;

        let next_era_marker = eras.last().filter(|&marker| marker != current_era_marker);

        Ok(EraEpochToken::new(
            current_epoch,
            current_era_marker.to_owned(),
            next_era_marker.cloned(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::super::adapters::EraReaderDummyAdapter as DummyAdapter;
    use super::*;

    fn get_basic_marker_sample() -> Vec<EraMarker> {
        vec![
            EraMarker {
                name: "one".to_string(),
                epoch: Some(Epoch(1)),
            },
            EraMarker {
                name: SupportedEra::dummy().to_string(),
                epoch: None,
            },
            EraMarker {
                name: SupportedEra::dummy().to_string(),
                epoch: Some(Epoch(10)),
            },
        ]
    }

    #[tokio::test]
    async fn current_era_is_supported() {
        let markers: Vec<EraMarker> = get_basic_marker_sample();
        let adapter = DummyAdapter::default();
        adapter.set_markers(markers);

        let reader = EraReader::new(Arc::new(adapter));
        let token = reader.read_era_epoch_token(Epoch(10)).await.unwrap();

        assert_eq!(
            EraEpochToken {
                current_epoch: Epoch(10),
                current_era: EraMarker {
                    name: SupportedEra::dummy().to_string(),
                    epoch: Some(Epoch(10))
                },
                next_era: None,
            },
            token
        );
    }

    #[tokio::test]
    async fn era_epoch_token() {
        let markers: Vec<EraMarker> = get_basic_marker_sample();
        let adapter = DummyAdapter::default();
        adapter.set_markers(markers);

        let reader = EraReader::new(Arc::new(adapter));
        let token = reader.read_era_epoch_token(Epoch(10)).await.unwrap();
        assert_eq!(
            SupportedEra::dummy(),
            token
                .get_current_supported_era()
                .expect("the given era is supported")
        );
        assert!(token.get_next_era_marker().is_none());
        assert!(token
            .get_next_supported_era()
            .expect("None era shall not fail when asked.")
            .is_none());
    }

    #[tokio::test]
    async fn previous_era_is_not_supported() {
        let markers: Vec<EraMarker> = get_basic_marker_sample();
        let adapter = DummyAdapter::default();
        adapter.set_markers(markers);

        let reader = EraReader::new(Arc::new(adapter));
        let token = reader.read_era_epoch_token(Epoch(9)).await.unwrap();

        assert_eq!(
            EraEpochToken {
                current_epoch: Epoch(9),
                current_era: EraMarker {
                    name: "one".to_string(),
                    epoch: Some(Epoch(1))
                },
                next_era: Some(EraMarker {
                    name: SupportedEra::dummy().to_string(),
                    epoch: Some(Epoch(10))
                }),
            },
            token
        );
    }

    #[tokio::test]
    async fn error_when_no_current_era() {
        let markers = vec![
            EraMarker {
                name: "one".to_string(),
                epoch: None,
            },
            EraMarker {
                name: "two".to_string(),
                epoch: None,
            },
            EraMarker {
                name: "three".to_string(),
                epoch: Some(Epoch(100)),
            },
        ];

        let adapter = DummyAdapter::default();
        adapter.set_markers(markers);

        let reader = EraReader::new(Arc::new(adapter));
        let _ = reader
            .read_era_epoch_token(Epoch(9))
            .await
            .expect_err("No current era must make the reader to fail.");
    }

    #[tokio::test]
    async fn error_when_no_era() {
        let adapter = DummyAdapter::default();

        let reader = EraReader::new(Arc::new(adapter));
        let _ = reader
            .read_era_epoch_token(Epoch(9))
            .await
            .expect_err("The adapter gave no result hence the reader should fail.");
    }

    #[tokio::test]
    async fn current_era_is_not_supported() {
        let markers: Vec<EraMarker> = get_basic_marker_sample();
        let adapter = DummyAdapter::default();
        adapter.set_markers(markers);

        let reader = EraReader::new(Arc::new(adapter));
        let token = reader.read_era_epoch_token(Epoch(9)).await.unwrap();

        token
            .get_current_supported_era()
            .expect_err("The era 'one' is not supported hence the token must issue an error.");

        assert_eq!(
            &EraMarker {
                name: "one".to_string(),
                epoch: Some(Epoch(1))
            },
            token.get_current_era_marker()
        );
        token
            .get_next_supported_era()
            .expect("The next era is supported hence this shall not fail.");
    }

    #[tokio::test]
    async fn epoch_0_should_work() {
        let markers = vec![EraMarker::new(
            &SupportedEra::dummy().to_string(),
            Some(Epoch(0)),
        )];
        let adapter = DummyAdapter::default();
        adapter.set_markers(markers);
        let reader = EraReader::new(Arc::new(adapter));
        let token = reader.read_era_epoch_token(Epoch(9)).await.unwrap();

        assert_eq!(
            &EraMarker {
                name: SupportedEra::dummy().to_string(),
                epoch: Some(Epoch(0))
            },
            token.get_current_era_marker()
        );
    }
}