mithril_aggregator/dependency_injection/builder/enablers/
misc.rs

1//! Miscellaneous enablers
2//!
3//! This naming is not ideal, we should either:
4//! - group these enablers into more logical categories
5//! - redefine the actual categories so those miscellaneous enablers fit into them
6
7use std::sync::Arc;
8use std::time::Duration;
9
10use mithril_signed_entity_lock::SignedEntityTypeLock;
11
12use crate::database::repository::CertificateRepository;
13use crate::dependency_injection::{DependenciesBuilder, Result};
14use crate::services::{
15    AggregatorClient, AggregatorHTTPClient, MessageService, MithrilMessageService,
16};
17
18impl DependenciesBuilder {
19    async fn build_signed_entity_lock(&mut self) -> Result<Arc<SignedEntityTypeLock>> {
20        let signed_entity_type_lock = Arc::new(SignedEntityTypeLock::default());
21        Ok(signed_entity_type_lock)
22    }
23
24    /// Get the [SignedEntityTypeLock] instance
25    pub async fn get_signed_entity_lock(&mut self) -> Result<Arc<SignedEntityTypeLock>> {
26        if self.signed_entity_type_lock.is_none() {
27            self.signed_entity_type_lock = Some(self.build_signed_entity_lock().await?);
28        }
29
30        Ok(self.signed_entity_type_lock.as_ref().cloned().unwrap())
31    }
32
33    /// build HTTP message service
34    pub async fn build_message_service(&mut self) -> Result<Arc<dyn MessageService>> {
35        let certificate_repository = Arc::new(CertificateRepository::new(
36            self.get_sqlite_connection().await?,
37        ));
38        let signed_entity_storer = self.get_signed_entity_storer().await?;
39        let immutable_file_digest_mapper = self.get_immutable_file_digest_mapper().await?;
40        let epoch_service = self.get_epoch_service().await?;
41        let service = MithrilMessageService::new(
42            certificate_repository,
43            signed_entity_storer,
44            immutable_file_digest_mapper,
45            epoch_service,
46        );
47
48        Ok(Arc::new(service))
49    }
50
51    /// [MessageService] service
52    pub async fn get_message_service(&mut self) -> Result<Arc<dyn MessageService>> {
53        if self.message_service.is_none() {
54            self.message_service = Some(self.build_message_service().await?);
55        }
56
57        Ok(self.message_service.as_ref().cloned().unwrap())
58    }
59
60    /// build an [AggregatorClient]
61    pub async fn build_leader_aggregator_client(&mut self) -> Result<Arc<dyn AggregatorClient>> {
62        let leader_aggregator_endpoint = self
63            .configuration
64            .leader_aggregator_endpoint
65            .to_owned()
66            .unwrap_or_default();
67        let aggregator_client = AggregatorHTTPClient::new(
68            leader_aggregator_endpoint,
69            None,
70            self.get_api_version_provider().await?,
71            Some(Duration::from_secs(30)),
72            self.root_logger(),
73        );
74
75        Ok(Arc::new(aggregator_client))
76    }
77
78    /// Returns a leader [AggregatorClient]
79    pub async fn get_leader_aggregator_client(&mut self) -> Result<Arc<dyn AggregatorClient>> {
80        if self.leader_aggregator_client.is_none() {
81            self.leader_aggregator_client = Some(self.build_leader_aggregator_client().await?);
82        }
83
84        Ok(self.leader_aggregator_client.as_ref().cloned().unwrap())
85    }
86}