Skip to content

Commit fab2a97

Browse files
committed
add first era reader adapters
1 parent 8380e27 commit fab2a97

File tree

6 files changed

+147
-26
lines changed

6 files changed

+147
-26
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use async_trait::async_trait;
2+
3+
use crate::entities::Epoch;
4+
5+
use super::super::{EraMarker, EraReaderAdapter, SupportedEra};
6+
7+
/// The goal of the bootstrap adapter is to advertise for the first existing Era
8+
/// while it does not exist yet on any backend. This adapter is intended to be
9+
/// removed once Eras are effectively written in a backend.
10+
pub struct BootstrapAdapter;
11+
12+
#[async_trait]
13+
impl EraReaderAdapter for BootstrapAdapter {
14+
async fn read(&self) -> Result<Vec<EraMarker>, Box<dyn std::error::Error>> {
15+
Ok(vec![EraMarker::new(
16+
&SupportedEra::Thales.to_string(),
17+
Some(Epoch(1)),
18+
)])
19+
}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use crate::era::SupportedEra;
25+
26+
use super::*;
27+
28+
#[tokio::test]
29+
async fn bootstrap_adapter() {
30+
let adapter = BootstrapAdapter;
31+
32+
assert_eq!(
33+
vec![EraMarker::new(
34+
&SupportedEra::dummy().to_string(),
35+
Some(Epoch(1))
36+
)],
37+
adapter
38+
.read()
39+
.await
40+
.expect("bootstrap adapter shall never fail")
41+
);
42+
}
43+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::error::Error;
2+
3+
use async_trait::async_trait;
4+
5+
use super::super::{EraMarker, EraReaderAdapter};
6+
7+
/// Dummy adapter is intended to be used in a test environment (end to end test)
8+
/// to simulate not yet activated Eras.
9+
#[derive(Default)]
10+
pub struct DummyAdapter {
11+
markers: Vec<EraMarker>,
12+
}
13+
14+
impl DummyAdapter {
15+
/// Tells what markers should be sent back by the adapter.
16+
pub fn set_markers(&mut self, markers: Vec<EraMarker>) {
17+
self.markers = markers;
18+
}
19+
}
20+
21+
#[async_trait]
22+
impl EraReaderAdapter for DummyAdapter {
23+
async fn read(&self) -> Result<Vec<EraMarker>, Box<dyn Error>> {
24+
Ok(self.markers.clone())
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use crate::entities::Epoch;
31+
32+
use super::super::super::SupportedEra;
33+
use super::*;
34+
35+
#[tokio::test]
36+
async fn empty_dummy_adapter() {
37+
let adapter = DummyAdapter::default();
38+
39+
assert!(adapter
40+
.read()
41+
.await
42+
.expect("dummy adapter shall not fail reading")
43+
.is_empty());
44+
}
45+
46+
#[tokio::test]
47+
async fn dummy_adapter_output() {
48+
let markers = vec![
49+
EraMarker::new("one", Some(Epoch(1))),
50+
EraMarker::new(&SupportedEra::dummy().to_string(), None),
51+
EraMarker::new(&SupportedEra::dummy().to_string(), Some(Epoch(10))),
52+
];
53+
54+
let mut adapter = DummyAdapter::default();
55+
adapter.set_markers(markers.clone());
56+
57+
assert_eq!(
58+
markers,
59+
adapter
60+
.read()
61+
.await
62+
.expect("dummy adapter shall not fail reading")
63+
);
64+
}
65+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Module dedicated to [EraReaderAdapter] implementations.
2+
mod bootstrap;
3+
mod dummy;
4+
5+
pub use bootstrap::BootstrapAdapter;
6+
pub use dummy::DummyAdapter;

mithril-common/src/era/era_reader.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ pub struct EraMarker {
1313
epoch: Option<Epoch>,
1414
}
1515

16+
impl EraMarker {
17+
/// instanciate a new [EraMarker].
18+
pub fn new(name: &str, epoch: Option<Epoch>) -> Self {
19+
let name = name.to_string();
20+
21+
Self { name, epoch }
22+
}
23+
}
24+
1625
/// Adapters are responsible of technically reading the information of
1726
/// [EraMarker]s from a backend.
1827
#[async_trait]
@@ -146,38 +155,21 @@ impl EraReader {
146155

147156
#[cfg(test)]
148157
mod tests {
158+
use super::super::adapters::DummyAdapter;
149159
use super::*;
150160

151-
#[derive(Default)]
152-
struct DummyAdapter {
153-
markers: Vec<EraMarker>,
154-
}
155-
156-
impl DummyAdapter {
157-
pub fn set_markers(&mut self, markers: Vec<EraMarker>) {
158-
self.markers = markers;
159-
}
160-
}
161-
162-
#[async_trait]
163-
impl EraReaderAdapter for DummyAdapter {
164-
async fn read(&self) -> Result<Vec<EraMarker>, Box<dyn StdError>> {
165-
Ok(self.markers.clone())
166-
}
167-
}
168-
169161
fn get_basic_marker_sample() -> Vec<EraMarker> {
170162
vec![
171163
EraMarker {
172164
name: "one".to_string(),
173165
epoch: Some(Epoch(1)),
174166
},
175167
EraMarker {
176-
name: "thales".to_string(),
168+
name: SupportedEra::dummy().to_string(),
177169
epoch: None,
178170
},
179171
EraMarker {
180-
name: "thales".to_string(),
172+
name: SupportedEra::dummy().to_string(),
181173
epoch: Some(Epoch(10)),
182174
},
183175
]
@@ -196,7 +188,7 @@ mod tests {
196188
EraEpochToken {
197189
current_epoch: Epoch(10),
198190
current_era: EraMarker {
199-
name: "thales".to_string(),
191+
name: SupportedEra::dummy().to_string(),
200192
epoch: Some(Epoch(10))
201193
},
202194
next_era: None,
@@ -243,7 +235,7 @@ mod tests {
243235
epoch: Some(Epoch(1))
244236
},
245237
next_era: Some(EraMarker {
246-
name: "thales".to_string(),
238+
name: SupportedEra::dummy().to_string(),
247239
epoch: Some(Epoch(10))
248240
}),
249241
},

mithril-common/src/era/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! The module used for handling eras
22
3+
pub mod adapters;
34
mod era_checker;
45
mod era_reader;
56
mod supported_era;

mithril-common/src/era/supported_era.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,24 @@ impl FromStr for SupportedEra {
2929
fn from_str(s: &str) -> Result<Self, Self::Err> {
3030
let s = s.trim().to_lowercase();
3131

32-
if &s == "thales" {
33-
Ok(Self::Thales)
34-
} else {
35-
Err(UnsupportedEraError(s))
32+
let era = match s.as_str() {
33+
"thales" => Self::Thales,
34+
_ => return Err(UnsupportedEraError(s)),
35+
};
36+
37+
// This is intended to make the compiler to complain when a new variant
38+
// is added in order not to forget to add a conversion for the new
39+
// variant.
40+
match era {
41+
Self::Thales => Ok(Self::Thales),
42+
}
43+
}
44+
}
45+
46+
impl ToString for SupportedEra {
47+
fn to_string(&self) -> String {
48+
match self {
49+
Self::Thales => "thales".to_owned(),
3650
}
3751
}
3852
}

0 commit comments

Comments
 (0)