Skip to content

Commit e729c13

Browse files
committed
refactor(client): introduce MithrilEraClient to improve extensibility for era-related features
- Update `Client` to expose a `MithrilEraClient` - Remove public re-exports of `EraFetcher` and `FetchedEra` from `lib.rs`
1 parent 2cbe20d commit e729c13

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

mithril-client-cli/src/utils/forced_era_fetcher.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use async_trait::async_trait;
22

3-
use mithril_client::{EraFetcher, FetchedEra, MithrilResult};
3+
use mithril_client::{
4+
MithrilResult,
5+
era::{EraFetcher, FetchedEra},
6+
};
47

58
pub struct ForcedEraFetcher {
69
era: String,

mithril-client/src/client.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::certificate_client::CertificateVerifierCache;
1818
use crate::certificate_client::{
1919
CertificateClient, CertificateVerifier, MithrilCertificateVerifier,
2020
};
21-
use crate::era::{AggregatorHttpEraFetcher, EraFetcher};
21+
use crate::era::{AggregatorHttpEraFetcher, EraFetcher, MithrilEraClient};
2222
use crate::feedback::{FeedbackReceiver, FeedbackSender};
2323
#[cfg(feature = "fs")]
2424
use crate::file_downloader::{
@@ -106,7 +106,7 @@ pub struct Client {
106106
cardano_database_client: Arc<CardanoDatabaseClient>,
107107
cardano_transaction_client: Arc<CardanoTransactionClient>,
108108
cardano_stake_distribution_client: Arc<CardanoStakeDistributionClient>,
109-
era_fetcher: Arc<dyn EraFetcher>,
109+
mithril_era_client: Arc<MithrilEraClient>,
110110
}
111111

112112
impl Client {
@@ -147,8 +147,8 @@ impl Client {
147147
}
148148

149149
/// Get the client that fetches the current Mithril era.
150-
pub fn era_fetcher(&self) -> Arc<dyn EraFetcher> {
151-
self.era_fetcher.clone()
150+
pub fn mithril_era_client(&self) -> Arc<MithrilEraClient> {
151+
self.mithril_era_client.clone()
152152
}
153153
}
154154

@@ -166,10 +166,10 @@ pub struct ClientBuilder {
166166
http_file_downloader: Option<Arc<dyn FileDownloader>>,
167167
#[cfg(feature = "unstable")]
168168
certificate_verifier_cache: Option<Arc<dyn CertificateVerifierCache>>,
169+
era_fetcher: Option<Arc<dyn EraFetcher>>,
169170
logger: Option<Logger>,
170171
feedback_receivers: Vec<Arc<dyn FeedbackReceiver>>,
171172
options: ClientOptions,
172-
era_fetcher: Option<Arc<dyn EraFetcher>>,
173173
}
174174

175175
impl ClientBuilder {
@@ -189,10 +189,10 @@ impl ClientBuilder {
189189
http_file_downloader: None,
190190
#[cfg(feature = "unstable")]
191191
certificate_verifier_cache: None,
192+
era_fetcher: None,
192193
logger: None,
193194
feedback_receivers: vec![],
194195
options: ClientOptions::default(),
195-
era_fetcher: None,
196196
}
197197
}
198198

@@ -214,10 +214,10 @@ impl ClientBuilder {
214214
http_file_downloader: None,
215215
#[cfg(feature = "unstable")]
216216
certificate_verifier_cache: None,
217+
era_fetcher: None,
217218
logger: None,
218219
feedback_receivers: vec![],
219220
options: ClientOptions::default(),
220-
era_fetcher: None,
221221
}
222222
}
223223

@@ -238,9 +238,11 @@ impl ClientBuilder {
238238
Some(client) => client,
239239
};
240240

241-
let era_fetcher = match self.era_fetcher {
242-
None => Arc::new(AggregatorHttpEraFetcher::new(aggregator_client.clone())),
243-
Some(era_fetcher) => era_fetcher,
241+
let mithril_era_client = match self.era_fetcher {
242+
None => Arc::new(MithrilEraClient::new(Arc::new(
243+
AggregatorHttpEraFetcher::new(aggregator_client.clone()),
244+
))),
245+
Some(era_fetcher) => Arc::new(MithrilEraClient::new(era_fetcher)),
244246
};
245247

246248
let certificate_verifier = match self.certificate_verifier {
@@ -326,7 +328,7 @@ impl ClientBuilder {
326328
cardano_database_client,
327329
cardano_transaction_client,
328330
cardano_stake_distribution_client,
329-
era_fetcher,
331+
mithril_era_client,
330332
})
331333
}
332334

mithril-client/src/era.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! A client to retrieve the current Mithril era.
22
//!
3+
//! In order to do so it defines a [MithrilEraClient] which exposes the following features:
4+
//! - [fetch_current][MithrilEraClient::fetch_current]: fetch the current Mithril era using its [EraFetcher]
5+
//!
36
//! This module defines the following components:
47
//!
58
//! - [EraFetcher]: defines an interface to retrieve the current Mithril era
@@ -16,7 +19,7 @@
1619
//!
1720
//! let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?;
1821
//!
19-
//! let fetched_era = client.era_fetcher().fetch_current_era().await?;
22+
//! let fetched_era = client.mithril_era_client().fetch_current().await?;
2023
//! match fetched_era.to_supported_era() {
2124
//! Ok(supported_era) => println!("Current Mithril era: {supported_era}"),
2225
//! Err(era) => println!(
@@ -41,6 +44,23 @@ use crate::{
4144
aggregator_client::{AggregatorClient, AggregatorRequest},
4245
};
4346

47+
/// Client for retrieving the current Mithril era.
48+
pub struct MithrilEraClient {
49+
era_fetcher: Arc<dyn EraFetcher>,
50+
}
51+
52+
impl MithrilEraClient {
53+
/// Constructs a new [MithrilEraClient].
54+
pub fn new(era_fetcher: Arc<dyn EraFetcher>) -> Self {
55+
Self { era_fetcher }
56+
}
57+
58+
/// Fetches the current Mithril era.
59+
pub async fn fetch_current(&self) -> MithrilResult<FetchedEra> {
60+
self.era_fetcher.fetch_current_era().await
61+
}
62+
}
63+
4464
/// Wrapper around a raw Mithril era string.
4565
#[derive(Debug, PartialEq, Serialize, Deserialize)]
4666
pub struct FetchedEra {

mithril-client/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! - [Cardano stake distribution][cardano_stake_distribution_client] list, get and get by epoch.
1313
//! - [Mithril stake distribution][mithril_stake_distribution_client] list and get.
1414
//! - [Certificates][certificate_client] list, get, and chain validation.
15-
//! - [EraFetcher][era_fetcher]: retrieve the current Mithril era.
15+
//! - [MithrilEraClient][era]: retrieve the current Mithril era.
1616
//!
1717
//! The [Client] aggregates the queries of all of those types.
1818
//!
@@ -139,7 +139,6 @@ mod type_alias;
139139
mod utils;
140140

141141
pub use client::*;
142-
pub use era::{EraFetcher, FetchedEra};
143142
pub use message::*;
144143
pub use type_alias::*;
145144

0 commit comments

Comments
 (0)