mithril_aggregator/dependency_injection/builder/support/
compatibility.rs

1use std::sync::Arc;
2
3use mithril_common::api_version::APIVersionProvider;
4use mithril_common::entities::Epoch;
5use mithril_common::era::adapters::{EraReaderAdapterBuilder, EraReaderDummyAdapter};
6use mithril_common::era::{EraChecker, EraMarker, EraReader, EraReaderAdapter, SupportedEra};
7
8use crate::dependency_injection::{DependenciesBuilder, DependenciesBuilderError, Result};
9use crate::ExecutionEnvironment;
10
11impl DependenciesBuilder {
12    async fn build_api_version_provider(&mut self) -> Result<Arc<APIVersionProvider>> {
13        let api_version_provider = Arc::new(APIVersionProvider::new(self.get_era_checker().await?));
14
15        Ok(api_version_provider)
16    }
17
18    /// [APIVersionProvider] service
19    pub async fn get_api_version_provider(&mut self) -> Result<Arc<APIVersionProvider>> {
20        if self.api_version_provider.is_none() {
21            self.api_version_provider = Some(self.build_api_version_provider().await?);
22        }
23
24        Ok(self.api_version_provider.as_ref().cloned().unwrap())
25    }
26
27    async fn build_era_reader(&mut self) -> Result<Arc<EraReader>> {
28        let era_adapter: Arc<dyn EraReaderAdapter> = match self.configuration.environment {
29            ExecutionEnvironment::Production => EraReaderAdapterBuilder::new(
30                &self.configuration.era_reader_adapter_type,
31                &self.configuration.era_reader_adapter_params,
32            )
33            .build(self.get_chain_observer().await?)
34            .map_err(|e| DependenciesBuilderError::Initialization {
35                message: "Could not build EraReader as dependency.".to_string(),
36                error: Some(e.into()),
37            })?,
38            _ => Arc::new(EraReaderDummyAdapter::from_markers(vec![EraMarker::new(
39                &SupportedEra::dummy().to_string(),
40                Some(Epoch(0)),
41            )])),
42        };
43
44        Ok(Arc::new(EraReader::new(era_adapter)))
45    }
46
47    /// [EraReader] service
48    pub async fn get_era_reader(&mut self) -> Result<Arc<EraReader>> {
49        if self.era_reader.is_none() {
50            self.era_reader = Some(self.build_era_reader().await?);
51        }
52
53        Ok(self.era_reader.as_ref().cloned().unwrap())
54    }
55
56    async fn build_era_checker(&mut self) -> Result<Arc<EraChecker>> {
57        let current_epoch = self
58            .get_ticker_service()
59            .await?
60            .get_current_epoch()
61            .await
62            .map_err(|e| DependenciesBuilderError::Initialization {
63                message: "Error while building EraChecker".to_string(),
64                error: Some(e),
65            })?;
66        let era_epoch_token = self
67            .get_era_reader()
68            .await?
69            .read_era_epoch_token(current_epoch)
70            .await
71            .map_err(|e| DependenciesBuilderError::Initialization {
72                message: "Error while building EraChecker".to_string(),
73                error: Some(e.into()),
74            })?;
75        let era_checker = Arc::new(EraChecker::new(
76            era_epoch_token.get_current_supported_era().map_err(|e| {
77                DependenciesBuilderError::Initialization {
78                    message: "Error while building EraChecker".to_string(),
79                    error: Some(e),
80                }
81            })?,
82            era_epoch_token.get_current_epoch(),
83        ));
84
85        Ok(era_checker)
86    }
87
88    /// [EraReader] service
89    pub async fn get_era_checker(&mut self) -> Result<Arc<EraChecker>> {
90        if self.era_checker.is_none() {
91            self.era_checker = Some(self.build_era_checker().await?);
92        }
93
94        Ok(self.era_checker.as_ref().cloned().unwrap())
95    }
96}