mithril_aggregator/dependency_injection/builder/protocol/
signables.rs

1use std::sync::Arc;
2
3use mithril_common::crypto_helper::MKTreeStoreInMemory;
4use mithril_common::signable_builder::{
5    CardanoDatabaseSignableBuilder, CardanoImmutableFilesFullSignableBuilder,
6    CardanoStakeDistributionSignableBuilder, CardanoTransactionsSignableBuilder,
7    MithrilSignableBuilderService, MithrilStakeDistributionSignableBuilder, SignableBuilderService,
8    SignableBuilderServiceDependencies, SignableSeedBuilder, TransactionsImporter,
9};
10
11use crate::dependency_injection::{DependenciesBuilder, Result};
12use crate::services::{AggregatorSignableSeedBuilder, CardanoTransactionsImporter};
13
14impl DependenciesBuilder {
15    async fn build_signable_builder_service(&mut self) -> Result<Arc<dyn SignableBuilderService>> {
16        let seed_signable_builder = self.get_signable_seed_builder().await?;
17        let mithril_stake_distribution_builder =
18            Arc::new(MithrilStakeDistributionSignableBuilder::default());
19        let immutable_signable_builder = Arc::new(CardanoImmutableFilesFullSignableBuilder::new(
20            self.get_immutable_digester().await?,
21            &self.configuration.db_directory,
22            self.root_logger(),
23        ));
24        let transactions_importer = self.get_transactions_importer().await?;
25        let block_range_root_retriever = self.get_transaction_repository().await?;
26        let cardano_transactions_builder = Arc::new(CardanoTransactionsSignableBuilder::<
27            MKTreeStoreInMemory,
28        >::new(
29            transactions_importer,
30            block_range_root_retriever,
31        ));
32        let cardano_stake_distribution_builder = Arc::new(
33            CardanoStakeDistributionSignableBuilder::new(self.get_stake_store().await?),
34        );
35        let cardano_database_signable_builder = Arc::new(CardanoDatabaseSignableBuilder::new(
36            self.get_immutable_digester().await?,
37            &self.configuration.db_directory,
38            self.root_logger(),
39        ));
40        let signable_builders_dependencies = SignableBuilderServiceDependencies::new(
41            mithril_stake_distribution_builder,
42            immutable_signable_builder,
43            cardano_transactions_builder,
44            cardano_stake_distribution_builder,
45            cardano_database_signable_builder,
46        );
47        let signable_builder_service = Arc::new(MithrilSignableBuilderService::new(
48            seed_signable_builder,
49            signable_builders_dependencies,
50            self.root_logger(),
51        ));
52
53        Ok(signable_builder_service)
54    }
55
56    /// [SignableBuilderService] service
57    pub async fn get_signable_builder_service(
58        &mut self,
59    ) -> Result<Arc<dyn SignableBuilderService>> {
60        if self.signable_builder_service.is_none() {
61            self.signable_builder_service = Some(self.build_signable_builder_service().await?);
62        }
63
64        Ok(self.signable_builder_service.as_ref().cloned().unwrap())
65    }
66
67    async fn build_signable_seed_builder(&mut self) -> Result<Arc<dyn SignableSeedBuilder>> {
68        let signable_seed_builder_service = Arc::new(AggregatorSignableSeedBuilder::new(
69            self.get_epoch_service().await?,
70        ));
71
72        Ok(signable_seed_builder_service)
73    }
74
75    /// [SignableSeedBuilder] service
76    pub async fn get_signable_seed_builder(&mut self) -> Result<Arc<dyn SignableSeedBuilder>> {
77        if self.signable_seed_builder.is_none() {
78            self.signable_seed_builder = Some(self.build_signable_seed_builder().await?);
79        }
80
81        Ok(self.signable_seed_builder.as_ref().cloned().unwrap())
82    }
83
84    async fn build_transactions_importer(&mut self) -> Result<Arc<dyn TransactionsImporter>> {
85        let transactions_importer = Arc::new(CardanoTransactionsImporter::new(
86            self.get_block_scanner().await?,
87            self.get_transaction_repository().await?,
88            self.root_logger(),
89        ));
90
91        Ok(transactions_importer)
92    }
93
94    /// Get the [TransactionsImporter] instance
95    pub async fn get_transactions_importer(&mut self) -> Result<Arc<dyn TransactionsImporter>> {
96        if self.transactions_importer.is_none() {
97            self.transactions_importer = Some(self.build_transactions_importer().await?);
98        }
99
100        Ok(self.transactions_importer.as_ref().cloned().unwrap())
101    }
102}